/
filelist.c
198 lines (162 loc) · 4.24 KB
/
filelist.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
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
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <assert.h>
#include "err.h"
#include "xm.h"
static char **fnams;
static int nfnams, sfnams;
static const char **ignores;
static int num_ignores;
// Recursively add a directory. prefix is the prefix used to store
// each name, as opposed to the real filename given by starting with
// dirname.
static void add_dir(const char *dirname, const char *prefix)
{
DIR *dh;
struct dirent *de;
char *newprefix;
char *newdirname;
char *fname;
if ((dh = opendir(dirname)) == NULL)
err(1, "cannot open directory %s", dirname);
while ((de = readdir(dh)) != NULL)
{
size_t namlen;
int filetype;
// TODO: check for wildcard match & skip
filetype = de->d_type;
namlen = strlen(de->d_name);
// Handle symlinks to regular files (but not directories,
// since that could get us stuck in a loop), and handle
// "unknown" entries. On OpenBSD, I get "unknown" entries
// when working with an ext2fs partition.
// XXX: if the entry is "unknown" and turns out to be a
// symlink, it won't be followed
if (filetype == DT_UNKNOWN || filetype == DT_LNK)
{
struct stat info;
fname = xm(1, strlen(dirname) + 1 + namlen + 1);
sprintf(fname, "%s%s%s", dirname,
strlen(dirname) == 0 ? "" : "/", de->d_name);
if (stat(fname, &info) == -1)
warnx("\rcan't stat file %s, skipping", fname);
else if (filetype == DT_LNK && S_ISDIR(info.st_mode))
{
warnx("\rskipping symlinked directory %s",
fname);
}
else if (S_ISDIR(info.st_mode))
filetype = DT_DIR; // XXX
else if (S_ISREG(info.st_mode))
filetype = DT_REG; // XXX
else
warnx("\rskipping non-regular file %s", fname);
free(fname);
fname = NULL;
}
if (filetype == DT_REG)
{
fname = xm(1, strlen(prefix) + 1 + namlen + 1);
sprintf(fname, "%s%s%s", prefix,
strlen(prefix) == 0 ? "" : "/", de->d_name);
XPND(fnams, nfnams, sfnams);
fnams[nfnams++] = fname;
}
else if (filetype == DT_DIR)
{
if (strcmp(de->d_name, ".") == 0
|| strcmp(de->d_name, "..") == 0)
{
continue;
}
newprefix = xm(1, strlen(prefix) + 1
+ namlen + 1);
sprintf(newprefix, "%s%s%s", prefix,
strlen(prefix) == 0 ? "" : "/", de->d_name);
newdirname = xm(1, strlen(dirname) + 1
+ namlen + 1);
sprintf(newdirname, "%s/%s", dirname, de->d_name);
add_dir(newdirname, newprefix);
free(newprefix);
free(newdirname);
}
else
{
warnx("\rskipping non-regular file %s%s%s (type %d)",
dirname,
strlen(dirname) == 0 ? "" : "/",
de->d_name,
filetype);
}
}
if (closedir(dh) == -1)
err(1, "cannot close directory %s", dirname);
}
int mystrcmp(const void *one, const void *two)
{
return strcmp(*(const char **)one, *(const char **)two);
}
#undef MIN
#define MIN(x,y) ((x) < (y) ? (x) : (y))
// Version that compares extensions before rest of filenames.
int extstrcmp(const void *one, const void *two)
{
const char *s1, *s2;
const char *ext1, *ext2;
const char *p;
int ret;
int x1, x2;
s1 = *(const char **)one;
s2 = *(const char **)two;
// Compare paths first. x1, x2 = length of path component.
x1 = 0;
p = strrchr(s1, '/');
if (p != NULL)
x1 = p - s1 + 1;
x2 = 0;
p = strrchr(s1, '/');
if (p != NULL)
x2 = p - s1 + 1;
ret = strncmp(s1, s2, MIN(x1, x2));
if (ret != 0)
return ret;
// Path components match, so compare file extensions.
ext1 = strrchr(s1, '.');
if (ext1 == NULL) ext1 = s1;
ext2 = strrchr(s2, '.');
if (ext2 == NULL) ext2 = s2;
ret = strcmp(ext1, ext2);
if (ret != 0)
return ret;
// Extensions match, so compare whole filename.
return strcmp(s1, s2);
}
void getfilelist(const char ***files, int *numfiles, const char *dirname,
int sort_by_ext,
const char **ignore_patterns, int num_ignore_patterns)
{
assert(fnams == NULL);
assert(nfnams == 0);
assert(sfnams == 0);
ignores = ignore_patterns;
num_ignores = num_ignore_patterns;
add_dir(dirname, "");
qsort(fnams, nfnams, sizeof fnams[0],
sort_by_ext ? extstrcmp : mystrcmp);
*files = (const char **)fnams;
*numfiles = nfnams;
}
void freefilelist(void)
{
int ix;
for (ix = 0; ix < nfnams; ix++)
free(fnams[ix]);
free(fnams);
nfnams = sfnams = 0;
fnams = NULL;
}