设置创建文件掩码遇到的问题

  设置文件创建掩码的函数是umask

mode_t umask(mode_t mask);

  mask是设置的掩码,返回值是之前的掩码。掩码由一下1个或者多个mode与操作组成。

              The following symbolic constants are provided for mode:

              S_IRWXU  00700 user (file owner) has read, write and execute permission

              S_IRUSR  00400 user has read permission

              S_IWUSR  00200 user has write permission

              S_IXUSR  00100 user has execute permission

              S_IRWXG  00070 group has read, write and execute permission

              S_IRGRP  00040 group has read permission

              S_IWGRP  00020 group has write permission

              S_IXGRP  00010 group has execute permission

              S_IRWXO  00007 others have read, write and execute permission

              S_IROTH  00004 others have read permission

              S_IWOTH  00002 others have write permission

              S_IXOTH  00001 others have execute permission

 但是通常为了图方便,直接用数字形式表示,比如002,表示就是 S_IWOTH,屏蔽其他用户的写权限。但是当我用下面代码测试时,发现问题不对:

umask(777);
creat("./foo", 666);

文件权限为:

thomas@thomas-laptop:~/test$ ls -l foo 
--w--w--wT 1 thomas thomas 0 12月 26 15:39 foo

与预期的无任何权限不相符,查阅了网上的资料发现,问题出在:当用数字表示文件权限时,使用的是8进制,而C语言中表示8进制是数字前面以‘0’开头,所以要传入的应该是0777,0666,修改后程序运行无误

原文地址:https://www.cnblogs.com/thammer/p/5078235.html