tst-getcwd.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* Test of getcwd function.
  2. Copyright (C) 2000-2018 Free Software Foundation, Inc.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #include <sys/param.h>
  20. #define TEST_FUNCTION do_test ()
  21. static int
  22. do_test (void)
  23. {
  24. char thepath[4096]; /* Yes, this limits the environment this test
  25. can run it but I honestly don't care about
  26. people which have this problem. */
  27. char *bufs[10];
  28. size_t lens[10];
  29. size_t sbs;
  30. size_t len, i;
  31. if (getcwd (thepath, sizeof thepath) == NULL)
  32. {
  33. if (errno == ERANGE)
  34. /* The path is too long, skip all tests. */
  35. return 0;
  36. puts ("getcwd (thepath, sizeof thepath) failed");
  37. return 1;
  38. }
  39. len = strlen (thepath);
  40. sbs = 1;
  41. while (sbs < len + 1)
  42. sbs <<= 1;
  43. for (i = 0; i < 4; ++i)
  44. {
  45. lens[i] = sbs;
  46. bufs[i] = (char *) malloc (sbs);
  47. }
  48. bufs[i] = getcwd (NULL, sbs);
  49. lens[i] = sbs;
  50. if (bufs[i] == NULL)
  51. {
  52. puts ("getcwd (NULL, sbs) failed");
  53. return 1;
  54. }
  55. ++i;
  56. for (; i < 10; sbs >>= 1, ++i)
  57. {
  58. bufs[i] = (char *) malloc (MAX (1, sbs));
  59. lens[i] = sbs;
  60. }
  61. /* Before we test the result write something in the memory to see
  62. whether the allocation went right. */
  63. for (i = 0; i < 10; ++i)
  64. if (i != 4 && bufs[i] != NULL)
  65. memset (bufs[i], '\xff', lens[i]);
  66. if (strcmp (thepath, bufs[4]) != 0)
  67. {
  68. printf ("\
  69. getcwd (NULL, sbs) = \"%s\", getcwd (thepath, sizeof thepath) = \"%s\"\n",
  70. bufs[4], thepath);
  71. return 1;
  72. }
  73. /* Now overwrite all buffers to see that getcwd allocated the buffer
  74. of right size. */
  75. for (i = 0; i < 10; ++i)
  76. memset (bufs[i], i, lens[i]);
  77. for (i = 0; i < 10; ++i)
  78. free (bufs[i]);
  79. /* Test whether the function signals success despite the buffer
  80. being too small. */
  81. if (getcwd (NULL, len) != NULL)
  82. {
  83. puts ("getcwd (NULL, len) didn't failed");
  84. return 1;
  85. }
  86. bufs[0] = malloc (len);
  87. bufs[1] = malloc (len);
  88. bufs[2] = malloc (len);
  89. if (bufs[1] != NULL)
  90. {
  91. if (getcwd (bufs[1], len) != NULL)
  92. {
  93. puts ("getcwd (bufs[1], len) didn't failed");
  94. return 1;
  95. }
  96. free (bufs[0]);
  97. free (bufs[1]);
  98. free (bufs[2]);
  99. }
  100. memset (thepath, '\xfe', sizeof (thepath));
  101. if (getcwd (thepath, len) != NULL)
  102. {
  103. puts ("getcwd (thepath, len) didn't failed");
  104. return 1;
  105. }
  106. for (i = len; i < sizeof thepath; ++i)
  107. if (thepath[i] != '\xfe')
  108. {
  109. puts ("thepath[i] != '\xfe'");
  110. return 1;
  111. }
  112. /* Now test handling of correctly sized buffers. */
  113. bufs[0] = getcwd (NULL, len + 1);
  114. if (bufs[0] == NULL)
  115. {
  116. puts ("getcwd (NULL, len + 1) failed");
  117. return 1;
  118. }
  119. free (bufs[0]);
  120. memset (thepath, '\xff', sizeof thepath);
  121. if (getcwd (thepath, len + 1) == NULL)
  122. {
  123. puts ("getcwd (thepath, len + 1) failed");
  124. return 1;
  125. }
  126. for (i = len + 1; i < sizeof thepath; ++i)
  127. if (thepath[i] != '\xff')
  128. {
  129. printf ("thepath[%zd] != '\xff'\n", i);
  130. return 1;
  131. }
  132. puts ("everything OK");
  133. return 0;
  134. }
  135. #include "../test-skeleton.c"