C/Umask
From Attie's Wiki
(Redirected from Umask)
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(void) { int fd; umask(0); fd = open("./1", O_RDWR | O_CREAT, 0777); /* 0777 & ~0000 = 0777 */ /* -rwxrwxrwx 1 attie attie 0 2012-03-15 10:13 1 */ close(fd); umask(07); fd = open("./2", O_RDWR | O_CREAT, 0777); /* 0777 & ~0007 = 0770 */ /* -rwxrwx--- 1 attie attie 0 2012-03-15 10:13 2 */ close(fd); umask(077); fd = open("./3", O_RDWR | O_CREAT, 0777); /* 0777 & ~0077 = 0700 */ /* -rwx------ 1 attie attie 0 2012-03-15 10:13 3 */ close(fd); umask(0777); fd = open("./4", O_RDWR | O_CREAT, 0777); /* 0777 & ~0777 = 0000 */ /* ---------- 1 attie attie 0 2012-03-15 10:13 4 */ close(fd); return 0; }
$ ls total 0 -rwxrwxrwx 1 attie attie 0 2012-03-15 10:13 1 -rwxrwx--- 1 attie attie 0 2012-03-15 10:13 2 -rwx------ 1 attie attie 0 2012-03-15 10:13 3 ---------- 1 attie attie 0 2012-03-15 10:13 4