1 /* Basic test for scandir function.
2 Copyright (C) 2015-2023 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 <https://www.gnu.org/licenses/>. */
30 static void prepare (void);
31 static int do_test (void);
32 #define PREPARE(argc, argv) prepare ()
33 #define TEST_FUNCTION do_test ()
34 #include "../test-skeleton.c"
36 static const char *scandir_test_dir
;
41 size_t test_dir_len
= strlen (test_dir
);
42 static const char dir_name
[] = "/tst-scandir.XXXXXX";
44 size_t dirbuflen
= test_dir_len
+ sizeof (dir_name
);
45 char *dirbuf
= malloc (dirbuflen
);
48 puts ("out of memory");
52 snprintf (dirbuf
, dirbuflen
, "%s%s", test_dir
, dir_name
);
53 if (mkdtemp (dirbuf
) == NULL
)
55 puts ("cannot create temporary directory");
59 add_temp_file (dirbuf
);
60 scandir_test_dir
= dirbuf
;
63 /* The directory should be empty save the . and .. files. */
65 verify_empty (const char *dirname
)
67 DIR *dir
= opendir (dirname
);
70 printf ("opendir (%s): %s\n", dirname
, strerror (errno
));
75 while ((d
= readdir64 (dir
)) != NULL
)
76 if (strcmp (d
->d_name
, ".") != 0 && strcmp (d
->d_name
, "..") != 0)
78 printf ("temp directory contains file \"%s\"\n", d
->d_name
);
86 make_file (const char *dirname
, const char *filename
)
89 if (asprintf (&name
, "%s/%s", dirname
, filename
) < 0)
91 puts ("out of memory");
95 int fd
= open (name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0600);
98 printf ("cannot create \"%s\": %s\n", name
, strerror (errno
));
107 remove_file (const char *dirname
, const char *filename
)
110 if (asprintf (&name
, "%s/%s", dirname
, filename
) < 0)
112 puts ("out of memory");
122 freelist (struct D(dirent
) **list
, size_t n
)
124 for (size_t i
= 0; i
< n
; ++i
)
130 select_a (const struct D(dirent
) *d
)
132 return d
->d_name
[0] == 'a';
138 verify_empty (scandir_test_dir
);
140 make_file (scandir_test_dir
, "c");
141 make_file (scandir_test_dir
, "aa");
142 make_file (scandir_test_dir
, "b");
143 make_file (scandir_test_dir
, "a");
146 /* First a basic test with no select or compare functions. */
148 struct D(dirent
) **list
;
149 int n
= D(scandir
) (scandir_test_dir
, &list
, NULL
, NULL
);
152 printf ("scandir failed on %s: %s\n",
153 scandir_test_dir
, strerror (errno
));
158 printf ("scandir returned %d entries instead of 6\n", n
);
176 /* Verify the results, ignoring the order. */
177 for (int i
= 0; i
< n
; ++i
)
179 for (size_t j
= 0; j
< sizeof expected
/ sizeof expected
[0]; ++j
)
180 if (!strcmp (list
[i
]->d_name
, expected
[j
].name
))
182 expected
[j
].found
= true;
186 printf ("scandir yields unexpected entry [%d] \"%s\"\n",
193 for (size_t j
= 0; j
< sizeof expected
/ sizeof expected
[0]; ++j
)
194 if (!expected
[j
].found
)
196 printf ("failed to produce \"%s\"\n", expected
[j
].name
);
203 /* Now a test with a comparison function. */
205 n
= D(scandir
) (scandir_test_dir
, &list
, NULL
, &D(alphasort
));
208 printf ("scandir failed on %s: %s\n",
209 scandir_test_dir
, strerror (errno
));
214 printf ("scandir returned %d entries instead of 6\n", n
);
218 assert (sizeof expected
/ sizeof expected
[0] == 6);
219 for (int i
= 0; i
< n
; ++i
)
220 if (strcmp (list
[i
]->d_name
, expected
[i
].name
))
222 printf ("scandir yields [%d] of \"%s\", expected \"%s\"\n",
223 i
, list
[i
]->d_name
, expected
[i
].name
);
230 /* Now a test with a select function but no comparison function. */
232 n
= D(scandir
) (scandir_test_dir
, &list
, &select_a
, NULL
);
235 printf ("scandir failed on %s: %s\n",
236 scandir_test_dir
, strerror (errno
));
241 printf ("scandir returned %d entries instead of 2\n", n
);
245 if (strcmp (list
[0]->d_name
, "a") && strcmp (list
[0]->d_name
, "aa"))
247 printf ("scandir yields [0] \"%s\", expected \"a\" or \"aa\"\n",
251 if (strcmp (list
[1]->d_name
, "a") && strcmp (list
[1]->d_name
, "aa"))
253 printf ("scandir yields [1] \"%s\", expected \"a\" or \"aa\"\n",
257 if (!strcmp (list
[0]->d_name
, list
[1]->d_name
))
259 printf ("scandir yields \"%s\" twice!\n", list
[0]->d_name
);
266 /* Now a test with both functions. */
268 n
= D(scandir
) (scandir_test_dir
, &list
, &select_a
, &D(alphasort
));
271 printf ("scandir failed on %s: %s\n",
272 scandir_test_dir
, strerror (errno
));
277 printf ("scandir returned %d entries instead of 2\n", n
);
281 if (strcmp (list
[0]->d_name
, "a") || strcmp (list
[1]->d_name
, "aa"))
283 printf ("scandir yields {\"%s\", \"%s\"}, expected {\"a\", \"aa\"}\n",
284 list
[0]->d_name
, list
[1]->d_name
);
291 /* Clean up the test directory. */
292 remove_file (scandir_test_dir
, "c");
293 remove_file (scandir_test_dir
, "aa");
294 remove_file (scandir_test_dir
, "b");
295 remove_file (scandir_test_dir
, "a");