1 /* Basic test for scandir function.
2 Copyright (C) 2015-2016 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/>. */
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
;
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
);
47 puts ("out of memory");
51 snprintf (dirbuf
, dirbuflen
, "%s%s", test_dir
, dir_name
);
52 if (mkdtemp (dirbuf
) == NULL
)
54 puts ("cannot create temporary directory");
58 add_temp_file (dirbuf
);
59 scandir_test_dir
= dirbuf
;
62 /* The directory should be empty save the . and .. files. */
64 verify_empty (const char *dirname
)
66 DIR *dir
= opendir (dirname
);
69 printf ("opendir (%s): %s\n", dirname
, strerror (errno
));
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
);
85 make_file (const char *dirname
, const char *filename
)
88 if (asprintf (&name
, "%s/%s", dirname
, filename
) < 0)
90 puts ("out of memory");
94 int fd
= open (name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0600);
97 printf ("cannot create \"%s\": %s\n", name
, strerror (errno
));
106 remove_file (const char *dirname
, const char *filename
)
109 if (asprintf (&name
, "%s/%s", dirname
, filename
) < 0)
111 puts ("out of memory");
121 freelist (struct D(dirent
) **list
, size_t n
)
123 for (size_t i
= 0; i
< n
; ++i
)
129 select_a (const struct D(dirent
) *d
)
131 return d
->d_name
[0] == 'a';
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
);
151 printf ("scandir failed on %s: %s\n",
152 scandir_test_dir
, strerror (errno
));
157 printf ("scandir returned %d entries instead of 6\n", n
);
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;
185 printf ("scandir yields unexpected entry [%d] \"%s\"\n",
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
);
202 /* Now a test with a comparison function. */
204 n
= D(scandir
) (scandir_test_dir
, &list
, NULL
, &D(alphasort
));
207 printf ("scandir failed on %s: %s\n",
208 scandir_test_dir
, strerror (errno
));
213 printf ("scandir returned %d entries instead of 6\n", n
);
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
);
229 /* Now a test with a select function but no comparison function. */
231 n
= D(scandir
) (scandir_test_dir
, &list
, &select_a
, NULL
);
234 printf ("scandir failed on %s: %s\n",
235 scandir_test_dir
, strerror (errno
));
240 printf ("scandir returned %d entries instead of 2\n", n
);
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",
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",
256 if (!strcmp (list
[0]->d_name
, list
[1]->d_name
))
258 printf ("scandir yields \"%s\" twice!\n", list
[0]->d_name
);
265 /* Now a test with both functions. */
267 n
= D(scandir
) (scandir_test_dir
, &list
, &select_a
, &D(alphasort
));
270 printf ("scandir failed on %s: %s\n",
271 scandir_test_dir
, strerror (errno
));
276 printf ("scandir returned %d entries instead of 2\n", n
);
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
);
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
);