-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_find.c
330 lines (310 loc) · 8.01 KB
/
simple_find.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#define _BSD_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <strings.h>
#include <features.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "limit_fork.h"
#define NONDIR_TYPE 0
#define DIR_TYPE 1
#define CHKTYPE_ERROR -1
#define DEFAULT_DICT_SIZE 3
#define DEFAULT_BUF_SIZE 100
#define DEFAULT_TABLE_SIZE 10
struct table;
struct entry
{
int type;
char *filename;
};
struct table
{
int size;
struct entry **table_entry;
};
void *checked_malloc(size_t size)
{
void *p = malloc(size);
if (!p) {
perror("");
exit(-1);
}
return p;
}
void *checked_realloc(void *p, size_t size)
{
void *q = realloc(p, size);
if (!q) {
perror("");
exit(-1);
}
return q;
}
int check_type(char *pathname)
{
struct stat st;
if (lstat(pathname, &st)) {
perror(pathname);
return CHKTYPE_ERROR;
}
if (S_ISDIR(st.st_mode))
return DIR_TYPE;
return NONDIR_TYPE;
}
struct entry *process_file(char *filename, char *key, int is_first)
{
int type;
struct entry *ety;
if (!is_first && (strcmp(filename, ".")==0 || strcmp(filename, "..")==0))
return NULL;
type = check_type(filename);
if (is_first && (type==CHKTYPE_ERROR))
return NULL;
if (key && !strstr(filename, key) && type != DIR_TYPE)
return NULL;
ety = checked_malloc(sizeof(struct entry));
ety->filename = checked_malloc(strlen(filename)+1);
strcpy(ety->filename, filename);
ety->type = (type==DIR_TYPE) ? DIR_TYPE : NONDIR_TYPE;
return ety;
}
int cmp(const void *p, const void *q)
{
struct entry *e1;
struct entry *e2;
if (p==NULL && q==NULL) return 0;
if (p == NULL) return -1;
if (q == NULL) return 1;
e1 = *(struct entry **)p;
e2 = *(struct entry **)q;
return strcasecmp(e1->filename, e2->filename);
}
struct table *new_table(int size, struct entry **table_entry)
{
struct table *t = checked_malloc(sizeof(struct table));
t->size = size;
t->table_entry = table_entry;
return t;
}
struct table *build_table(char *pathname, char *key, int is_first)
{
DIR *dp;
int size = 0;
int tb_size = DEFAULT_TABLE_SIZE;
struct dirent *f;
struct entry **table_entry = NULL;
if (is_first) {
table_entry = checked_malloc(sizeof(struct entry *));
table_entry[0] = process_file(pathname, key, is_first);
if (table_entry[0] == NULL) {
free(table_entry);
return NULL;
}
return new_table(1, table_entry);
}
if (!(dp = opendir(pathname))) {
perror(pathname);
return NULL;
}
if (chdir(pathname)) {
perror(pathname);
closedir(dp);
return NULL;
}
while ((f = readdir(dp))) {
struct entry *ety;
if ((ety = process_file(f->d_name, key, is_first)) == NULL)
continue;
if (!table_entry)
table_entry = checked_malloc(tb_size * sizeof(struct entry *));
if (size+1 > tb_size) {
tb_size *= 2;
table_entry = checked_realloc(table_entry, tb_size * sizeof(struct entry *));
}
table_entry[size++] = ety;
}
closedir(dp);
qsort(table_entry, size, sizeof(table_entry[0]), cmp);
return new_table(size, table_entry);
}
void free_entry(struct entry *e)
{
free(e->filename);
free(e);
}
void free_table(struct table *t)
{
struct entry **table_entry = t->table_entry;
free(table_entry);
free(t);
}
int generate_path(char *filename, char **buffer, int p_offset, int *buf_size)
{
int off_set = p_offset;
char *buf_start;
char *buf = *buffer;
if (!buf) {
*buf_size = DEFAULT_BUF_SIZE;
buf = checked_malloc(*buf_size);
buf_start = buf;
buf[0] = '\0';
strcpy(buf, filename);
off_set = strlen(filename);
} else {
if (strlen(filename)+off_set+1 >= *buf_size) {
*buf_size += 2 * strlen(filename);
buf = checked_realloc(buf, *buf_size);
}
buf_start = buf;
buf += off_set;
if (*(buf-1) != '/') {
*buf = '/';
buf++;
off_set++;
}
strcpy(buf, filename);
off_set += strlen(filename);
}
*buffer = buf_start;
return off_set;
}
void execute(char *path, char *prog_argv[], int *substitute_dict, int initial_fd)
{
int i;
pid_t pid;
if (prog_argv[0] == NULL) return;
for (i=0; substitute_dict[i]!=-1; i++)
prog_argv[substitute_dict[i]] = path;
if ((pid = fork()) < 0) {
perror("");
return;
} else if (pid == 0) {
fchdir(initial_fd);
if (execvp(prog_argv[0], prog_argv) < 0) {
perror(prog_argv[0]);
exit(-1);
}
} else {
wait(NULL);
}
}
void traverse(char *pathname, char *key, int is_first,
char **buffer, int p_offset, int *buf_size,
int initial_fd, char **prog_argv, int *substitute_dict)
{
int i;
struct table *t;
struct entry **table_entry;
if ((t = build_table(pathname, key, is_first)) == NULL)
return;
table_entry = t->table_entry;
for (i=0; i<t->size; i++) {
int tmp = generate_path(table_entry[i]->filename, buffer, p_offset, buf_size);
if (table_entry[i]->type == NONDIR_TYPE) {
if (prog_argv == NULL)
printf("%s\n", *buffer);
else execute(*buffer, prog_argv, substitute_dict, initial_fd);
} else if (table_entry[i]->type == DIR_TYPE) {
if (key==NULL || (key!=NULL && strstr(table_entry[i]->filename, key))) {
if (prog_argv == NULL)
printf("%s\n", *buffer);
else execute(*buffer, prog_argv, substitute_dict, initial_fd);
}
traverse(table_entry[i]->filename, key, 0, buffer, tmp, buf_size,
initial_fd, prog_argv, substitute_dict);
}
free_entry(table_entry[i]);
}
free_table(t);
chdir("..");
}
void arg_err()
{
fprintf(stderr, "simple_find: invalid arguments \n");
exit(-1);
}
char **parse_argv(int argc, char *argv[], char **filename, char **key)
{
int i;
int print = 0;
int arg_end = 0;
char *_key = NULL;
char *_filename = NULL;
char **prog_argv = NULL;
for (i=1; i<argc; i++) {
if (arg_end) arg_err();
if (argv[i][0] != '-') {
if (_filename) arg_err();
_filename = argv[i];
} else if (strcmp(argv[i], "-name") == 0) {
if (_key) arg_err();
if (i+1 >= argc) arg_err();
_key = argv[++i];
} else if (strcmp(argv[i], "-exec") == 0) {
if (prog_argv) arg_err();
if (i+1 >= argc) arg_err();
prog_argv = &argv[i+1];
while (argv[++i][0] != ';') {
if (i+1 >= argc) {
fprintf(stderr, "simple_find: no end symbol \\; for exec cmd\n");
exit(-1);
}
}
arg_end = 1;
argv[i] = NULL;
} else if (strcmp(argv[i], "-print") == 0)
print = 1;
else arg_err();
}
if (print==1 && prog_argv) arg_err();
if (!_filename) _filename = ".";
if (!print && !prog_argv) exit(0);
*key = _key;
*filename = _filename;
return prog_argv;
}
int *generate_dict(char **prog_argv)
{
int i;
int *dict = NULL;
int size = 1;
int dict_size = DEFAULT_DICT_SIZE;
if (!prog_argv) return NULL;
dict = checked_malloc(dict_size * sizeof(int));
for (i=0; prog_argv[i]!=NULL; i++) {
if (strcmp(prog_argv[i], "{}") == 0) {
if (size+1 > dict_size) {
dict_size *= 2;
dict = checked_realloc(dict, dict_size * sizeof(int));
}
dict[(size++)-1] = i;
}
}
dict[size-1] = -1;
return dict;
}
int main(int argc, char *argv[])
{
int initial_fd;
int buf_size = 0;
int *dict;
char *key;
char *filename;
char **prog_argv;
char *buffer = NULL;
limit_fork(10);
initial_fd = open(".", O_RDONLY);
prog_argv = parse_argv(argc, argv, &filename, &key);
dict = generate_dict(prog_argv);
traverse(filename, key, 1, &buffer, 0, &buf_size, initial_fd, prog_argv, dict);
if (buffer) free(buffer);
if (dict) free(dict);
close(initial_fd);
return 0;
}