test-canon2.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #include <errno.h>
  18. #include <string.h>
  19. /* Prototype for our test function. */
  20. extern void do_prepare (int argc, char *argv[]);
  21. extern int do_test (int argc, char *argv[]);
  22. /* We have a preparation function. */
  23. #define PREPARE do_prepare
  24. #include <test-skeleton.c>
  25. /* Name of the temporary files we create. */
  26. char *name1;
  27. char *name2;
  28. /* Preparation. */
  29. void
  30. do_prepare (int argc, char *argv[])
  31. {
  32. size_t test_dir_len;
  33. int tfd;
  34. test_dir_len = strlen (test_dir);
  35. /* Generate the circular symlinks. */
  36. name1 = malloc (test_dir_len + sizeof ("/canonXXXXXX"));
  37. mempcpy (mempcpy (name1, test_dir, test_dir_len),
  38. "/canonXXXXXX", sizeof ("/canonXXXXXX"));
  39. name2 = strdup (name1);
  40. tfd = mkstemp(name1);
  41. if (tfd < 0) {
  42. printf("%s: cannot generate temp file name\n", __FUNCTION__);
  43. exit(1);
  44. }
  45. close(tfd);
  46. add_temp_file (name1);
  47. tfd = mkstemp(name2);
  48. if (tfd < 0) {
  49. printf("%s: cannot generate temp file name\n", __FUNCTION__);
  50. exit(1);
  51. }
  52. close(tfd);
  53. add_temp_file (name2);
  54. }
  55. /* Run the test. */
  56. int
  57. do_test (int argc, char *argv[])
  58. {
  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. }