-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmain.cpp
More file actions
45 lines (41 loc) · 1.02 KB
/
main.cpp
File metadata and controls
45 lines (41 loc) · 1.02 KB
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
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>
// entry should:
// - be a regular file
// - .d_name len > 4 (.xml)
// - .d_name NOT start with .
// - .d_name ends with .xml
int matchFilename(const struct dirent* entry) {
if (entry->d_type != DT_REG) return false;
if (entry->d_name[0] == '.') return false;
int len = strlen(entry->d_name);
if (len < 5) return false;
if (strcmp(&entry->d_name[len-4], ".xml") == 0) return true;
return false;
}
void printdir(const char* name) {
DIR* dir = opendir(name);
if (dir == NULL) {
perror("opendir");
return;
}
struct dirent* current = readdir(dir);
while (current != 0) {
if (matchFilename(current) ) {
printf(" %s\n", current->d_name);
}
current = readdir(dir);
}
closedir(dir);
}
int main(int argc, char* argv[]) {
if (argc != 2) {
printf("test <dir>\n");
return 1;
}
printdir(argv[1]);
return 0;
}