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#define FOR_find
55#include "toys.h"
56
57GLOBALS(
58 char **filter;
59 struct double_list *argdata;
60 int topdir, xdev, depth;
61 time_t now;
62 long max_bytes;
63)
64
65struct execdir_data {
66 struct execdir_data *next;
67
68 int namecount;
69 struct double_list *names;
70};
71
72
73struct exec_range {
74 char *next, *prev;
75
76 int dir, plus, arglen, argsize, curly;
77 char **argstart;
78 struct execdir_data exec, *execdir;
79};
80
81
82static int flush_exec(struct dirtree *new, struct exec_range *aa)
83{
84 struct execdir_data *bb = aa->execdir ? aa->execdir : &aa->exec;
85 char **newargs;
86 int rc, revert = 0;
87
88 if (!bb->namecount) return 0;
89
90 dlist_terminate(bb->names);
91
92
93
94 if (TT.topdir != -1) {
95 if (aa->dir && new && new->parent) {
96 revert++;
97 rc = fchdir(new->parent->dirfd);
98 } else rc = fchdir(TT.topdir);
99 if (rc) {
100 perror_msg_raw(revert ? new->name : ".");
101
102 return rc;
103 }
104 }
105
106
107 newargs = xmalloc(sizeof(char *)*(aa->arglen+bb->namecount+1));
108 if (aa->curly < 0) {
109 memcpy(newargs, aa->argstart, sizeof(char *)*aa->arglen);
110 newargs[aa->arglen] = 0;
111 } else {
112 int pos = aa->curly, rest = aa->arglen - aa->curly;
113 struct double_list *dl;
114
115
116 memcpy(newargs, aa->argstart, sizeof(char *)*pos);
117 for (dl = bb->names; dl; dl = dl->next) newargs[pos++] = dl->data;
118 rest = aa->arglen - aa->curly - 1;
119 memcpy(newargs+pos, aa->argstart+aa->curly+1, sizeof(char *)*rest);
120 newargs[pos+rest] = 0;
121 }
122
123 rc = xrun(newargs);
124
125 llist_traverse(bb->names, llist_free_double);
126 bb->names = 0;
127 bb->namecount = 0;
128
129 if (revert) revert = fchdir(TT.topdir);
130
131 return rc;
132}
133
134
135static int compare_numsign(long val, long units, char *str)
136{
137 char sign = 0;
138 long myval;
139
140 if (*str == '+' || *str == '-') sign = *(str++);
141 else if (!isdigit(*str)) error_exit("%s not [+-]N", str);
142 myval = atolx(str);
143 if (units && isdigit(str[strlen(str)-1])) myval *= units;
144
145 if (sign == '+') return val > myval;
146 if (sign == '-') return val < myval;
147 return val == myval;
148}
149
150static void do_print(struct dirtree *new, char c)
151{
152 char *s=dirtree_path(new, 0);
153
154 xprintf("%s%c", s, c);
155 free(s);
156}
157
158
159static void execdir(struct dirtree *new, int flush)
160{
161 struct double_list *dl;
162 struct exec_range *aa;
163 struct execdir_data *bb;
164
165 if (new && TT.topdir == -1) return;
166
167 for (dl = TT.argdata; dl; dl = dl->next) {
168 if (dl->prev != (void *)1) continue;
169 aa = (void *)dl;
170 if (!aa->plus || (new && !aa->dir)) continue;
171
172 if (flush) {
173
174
175
176 toys.exitval |= flush_exec(new, aa);
177
178
179 if ((bb = aa->execdir)) {
180 aa->execdir = bb->next;
181 free(bb);
182 }
183 } else if (aa->dir) {
184
185
186
187 bb = xzalloc(sizeof(struct execdir_data));
188 bb->next = aa->execdir;
189 aa->execdir = bb;
190 }
191 }
192}
193
194
195
196
197static int do_find(struct dirtree *new)
198{
199 int pcount = 0, print = 0, not = 0, active = !!new, test = active, recurse;
200 struct double_list *argdata = TT.argdata;
201 char *s, **ss;
202
203 recurse = DIRTREE_COMEAGAIN|(DIRTREE_SYMFOLLOW*!!(toys.optflags&FLAG_L));
204
205
206 if (new) {
207 if (new->parent) {
208 if (!dirtree_notdotdot(new)) return 0;
209 if (TT.xdev && new->st.st_dev != new->parent->st.st_dev) recurse = 0;
210 }
211
212 if (S_ISDIR(new->st.st_mode)) {
213
214 if (!new->again) {
215 struct dirtree *n;
216
217 for (n = new->parent; n; n = n->parent) {
218 if (n->st.st_ino==new->st.st_ino && n->st.st_dev==new->st.st_dev) {
219 error_msg("'%s': loop detected", s = dirtree_path(new, 0));
220 free(s);
221
222 return 0;
223 }
224 }
225
226 if (TT.depth) {
227 execdir(new, 0);
228
229 return recurse;
230 }
231
232 } else {
233 execdir(new, 1);
234 recurse = 0;
235 if (!TT.depth) return 0;
236 }
237 }
238 }
239
240
241
242
243
244
245
246 if (TT.filter) for (ss = TT.filter; *ss; ss++) {
247 int check = active && test;
248
249 s = *ss;
250
251
252 if (*s != '-') {
253 if (s[1]) goto error;
254
255 if (*s == '!') {
256
257 if (check) not = !not;
258
259
260
261
262 } else if (*s == '(') {
263 if (pcount == sizeof(toybuf)) goto error;
264 toybuf[pcount++] = not+(active<<1);
265 if (!check) active = 0;
266 not = 0;
267
268
269 } else if (*s == ')') {
270 if (--pcount < 0) goto error;
271
272 active = (toybuf[pcount]>>1)&1;
273 if (active && (toybuf[pcount]&1)) test = !test;
274 not = 0;
275 } else goto error;
276
277 continue;
278 } else s++;
279
280 if (!strcmp(s, "xdev")) TT.xdev = 1;
281 else if (!strcmp(s, "delete")) {
282
283 TT.depth = 1;
284 if (new && check)
285 test = !unlinkat(dirtree_parentfd(new), new->name,
286 S_ISDIR(new->st.st_mode) ? AT_REMOVEDIR : 0);
287 } else if (!strcmp(s, "depth")) TT.depth = 1;
288 else if (!strcmp(s, "o") || !strcmp(s, "or")) {
289 if (not) goto error;
290 if (active) {
291 if (!test) test = 1;
292 else active = 0;
293 }
294 } else if (!strcmp(s, "not")) {
295 if (check) not = !not;
296 continue;
297
298 } else if (!strcmp(s, "a") || !strcmp(s, "and") || !strcmp(s, "noleaf")) {
299 if (not) goto error;
300
301 } else if (!strcmp(s, "print") || !strcmp("print0", s)) {
302 print++;
303 if (check) do_print(new, s[5] ? 0 : '\n');
304
305 } else if (!strcmp(s, "empty")) {
306 if (check) {
307
308 if (S_ISDIR(new->st.st_mode)) {
309 int fd = openat(dirtree_parentfd(new), new->name, O_RDONLY);
310 DIR *dfd = fdopendir(fd);
311 struct dirent *de = (void *)1;
312 if (dfd) {
313 while ((de = readdir(dfd)) && isdotdot(de->d_name));
314 closedir(dfd);
315 }
316 if (de) test = 0;
317 } else if (S_ISREG(new->st.st_mode)) {
318 if (new->st.st_size) test = 0;
319 } else test = 0;
320 }
321 } else if (!strcmp(s, "nouser")) {
322 if (check) if (bufgetpwuid(new->st.st_uid)) test = 0;
323 } else if (!strcmp(s, "nogroup")) {
324 if (check) if (bufgetgrgid(new->st.st_gid)) test = 0;
325 } else if (!strcmp(s, "prune")) {
326 if (check && S_ISDIR(new->st.st_mode) && !TT.depth) recurse = 0;
327
328
329 } else {
330 if (!strcmp(s, "name") || !strcmp(s, "iname")
331 || !strcmp(s, "path") || !strcmp(s, "ipath"))
332 {
333 int i = (*s == 'i');
334 char *arg = ss[1], *path = 0, *name = new ? new->name : arg;
335
336
337 if (new && s[i] == 'p') name = path = dirtree_path(new, 0);
338 if (i) {
339 if ((check || !new) && name) name = strlower(name);
340 if (!new) dlist_add(&TT.argdata, name);
341 else arg = ((struct double_list *)llist_pop(&argdata))->data;
342 }
343
344 if (check) {
345 test = !fnmatch(arg, name, FNM_PATHNAME*(s[i] == 'p'));
346 if (i) free(name);
347 }
348 free(path);
349 } else if (!strcmp(s, "perm")) {
350 if (check) {
351 char *m = ss[1];
352 int match_min = *m == '-',
353 match_any = *m == '/';
354 mode_t m1 = string_to_mode(m+(match_min || match_any), 0),
355 m2 = new->st.st_mode & 07777;
356
357 if (match_min || match_any) m2 &= m1;
358 test = match_any ? !m1 || m2 : m1 == m2;
359 }
360 } else if (!strcmp(s, "type")) {
361 if (check) {
362 int types[] = {S_IFBLK, S_IFCHR, S_IFDIR, S_IFLNK, S_IFIFO,
363 S_IFREG, S_IFSOCK}, i = stridx("bcdlpfs", *ss[1]);
364
365 if (i<0) error_exit("bad -type '%c'", *ss[1]);
366 if ((new->st.st_mode & S_IFMT) != types[i]) test = 0;
367 }
368
369 } else if (strchr("acm", *s)
370 && (!strcmp(s+1, "time") || !strcmp(s+1, "min")))
371 {
372 if (check) {
373 char *copy = ss[1];
374 time_t thyme = (int []){new->st.st_atime, new->st.st_ctime,
375 new->st.st_mtime}[stridx("acm", *s)];
376 int len = strlen(copy), uu, units = (s[1]=='m') ? 60 : 86400;
377
378 if (len && -1!=(uu = stridx("dhms",tolower(copy[len-1])))) {
379 copy = xstrdup(copy);
380 copy[--len] = 0;
381 units = (int []){86400, 3600, 60, 1}[uu];
382 }
383 test = compare_numsign(TT.now - thyme, units, copy);
384 if (copy != ss[1]) free(copy);
385 }
386 } else if (!strcmp(s, "size")) {
387 if (check)
388 test = compare_numsign(new->st.st_size, 512, ss[1]);
389 } else if (!strcmp(s, "links")) {
390 if (check) test = compare_numsign(new->st.st_nlink, 0, ss[1]);
391 } else if (!strcmp(s, "inum")) {
392 if (check)
393 test = compare_numsign(new->st.st_ino, 0, ss[1]);
394 } else if (!strcmp(s, "mindepth") || !strcmp(s, "maxdepth")) {
395 if (check) {
396 struct dirtree *dt = new;
397 int i = 0, d = atolx(ss[1]);
398
399 while ((dt = dt->parent)) i++;
400 if (s[1] == 'i') {
401 test = i >= d;
402 if (i == d && not) recurse = 0;
403 } else {
404 test = i <= d;
405 if (i == d && !not) recurse = 0;
406 }
407 }
408 } else if (!strcmp(s, "user") || !strcmp(s, "group")
409 || !strcmp(s, "newer"))
410 {
411 struct {
412 void *next, *prev;
413 union {
414 uid_t uid;
415 gid_t gid;
416 struct timespec tm;
417 } u;
418 } *udl;
419
420 if (!new) {
421 if (ss[1]) {
422 udl = xmalloc(sizeof(*udl));
423 dlist_add_nomalloc(&TT.argdata, (void *)udl);
424
425 if (*s == 'u') udl->u.uid = xgetuid(ss[1]);
426 else if (*s == 'g') udl->u.gid = xgetgid(ss[1]);
427 else {
428 struct stat st;
429
430 xstat(ss[1], &st);
431 udl->u.tm = st.st_mtim;
432 }
433 }
434 } else {
435 udl = (void *)llist_pop(&argdata);
436 if (check) {
437 if (*s == 'u') test = new->st.st_uid == udl->u.uid;
438 else if (*s == 'g') test = new->st.st_gid == udl->u.gid;
439 else {
440 test = new->st.st_mtim.tv_sec > udl->u.tm.tv_sec;
441 if (new->st.st_mtim.tv_sec == udl->u.tm.tv_sec)
442 test = new->st.st_mtim.tv_nsec > udl->u.tm.tv_nsec;
443 }
444 }
445 }
446 } else if (!strcmp(s, "exec") || !strcmp("ok", s)
447 || !strcmp(s, "execdir") || !strcmp(s, "okdir"))
448 {
449 struct exec_range *aa;
450
451 print++;
452
453
454 if (!new) {
455 int len;
456
457
458 if (!ss[1] || !strcmp(ss[1], ";")) error_exit("'%s' needs 1 arg", s);
459
460 dlist_add_nomalloc(&TT.argdata, (void *)(aa = xzalloc(sizeof(*aa))));
461 aa->argstart = ++ss;
462 aa->curly = -1;
463
464
465 for (len = 0; ss[len]; len++) {
466 if (!strcmp(ss[len], ";")) break;
467 else if (!strcmp(ss[len], "{}")) {
468 aa->curly = len;
469 if (ss[len+1] && !strcmp(ss[len+1], "+")) {
470 aa->plus++;
471 len++;
472 break;
473 }
474 } else aa->argsize += sizeof(char *) + strlen(ss[len]) + 1;
475 }
476 if (!ss[len]) error_exit("-exec without %s",
477 aa->curly!=-1 ? "\\;" : "{}");
478 ss += len;
479 aa->arglen = len;
480 aa->dir = !!strchr(s, 'd');
481 if (TT.topdir == -1) TT.topdir = xopenro(".");
482
483
484 } else {
485 char *name, *ss1 = ss[1];
486 struct execdir_data *bb;
487
488
489 aa = (void *)llist_pop(&argdata);
490 ss += aa->arglen + 1;
491
492 if (!check) goto cont;
493
494 name = aa->dir ? xstrdup(new->name) : dirtree_path(new, 0);
495
496 if (*s == 'o') {
497 fprintf(stderr, "[%s] %s", ss1, name);
498 if (!(test = yesno(0))) {
499 free(name);
500 goto cont;
501 }
502 }
503
504
505 bb = aa->execdir ? aa->execdir : &aa->exec;
506 dlist_add(&bb->names, name);
507 bb->namecount++;
508
509
510 if (aa->plus) {
511
512
513
514 aa->prev = (void *)1;
515
516
517
518
519 if ((aa->plus += sizeof(char *)+strlen(name)+1) > TT.max_bytes) {
520 aa->plus = 1;
521 toys.exitval |= flush_exec(new, aa);
522 }
523 } else test = flush_exec(new, aa);
524 }
525
526
527 goto cont;
528 } else goto error;
529
530
531
532
533 if (!*++ss) error_exit("'%s' needs 1 arg", --s);
534 }
535cont:
536
537 if (active && not) test = !test;
538 not = 0;
539 }
540
541 if (new) {
542
543 if (!print && test) do_print(new, '\n');
544
545 if (S_ISDIR(new->st.st_mode)) execdir(new, 0);
546
547 } else dlist_terminate(TT.argdata);
548
549 return recurse;
550
551error:
552 error_exit("bad arg '%s'", *ss);
553}
554
555void find_main(void)
556{
557 int i, len;
558 char **ss = toys.optargs;
559
560 TT.topdir = -1;
561 TT.max_bytes = sysconf(_SC_ARG_MAX) - environ_bytes();
562
563
564 for (len = 0; toys.optargs[len]; len++)
565 if (strchr("-!(", *toys.optargs[len])) break;
566 TT.filter = toys.optargs+len;
567
568
569 if (!len) {
570 ss = (char *[]){"."};
571 len = 1;
572 }
573
574
575 TT.now = time(0);
576 do_find(0);
577
578
579 for (i = 0; i < len; i++)
580 dirtree_flagread(ss[i], DIRTREE_SYMFOLLOW*!!(toys.optflags&(FLAG_H|FLAG_L)),
581 do_find);
582
583 execdir(0, 1);
584
585 if (CFG_TOYBOX_FREE) {
586 close(TT.topdir);
587 llist_traverse(TT.argdata, free);
588 }
589}
590