-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdir.cpp
67 lines (55 loc) · 1.52 KB
/
dir.cpp
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
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <array>
#include "dir.h"
using namespace std;
static bool skip(const char *dirname, int opts) noexcept
{
if (dirname[0] == '.') {
return (!(opts & DW_HIDDEN)) ||
(dirname[1] == '\0') ||
(dirname[1] == '.' && dirname[2] == '\0');
}
return false;
}
static int is_dir(struct dirent *d, const char *name) noexcept
{
struct stat s;
#if defined(DT_DIR) && defined(DT_UNKNOWN)
return d->d_type == DT_DIR ||
(d->d_type == DT_UNKNOWN && stat(name, &s) == 0 && S_ISDIR(s.st_mode));
#else
return stat(name, &s) == 0 && S_ISDIR(s.st_mode);
#endif
}
static void walk(char *name, int opts, function<void(const char*)> func) noexcept
{
auto dir = opendir(name);
auto namelen = strlen(name);
auto endp = name + namelen;
struct dirent *d;
if (!dir) return;
*endp++ = '/';
while ((d = readdir(dir)) != NULL) {
if (skip(d->d_name, opts)) continue;
if (strlen(d->d_name) + namelen >= FILENAME_MAX) continue;
strcpy(endp, d->d_name);
if (is_dir(d, name)) {
if (opts & DW_DIRECTORIES) func(name);
walk(name, opts, func);
}
else if (opts & DW_FILES) {
func(name);
}
}
closedir(dir);
}
void dirwalk(string dirname, int opts, function<void(const char*)> func)
{
char buf[FILENAME_MAX];
strncpy(buf, dirname.c_str(), FILENAME_MAX - 1);
walk(buf, opts, func);
}