-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_scandir.c
49 lines (44 loc) · 953 Bytes
/
test_scandir.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
48
49
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int
datesort(const struct dirent **a, const struct dirent **b) {
struct stat st_a, st_b;
if (stat(a[0]->d_name, &st_a)) {
perror("stat failed");
return 0;
}
if (stat(b[0]->d_name, &st_b)) {
perror("stat failed");
return 0;
}
if (st_a.st_mtime > st_b.st_mtime)
return 1;
if (st_a.st_mtime < st_b.st_mtime)
return -1;
return 0;
}
int selector(const struct dirent *ent) {
if(ent->d_type == DT_REG)
return 1;
return 0;
}
int
main(int argc, char *argv[])
{
struct dirent **namelist;
int n;
n = scandir(argv[1], &namelist, selector, datesort);
if (n < 0)
perror("scandir");
else {
while (n--) {
printf("%s\n", namelist[n]->d_name);
free(namelist[n]);
}
free(namelist);
}
}