在创建文件时有一个缺省的权限,这个缺省的权限与umask所设立的权限相“AND”,得到该文件最终的权限。所以,文件不可能得到比缺省权限更大的权限,除非文件创建后再改。
手册说:(关于fopen)
Any created files will have mode
S_IRUSR |
S_IWUSR |
S_IRGRP |
S_IWGRP |
S_IROTH |
S_IWOTH (0666),
as modified by the process's umask value
如umask(022), 则用0666 & ~022, 结果是0644.
验证程序如下:
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <stdio.h>
- void create1file(int i);
- void main(void) {
- mode_t mode;
- int i = 0;
-
- printf("umask was not set:\n");
- create1file(i++);
- mode = umask(0777);
- create1file(i++);
- printf("umask was set to 0777,last umask is: %04o\n",mode);
- mode = umask(0222);
- create1file(i++);
- printf("umask was set to 0777,last umask is: %04o\n",mode);
- mode = umask(0000);
- create1file(i++);
- printf("umask was set to 0777,last umask is: %04o\n",mode);
- }
- void create1file(int i)
- {
- FILE* fp;
- char fn[10];
- sprintf(fn,"file%d",i);
- fp = fopen(fn,"w");
- fclose(fp);
- }
输出结果:
- $ ./a
- umask was not set:
- umask was set to 0777,last umask is: 0022
- umask was set to 0777,last umask is: 0777
- umask was set to 0777,last umask is: 0222
- $ ls -al file*
- -rw-r--r-- 1 a16000 mkgroup 0 Aug 29 15:41 file0
- ---------- 1 a16000 mkgroup 0 Aug 29 15:41 file1
- -r--r--r-- 1 a16000 mkgroup 0 Aug 29 15:41 file2
- -rw-rw-rw- 1 a16000 mkgroup 0 Aug 29 15:41 file3
阅读(1124) | 评论(0) | 转发(0) |