c - 权限位解释

标签 c linux posix gnu

我正在查看 stat()chmod() 等各种函数中使用的权限位,并且我想要描述实际定义的宏是什么。例如,S_IRUSR 表示它由 00400 (GNU/Linux) 表示。我的问题是,有人可以描述一下 00400 实际上是什么吗?是数字吗?我了解如何对宏进行“或”运算,但我只是不明白宏实际上是什么。

最佳答案

我将描述许可中最左边的三个数字,这也将解释有关 S_IRUSR 的信息,

所以每个数字都是一个八进制数。每个数字可以是从0到7。每个八进制数可以转换为3位二进制数。每一位代表一个权限。

Left most bit = Read permission
Middle bit    = Write permission
Right most bit= Execute permission

让我们将 0 到 7 写入二进制文件并查看权限位:

Octal    Binary 
0        0 0 0 (No Read,  No Write,  No Execute) -- No permission
1        0 0 1 (No Read,  No Write,  Yes you can execute)
2        0 1 0 (No Read,  Can Write, No execute)
3        0 1 1 (No Read,  Can Write, Can execute)
4        1 0 0 (Can Read, No Write,  No Execute)
5        1 0 1 (Can Read, No Write,  Can execute)
6        1 1 0 (Can Read, Can Write, No execute)
7        1 1 1 (Can Read, Can Write, Can execute)

所以每个数字代表权限。现在下一部分是关于这些权限是谁的。 设最左边的三个数为 XYZ: 现在,

X means permission given to the owner of the file.
Y means permission given to the group of the owner.
Z means permission given to all other users in system , outside of user's group.

鉴于 Z_ISUSR = 00400,现在 4 表示用户可读 IRUSR = 用户可读。

这三个是权限中的重要数字,它们仅指定赋予文件的权限。

关于c - 权限位解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13535654/

相关文章:

c - OpenSSL SHA1 未验证 NIST SHAKE 测试 vector ?

linux - 在 perl 中使用 perl 库而不是模块

linux - 零填充和排序 - Linux Shell 脚本

windows - 为什么 GCC-Windows 依赖于 cygwin?

c++ - 关于意外断电的原子写入

regex - 无法将否定先行断言应用于 bash 上的 nagios 插件输出

c - 流的位置指示器,使用 fwrite

c - 用 GDB 检查 Bison 的 $$ 变量

Const 自引用结构

python - 使用python在linux上运行系统命令?