"Lewei Xu" <23*0*0*
8@s*u*e*t*u*a*e*u*a*> wrote:
> So I was chatting with a friend who said he was getting some weird file permissions when trying to create a file, so we ran the following code on our respective outputs and ended up with different file permissions:
> ...
> We are both running cc 11.4.0 and ubuntu 22.04 WSL2 for Windows.
You and your friend may have different values of
umask
set in your shells.
https://www.geeksforgeeks.org/umask-command-in-linux-with-examples/
The umask is used to modify the initial permissions of any newly created file or directory, to set it to a per-user preferred value.
You can see your shell's umask with the
umask
command.
My value of umask is 077 (an octal value), which removes all rwx permissions for everyone other than myself.
If you really wish a newly created file to have a specific mode, you'll need to:
int fd = open(filename, ....., 0666);
close(fd);
chmod(filename, 0666);