如何理解O_RDONLY = 0? [英] How to make sense of O_RDONLY = 0?

查看:535
本文介绍了如何理解O_RDONLY = 0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理文件状态标志. 在执行的测试中,我发现了

I am dealing with file status flags. Among test I performed, I found

#include <stdio.h>
#include "fcntl.h"

int main() {
    const int flag = O_RDONLY;
    printf( "*** Flag O_RDONLY = %5d\n", flag);
    return 0;
}

产生此输出

*** Flag O_RDONLY =     0

#define O_RDONLY         00

来自fcntl-linux.h.

如何将零值用作标志?

我希望"atomic"标志为2^n(n>=1),而"composite"标志(如O_ACCMODE)只是几个原子标志的总和(与按位或那些原子标记).
据我了解,我无法检测"任何东西,并且此类标志永远无法设置. 像(stat & O_RDONLY)这样的按位运算符始终为false.

I expect an "atomic" flag to be 2^n (n>=1), and "composite" flags (like O_ACCMODE) to be simply the sum of several atomic flags (which is the same as bitwise-or'ing those atomic flags).
As far as I understand, I cannot "detect" anything, and such flag cannot be ever set. A bitwise-and'ed expression like (stat & O_RDONLY) will always be false.

相关:

如何获取文件描述符的模式?(我问了这个问题)

推荐答案

尽管在文档中将它们称为标记,但实际上这三个标记不是原子标记,可以像其他标记一样组合使用.它们是O_ACCMODE位的互斥替代值.您不使用stat & RDONLY进行测试,而是使用(stat & O_ACCMODE) == O_RDONLY.

Although these are called flags in the documentation, these three are not actually atomic flags that can be combined like the rest. They're mutually exclusive alternative values for the O_ACCMODE bits. You don't use stat & RDONLY to test for it, you use (stat & O_ACCMODE) == O_RDONLY.

这篇关于如何理解O_RDONLY = 0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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