Skip to content
Advertisement

unexpected result(-wS-wx–T) on file permission with open() function in C

I wrote this program to open a file. Everything was OK until I saw this permission(-wS-wx–T) with 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 succeededn");
        }

        return 0;
}

I compiled the program featly, didn’t take any error or warning.

$ ./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

I haven’t ever seen kind of permission. What are ‘S’ and ‘T’ meaning in the file permissions section? (NOTE: I took the answer to this question in the comments.)

Advertisement

Answer

If you include O_CREAT in the flags passed to open() then you must use the three-arg form of the function, which takes a numeric file mode as the third argument. This requirement is documented in the Linux manual page for the function (emphasis added):

The mode argument specifies the file mode bits be applied when a new file is created. This argument must be supplied when O_CREAT or O_TMPFILE is specified in flags; if neither O_CREAT nor O_TMPFILE is specified, then mode is ignored.

What mode you actually want is unclear, but perhaps S_IRUSR | S_IWUSR | S_IRGRP would be suitable (== 0640; read and write for the owner, read only for the owner’s group, no permission for anyone else).

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement