1 /* test27: stat() fstat() Author: Jan-Mark Wams (jms@cs.vu.nl) */
15 #define MODE_MASK (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID)
19 #define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
20 #define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
25 char MaxName
[NAME_MAX
+ 1]; /* Name of maximum length */
26 char MaxPath
[PATH_MAX
];
27 char ToLongName
[NAME_MAX
+ 2]; /* Name of maximum +1 length */
28 char ToLongPath
[PATH_MAX
+ 1];
30 _PROTOTYPE(void main
, (int argc
, char *argv
[]));
31 _PROTOTYPE(void test27a
, (void));
32 _PROTOTYPE(void test27b
, (void));
33 _PROTOTYPE(void test27c
, (void));
34 _PROTOTYPE(void makelongnames
, (void));
35 _PROTOTYPE(void e
, (int __n
));
36 _PROTOTYPE(void quit
, (void));
45 if (argc
== 2) m
= atoi(argv
[1]);
48 System("rm -rf DIR_27; mkdir DIR_27");
50 superuser
= (getuid() == 0);
53 for (i
= 0; i
< ITERATIONS
; i
++) {
54 if (m
& 0001) test27a();
55 if (m
& 0002) test27b();
56 if (m
& 0004) test27c();
62 { /* Test Normal operation. */
69 time(&time1
); /* get time before */
70 while (time1
>= time((time_t *)0))
71 ; /* Wait for time to change. */
72 System("echo 7bytes > foo; chmod 4750 foo");
73 if (stat("foo", &st1
) != 0) e(1); /* get foo's info */
75 while (time2
>= time((time_t *)0))
76 ; /* Wait for next second. */
77 time(&time2
); /* get time after */
78 if ((st1
.st_mode
& MODE_MASK
) != 04750) e(2);
79 if (st1
.st_nlink
!= 1) e(3); /* check stat */
80 if (st1
.st_uid
!= geteuid()) e(4);
81 #if defined(NGROUPS_MAX) && NGROUPS_MAX == 0
82 if (st1
.st_gid
!= getegid()) e(5);
83 #endif /* defined(NGROUPS_MAX) && NGROUPS_MAX == 0 */
84 if (st1
.st_size
!= (size_t) 7) e(6);
85 if (st1
.st_atime
<= time1
) e(7);
86 if (st1
.st_atime
>= time2
) e(8);
87 if (st1
.st_ctime
<= time1
) e(9);
88 if (st1
.st_ctime
>= time2
) e(10);
89 if (st1
.st_mtime
<= time1
) e(11);
90 if (st1
.st_mtime
>= time2
) e(12);
92 /* Compair stat and fstat. */
93 System("echo 7bytes > bar");
94 fd
= open("bar", O_RDWR
| O_APPEND
); /* the bar is open! */
95 if (fd
!= 3) e(13); /* should be stderr + 1 */
96 if (stat("bar", &st1
) != 0) e(14); /* get bar's info */
97 if (fstat(fd
, &st2
) != 0) e(15); /* get bar's info */
99 /* St1 en st2 should be the same. */
100 if (st1
.st_dev
!= st2
.st_dev
) e(16);
101 if (st1
.st_ino
!= st2
.st_ino
) e(17);
102 if (st1
.st_mode
!= st2
.st_mode
) e(18);
103 if (st1
.st_nlink
!= st2
.st_nlink
) e(19);
104 if (st1
.st_uid
!= st2
.st_uid
) e(20);
105 if (st1
.st_gid
!= st2
.st_gid
) e(21);
106 if (st1
.st_size
!= st2
.st_size
) e(22);
107 if (st1
.st_atime
!= st2
.st_atime
) e(23);
108 if (st1
.st_ctime
!= st2
.st_ctime
) e(24);
109 if (st1
.st_mtime
!= st2
.st_mtime
) e(25);
110 time(&time1
); /* wait a sec. */
111 while (time1
>= time((time_t *)0))
113 System("chmod 755 bar"); /* chainge mode */
114 System("rm -f foobar; ln bar foobar"); /* chainge # links */
115 if (write(fd
, "foo", 4) != 4) e(26); /* write a bit (or two) */
116 if (stat("bar", &st2
) != 0) e(27); /* get new info */
117 if (st2
.st_dev
!= st1
.st_dev
) e(28);
118 if (st2
.st_ino
!= st1
.st_ino
) e(29); /* compair the fealds */
119 if ((st2
.st_mode
& MODE_MASK
) != 0755) e(30);
120 if (!S_ISREG(st2
.st_mode
)) e(31);
121 if (st2
.st_nlink
!= st1
.st_nlink
+ 1) e(32);
122 if (st2
.st_uid
!= st1
.st_uid
) e(33);
123 if (st2
.st_gid
!= st1
.st_gid
) e(34);
124 if (st2
.st_size
!= (size_t) 11) e(35);
125 if (st2
.st_atime
!= st1
.st_atime
) e(36);
126 if (st2
.st_ctime
<= st1
.st_ctime
) e(37);
127 if (st2
.st_mtime
<= st1
.st_mtime
) e(38);
128 if (close(fd
) != 0) e(39); /* sorry the bar is closed */
130 /* Check special file. */
131 if (stat("/dev/tty", &st1
) != 0) e(40);
132 if (!S_ISCHR(st1
.st_mode
)) e(41);
134 if (stat("/dev/ram", &st1
) != 0) e(42);
135 if (!S_ISBLK(st1
.st_mode
)) e(43);
140 while (time1
>= time((time_t *)0))
142 if (mkfifo("fifo", 0640) != 0) e(44);
143 if (stat("fifo", &st1
) != 0) e(45); /* get fifo's info */
145 while (time2
>= time((time_t *)0))
148 if (!S_ISFIFO(st1
.st_mode
)) e(46);
149 if (st1
.st_nlink
!= 1) e(47); /* check the stat info */
150 if (st1
.st_uid
!= geteuid()) e(48);
151 #if defined(NGROUPS_MAX) && NGROUPS_MAX == 0
152 if (st1
.st_gid
!= getegid()) e(49);
153 #endif /* defined(NGROUPS_MAX) && NGROUPS_MAX == 0 */
154 if (st1
.st_size
!= (size_t) 0) e(50);
155 if (st1
.st_atime
<= time1
) e(51);
156 if (st1
.st_atime
>= time2
) e(52);
157 if (st1
.st_ctime
<= time1
) e(53);
158 if (st1
.st_ctime
>= time2
) e(54);
159 if (st1
.st_mtime
<= time1
) e(55);
160 if (st1
.st_mtime
>= time2
) e(56);
162 /* Note: the st_mode of a fstat on a pipe should contain a isfifo bit. */
165 while (time1
>= time((time_t *)0))
167 if (pipe(pfd
) != 0) e(57);
168 if (fstat(pfd
[0], &st1
) != 0) e(58); /* get pipe input info */
170 while (time2
>= time((time_t *)0))
173 if (!(S_ISFIFO(st1
.st_mode
))) e(59); /* check stat struct */
174 if (st1
.st_uid
!= geteuid()) e(60);
175 if (st1
.st_gid
!= getegid()) e(61);
176 if (st1
.st_size
!= (size_t) 0) e(62);
177 if (st1
.st_atime
<= time1
) e(63);
178 if (st1
.st_atime
>= time2
) e(64);
179 if (st1
.st_ctime
<= time1
) e(65);
180 if (st1
.st_ctime
>= time2
) e(66);
181 if (st1
.st_mtime
<= time1
) e(67);
182 if (st1
.st_mtime
>= time2
) e(68);
183 if (fstat(pfd
[1], &st1
) != 0) e(69); /* get pipe output info */
184 if (!(S_ISFIFO(st1
.st_mode
))) e(70);
185 if (st1
.st_uid
!= geteuid()) e(71);
186 if (st1
.st_gid
!= getegid()) e(72);
187 if (st1
.st_size
!= (size_t) 0) e(73);
188 if (st1
.st_atime
< time1
) e(74);
189 if (st1
.st_atime
> time2
) e(75);
190 if (st1
.st_ctime
< time1
) e(76);
191 if (st1
.st_ctime
> time2
) e(77);
192 if (st1
.st_mtime
< time1
) e(78);
193 if (st1
.st_mtime
> time2
) e(79);
194 if (close(pfd
[0]) != 0) e(80);
195 if (close(pfd
[1]) != 0) e(81);/* close pipe */
199 while (time1
>= time((time_t *)0))
202 if (stat("dir", &st1
) != 0) e(82); /* get dir info */
204 while (time2
>= time((time_t *)0))
207 if (!(S_ISDIR(st1
.st_mode
))) e(83); /* check stat struct */
208 if (st1
.st_uid
!= geteuid()) e(84);
209 #if defined(NGROUPS_MAX) && NGROUPS_MAX == 0
210 if (st1
.st_gid
!= getegid()) e(85);
211 #endif /* defined(NGROUPS_MAX) && NGROUPS_MAX == 0 */
212 if (st1
.st_atime
< time1
) e(86);
213 if (st1
.st_atime
> time2
) e(87);
214 if (st1
.st_ctime
< time1
) e(88);
215 if (st1
.st_ctime
> time2
) e(89);
216 if (st1
.st_mtime
< time1
) e(90);
217 if (st1
.st_mtime
> time2
) e(91);
218 System("rm -rf ../DIR_27/*");
228 /* Check stats on maximum length files names. */
229 if (mkdir(MaxName
, 0777) != 0) e(1);
230 if (stat(MaxName
, &st
) != 0) e(2);
231 if ((fd
= open(MaxName
, O_RDONLY
)) != 3) e(3);
232 if (fstat(fd
, &st
) != 0) e(4);
233 if (close(fd
) != 0) e(5);
234 if (rmdir(MaxName
) != 0) e(6);
235 if (stat(MaxPath
, &st
) != 0) e(7);
236 if ((fd
= open(MaxPath
, O_RDONLY
)) != 3) e(8);
237 if (fstat(fd
, &st
) != 0) e(9);
238 if (close(fd
) != 0) e(10);
239 System("rm -rf ../DIR_27/*");
243 { /* Test error response. */
249 System("echo Hi > foo"); /* Make a file called foo. */
250 /* Check if a un searchable dir is handled ok. */
251 Chdir(".."); /* cd .. */
252 System("chmod 677 DIR_27"); /* no search permission */
253 if (stat("DIR_27/nono", &st
) != -1) e(1);
255 if (errno
!= ENOENT
) e(2); /* su has access */
258 if (errno
!= EACCES
) e(3); /* we don't ;-) */
260 System("chmod 777 DIR_27");
261 Chdir("DIR_27"); /* back to test dir */
263 /* Check on ToLongName etc. */
264 #ifdef _POSIX_NO_TRUNC
265 # if _POSIX_NO_TRUNC - 0 != -1
266 if (stat(ToLongName
, &st
) != -1) e(4); /* name is too long */
267 if (errno
!= ENAMETOOLONG
) e(5);
270 # include "error, this case requires dynamic checks and is not handled"
272 if (stat(ToLongPath
, &st
) != -1) e(6); /* path is too long */
273 if (errno
!= ENAMETOOLONG
) e(7);
275 /* Test some common errors. */
276 if (stat("nono", &st
) != -1) e(8); /* nono nonexistent */
277 if (errno
!= ENOENT
) e(9);
278 if (stat("", &st
) != -1) e(10); /* try empty */
279 if (errno
!= ENOENT
) e(11);
280 if (stat("foo/bar", &st
) != -1) e(12); /* foo is a file */
281 if (errno
!= ENOTDIR
) e(13);
283 /* Test fstat on file descriptors that are not open. */
284 for (i
= 3; i
< 6; i
++) {
285 if (fstat(i
, &st
) != -1) e(14);
286 if (errno
!= EBADF
) e(15);
289 /* Test if a just closed file is `fstat()'-able. */
290 if ((fd
= open("foo", O_RDONLY
)) != 3) e(16); /* open foo */
291 if (fstat(fd
, &st
) != 0) e(17); /* get stat */
292 if (close(fd
) != 0) e(18); /* close it */
293 if (fstat(fd
, &st
) != -1) e(19); /* get stat */
294 if (errno
!= EBADF
) e(20);
295 System("rm -rf ../DIR_27/*");
302 memset(MaxName
, 'a', NAME_MAX
);
303 MaxName
[NAME_MAX
] = '\0';
304 for (i
= 0; i
< PATH_MAX
- 1; i
++) { /* idem path */
308 MaxPath
[PATH_MAX
- 1] = '\0';
310 strcpy(ToLongName
, MaxName
); /* copy them Max to ToLong */
311 strcpy(ToLongPath
, MaxPath
);
313 ToLongName
[NAME_MAX
] = 'a';
314 ToLongName
[NAME_MAX
+ 1] = '\0'; /* extend ToLongName by one too many */
315 ToLongPath
[PATH_MAX
- 1] = '/';
316 ToLongPath
[PATH_MAX
] = '\0'; /* inc ToLongPath by one */
322 int err_num
= errno
; /* Save in case printf clobbers it. */
324 printf("Subtest %d, error %d errno=%d: ", subtest
, n
, errno
);
327 if (errct
++ > MAX_ERROR
) {
328 printf("Too many errors; test aborted\n");
330 system("rm -rf DIR*");
339 System("rm -rf DIR_27");
345 printf("%d errors\n", errct
);