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