test-canon2.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* Test for realpath/canonicalize function.
  2. Copyright (C) 1998 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
  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.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <errno.h>
  17. #include <string.h>
  18. /* Prototype for our test function. */
  19. extern void do_prepare (int argc, char *argv[]);
  20. extern int do_test (int argc, char *argv[]);
  21. /* We have a preparation function. */
  22. #define PREPARE do_prepare
  23. #include <test-skeleton.c>
  24. /* Name of the temporary files we create. */
  25. char *name1;
  26. char *name2;
  27. /* Preparation. */
  28. void
  29. do_prepare (int argc, char *argv[])
  30. {
  31. size_t test_dir_len;
  32. int tfd;
  33. test_dir_len = strlen (test_dir);
  34. /* Generate the circular symlinks. */
  35. name1 = malloc (test_dir_len + sizeof ("/canonXXXXXX"));
  36. mempcpy (mempcpy (name1, test_dir, test_dir_len),
  37. "/canonXXXXXX", sizeof ("/canonXXXXXX"));
  38. name2 = strdup (name1);
  39. tfd = mkstemp(name1);
  40. if (tfd < 0) {
  41. printf("%s: cannot generate temp file name\n", __FUNCTION__);
  42. exit(1);
  43. }
  44. close(tfd);
  45. add_temp_file (name1);
  46. tfd = mkstemp(name2);
  47. if (tfd < 0) {
  48. printf("%s: cannot generate temp file name\n", __FUNCTION__);
  49. exit(1);
  50. }
  51. close(tfd);
  52. add_temp_file (name2);
  53. }
  54. /* Run the test. */
  55. int
  56. do_test (int argc, char *argv[])
  57. {
  58. #if defined(__GLIBC__) || defined(__UCLIBC__)
  59. char *canon;
  60. printf ("create symlinks from %s to %s and vice versa\n", name1, name2);
  61. if (symlink (name1, name2) == -1
  62. || symlink (name2, name1) == -1)
  63. /* We cannot test this. */
  64. return 0;
  65. /* Call the function. This is equivalent the using `realpath' but the
  66. function allocates the room for the result. */
  67. errno = 0;
  68. canon = canonicalize_file_name (name1);
  69. return canon != NULL || errno != ELOOP;
  70. #else
  71. return 23;
  72. #endif
  73. }