C / Unix:如何从st_mode中提取位? [英] C/Unix: How to extract the bits from st_mode?

查看:83
本文介绍了C / Unix:如何从st_mode中提取位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Unix编程和C语言的初学者,我对 struct stat 及其字段 st_mode :

I am a beginner to Unix programming and C and I have two questions regarding the struct stat and its field st_mode:


  1. 在访问 st_mode 字段时,输入什么类型返回的数量(八进制,十进制等)?

  1. When accessing the st_mode field as below, what type of number is returned( octal, decimal, etc.) ?

struct stat file;
stat( someFilePath, &file);
printf("%d", file.st_mode );


我以为数字是八进制的,但是当我运行了这段代码,并得到了值 33188 。基础是什么?

I thought the number is in octal but when I ran this code, and I got the value 33188. What is the base ?


  1. 我发现 st_mode 编码一个16位二进制数字,代表文件类型和文件许可。如何从上面的输出中获得16位数字(尤其是当它看起来不是八进制时)。以及16位数字的哪些部分编码哪些信息?

  1. I found out that the st_mode encodes a 16 bit binary number that represents the file type and file permissions. How do I get the 16-bit number from the above output(esp. when it doesn't seem to be in octal). And which parts of the 16-bit digit encodes which information ?

感谢您的帮助。

推荐答案

我认为您在这里混用概念。

I think you are mixing concepts here.

在内存中,整数值始终为二进制,让我们称为本机格式 st_mode mode_t 类型,并且该类型是未指定的整数类型,可能是 int

In memory, integer values are always in binary, let's call it native format. st_mode is of type mode_t and that type is an unspecified integer type, probably int.

base 的概念,即十进制,八进制或十六进制,仅当您隐瞒了以本机格式存储为文本表示形式(或从文本转换为本机)。

The concept of base, that is decimal, octal or hexadecimal, is useful only when you covert the number in memory in native format to a text representation, (or back from text to native).

例如:

int x = 42;

将数字42分配给整数变量。由于源代码是文本,因此 42 作为文本输入,我们知道它是一个十进制值(无前缀)。但是请注意我们如何不为变量指定基数:它没有基数。
其他代码:

assigns the number 42 to the integer variable. Since source code is text, the 42 is input as text, and we know that it is a decimal value (no prefix). But note how we do not specify a base for the variable: it does not have one. This other code:

int x = 0x2A;

完全等效。它使用十六进制常量 0x2A 代替十进制常量 42 ,但这是相同的,并且 x 在两种情况下的值相同。同样:

is exactly equivalent. Instead of the decimal constant 42 it uses the hexadecimal constant 0x2A, but that is identical, and x got the same value on both cases. Likewise:

int x = 052;

也是等效的,但具有八进制常数。

is also equivalent, but with an octal constant.

现在输入您的代码。当您这样做时:

Now to your code. When you do:

printf("%d", file.st_mode);
33188

您告诉程序将变量的值输出为十进制数。请记住, printf 会将数字从本机格式转换为文本,因此该文本的基数很重要。如果您希望将值视为八进制,则只需输入:

you tell the program to output the value of that variable as a decimal number. Remember that printf converts the number from native format to text, so the base of that text matters. If you prefer to see the value as octal, just write:

printf("%o", file.st_mode);
100644

或以十六进制表示:

printf("%x", file.st_mode);
81A4

关于八进制的妙处在于,它每八进制数字恰好表示3位(4位,以十六进制为单位)。因此,通过一些实践,您可以看到无需计算的位。

The nice thing about octal is that itrepresents exactly 3 bit per octal digit (4 bits per digit for hex.), so with a bit of practise you can see the bits without computations.

例如,您的 st_mode 十进制为 33188 或八进制为 0100644 。十进制什么也没告诉我,但是八进制确实有什么意义,因为我记得最后9位(3个八进制数字)是权限:所有者3位,组3位,其他3位。因此:

For example, your st_mode is 33188 in decimal or 0100644 in octal. The decimal tells me nothing, but the octal does mean something, because I remember that the last 9 bits (3 octal digits) are the permissions: 3 bits for the owner, 3 bits for the group, 3 bits for other. So:

* Owner: 6 that is rw-
* Group: 4 that is r--
* Other: 4 that is r--

BTW,最后一个为该常数:

BTW, the last 1 is this constant:

#define S_IFREG 0100000

只是意味着它是一个常规文件。

that just means that it is a regular file.

这篇关于C / Unix:如何从st_mode中提取位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆