Chinaunix首页 | 论坛 | 博客
  • 博客访问: 110391
  • 博文数量: 94
  • 博客积分: 2245
  • 博客等级: 大尉
  • 技术积分: 613
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-26 16:35
文章分类

全部博文(94)

文章存档

2013年(8)

2012年(86)

我的朋友

分类: LINUX

2012-08-29 15:43:46

在创建文件时有一个缺省的权限,这个缺省的权限与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.

验证程序如下:

点击(此处)折叠或打开

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <stdio.h>

  4. void create1file(int i);

  5. void main(void) {
  6.     mode_t mode;
  7.     int i = 0;
  8.     
  9.     printf("umask was not set:\n");
  10.     create1file(i++);

  11.     mode = umask(0777);
  12.     create1file(i++);
  13.     printf("umask was set to 0777,last umask is: %04o\n",mode);

  14.     mode = umask(0222);
  15.     create1file(i++);
  16.     printf("umask was set to 0777,last umask is: %04o\n",mode);

  17.     mode = umask(0000);
  18.     create1file(i++);
  19.     printf("umask was set to 0777,last umask is: %04o\n",mode);
  20. }

  21. void create1file(int i)
  22. {
  23.     FILE* fp;
  24.     char fn[10];

  25.     sprintf(fn,"file%d",i);
  26.     fp = fopen(fn,"w");

  27.     fclose(fp);
  28. }
输出结果:

点击(此处)折叠或打开

  1. $ ./a
  2. umask was not set:
  3. umask was set to 0777,last umask is: 0022
  4. umask was set to 0777,last umask is: 0777
  5. umask was set to 0777,last umask is: 0222

  6. $ ls -al file*
  7. -rw-r--r-- 1 a16000 mkgroup 0 Aug 29 15:41 file0
  8. ---------- 1 a16000 mkgroup 0 Aug 29 15:41 file1
  9. -r--r--r-- 1 a16000 mkgroup 0 Aug 29 15:41 file2
  10. -rw-rw-rw- 1 a16000 mkgroup 0 Aug 29 15:41 file3



阅读(1100) | 评论(0) | 转发(0) |
0

上一篇:生成android javadoc的简单办法

下一篇:vim

给主人留下些什么吧!~~