1 /*
2  * Copyright (c) International Business Machines  Corp., 2001
3  *  07/2001 Ported by John George
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it would be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  * the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 /*
21  * umask(2) sets the mask from 0000 to 0777 while we create files,
22  * the previous value of the mask should be returned correctly,
23  * and the file mode should be correct for each creation mask.
24  */
25 
26 #include <errno.h>
27 #include <stdio.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include "tst_test.h"
31 
32 static void verify_umask(void)
33 {
34 	struct stat statbuf;
35 	int mskval;
36 	int fd;
37 	int failflag = 0;
38 	unsigned low9mode;
39 
40 	for (mskval = 0000; mskval < 01000; mskval++) {
41 		TEST(umask(mskval));
42 		if (TEST_RETURN < 0 || TEST_RETURN > 0777) {
43 			tst_brk(TFAIL, "umask(%o) result outside range %ld",
44 				mskval, TEST_RETURN);
45 		}
46 
47 		if (mskval > 0000 && TEST_RETURN != mskval - 1) {
48 			failflag = 1;
49 			tst_res(TFAIL, "umask(%o) returned %ld, expected %d",
50 				mskval, TEST_RETURN, mskval - 1);
51 		}
52 
53 		fd = SAFE_CREAT("testfile", 0777);
54 		SAFE_CLOSE(fd);
55 
56 		SAFE_STAT("testfile", &statbuf);
57 
58 		low9mode = statbuf.st_mode & 0777;
59 
60 		if (low9mode != (~mskval & 0777)) {
61 			failflag = 1;
62 			tst_res(TFAIL, "File mode got %o, expected %o",
63 				low9mode, ~mskval & 0777);
64 		}
65 
66 		SAFE_UNLINK("testfile");
67 	}
68 
69 	if (!failflag)
70 		tst_res(TPASS, "All files created with correct mode");
71 }
72 
73 static struct tst_test test = {
74 	.test_all = verify_umask,
75 	.needs_tmpdir = 1,
76 };
77