1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
/*
* Make sure that scandir function works OK.
*
* Copyright (C) 1998-2019 Toni Ronkko
* This file is part of dirent. Dirent may be freely distributed
* under the MIT license. For all details and documentation, see
* https://github.com/tronkko/dirent
*/
/* Silence warning about fopen being insecure (MS Visual Studio) */
#define _CRT_SECURE_NO_WARNINGS
/* Include prototype for versionsort (Linux) */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <time.h>
#include <limits.h>
#undef NDEBUG
#include <assert.h>
/* Filter and sort functions */
static int only_readme(const struct dirent *entry);
static int no_directories(const struct dirent *entry);
static int reverse_alpha(const struct dirent **a, const struct dirent **b);
int
main(int argc, char *argv[])
{
struct dirent **files;
int i;
int n;
(void) argc;
(void) argv;
/* Initialize random number generator */
srand((unsigned) time(NULL));
/* Basic scan with simple filter function */
{
/* Read directory entries */
n = scandir("tests/3", &files, only_readme, alphasort);
assert(n == 1);
/* Make sure that the filter works */
assert(strcmp(files[0]->d_name, "README.txt") == 0);
/* Release file names */
for (i = 0; i < n; i++) {
free(files[i]);
}
free(files);
}
/* Basic scan with default sorting function */
{
/* Read directory entries in alphabetic order */
n = scandir("tests/3", &files, NULL, alphasort);
assert(n == 13);
/* Make sure that we got all the names in the proper order */
assert(strcmp(files[0]->d_name, ".") == 0);
assert(strcmp(files[1]->d_name, "..") == 0);
assert(strcmp(files[2]->d_name, "3zero.dat") == 0);
assert(strcmp(files[3]->d_name, "666.dat") == 0);
assert(strcmp(files[4]->d_name, "Qwerty-my-aunt.dat") == 0);
assert(strcmp(files[5]->d_name, "README.txt") == 0);
assert(strcmp(files[6]->d_name, "aaa.dat") == 0);
assert(strcmp(files[7]->d_name, "dirent.dat") == 0);
assert(strcmp(files[8]->d_name, "empty.dat") == 0);
assert(strcmp(files[9]->d_name, "sane-1.12.0.dat") == 0);
assert(strcmp(files[10]->d_name, "sane-1.2.30.dat") == 0);
assert(strcmp(files[11]->d_name, "sane-1.2.4.dat") == 0);
assert(strcmp(files[12]->d_name, "zebra.dat") == 0);
/* Release file names */
for (i = 0; i < n; i++) {
free(files[i]);
}
free(files);
}
/* Custom filter AND sort function */
{
/* Read directory entries in alphabetic order */
n = scandir("tests/3", &files, no_directories, reverse_alpha);
assert(n == 11);
/* Make sure that we got file names in the reverse order */
assert(strcmp(files[0]->d_name, "zebra.dat") == 0);
assert(strcmp(files[1]->d_name, "sane-1.2.4.dat") == 0);
assert(strcmp(files[2]->d_name, "sane-1.2.30.dat") == 0);
assert(strcmp(files[3]->d_name, "sane-1.12.0.dat") == 0);
assert(strcmp(files[4]->d_name, "empty.dat") == 0);
assert(strcmp(files[5]->d_name, "dirent.dat") == 0);
assert(strcmp(files[6]->d_name, "aaa.dat") == 0);
assert(strcmp(files[7]->d_name, "README.txt") == 0);
assert(strcmp(files[8]->d_name, "Qwerty-my-aunt.dat") == 0);
assert(strcmp(files[9]->d_name, "666.dat") == 0);
assert(strcmp(files[10]->d_name, "3zero.dat") == 0);
/* Release file names */
for (i = 0; i < n; i++) {
free(files[i]);
}
free(files);
}
/* Trying to read from non-existent directory leads to an error */
{
files = NULL;
n = scandir("tests/invalid", &files, NULL, alphasort);
assert(n == -1);
assert(files == NULL);
assert(errno == ENOENT);
}
/* Trying to open file as a directory produces ENOTDIR error */
{
files = NULL;
n = scandir("tests/3/666.dat", &files, NULL, alphasort);
assert(n == -1);
assert(files == NULL);
assert(errno == ENOTDIR);
}
/* Sort files using versionsort() */
{
files = NULL;
n = scandir("tests/3", &files, no_directories, versionsort);
assert(n == 11);
/*
* Make sure that we got all the file names in the proper order:
* 1.2.4 < 1.2.30 < 1.12.0
*/
assert(strcmp(files[0]->d_name, "3zero.dat") == 0);
assert(strcmp(files[1]->d_name, "666.dat") == 0);
assert(strcmp(files[2]->d_name, "Qwerty-my-aunt.dat") == 0);
assert(strcmp(files[3]->d_name, "README.txt") == 0);
assert(strcmp(files[4]->d_name, "aaa.dat") == 0);
assert(strcmp(files[5]->d_name, "dirent.dat") == 0);
assert(strcmp(files[6]->d_name, "empty.dat") == 0);
assert(strcmp(files[7]->d_name, "sane-1.2.4.dat") == 0);
assert(strcmp(files[8]->d_name, "sane-1.2.30.dat") == 0);
assert(strcmp(files[9]->d_name, "sane-1.12.0.dat") == 0);
assert(strcmp(files[10]->d_name, "zebra.dat") == 0);
/* Release file names */
for (i = 0; i < n; i++) {
free(files[i]);
}
free(files);
}
/* Scan large directory */
{
char dirname[PATH_MAX+1];
int i;
int ok;
/* Copy name of temporary directory to variable dirname */
#ifdef WIN32
i = GetTempPathA(PATH_MAX, dirname);
assert(i > 0);
#else
strcpy(dirname, "/tmp/");
i = strlen(dirname);
#endif
/* Append random characters to dirname */
for (int j = 0; j < 10; j++) {
char c;
/* Generate random character */
c = "abcdefghijklmnopqrstuvwxyz"[rand() % 26];
/* Append character to dirname */
assert(i < PATH_MAX);
dirname[i++] = c;
}
/* Terminate directory name */
assert(i < PATH_MAX);
dirname[i] = '\0';
/* Create directory */
#ifdef WIN32
ok = CreateDirectoryA(dirname, NULL);
assert(ok);
#else
ok = mkdir(dirname, 0700);
assert(ok == /*success*/0);
#endif
/* Create one thousand files */
assert(i + 5 < PATH_MAX);
for (int j = 0; j < 1000; j++) {
FILE *fp;
/* Construct file name */
dirname[i] = '/';
dirname[i+1] = 'z';
dirname[i+2] = '0' + ((j / 100) % 10);
dirname[i+3] = '0' + ((j / 10) % 10);
dirname[i+4] = '0' + (j % 10);
dirname[i+5] = '\0';
/* Create file */
fp = fopen(dirname, "w");
assert(fp != NULL);
fclose(fp);
}
/* Cut out the file name part */
dirname[i] = '\0';
/* Scan directory */
n = scandir(dirname, &files, no_directories, alphasort);
assert(n == 1000);
/* Make sure that all 1000 files are read back */
for (int j = 0; j < n; j++) {
char match[100];
/* Construct file name */
match[0] = 'z';
match[1] = '0' + ((j / 100) % 10);
match[2] = '0' + ((j / 10) % 10);
match[3] = '0' + (j % 10);
match[4] = '\0';
/* Make sure that file name matches that on the disk */
assert(strcmp(files[j]->d_name, match) == 0);
}
/* Release file names */
for (int j = 0; j < n; j++) {
free(files[j]);
}
free(files);
}
printf("OK\n");
return EXIT_SUCCESS;
}
/* Only pass README.txt file */
static int
only_readme(const struct dirent *entry)
{
return strcmp(entry->d_name, "README.txt") == 0;
}
/* Filter out directories */
static int
no_directories(const struct dirent *entry)
{
return entry->d_type != DT_DIR;
}
/* Sort in reverse direction */
static int
reverse_alpha(const struct dirent **a, const struct dirent **b)
{
return strcoll((*b)->d_name, (*a)->d_name);
}
|