forked from win32ports/dirent_h
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.c
47 lines (40 loc) · 779 Bytes
/
example.c
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
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <dirent.h>
#include <assert.h>
#include <stdio.h>
static int alphasort_reverse(const void* a, const void* b)
{
return -alphasort(a, b);
}
int main(int argc, char * argv[])
{
DIR* d;
char * path;
if (argc > 1)
path = argv[1];
else
path = "C:\\Windows";
d = opendir(path);
if (!d)
return -1;
struct dirent* entry = NULL;
while (entry = readdir(d))
printf("%s\n", entry->d_name);
closedir(d);
struct dirent** namelist = NULL;
struct dirent** p;
scandir(path, &namelist, NULL, alphasort_reverse);
printf("reversed\n");
for (p = namelist; *p != NULL; ++p)
{
if ((*p)->d_type == DT_DIR)
printf("%s\\\n", (*p)->d_name);
else
printf("%s\n", (*p)->d_name);
free(*p);
}
free(namelist);
return 0;
}