1 /* Copyright (C) 2000, 2001 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
31 /* We expect four arguments:
32 - source directory name
34 - common object directory
35 - the program name with path
38 main (int argc
, char *argv
[])
42 const char *common_objdir
;
51 struct dirent64 direntbuf
;
61 puts ("not enough parameters");
65 /* Make parameters available with nicer names. */
68 common_objdir
= argv
[3];
71 /* First test the current source dir. We cannot really compare the
72 result of `getpwd' with the srcdir string but we have other means. */
73 if (stat64 (".", &st1
) < 0)
75 printf ("cannot stat starting directory: %m\n");
79 if (chdir (srcdir
) < 0)
81 printf ("cannot change to source directory: %m\n");
84 if (stat64 (".", &st2
) < 0)
86 printf ("cannot stat source directory: %m\n");
90 /* The two last stat64 calls better were for the same directory. */
91 if (st1
.st_dev
!= st2
.st_dev
|| st1
.st_ino
!= st2
.st_ino
)
93 printf ("stat of source directory failed: (%lld,%lld) vs (%lld,%lld)\n",
94 (long long int) st1
.st_dev
, (long long int) st1
.st_ino
,
95 (long long int) st2
.st_dev
, (long long int) st2
.st_ino
);
99 /* Change to the object directory. */
100 if (chdir (objdir
) < 0)
102 printf ("cannot change to object directory: %m\n");
105 if (stat64 (".", &st1
) < 0)
107 printf ("cannot stat object directory: %m\n");
110 /* Is this the same we get as with the full path? */
111 if (stat64 (objdir
, &st2
) < 0)
113 printf ("cannot stat object directory with full path: %m\n");
116 if (st1
.st_dev
!= st2
.st_dev
|| st1
.st_ino
!= st2
.st_ino
)
118 printf ("stat of object directory failed: (%lld,%lld) vs (%lld,%lld)\n",
119 (long long int) st1
.st_dev
, (long long int) st1
.st_ino
,
120 (long long int) st2
.st_dev
, (long long int) st2
.st_ino
);
124 objdir_copy1
= getcwd (NULL
, 0);
125 if (objdir_copy1
== NULL
)
127 printf ("cannot get current directory name for object directory: %m\n");
131 /* First test: this directory must include our program. */
132 if (stat64 (progpath
, &st2
) < 0)
134 printf ("cannot stat program: %m\n");
138 dir1
= opendir (".");
141 printf ("cannot open object directory: %m\n");
145 while ((d
= readdir64 (dir1
)) != NULL
)
147 #ifdef _DIRENT_HAVE_D_TYPE
148 if (d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_REG
)
152 if (d
->d_ino
== st2
.st_ino
)
154 /* Might be it. Test the device. We could use the st_dev
155 element from st1 but what the heck, do more testing. */
156 if (stat64 (d
->d_name
, &st3
) < 0)
158 printf ("cannot stat entry from readdir: %m\n");
164 if (st3
.st_dev
== st2
.st_dev
)
171 puts ("haven't found program in object directory");
175 /* We leave dir1 open. */
177 /* Stat using file descriptor. */
178 if (fstat64 (dirfd (dir1
), &st2
) < 0)
180 printf ("cannot fstat object directory: %m\n");
183 if (st1
.st_dev
!= st2
.st_dev
|| st1
.st_ino
!= st2
.st_ino
)
185 printf ("fstat of object directory failed: (%lld,%lld) vs (%lld,%lld)\n",
186 (long long int) st1
.st_dev
, (long long int) st1
.st_ino
,
187 (long long int) st2
.st_dev
, (long long int) st2
.st_ino
);
191 if (chdir ("..") < 0)
193 printf ("cannot go to common object directory with \"..\": %m\n");
197 if (stat64 (".", &st1
) < 0)
199 printf ("cannot stat common object directory: %m\n");
202 /* Is this the same we get as with the full path? */
203 if (stat64 (common_objdir
, &st2
) < 0)
205 printf ("cannot stat common object directory with full path: %m\n");
208 if (st1
.st_dev
!= st2
.st_dev
|| st1
.st_ino
!= st2
.st_ino
)
210 printf ("stat of object directory failed: (%lld,%lld) vs (%lld,%lld)\n",
211 (long long int) st1
.st_dev
, (long long int) st1
.st_ino
,
212 (long long int) st2
.st_dev
, (long long int) st2
.st_ino
);
216 /* Stat using file descriptor. */
217 if (fstat64 (dirfd (dir1
), &st2
) < 0)
219 printf ("cannot fstat object directory: %m\n");
223 dir2
= opendir (common_objdir
);
226 printf ("cannot open common object directory: %m\n");
230 while ((d
= readdir64 (dir2
)) != NULL
)
232 #ifdef _DIRENT_HAVE_D_TYPE
233 if (d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_DIR
)
237 if (d
->d_ino
== st2
.st_ino
)
239 /* Might be it. Test the device. We could use the st_dev
240 element from st1 but what the heck, do more testing. */
241 if (stat64 (d
->d_name
, &st3
) < 0)
243 printf ("cannot stat entry from readdir: %m\n");
249 if (st3
.st_dev
== st2
.st_dev
)
254 /* This better should be the object directory again. */
255 if (fchdir (dirfd (dir1
)) < 0)
257 printf ("cannot fchdir to object directory: %m\n");
261 objdir_copy2
= getcwd (NULL
, 0);
262 if (objdir_copy2
== NULL
)
264 printf ("cannot get current directory name for object directory: %m\n");
267 if (strcmp (objdir_copy1
, objdir_copy2
) != 0)
269 puts ("getcwd returned a different string the second time");
273 /* This better should be the common object directory again. */
274 if (fchdir (dirfd (dir2
)) < 0)
276 printf ("cannot fchdir to common object directory: %m\n");
280 if (stat64 (".", &st2
) < 0)
282 printf ("cannot stat common object directory: %m\n");
285 if (st1
.st_dev
!= st2
.st_dev
|| st1
.st_ino
!= st2
.st_ino
)
287 printf ("stat of object directory failed: (%lld,%lld) vs (%lld,%lld)\n",
288 (long long int) st1
.st_dev
, (long long int) st1
.st_ino
,
289 (long long int) st2
.st_dev
, (long long int) st2
.st_ino
);
293 buf
= (char *) malloc (strlen (objdir_copy1
) + 1 + sizeof "tst-dir.XXXXXX");
296 printf ("cannot allocate buffer: %m");
300 stpcpy (stpcpy (stpcpy (buf
, objdir_copy1
), "/"), "tst-dir.XXXXXX");
301 if (mkdtemp (buf
) == NULL
)
303 printf ("cannot create test directory in object directory: %m\n");
306 if (stat64 (buf
, &st1
) < 0)
308 printf ("cannot stat new directory \"%s\": %m\n", buf
);
311 if (chmod (buf
, 0700) < 0)
313 printf ("cannot change mode of new directory: %m\n");
317 /* Try to find the new directory. */
319 while (readdir64_r (dir1
, &direntbuf
, &d
) == 0 && d
!= NULL
)
321 #ifdef _DIRENT_HAVE_D_TYPE
322 if (d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_DIR
)
326 if (d
->d_ino
== st1
.st_ino
)
328 /* Might be it. Test the device. We could use the st_dev
329 element from st1 but what the heck, do more testing. */
330 size_t len
= strlen (objdir
) + 1 + _D_EXACT_NAMLEN (d
) + 1;
333 stpcpy (stpcpy (stpcpy (tmpbuf
, objdir
), "/"), d
->d_name
);
335 if (stat64 (tmpbuf
, &st3
) < 0)
337 printf ("cannot stat entry from readdir: %m\n");
343 if (st3
.st_dev
== st2
.st_dev
344 && strcmp (d
->d_name
, buf
+ strlen (buf
) - 14) == 0)
351 printf ("haven't found new directory \"%s\"\n", buf
);
355 if (closedir (dir2
) < 0)
357 printf ("closing dir2 failed: %m\n");
363 printf ("cannot change to new directory: %m\n");
367 dir2
= opendir (buf
);
370 printf ("cannot open new directory: %m\n");
374 if (fstat64 (dirfd (dir2
), &st2
) < 0)
376 printf ("cannot fstat new directory \"%s\": %m\n", buf
);
379 if (st1
.st_dev
!= st2
.st_dev
|| st1
.st_ino
!= st2
.st_ino
)
381 printf ("stat of new directory failed: (%lld,%lld) vs (%lld,%lld)\n",
382 (long long int) st1
.st_dev
, (long long int) st1
.st_ino
,
383 (long long int) st2
.st_dev
, (long long int) st2
.st_ino
);
387 if (mkdir ("another-dir", 0777) < 0)
389 printf ("cannot create \"another-dir\": %m\n");
392 fd
= open ("and-a-file", O_RDWR
| O_CREAT
| O_EXCL
, 0666);
395 printf ("cannot create \"and-a-file\": %m\n");
400 /* Some tests about error reporting. */
402 if (chdir ("and-a-file") >= 0)
404 printf ("chdir to \"and-a-file\" succeeded\n");
407 if (errno
!= ENOTDIR
)
409 printf ("chdir to \"and-a-file\" didn't set correct error\n");
414 if (chdir ("and-a-file/..") >= 0)
416 printf ("chdir to \"and-a-file/..\" succeeded\n");
419 if (errno
!= ENOTDIR
)
421 printf ("chdir to \"and-a-file/..\" didn't set correct error\n");
426 if (chdir ("another-dir/../and-a-file") >= 0)
428 printf ("chdir to \"another-dir/../and-a-file\" succeeded\n");
431 if (errno
!= ENOTDIR
)
433 printf ("chdir to \"another-dir/../and-a-file\" didn't set correct error\n");
437 /* We now should have a directory and a file in the new directory. */
439 while (readdir64_r (dir2
, &direntbuf
, &d
) == 0 && d
!= NULL
)
441 if (strcmp (d
->d_name
, ".") == 0
442 || strcmp (d
->d_name
, "..") == 0
443 || strcmp (d
->d_name
, "another-dir") == 0)
445 #ifdef _DIRENT_HAVE_D_TYPE
446 if (d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_DIR
)
448 printf ("d_type for \"%s\" is wrong\n", d
->d_name
);
452 if (stat64 (d
->d_name
, &st3
) < 0)
454 printf ("cannot stat \"%s\" is wrong\n", d
->d_name
);
457 else if (! S_ISDIR (st3
.st_mode
))
459 printf ("\"%s\" is no directory\n", d
->d_name
);
463 else if (strcmp (d
->d_name
, "and-a-file") == 0)
465 #ifdef _DIRENT_HAVE_D_TYPE
466 if (d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_REG
)
468 printf ("d_type for \"%s\" is wrong\n", d
->d_name
);
472 if (stat64 (d
->d_name
, &st3
) < 0)
474 printf ("cannot stat \"%s\" is wrong\n", d
->d_name
);
477 else if (! S_ISREG (st3
.st_mode
))
479 printf ("\"%s\" is no regular file\n", d
->d_name
);
485 printf ("unexpected directory entry \"%s\"\n", d
->d_name
);
490 if (stat64 ("does-not-exist", &st1
) >= 0)
492 puts ("stat for unexisting file did not fail");
496 /* Free all resources. */
498 if (closedir (dir1
) < 0)
500 printf ("closing dir1 failed: %m\n");
503 if (closedir (dir2
) < 0)
505 printf ("second closing dir2 failed: %m\n");
509 if (rmdir ("another-dir") < 0)
511 printf ("cannot remove \"another-dir\": %m\n");
515 if (unlink ("and-a-file") < 0)
517 printf ("cannot remove \"and-a-file\": %m\n");
521 /* One more test before we leave: mkdir() is supposed to fail with
522 EEXIST if the named file is a symlink. */
523 if (symlink ("a-symlink", "a-symlink") != 0)
525 printf ("cannot create symlink \"a-symlink\": %m\n");
530 if (mkdir ("a-symlink", 0666) == 0)
532 puts ("can make directory \"a-symlink\"");
535 else if (errno
!= EEXIST
)
537 puts ("mkdir(\"a-symlink\") does not fail with EEXIST\n");
540 if (unlink ("a-symlink") < 0)
542 printf ("cannot unlink \"a-symlink\": %m\n");
547 if (chdir (srcdir
) < 0)
549 printf ("cannot change back to source directory: %m\n");
555 printf ("cannot remove \"%s\": %m\n", buf
);