test-canon2.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. char *canon;
  59. printf ("create symlinks from %s to %s and vice versa\n", name1, name2);
  60. if (symlink (name1, name2) == -1
  61. || symlink (name2, name1) == -1)
  62. /* We cannot test this. */
  63. return 0;
  64. /* Call the function. This is equivalent the using `realpath' but the
  65. function allocates the room for the result. */
  66. errno = 0;
  67. canon = canonicalize_file_name (name1);
  68. return canon != NULL || errno != ELOOP;
  69. }