概述
如有错误,欢迎批评指正,本人也是才学APUE的菜鸟
先贴上代码:
#include "apue.h"
#include <fcntl.h>
#define RWRWRW (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
int main(void) {
if(creat("my1.txt", RWRWRW) < 0)
err_sys("creat error for my1.txt");
umask(0);
if(creat("foo", RWRWRW) < 0)
err_sys("creat error for foo");
umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if(creat("bar", RWRWRW) < 0)
err_sys("creat error for bar");
umask(S_IXUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if(creat("my2.txt", RWRWRW) < 0)
err_sys("creat error for my2.txt");
exit(0);
}
其中的 my1.txt 与 my2.txt 是我自己添加的。
/
/
这里是终端操作过程:
hjm@hjm-Inspiron:~/InterviewPreparation/apue/Examples/4-9$ gcc -o 4-9 4-9.c -lapue
hjm@hjm-Inspiron:~/InterviewPreparation/apue/Examples/4-9$ umask
0022
hjm@hjm-Inspiron:~/InterviewPreparation/apue/Examples/4-9$ ./4-9
hjm@hjm-Inspiron:~/InterviewPreparation/apue/Examples/4-9$ ls -l my1.txt foo bar my2.txt
-rw------- 1 hjm hjm 0 4月 18 09:28 bar
-rw-rw-rw- 1 hjm hjm 0 4月 18 09:28 foo
-rw-r--r-- 1 hjm hjm 0 4月 18 09:28 my1.txt
-rw------- 1 hjm hjm 0 4月 18 09:28 my2.txt
hjm@hjm-Inspiron:~/InterviewPreparation/apue/Examples/4-9$ umask
0022
结合代码。分析整个过程:
-
首先查看了 umask,为 0022,即进程的文件模式创建屏蔽字为 000 010 010 (2进制),在此屏蔽字在创建了文件 mode 为 RWRWRW 的文件 my1.txt
-
文件模式创建屏蔽字改为 0,即 000 000 000,在此屏蔽字在创建了文件 mode 为 RWRWRW 的文件 foo
-
文件模式创建屏蔽字改为 000 110 110,在此屏蔽字在创建了文件 mode 为 RWRWRW 的文件 bar
-
文件模式创建屏蔽字改为 001 110 110,在此屏蔽字在创建了文件 mode 为 RWRWRW 的文件 my2.txt
结合输出,我们可发现:
my1.txt 的文件 mode 为 110 100 100;
foo 的文件 mode 为 110 110 110;
bar 的文件 mode 为 110 000 000;
my2.txt 的文件 mode 为 110 000 000;
而我们想要创建的文件 mode 均为 110 110 110;
结合创建文件时的文件模式创建屏蔽字,可知,在文件模式创建屏蔽字为 1 的位,在文件 mode 中的相应位一定被关闭。
最后
以上就是文艺电话为你收集整理的APUE第三版 程序 4-9(umask 与文件模式创建屏蔽字,文件创建时的访问权限)的全部内容,希望文章能够帮你解决APUE第三版 程序 4-9(umask 与文件模式创建屏蔽字,文件创建时的访问权限)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复