clinuxunixumask

UMASK function in C


#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
void main()
{
    umask(0000);
    creat("a.txt",666);
}

My expected output is, the file created with the name "a.txt" with the permission as "rwrwrw". But, the output is as follows.

Output:

$ ls -l a.txt 
--w--wx-wT 1 mohanraj mohanraj 0 Sep 11 19:04 a.txt
$

The umask is set to 0. So, I expected the file is created with the default file permission with 666. But, it gives some other output. So, How umask internally worked. And How do I get the expected result.


Solution

  • The file creation mode you specified is in decimal format. File creation modes are typically specified in octal, so you should prefix the number with a leading 0, which tells the compiler it is an octal constant.

    creat("a.txt",0666);
    

    Decimal 666 = octal 1232, which matches up with the result you got.

    Please refer to your friendly man page (man 2 umask) or to this one.