C中open()函数的文件权限出现意外结果(-wS wx--T)
我写这个程序是为了打开一个文件。一切都很好,直到我看到这个带有C中open()函数的文件权限出现意外结果(-wS wx--T),c,linux,unix,C,Linux,Unix,我写这个程序是为了打开一个文件。一切都很好,直到我看到这个带有ls-lh open.c #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <errno.h> #include <string.h> #define FILE "foo.txt" int main() { in
ls-lh
open.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#define FILE "foo.txt"
int main()
{
int fd;
int errnum;
fd = open(FILE, O_RDWR | O_CREAT);
if(fd == -1)
{
printf("[error] The file hasn't opened.\n");
perror("Error printed by perror");
}else {
printf("The process was succeeded\n");
}
return 0;
}
我从来没有见过这样的许可。中的“S”和“T”是什么意思
文件权限部分?
(注意:我在评论中回答了这个问题。)如果在传递给
open()
的标志中包含O_create
,则必须使用函数的三个参数形式,该函数将数字文件模式作为第三个参数。该要求记录在(重点补充)中:
mode
参数指定在
创建新文件必须在O_create
或
O_TMPFILE
在标志中指定;如果O_create
和O_TMPFILE
都不可用
指定,则忽略模式
您真正想要的模式还不清楚,但可能适合(=S|IRUSR | S|IWUSR | S|IRGRP
;为所有者读写,仅为所有者组读,不允许任何其他人使用)。我发现了两种不同的开放函数定义,您可以在linux终端中通过man 2 open
和man 3 open
查看它们。
man 2为Linux程序员手册打开
。
man 3为POSIX程序员手册打开
。
如果对open函数使用POSIX程序员手册定义(问题中的示例代码),则可以获得意外的权限(如-wS-wx--T)。
如果您使用Linux程序员手册定义(下面的示例代码),您可以获得常规权限。
我不确定那是真的,但我注意到了
如果我有错,请纠正我
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int fd;
if(argc != 2){
printf("[usage] %s <file_name>\n", argv[0]);
exit(0);
}
fd = open(argv[1], O_RDWR | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IROTH);
if(fd == -1){
perror("[error] open function\n");
exit(-1);
}else{
printf("[succeeded]\n");
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
int-fd;
如果(argc!=2){
printf(“[usage]%s\n”,argv[0]);
出口(0);
}
fd=开放(argv[1],O|RDWR | O|u APPEND | O|u CREAT,S|IRWXU | S|IRWXG | S|IROTH);
如果(fd==-1){
perror(“[error]open function\n”);
出口(-1);
}否则{
printf(“[成功]\n”);
}
返回0;
}
如果您想了解有关Linux文件系统权限的说明,最好查看一下。@antoh此权限是在C程序之后获得的。我认为它在这里也是相关的。@jxh umask是0002。open()允许第三个参数:文件是否需要创建的预期模式。-->><代码>fd=打开(文件,O|RDWR | O|u CREAT,0644)代码>顺便说一句:#定义文件
在自找麻烦。stdio定义文件(一个坏名字,同意)请使用另一个宏名,如#define FILENAME“omg.txt”
这是什么意思?@H.Emre,ls
输出中的“s”代表文件的SUID位。“T”代表粘性位。
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int fd;
if(argc != 2){
printf("[usage] %s <file_name>\n", argv[0]);
exit(0);
}
fd = open(argv[1], O_RDWR | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IROTH);
if(fd == -1){
perror("[error] open function\n");
exit(-1);
}else{
printf("[succeeded]\n");
}
return 0;
}