Hai
Hai

Reputation: 83

open() function in C to creat new file with wrong permission?

I tried to create new file with rwx permission for Others user bellow:

int fd = open (myfile, O_CREAT, S_IRWXO);

but when I check, "myfile" only have r-x permission for Others user. I use Ubuntu 12.0.

What issue occurred?

Thanks for your help!

Upvotes: 3

Views: 657

Answers (1)

iElden
iElden

Reputation: 1280

It's because the default value of umask is 022, so when a new file is created, the write permission is automatically removed.
For fix your problem, remove the umask (or reconfigure it) :

$ umask 000

or in your programm with the umask() function

mode_t umask(mode_t mask);
umask(0)

But care, give write and execution privilege to everyone can be dangerous

Upvotes: 4

Related Questions