-
Notifications
You must be signed in to change notification settings - Fork 0
/
ForEachFile.c
166 lines (137 loc) · 3.27 KB
/
ForEachFile.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
/* This file is part of the auxiliaries library.
Written by Dick Grune, Vrije Universiteit, Amsterdam.
$Id: ForEachFile.c,v 1.25 2017-12-13 17:41:34 Gebruiker Exp $
*/
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include "ForEachFile.h"
/* Library module source prelude */
#undef _FOREACHFILE_CODE_
#ifndef lint
#define _FOREACHFILE_CODE_
#endif
#ifdef LIB
#define _FOREACHFILE_CODE_
#endif
#ifdef _FOREACHFILE_CODE_
/* Library module source code */
/* TREE SCANNING */
#ifdef S_IFLNK /* system with symbolic links */
#define LSTAT lstat
#else /* S_IFLNK */
#define LSTAT Stat
#endif /* S_IFLNK */
int
is_dirstat(const struct stat *fs) {
if (!fs) return 0;
return ((fs->st_mode & S_IFMT) == S_IFDIR);
}
int
is_Dirname(const Fchar *Fn) {
if (!Fn) return 0;
struct stat stb;
if (LSTAT(Fn, &stb) < 0) return 0;
return ((stb.st_mode & S_IFMT) == S_IFDIR);
}
int
is_Admin_Dirname(const Fchar *Fn) {
if (!Fn) return 0;
return Fnamecmp(Fn, str2Fname(".")) == 0
|| Fnamecmp(Fn, str2Fname("..")) == 0;
}
static void do_dir( /* mutually recursive with do_name() */
Fchar *Fn,
int (*proc)(const Fchar *, const char *, const struct stat *)
);
static void
do_name(Fchar *Fn,
int (*proc)(const Fchar *, const char *, const struct stat *),
int top_level
) {
/* examine Fn */
struct stat fs;
if (LSTAT(Fn, &fs) < 0) {
(void)(*proc)(Fn, strerror(errno), 0);
return;
}
/* report on Fn and get possible return code */
int rc = (*proc)(Fn, (char*)0, &fs);
if (!is_dirstat(&fs)) return;
/* Fn is a directory, so rc may be meaningful */
if (!top_level) if (!rc) return;
#ifdef S_IFLNK
/* don't follow links */
if ((fs.st_mode & S_IFMT) == S_IFLNK) return;
#endif
do_dir(Fn, proc);
}
static void
do_dir(
Fchar *Fn,
int (*proc)(const Fchar *, const char *, const struct stat *)
) {
/* treat directory */
Dir_t *dir = Opendir(Fn);
if (dir == 0) {
(void)(*proc)(Fn, "directory not readable", 0);
return;
}
/* scan new directory */
/* append separator */
int Fn_len = Fnamelen(Fn);
Fn[Fn_len++] = '/';
Fn[Fn_len] = '\0';
/* descend */
Dirent_t *dent;
while ((dent = Readdir(dir)) != (Dirent_t *)0) {
const Fchar *d_name = dent->d_name;
if (is_Admin_Dirname(d_name)) continue;
/* append name */
Fnamecat(Fn, d_name);
do_name(Fn, proc, 0);
/* remove appended name*/
Fn[Fn_len] = '\0';
}
/* remove appended separator*/
Fn[--Fn_len] = '\0';
Closedir(dir);
}
static Fchar MSDOS_sep = (Fchar)'\\';
static Fchar UNIX_sep = (Fchar)'/';
static void
clean_name(Fchar *Fn) {
/* remove a trailing separator */
int Fn_len = Fnamelen(Fn);
if (Fn_len > 1 && (Fn[Fn_len-1] == MSDOS_sep || Fn[Fn_len-1] == UNIX_sep)) {
Fn[Fn_len-1] = '\0';
}
}
/* THE ENTRIES */
void
ForEachFile(
const Fchar *Fname,
int (*proc)(const Fchar *, const char *, const struct stat *)
) {
if (!Fname || !Fname[0] || !proc) return; /* just to make sure */
/* get Fn */
Fchar Fn[MAX_FILE_NAME_LENGTH];
Fnamecpy(Fn, Fname);
clean_name(Fn);
/* top level */
do_name(Fn, proc, 1);
}
/* End library module source code */
#endif /* _FOREACHFILE_CODE_ */
#ifdef lint
static void
satisfy_lint(void *x) {
(void)is_dirstat(0);
(void)is_Dirname(0);
(void)is_Admin_Dirname(0);
ForEachFile(0, 0);
satisfy_lint(x);
}
#endif /* lint */