tst-scandir.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* Basic test for scandir function.
  2. Copyright (C) 2015-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <stdbool.h>
  16. #include <dirent.h>
  17. #include <errno.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #ifndef D
  22. # define D(x) x
  23. #endif
  24. static void prepare (void);
  25. static int do_test (void);
  26. #define PREPARE(argc, argv) prepare ()
  27. #define TEST_FUNCTION do_test ()
  28. #include "../test-skeleton.c"
  29. static const char *scandir_test_dir;
  30. static void
  31. prepare (void)
  32. {
  33. size_t test_dir_len = strlen (test_dir);
  34. static const char dir_name[] = "/tst-scandir.XXXXXX";
  35. size_t dirbuflen = test_dir_len + sizeof (dir_name);
  36. char *dirbuf = malloc (dirbuflen);
  37. if (dirbuf == NULL)
  38. {
  39. puts ("out of memory");
  40. exit (1);
  41. }
  42. snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name);
  43. if (mkdtemp (dirbuf) == NULL)
  44. {
  45. puts ("cannot create temporary directory");
  46. exit (1);
  47. }
  48. add_temp_file (dirbuf);
  49. scandir_test_dir = dirbuf;
  50. }
  51. /* The directory should be empty save the . and .. files. */
  52. static void
  53. verify_empty (const char *dirname)
  54. {
  55. DIR *dir = opendir (dirname);
  56. if (dir == NULL)
  57. {
  58. printf ("opendir (%s): %s\n", dirname, strerror (errno));
  59. exit (1);
  60. }
  61. struct dirent64 *d;
  62. while ((d = readdir64 (dir)) != NULL)
  63. if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0)
  64. {
  65. printf ("temp directory contains file \"%s\"\n", d->d_name);
  66. exit (1);
  67. }
  68. closedir (dir);
  69. }
  70. static void
  71. make_file (const char *dirname, const char *filename)
  72. {
  73. char *name = NULL;
  74. if (asprintf (&name, "%s/%s", dirname, filename) < 0)
  75. {
  76. puts ("out of memory");
  77. exit (1);
  78. }
  79. int fd = open (name, O_WRONLY | O_CREAT | O_EXCL, 0600);
  80. if (fd < 0)
  81. {
  82. printf ("cannot create \"%s\": %s\n", name, strerror (errno));
  83. exit (1);
  84. }
  85. close (fd);
  86. free (name);
  87. }
  88. static void
  89. remove_file (const char *dirname, const char *filename)
  90. {
  91. char *name = NULL;
  92. if (asprintf (&name, "%s/%s", dirname, filename) < 0)
  93. {
  94. puts ("out of memory");
  95. exit (1);
  96. }
  97. remove (name);
  98. free (name);
  99. }
  100. static void
  101. freelist (struct D(dirent) **list, size_t n)
  102. {
  103. for (size_t i = 0; i < n; ++i)
  104. free (list[i]);
  105. free (list);
  106. }
  107. static int
  108. select_a (const struct D(dirent) *d)
  109. {
  110. return d->d_name[0] == 'a';
  111. }
  112. static int
  113. do_test (void)
  114. {
  115. verify_empty (scandir_test_dir);
  116. make_file (scandir_test_dir, "c");
  117. make_file (scandir_test_dir, "aa");
  118. make_file (scandir_test_dir, "b");
  119. make_file (scandir_test_dir, "a");
  120. /* First a basic test with no select or compare functions. */
  121. struct D(dirent) **list;
  122. int n = D(scandir) (scandir_test_dir, &list, NULL, NULL);
  123. if (n < 0)
  124. {
  125. printf ("scandir failed on %s: %s\n",
  126. scandir_test_dir, strerror (errno));
  127. return 1;
  128. }
  129. if (n != 6)
  130. {
  131. printf ("scandir returned %d entries instead of 6\n", n);
  132. return 1;
  133. }
  134. struct
  135. {
  136. const char *name;
  137. bool found;
  138. } expected[] =
  139. {
  140. { ".", },
  141. { "..", },
  142. { "a", },
  143. { "aa", },
  144. { "b", },
  145. { "c", },
  146. };
  147. /* Verify the results, ignoring the order. */
  148. for (int i = 0; i < n; ++i)
  149. {
  150. for (size_t j = 0; j < sizeof expected / sizeof expected[0]; ++j)
  151. if (!strcmp (list[i]->d_name, expected[j].name))
  152. {
  153. expected[j].found = true;
  154. goto found;
  155. }
  156. printf ("scandir yields unexpected entry [%d] \"%s\"\n",
  157. i, list[i]->d_name);
  158. return 1;
  159. found:;
  160. }
  161. for (size_t j = 0; j < sizeof expected / sizeof expected[0]; ++j)
  162. if (!expected[j].found)
  163. {
  164. printf ("failed to produce \"%s\"\n", expected[j].name);
  165. return 1;
  166. }
  167. freelist (list, n);
  168. /* Now a test with a comparison function. */
  169. n = D(scandir) (scandir_test_dir, &list, NULL, &D(alphasort));
  170. if (n < 0)
  171. {
  172. printf ("scandir failed on %s: %s\n",
  173. scandir_test_dir, strerror (errno));
  174. return 1;
  175. }
  176. if (n != 6)
  177. {
  178. printf ("scandir returned %d entries instead of 6\n", n);
  179. return 1;
  180. }
  181. assert (sizeof expected / sizeof expected[0] == 6);
  182. for (int i = 0; i < n; ++i)
  183. if (strcmp (list[i]->d_name, expected[i].name))
  184. {
  185. printf ("scandir yields [%d] of \"%s\", expected \"%s\"\n",
  186. i, list[i]->d_name, expected[i].name);
  187. return 1;
  188. }
  189. freelist (list, n);
  190. /* Now a test with a select function but no comparison function. */
  191. n = D(scandir) (scandir_test_dir, &list, &select_a, NULL);
  192. if (n < 0)
  193. {
  194. printf ("scandir failed on %s: %s\n",
  195. scandir_test_dir, strerror (errno));
  196. return 1;
  197. }
  198. if (n != 2)
  199. {
  200. printf ("scandir returned %d entries instead of 2\n", n);
  201. return 1;
  202. }
  203. if (strcmp (list[0]->d_name, "a") && strcmp (list[0]->d_name, "aa"))
  204. {
  205. printf ("scandir yields [0] \"%s\", expected \"a\" or \"aa\"\n",
  206. list[0]->d_name);
  207. return 1;
  208. }
  209. if (strcmp (list[1]->d_name, "a") && strcmp (list[1]->d_name, "aa"))
  210. {
  211. printf ("scandir yields [1] \"%s\", expected \"a\" or \"aa\"\n",
  212. list[1]->d_name);
  213. return 1;
  214. }
  215. if (!strcmp (list[0]->d_name, list[1]->d_name))
  216. {
  217. printf ("scandir yields \"%s\" twice!\n", list[0]->d_name);
  218. return 1;
  219. }
  220. freelist (list, n);
  221. /* Now a test with both functions. */
  222. n = D(scandir) (scandir_test_dir, &list, &select_a, &D(alphasort));
  223. if (n < 0)
  224. {
  225. printf ("scandir failed on %s: %s\n",
  226. scandir_test_dir, strerror (errno));
  227. return 1;
  228. }
  229. if (n != 2)
  230. {
  231. printf ("scandir returned %d entries instead of 2\n", n);
  232. return 1;
  233. }
  234. if (strcmp (list[0]->d_name, "a") || strcmp (list[1]->d_name, "aa"))
  235. {
  236. printf ("scandir yields {\"%s\", \"%s\"}, expected {\"a\", \"aa\"}\n",
  237. list[0]->d_name, list[1]->d_name);
  238. return 1;
  239. }
  240. freelist (list, n);
  241. /* Clean up the test directory. */
  242. remove_file (scandir_test_dir, "c");
  243. remove_file (scandir_test_dir, "aa");
  244. remove_file (scandir_test_dir, "b");
  245. remove_file (scandir_test_dir, "a");
  246. return 0;
  247. }