1 /* test23: chdir(), getcwd() Author: Jan-Mark Wams (jms@cs.vu.nl) */
17 #define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
18 #define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
22 int superuser
; /* True if we are root. */
24 char cwd
[PATH_MAX
]; /* Space for path names. */
27 char MaxName
[NAME_MAX
+ 1]; /* Name of maximum length */
28 char MaxPath
[PATH_MAX
]; /* Same for path */
29 char ToLongName
[NAME_MAX
+ 2]; /* Name of maximum +1 length */
30 char ToLongPath
[PATH_MAX
+ 1]; /* Same for path, both too long */
32 _PROTOTYPE(void main
, (int argc
, char *argv
[]));
33 _PROTOTYPE(void test23a
, (void));
34 _PROTOTYPE(void test23b
, (void));
35 _PROTOTYPE(void test23c
, (void));
36 _PROTOTYPE(void makelongnames
, (void)); /* Fill MaxName etc. */
37 _PROTOTYPE(char *last_index
, (char *string
, int ch
));
38 _PROTOTYPE(char *my_getcwd
, (char *buf
, int size
));
39 _PROTOTYPE(void e
, (int number
));
40 _PROTOTYPE(void quit
, (void));
49 if (argc
== 2) m
= atoi(argv
[1]);
52 System("rm -rf DIR_23; mkdir DIR_23");
55 superuser
= (geteuid() == 0);
57 for (i
= 0; i
< ITERATIONS
; i
++) {
58 if (m
& 0001) test23a(); /* Test normal operation */
59 if (m
& 0002) test23b(); /* Test critical operation */
60 if (m
& 0004) test23c(); /* Test error operation */
67 { /* Test normal operation. */
72 System("rm -rf ../DIR_23/*");
74 /* Let's do some fiddeling with path names. */
75 if (getcwd(cwd
, PATH_MAX
) != cwd
) e(1);
76 if (chdir(cwd
) != 0) e(2);
77 if (getcwd(buf
, PATH_MAX
) != buf
) e(3);
78 if (strcmp(buf
, cwd
) != 0) e(4);
79 if (chdir(".") != 0) e(5);
80 if (getcwd(buf
, PATH_MAX
) != buf
) e(6);
81 if (strcmp(buf
, cwd
) != 0) e(7);
82 if (chdir("./././.") != 0) e(8);
83 if (getcwd(buf
, PATH_MAX
) != buf
) e(9);
84 if (strcmp(buf
, cwd
) != 0) e(10);
86 /* Creat a working dir named "foo", remove any previous residues. */
88 if (mkdir("foo", 0777) != 0) e(11);
90 /* Do some more fiddeling with path names. */
91 if (chdir("foo/.././foo/..") != 0) e(12); /* change to cwd */
92 if (getcwd(buf
, PATH_MAX
) != buf
) e(13);
93 if (strcmp(buf
, cwd
) != 0) e(13);
94 if (chdir("foo") != 0) e(14); /* change to foo */
95 if (chdir("..") != 0) e(15); /* and back again */
96 if (getcwd(buf
, PATH_MAX
) != buf
) e(16);
97 if (strcmp(buf
, cwd
) != 0) e(17);
99 /* Make 30 sub dirs, eg. ./bar/bar/bar/bar/bar...... */
100 System("rm -rf bar"); /* get ridd of bar */
101 for (i
= 0; i
< 30; i
++) {
102 if (mkdir("bar", 0777) != 0) e(18);
103 if (chdir("bar") != 0) e(19); /* change to bar */
105 for (i
= 0; i
< 30; i
++) {
106 if (chdir("..") != 0) e(20); /* and back again */
107 if (rmdir("bar") != 0) e(21);
110 /* Make sure we are back where we started. */
111 if (getcwd(buf
, PATH_MAX
) != buf
) e(22);
112 if (strcmp(buf
, cwd
) != 0) e(23);
113 System("rm -rf bar"); /* just incase */
115 /* Do some normal checks on `Chdir()' and `getcwd()' */
116 if (chdir("/") != 0) e(24);
117 if (getcwd(buf
, PATH_MAX
) != buf
) e(25);
118 if (strcmp(buf
, "/") != 0) e(26);
119 if (chdir("..") != 0) e(27); /* go to parent of / */
120 if (getcwd(buf
, PATH_MAX
) != buf
) e(28);
121 if (strcmp(buf
, "/") != 0) e(29);
122 if (chdir(cwd
) != 0) e(30);
123 if (getcwd(buf
, PATH_MAX
) != buf
) e(31);
124 if (strcmp(buf
, cwd
) != 0) e(32);
125 if (chdir("/etc") != 0) e(33); /* /etc might be on RAM */
126 if (getcwd(buf
, PATH_MAX
) != buf
) e(34); /* might make a difference */
127 if (strcmp(buf
, "/etc") != 0) e(35);
128 if (chdir(cwd
) != 0) e(36);
129 if (getcwd(buf
, PATH_MAX
) != buf
) e(37);
130 if (strcmp(buf
, cwd
) != 0) e(38);
131 if (chdir(".//.//") != 0) e(39); /* .//.// == current dir */
132 if (getcwd(buf
, PATH_MAX
) != buf
) e(40);
133 if (strcmp(buf
, cwd
) != 0) e(41); /* we might be at '/' */
135 /* XXX - my_getcwd() is old rubbish. It reads the directory directly instead
136 * of through the directory library. It uses a fixed size buffer instead of
137 * a size related to PATH_MAX, NAME_MAX or the size required.
139 if (my_getcwd(buf
, PATH_MAX
) != buf
) e(42); /* get cwd my way */
140 if (strcmp(cwd
, buf
) != 0) e(43);
142 System("rm -rf foo");
146 { /* Test critical values. */
149 System("rm -rf ../DIR_23/*");
151 /* Fiddle with the size (2nt) parameter of `getcwd ()'. */
152 if (getcwd(cwd
, PATH_MAX
) != cwd
) e(1); /* get cwd */
153 if (getcwd(buf
, strlen(cwd
)) != (char *) 0) e(2); /* size 1 to small */
154 if (errno
!= ERANGE
) e(3);
155 if (getcwd(buf
, PATH_MAX
) != buf
) e(4);
156 if (strcmp(buf
, cwd
) != 0) e(5);
157 Chdir(cwd
); /* getcwd might cd / */
158 if (getcwd(buf
, strlen(cwd
) + 1) != buf
) e(6); /* size just ok */
159 if (getcwd(buf
, PATH_MAX
) != buf
) e(7);
160 if (strcmp(buf
, cwd
) != 0) e(8);
162 /* Let's see how "MaxName" and "ToLongName" are handled. */
163 if (mkdir(MaxName
, 0777) != 0) e(9);
164 if (chdir(MaxName
) != 0) e(10);
165 if (chdir("..") != 0) e(11);
166 if (rmdir(MaxName
) != 0) e(12);
167 if (getcwd(buf
, PATH_MAX
) != buf
) e(13);
168 if (strcmp(buf
, cwd
) != 0) e(14);
169 if (chdir(MaxPath
) != 0) e(15);
170 if (getcwd(buf
, PATH_MAX
) != buf
) e(16);
171 if (strcmp(buf
, cwd
) != 0) e(17);
173 if (chdir(ToLongName
) != -1) e(18);
174 #ifdef _POSIX_NO_TRUNC
175 # if _POSIX_NO_TRUNC - 0 != -1
176 if (errno
!= ENAMETOOLONG
) e(20);
178 if (errno
!= ENOENT
) e(20);
181 # include "error, this case requires dynamic checks and is not handled"
184 if (getcwd(buf
, PATH_MAX
) != buf
) e(21);
185 if (strcmp(buf
, cwd
) != 0) e(22);
186 if (chdir(ToLongPath
) != -1) e(23);
187 if (errno
!= ENAMETOOLONG
) e(24);
188 if (getcwd(buf
, PATH_MAX
) != buf
) e(25);
189 if (strcmp(buf
, cwd
) != 0) e(26);
193 { /* Check reaction to errors */
196 System("rm -rf ../DIR_23/*");
198 if (getcwd(cwd
, PATH_MAX
) != cwd
) e(1); /* get cwd */
200 /* Creat a working dir named "foo", remove any previous residues. */
201 System("rm -rf foo; mkdir foo");
203 /* Check some obviouse errors. */
204 if (chdir("") != -1) e(2);
205 if (errno
!= ENOENT
) e(3);
206 if (getcwd(buf
, PATH_MAX
) != buf
) e(4);
207 if (strcmp(buf
, cwd
) != 0) e(5);
208 if (getcwd(buf
, 0) != (char *) 0) e(6);
209 if (errno
!= EINVAL
) e(7);
210 if (getcwd(buf
, PATH_MAX
) != buf
) e(8);
211 if (strcmp(buf
, cwd
) != 0) e(9);
212 if (getcwd(buf
, 0) != (char *) 0) e(10);
213 if (errno
!= EINVAL
) e(11);
214 if (getcwd(buf
, PATH_MAX
) != buf
) e(12);
215 if (strcmp(buf
, cwd
) != 0) e(13);
216 if (chdir(cwd
) != 0) e(14); /* getcwd might be buggy. */
218 /* Change the mode of foo, and check the effect. */
219 if (chdir("foo") != 0) e(15); /* change to foo */
220 if (mkdir("bar", 0777) != 0) e(16); /* make a new dir bar */
221 if (getcwd(cwd2
, PATH_MAX
) != cwd2
) e(17); /* get the new cwd */
222 if (getcwd(buf
, 3) != (char *) 0) e(18); /* size is too small */
223 if (errno
!= ERANGE
) e(19);
224 if (getcwd(buf
, PATH_MAX
) != buf
) e(20);
225 if (strcmp(buf
, cwd2
) != 0) e(21);
226 Chdir(cwd2
); /* getcwd() might cd / */
227 System("chmod 377 ."); /* make foo unreadable */
228 if (getcwd(buf
, PATH_MAX
) != buf
) e(22); /* dir not readable */
229 if (getcwd(buf
, PATH_MAX
) != buf
) e(23);
230 if (strcmp(buf
, cwd2
) != 0) e(24);
231 if (chdir("bar") != 0) e(25); /* at .../foo/bar */
233 if (getcwd(buf
, PATH_MAX
) != (char *) 0) e(26);
234 if (errno
!= EACCES
) e(27);
237 if (getcwd(buf
, PATH_MAX
) != buf
) e(28);
239 if (chdir(cwd2
) != 0) e(29);
240 if (getcwd(buf
, PATH_MAX
) != buf
) e(30);
241 if (strcmp(buf
, cwd2
) != 0) e(31);
242 System("chmod 677 ."); /* make foo inaccessable */
244 if (getcwd(buf
, PATH_MAX
) != (char *) 0) e(32); /* try to get cwd */
245 if (errno
!= EACCES
) e(33); /* but no access */
246 if (chdir("..") != -1) e(34); /* try to get back */
247 if (errno
!= EACCES
) e(35); /* again no access */
248 if (chdir(cwd
) != 0) e(36); /* get back to cwd */
249 /* `Chdir()' might do path optimizing, it shouldn't. */
250 if (chdir("foo/..") != -1) e(37); /* no op */
251 if (chdir("foo") != -1) e(38); /* try to cd to foo */
252 if (errno
!= EACCES
) e(39); /* no have access */
253 if (getcwd(buf
, PATH_MAX
) != buf
) e(40);
254 if (strcmp(buf
, cwd
) != 0) e(41);
257 if (getcwd(buf
, PATH_MAX
) != buf
) e(42);
258 if (strcmp(buf
, cwd2
) != 0) e(43);
259 if (chdir("..") != 0) e(44); /* get back to cwd */
260 if (chdir("foo") != 0) e(45); /* get back to foo */
261 if (chdir(cwd
) != 0) e(46); /* get back to cwd */
263 if (getcwd(buf
, PATH_MAX
) != buf
) e(47); /* check we are */
264 if (strcmp(buf
, cwd
) != 0) e(48); /* back at cwd. */
265 Chdir(cwd
); /* just in case... */
267 if (chdir("/etc/passwd") != -1) e(49); /* try to change to a file */
268 if (errno
!= ENOTDIR
) e(50);
269 if (getcwd(buf
, PATH_MAX
) != buf
) e(51);
270 if (strcmp(buf
, cwd
) != 0) e(52);
271 if (chdir("/notexist") != -1) e(53);
272 if (errno
!= ENOENT
) e(54);
273 if (getcwd(buf
, PATH_MAX
) != buf
) e(55);
274 if (strcmp(buf
, cwd
) != 0) e(56);
275 System("chmod 777 foo");
276 if (chdir("foo") != 0) e(57);
278 /* XXX - this comment was botched by 'pretty'. */
279 /* * Since `foo' is the cwd, it should not be removeable but * if it
280 * were, this code would be found here; *
282 * System ("cd .. ; rm -rf foo"); remove foo * if (chdir (".")
283 * != -1) e(); try go to. * if (errno != ENOENT) e();
284 * hould not be an entry * if (chdir ("..") != -1) e(); try
285 * to get back * if (errno != ENOENT) e(); should not be
286 * an entry * if (getcwd (buf, PATH_MAX) != (char *)0) e(); don't
287 * know where we are *
289 * What should errno be now ? The cwd might be gone if te superuser *
290 * removed the cwd. (Might even have linked it first.) But this *
291 * testing should be done by the test program for `rmdir()'. */
292 if (chdir(cwd
) != 0) e(58);
299 memset(MaxName
, 'a', NAME_MAX
);
300 MaxName
[NAME_MAX
] = '\0';
301 for (i
= 0; i
< PATH_MAX
- 1; i
++) { /* idem path */
305 MaxPath
[PATH_MAX
- 1] = '\0';
307 strcpy(ToLongName
, MaxName
); /* copy them Max to ToLong */
308 strcpy(ToLongPath
, MaxPath
);
310 ToLongName
[NAME_MAX
] = 'a';
311 ToLongName
[NAME_MAX
+ 1] = '\0'; /* extend ToLongName by one too many */
312 ToLongPath
[PATH_MAX
- 1] = '/';
313 ToLongPath
[PATH_MAX
] = '\0'; /* inc ToLongPath by one */
316 /* The following code, is take from pwd written by Adri Koppes */
318 /* My_getcwd() helper. */
319 char *last_index(string
, ch
)
323 register char *retval
= '\0';
325 while (*string
!= '\0') {
326 if (*string
== ch
) retval
= string
;
332 char *my_getcwd(buf
, size
)
335 { /* should be like getcwd() */
340 struct stat s
, st
, sk
;
343 if (size
<= 0) return(char *) 0;
349 if ((fd
= open("..", O_RDONLY
)) < 0) return(char *) 0;
350 st
.st_dev
= s
.st_dev
;
351 st
.st_ino
= s
.st_ino
;
354 sd
= sizeof(struct direct
);
355 if (s
.st_dev
== st
.st_dev
) {
357 if (read(fd
, (char *) &d
, sd
) < sd
) return(char *) 0;
358 } while (d
.d_ino
!= st
.st_ino
);
361 if (read(fd
, (char *) &d
, sd
) < sd
) return(char *) 0;
363 } while ((sk
.st_dev
!= st
.st_dev
) ||
364 (sk
.st_ino
!= st
.st_ino
));
367 if (strcmp(".", d
.d_name
) != 0) {
369 strcat(name
, d
.d_name
);
371 } while ((s
.st_ino
!= st
.st_ino
) || (s
.st_dev
!= st
.st_dev
));
373 strncat(buf
, "/", size
);
375 while ((n
= last_index(name
, '/')) != NULL
) {
377 strncat(buf
, n
, size
- strlen(buf
));
380 strncat(buf
, name
, size
- strlen(buf
));
381 buf
[size
- 1] = '\0';
382 Chdir(buf
); /* get back there */
389 int err_num
= errno
; /* Save in case printf clobbers it. */
391 printf("Subtest %d, error %d errno=%d: ", subtest
, n
, errno
);
394 if (errct
++ > MAX_ERROR
) {
395 printf("Too many errors; test aborted\n");
397 system("rm -rf DIR*");
406 System("rm -rf DIR_23");
412 printf("%d errors\n", errct
);