C语言中带有open()函数的文件权限的意外结果(-wS-wx-T)

met*_*ose 3 c unix linux

我写了这个程序来打开一个文件.一切都很好,直到我看到这个权限(-wS-wx-T)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;
}
Run Code Online (Sandbox Code Playgroud)

我很好地编译了程序,没有收到任何错误或警告.

$ ./open
The process was succeeded
$ ls -lh
-rwxrwxr-x 1 hemre hemre 8.5K Feb  1 23:38 open
--wS-wx--T 1 hemre hemre    0 Feb  1 23:39 foo.txt
Run Code Online (Sandbox Code Playgroud)

我没见过任何许可.文件权限部分中的"S"和"T"是什么意思?(注意:我在评论中回答了这个问题.)

Joh*_*ger 8

如果包含O_CREAT在传递给的标志中,open()那么必须使用函数的三元格形式,它将数字文件模式作为第三个参数.该要求记录在该函数的Linux手册页中(重点已添加):

mode参数指定在创建新的文件时,文件模式位来施加. 该参数必须在供给 O_CREATO_TMPFILE在指定flags ; 如果既未指定也O_CREATO_TMPFILE指定,则忽略模式.

你真正想要的模式是不清楚的,但也许S_IRUSR | S_IWUSR | S_IRGRP是合适的(== 0640;对所有者的读写,只读给所有者的团体,没有其他人的许可).