标志统计系统调用的ST_MODE [英] flags for st_mode of stat system call

查看:179
本文介绍了标志统计系统调用的ST_MODE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解该stat命令的stat结构的ST_MODE领域的标志,但我有这样艰难的时刻!我发现这个例子 rel=\"nofollow\">,但我真的不明白这code片段:

I'm trying to understand the flags for the st_mode field of the stat structure of that stat command, but I'm having such a hard time! I found this example here, but I really don't understand this code fragment:

if ( mode & S_IRUSR ) str[1] = 'r';    /* 3 bits for user  */
if ( mode & S_IWUSR ) str[2] = 'w';
if ( mode & S_IXUSR ) str[3] = 'x';

if ( mode & S_IRGRP ) str[4] = 'r';    /* 3 bits for group */
if ( mode & S_IWGRP ) str[5] = 'w';
if ( mode & S_IXGRP ) str[6] = 'x';

if ( mode & S_IROTH ) str[7] = 'r';    /* 3 bits for other */
if ( mode & S_IWOTH ) str[8] = 'w';
if ( mode & S_IXOTH ) str[9] = 'x';

我知道,&放大器;是按位与运算,但没有别的。我甚至不知道要问什么。

I know that "&" is the bitwise AND operator, but nothing else. I don't even know what to ask.

PD:很抱歉的previous问题我问。我不知道如何标记的问题回答或类似的东西:(

PD: Sorry about the previous questions I asked. I don't know how to mark a question answered or anything like that :(

推荐答案

它在操作系统编程使用无符号整数举行一系列描述事情位常见。在这种情况下,模式具有感兴趣9位。一位告知用户,如果拥有者可以读取,一个用于写入业主,一个业主执行,以及类似位为组和其他人。诀窍是在提取位是否被设置或没有。

It's common in operating system programing to use unsigned integers to hold a series of bits that describe something. In this case, mode has 9 bits of interest. One bit tells if the owner can read, one for owner write, one for owner execute, and similar bits for the group and others. The trick is in extracting whether a bit is set or not.

下面是一个例子。该S_IRUSR是描述所有者读位位掩码的名称。它在规格的为0400(八进制),转换亿二进制。当你把二进制和用面膜这个值,你得到,如果面具的第9位设置的值亿(二进制)。否则,你得到0。

Here's an example. The S_IRUSR is a name for a bitmask that describes the "owner read" bit. It's defined in the specs as 0400 (octal) which translates to 100000000 in binary. When you take the binary and of this value with the mask, you get the value 100000000 (binary) if the 9th bit of the mask is set. Otherwise you get 0.

如果大的值返回,否则为假的if语句的计算结果为真。这样,如果设置了位,R被写入到串入位置1同样的方法适用于所有的其它位,以及

The if-statement evaluates to true if the large value is returned, and false otherwise. This way if the bit is set, 'r' is written into the string into position 1. The same technique applies to all of the other bits as well.

这篇关于标志统计系统调用的ST_MODE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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