1 /* Copyright (C) 2000-2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
32 /* We expect four arguments:
33 - source directory name
35 - common object directory
36 - the program name with path
39 main (int argc, char *argv[])
43 const char *common_objdir;
55 char room [offsetof (struct dirent64, d_name[0]) + NAME_MAX + 1];
67 puts ("not enough parameters");
71 /* Make parameters available with nicer names. */
74 common_objdir = argv[3];
77 /* First test the current source dir. We cannot really compare the
78 result of `getpwd' with the srcdir string but we have other means. */
79 if (stat64 (".", &st1) < 0)
81 printf ("cannot stat starting directory: %m\n");
85 if (chdir (srcdir) < 0)
87 printf ("cannot change to source directory: %m\n");
90 if (stat64 (".", &st2) < 0)
92 printf ("cannot stat source directory: %m\n");
96 /* The two last stat64 calls better were for the same directory. */
97 if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
99 printf ("stat of source directory failed: (%lld,%lld) vs (%lld,%lld)\n",
100 (long long int) st1.st_dev, (long long int) st1.st_ino,
101 (long long int) st2.st_dev, (long long int) st2.st_ino);
105 /* Change to the object directory. */
106 if (chdir (objdir) < 0)
108 printf ("cannot change to object directory: %m\n");
111 if (stat64 (".", &st1) < 0)
113 printf ("cannot stat object directory: %m\n");
116 /* Is this the same we get as with the full path? */
117 if (stat64 (objdir, &st2) < 0)
119 printf ("cannot stat object directory with full path: %m\n");
122 if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
124 printf ("stat of object directory failed: (%lld,%lld) vs (%lld,%lld)\n",
125 (long long int) st1.st_dev, (long long int) st1.st_ino,
126 (long long int) st2.st_dev, (long long int) st2.st_ino);
130 objdir_copy1 = getcwd (NULL, 0);
131 if (objdir_copy1 == NULL)
133 printf ("cannot get current directory name for object directory: %m\n");
137 /* First test: this directory must include our program. */
138 if (stat64 (progpath, &st2) < 0)
140 printf ("cannot stat program: %m\n");
144 dir1 = opendir (".");
147 printf ("cannot open object directory: %m\n");
151 while ((d = readdir64 (dir1)) != NULL)
153 #ifdef _DIRENT_HAVE_D_TYPE
154 if (d->d_type != DT_UNKNOWN && d->d_type != DT_REG)
158 if (d->d_ino == st2.st_ino)
160 /* Might be it. Test the device. We could use the st_dev
161 element from st1 but what the heck, do more testing. */
162 if (stat64 (d->d_name, &st3) < 0)
164 printf ("cannot stat entry from readdir: %m\n");
170 if (st3.st_dev == st2.st_dev)
177 puts ("haven't found program in object directory");
181 /* We leave dir1 open. */
183 /* Stat using file descriptor. */
184 if (fstat64 (dirfd (dir1), &st2) < 0)
186 printf ("cannot fstat object directory: %m\n");
189 if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
191 printf ("fstat of object directory failed: (%lld,%lld) vs (%lld,%lld)\n",
192 (long long int) st1.st_dev, (long long int) st1.st_ino,
193 (long long int) st2.st_dev, (long long int) st2.st_ino);
197 if (chdir ("..") < 0)
199 printf ("cannot go to common object directory with \"..\": %m\n");
203 if (stat64 (".", &st1) < 0)
205 printf ("cannot stat common object directory: %m\n");
208 /* Is this the same we get as with the full path? */
209 if (stat64 (common_objdir, &st2) < 0)
211 printf ("cannot stat common object directory with full path: %m\n");
214 if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
216 printf ("stat of object directory failed: (%lld,%lld) vs (%lld,%lld)\n",
217 (long long int) st1.st_dev, (long long int) st1.st_ino,
218 (long long int) st2.st_dev, (long long int) st2.st_ino);
222 /* Stat using file descriptor. */
223 if (fstat64 (dirfd (dir1), &st2) < 0)
225 printf ("cannot fstat object directory: %m\n");
229 dir2 = opendir (common_objdir);
232 printf ("cannot open common object directory: %m\n");
236 while ((d = readdir64 (dir2)) != NULL)
238 #ifdef _DIRENT_HAVE_D_TYPE
239 if (d->d_type != DT_UNKNOWN && d->d_type != DT_DIR)
243 if (d->d_ino == st2.st_ino)
245 /* Might be it. Test the device. We could use the st_dev
246 element from st1 but what the heck, do more testing. */
247 if (stat64 (d->d_name, &st3) < 0)
249 printf ("cannot stat entry from readdir: %m\n");
255 if (st3.st_dev == st2.st_dev)
260 /* This better should be the object directory again. */
261 if (fchdir (dirfd (dir1)) < 0)
263 printf ("cannot fchdir to object directory: %m\n");
267 objdir_copy2 = getcwd (NULL, 0);
268 if (objdir_copy2 == NULL)
270 printf ("cannot get current directory name for object directory: %m\n");
273 if (strcmp (objdir_copy1, objdir_copy2) != 0)
275 puts ("getcwd returned a different string the second time");
279 /* This better should be the common object directory again. */
280 if (fchdir (dirfd (dir2)) < 0)
282 printf ("cannot fchdir to common object directory: %m\n");
286 if (stat64 (".", &st2) < 0)
288 printf ("cannot stat common object directory: %m\n");
291 if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
293 printf ("stat of object directory failed: (%lld,%lld) vs (%lld,%lld)\n",
294 (long long int) st1.st_dev, (long long int) st1.st_ino,
295 (long long int) st2.st_dev, (long long int) st2.st_ino);
299 buf = (char *) malloc (strlen (objdir_copy1) + 1 + sizeof "tst-dir.XXXXXX");
302 printf ("cannot allocate buffer: %m");
306 stpcpy (stpcpy (stpcpy (buf, objdir_copy1), "/"), "tst-dir.XXXXXX");
307 if (mkdtemp (buf) == NULL)
309 printf ("cannot create test directory in object directory: %m\n");
312 if (stat64 (buf, &st1) < 0)
314 printf ("cannot stat new directory \"%s\": %m\n", buf);
317 if (chmod (buf, 0700) < 0)
319 printf ("cannot change mode of new directory: %m\n");
323 /* Try to find the new directory. */
325 while (readdir64_r (dir1, &direntbuf.d, &d) == 0 && d != NULL)
327 #ifdef _DIRENT_HAVE_D_TYPE
328 if (d->d_type != DT_UNKNOWN && d->d_type != DT_DIR)
332 if (d->d_ino == st1.st_ino)
334 /* Might be it. Test the device. We could use the st_dev
335 element from st1 but what the heck, do more testing. */
336 size_t len = strlen (objdir) + 1 + _D_EXACT_NAMLEN (d) + 1;
339 stpcpy (stpcpy (stpcpy (tmpbuf, objdir), "/"), d->d_name);
341 if (stat64 (tmpbuf, &st3) < 0)
343 printf ("cannot stat entry from readdir: %m\n");
349 if (st3.st_dev == st2.st_dev
350 && strcmp (d->d_name, buf + strlen (buf) - 14) == 0)
357 printf ("haven't found new directory \"%s\"\n", buf);
361 if (closedir (dir2) < 0)
363 printf ("closing dir2 failed: %m\n");
369 printf ("cannot change to new directory: %m\n");
373 dir2 = opendir (buf);
376 printf ("cannot open new directory: %m\n");
380 if (fstat64 (dirfd (dir2), &st2) < 0)
382 printf ("cannot fstat new directory \"%s\": %m\n", buf);
385 if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
387 printf ("stat of new directory failed: (%lld,%lld) vs (%lld,%lld)\n",
388 (long long int) st1.st_dev, (long long int) st1.st_ino,
389 (long long int) st2.st_dev, (long long int) st2.st_ino);
393 if (mkdir ("another-dir", 0777) < 0)
395 printf ("cannot create \"another-dir\": %m\n");
398 fd = open ("and-a-file", O_RDWR | O_CREAT | O_EXCL, 0666);
401 printf ("cannot create \"and-a-file\": %m\n");
406 /* Some tests about error reporting. */
408 if (chdir ("and-a-file") >= 0)
410 printf ("chdir to \"and-a-file\" succeeded\n");
413 if (errno != ENOTDIR)
415 printf ("chdir to \"and-a-file\" didn't set correct error\n");
420 if (chdir ("and-a-file/..") >= 0)
422 printf ("chdir to \"and-a-file/..\" succeeded\n");
425 if (errno != ENOTDIR)
427 printf ("chdir to \"and-a-file/..\" didn't set correct error\n");
432 if (chdir ("another-dir/../and-a-file") >= 0)
434 printf ("chdir to \"another-dir/../and-a-file\" succeeded\n");
437 if (errno != ENOTDIR)
439 printf ("chdir to \"another-dir/../and-a-file\" didn't set correct error\n");
443 /* We now should have a directory and a file in the new directory. */
445 while (readdir64_r (dir2, &direntbuf.d, &d) == 0 && d != NULL)
447 if (strcmp (d->d_name, ".") == 0
448 || strcmp (d->d_name, "..") == 0
449 || strcmp (d->d_name, "another-dir") == 0)
451 #ifdef _DIRENT_HAVE_D_TYPE
452 if (d->d_type != DT_UNKNOWN && d->d_type != DT_DIR)
454 printf ("d_type for \"%s\" is wrong\n", d->d_name);
458 if (stat64 (d->d_name, &st3) < 0)
460 printf ("cannot stat \"%s\" is wrong\n", d->d_name);
463 else if (! S_ISDIR (st3.st_mode))
465 printf ("\"%s\" is no directory\n", d->d_name);
469 else if (strcmp (d->d_name, "and-a-file") == 0)
471 #ifdef _DIRENT_HAVE_D_TYPE
472 if (d->d_type != DT_UNKNOWN && d->d_type != DT_REG)
474 printf ("d_type for \"%s\" is wrong\n", d->d_name);
478 if (stat64 (d->d_name, &st3) < 0)
480 printf ("cannot stat \"%s\" is wrong\n", d->d_name);
483 else if (! S_ISREG (st3.st_mode))
485 printf ("\"%s\" is no regular file\n", d->d_name);
491 printf ("unexpected directory entry \"%s\"\n", d->d_name);
496 if (stat64 ("does-not-exist", &st1) >= 0)
498 puts ("stat for unexisting file did not fail");
502 /* Free all resources. */
504 if (closedir (dir1) < 0)
506 printf ("closing dir1 failed: %m\n");
509 if (closedir (dir2) < 0)
511 printf ("second closing dir2 failed: %m\n");
515 if (rmdir ("another-dir") < 0)
517 printf ("cannot remove \"another-dir\": %m\n");
521 if (unlink ("and-a-file") < 0)
523 printf ("cannot remove \"and-a-file\": %m\n");
527 /* One more test before we leave: mkdir() is supposed to fail with
528 EEXIST if the named file is a symlink. */
529 if (symlink ("a-symlink", "a-symlink") != 0)
531 printf ("cannot create symlink \"a-symlink\": %m\n");
536 if (mkdir ("a-symlink", 0666) == 0)
538 puts ("can make directory \"a-symlink\"");
541 else if (errno != EEXIST)
543 puts ("mkdir(\"a-symlink\") does not fail with EEXIST\n");
546 if (unlink ("a-symlink") < 0)
548 printf ("cannot unlink \"a-symlink\": %m\n");
553 if (chdir (srcdir) < 0)
555 printf ("cannot change back to source directory: %m\n");
561 printf ("cannot remove \"%s\": %m\n", buf);