APUE第三版 程序 4-9(umask 与文件模式创建屏蔽字,文件创建时的访问权限)

如有错误,欢迎批评指正,本人也是才学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

结合代码。分析整个过程:

  1. 首先查看了 umask,为 0022,即进程的文件模式创建屏蔽字为 000 010 010 (2进制),在此屏蔽字在创建了文件 mode 为 RWRWRW 的文件 my1.txt

  2. 文件模式创建屏蔽字改为 0,即 000 000 000,在此屏蔽字在创建了文件 mode 为 RWRWRW 的文件 foo

  3. 文件模式创建屏蔽字改为 000 110 110,在此屏蔽字在创建了文件 mode 为 RWRWRW 的文件 bar

  4. 文件模式创建屏蔽字改为 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 中的相应位一定被关闭。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值