S390: Add new s390 platform.
[glibc.git] / dirent / tst-scandir.c
bloba7ad9b62846eb86881183ac059bc431dd5062c02
1 /* Basic test for scandir function.
2 Copyright (C) 2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #include <dirent.h>
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #ifndef D
26 # define D(x) x
27 #endif
29 static void prepare (void);
30 static int do_test (void);
31 #define PREPARE(argc, argv) prepare ()
32 #define TEST_FUNCTION do_test ()
33 #include "../test-skeleton.c"
35 static const char *scandir_test_dir;
37 static void
38 prepare (void)
40 size_t test_dir_len = strlen (test_dir);
41 static const char dir_name[] = "/tst-scandir.XXXXXX";
43 size_t dirbuflen = test_dir_len + sizeof (dir_name);
44 char *dirbuf = malloc (dirbuflen);
45 if (dirbuf == NULL)
47 puts ("out of memory");
48 exit (1);
51 snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name);
52 if (mkdtemp (dirbuf) == NULL)
54 puts ("cannot create temporary directory");
55 exit (1);
58 add_temp_file (dirbuf);
59 scandir_test_dir = dirbuf;
62 /* The directory should be empty save the . and .. files. */
63 static void
64 verify_empty (const char *dirname)
66 DIR *dir = opendir (dirname);
67 if (dir == NULL)
69 printf ("opendir (%s): %s\n", dirname, strerror (errno));
70 exit (1);
73 struct dirent64 *d;
74 while ((d = readdir64 (dir)) != NULL)
75 if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0)
77 printf ("temp directory contains file \"%s\"\n", d->d_name);
78 exit (1);
81 closedir (dir);
84 static void
85 make_file (const char *dirname, const char *filename)
87 char *name = NULL;
88 if (asprintf (&name, "%s/%s", dirname, filename) < 0)
90 puts ("out of memory");
91 exit (1);
94 int fd = open (name, O_WRONLY | O_CREAT | O_EXCL, 0600);
95 if (fd < 0)
97 printf ("cannot create \"%s\": %s\n", name, strerror (errno));
98 exit (1);
100 close (fd);
102 free (name);
105 static void
106 remove_file (const char *dirname, const char *filename)
108 char *name = NULL;
109 if (asprintf (&name, "%s/%s", dirname, filename) < 0)
111 puts ("out of memory");
112 exit (1);
115 remove (name);
117 free (name);
120 static void
121 freelist (struct D(dirent) **list, size_t n)
123 for (size_t i = 0; i < n; ++i)
124 free (list[i]);
125 free (list);
128 static int
129 select_a (const struct D(dirent) *d)
131 return d->d_name[0] == 'a';
134 static int
135 do_test (void)
137 verify_empty (scandir_test_dir);
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");
145 /* First a basic test with no select or compare functions. */
147 struct D(dirent) **list;
148 int n = D(scandir) (scandir_test_dir, &list, NULL, NULL);
149 if (n < 0)
151 printf ("scandir failed on %s: %s\n",
152 scandir_test_dir, strerror (errno));
153 return 1;
155 if (n != 6)
157 printf ("scandir returned %d entries instead of 6\n", n);
158 return 1;
161 struct
163 const char *name;
164 bool found;
165 } expected[] =
167 { ".", },
168 { "..", },
169 { "a", },
170 { "aa", },
171 { "b", },
172 { "c", },
175 /* Verify the results, ignoring the order. */
176 for (int i = 0; i < n; ++i)
178 for (size_t j = 0; j < sizeof expected / sizeof expected[0]; ++j)
179 if (!strcmp (list[i]->d_name, expected[j].name))
181 expected[j].found = true;
182 goto found;
185 printf ("scandir yields unexpected entry [%d] \"%s\"\n",
186 i, list[i]->d_name);
187 return 1;
189 found:;
192 for (size_t j = 0; j < sizeof expected / sizeof expected[0]; ++j)
193 if (!expected[j].found)
195 printf ("failed to produce \"%s\"\n", expected[j].name);
196 return 1;
199 freelist (list, n);
202 /* Now a test with a comparison function. */
204 n = D(scandir) (scandir_test_dir, &list, NULL, &D(alphasort));
205 if (n < 0)
207 printf ("scandir failed on %s: %s\n",
208 scandir_test_dir, strerror (errno));
209 return 1;
211 if (n != 6)
213 printf ("scandir returned %d entries instead of 6\n", n);
214 return 1;
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))
221 printf ("scandir yields [%d] of \"%s\", expected \"%s\"\n",
222 i, list[i]->d_name, expected[i].name);
223 return 1;
226 freelist (list, n);
229 /* Now a test with a select function but no comparison function. */
231 n = D(scandir) (scandir_test_dir, &list, &select_a, NULL);
232 if (n < 0)
234 printf ("scandir failed on %s: %s\n",
235 scandir_test_dir, strerror (errno));
236 return 1;
238 if (n != 2)
240 printf ("scandir returned %d entries instead of 2\n", n);
241 return 1;
244 if (strcmp (list[0]->d_name, "a") && strcmp (list[0]->d_name, "aa"))
246 printf ("scandir yields [0] \"%s\", expected \"a\" or \"aa\"\n",
247 list[0]->d_name);
248 return 1;
250 if (strcmp (list[1]->d_name, "a") && strcmp (list[1]->d_name, "aa"))
252 printf ("scandir yields [1] \"%s\", expected \"a\" or \"aa\"\n",
253 list[1]->d_name);
254 return 1;
256 if (!strcmp (list[0]->d_name, list[1]->d_name))
258 printf ("scandir yields \"%s\" twice!\n", list[0]->d_name);
259 return 1;
262 freelist (list, n);
265 /* Now a test with both functions. */
267 n = D(scandir) (scandir_test_dir, &list, &select_a, &D(alphasort));
268 if (n < 0)
270 printf ("scandir failed on %s: %s\n",
271 scandir_test_dir, strerror (errno));
272 return 1;
274 if (n != 2)
276 printf ("scandir returned %d entries instead of 2\n", n);
277 return 1;
280 if (strcmp (list[0]->d_name, "a") || strcmp (list[1]->d_name, "aa"))
282 printf ("scandir yields {\"%s\", \"%s\"}, expected {\"a\", \"aa\"}\n",
283 list[0]->d_name, list[1]->d_name);
284 return 1;
287 freelist (list, n);
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);
297 return 0;