#include <sys/types.h>
#include <sys/stat.h>

mode_t umask(mode_t mask);

在进程创建一个新的文件或目录时,如调用open函数创建一个新文件,新文件的实际存取权限是mode与umask按照  mode&~umask运算以后的结果。umask函数用来修改进程的

umask。参数mask可以直接取数值也可以为open系统调用第三个参数mode的11个宏或他们的组合。

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

 

int main(int argc, char* argv[])
{
  umask(0);//不屏蔽任何权限
  creat("t1", S_IRWXU|S_IRWXG|S_IRWXO);

  umask(S_IRWXO);//屏蔽其他用户的所有权限
  creat("t2", S_IRWXU|S_IRWXG|S_IRWXO);

  return 0;
}

 

-rwxrwxrwx 1 root root 0 Jan 22 10:26 t1
-rwxrwx--- 1 root root 0 Jan 22 10:26 t2

 

posted on 2017-01-22 10:29  邶风  阅读(2467)  评论(0编辑  收藏  举报