Search code examples
cunixpermissionssystem-callsumask

How to give write permission to everybody?


After the following code runs, file tasty has permission bits set to 0700, which is unexpected.

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main()
{
    int fild = creat("tasty", 0722);
    close(fild);
    return 0;
}

How to allow everybody to write into the file?


Solution

  • Your shell probably has a umask of 022, which means any new files created will have the specified bits (i.e. group write and other write) cleared.

    You need to set the umask to 0 before creating the file:

    #include <fcntl.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    
    int main()
    {
        umask(0);
        int fild = creat("tasty", 0722);
        close(fild);
        return 0;
    }