2

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?

Radical Ed
  • 178
  • 2
  • 13

1 Answers1

3

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;
}
dbush
  • 205,898
  • 23
  • 218
  • 273