-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht_umask.c
49 lines (41 loc) · 1.44 KB
/
t_umask.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* t_umask.c
Demonstrate the affect of umask() in conjunction with open() and mkdir().
*/
#include <sys/stat.h>
#include <fcntl.h>
#include "file_perms.h"
#include "tlpi_hdr.h"
#define MYFILE "myfile"
#define MYDIR "mydir"
#define FILE_PERMS (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)
#define DIR_PERMS (S_IRWXU | S_IRWXG | S_IRWXO)
#define UMASK_SETTING (S_IWGRP | S_IXGRP | S_IWOTH | S_IXOTH)
int
main(int argc, char *argv[])
{
int fd;
struct stat sb;
mode_t u;
umask(UMASK_SETTING);
fd = open(MYFILE, O_RDWR | O_CREAT | O_EXCL, FILE_PERMS);
if (fd == -1)
errExit("open-%s", MYFILE);
if (mkdir(MYDIR, DIR_PERMS) == -1)
errExit("mkdir-%s", MYDIR);
u = umask(0); /* Retrieves (and clears) umask value */
if (stat(MYFILE, &sb) == -1)
errExit("stat-%s", MYFILE);
printf("Requested file perms: %s\n", filePermStr(FILE_PERMS, 0));
printf("Process umask: %s\n", filePermStr(u, 0));
printf("Actual file perms: %s\n\n", filePermStr(sb.st_mode, 0));
if (stat(MYDIR, &sb) == -1)
errExit("stat-%s", MYDIR);
printf("Requested dir. perms: %s\n", filePermStr(DIR_PERMS, 0));
printf("Process umask: %s\n", filePermStr(u, 0));
printf("Actual dir. perms: %s\n", filePermStr(sb.st_mode, 0));
if (unlink(MYFILE) == -1)
errMsg("unlink-%s", MYFILE);
if (rmdir(MYDIR) == -1)
errMsg("rmdir-%s", MYDIR);
exit(EXIT_SUCCESS);
}