我使用以下代码创建文件:
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
const char* filename = "./test.out";
int fd;
if(-1 == (fd = open(filename, O_CREAT|O_RDWR, 0666)))
{
perror("Error");
errno = 0;
}
else
puts("File opened");
if(-1 == (close(fd)))
{
perror("Error");
errno = 0;
}
else
puts("File closed");
return 0;
}
我将mode
参数指定为0666
,这应该授予每个人读写权限。然而,ls -l
显示:
-rw-r--r-- 1 kmehta users 0 2012-01-29 16:29 test.out
如您所见,只有文件所有者被授予写权限。我不知道为什么其他人没有正确地被授予权限。chmod a+w test.out
可以正确设置权限。
代码编译为gcc -Wall test.c
规格:gcc v 4.5.0 on Opensuse 11.3 64 bit