1 /* find.c - Search directories for matching files.
3 * Copyright 2014 Rob Landley <rob@landley.net>
5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/find.c
7 * Our "unspecified" behavior for no paths is to use "."
8 * Parentheses can only stack 4096 deep
9 * Not treating two {} as an error, but only using last
11 * TODO: -empty (dirs too!)
13 USE_FIND(NEWTOY(find, "?^HL[-HL]", TOYFLAG_USR|TOYFLAG_BIN))
19 usage: find [-HL] [DIR...] [<options>]
21 Search directories for matching files.
22 Default: search "." match all -print all matches.
24 -H Follow command line symlinks -L Follow all symlinks
27 -name PATTERN filename with wildcards -iname case insensitive -name
28 -path PATTERN path name with wildcards -ipath case insensitive -path
29 -user UNAME belongs to user UNAME -nouser user ID not known
30 -group GROUP belongs to group GROUP -nogroup group ID not known
31 -perm [-/]MODE permissions (-=min /=any) -prune ignore contents of dir
32 -size N[c] 512 byte blocks (c=bytes) -xdev only this filesystem
33 -links N hardlink count -atime N[u] accessed N units ago
34 -ctime N[u] created N units ago -mtime N[u] modified N units ago
35 -newer FILE newer mtime than FILE -mindepth # at least # dirs down
36 -depth ignore contents of dir -maxdepth # at most # dirs down
37 -inum N inode number N -empty empty files and dirs
38 -type [bcdflps] (block, char, dir, file, symlink, pipe, socket)
40 Numbers N may be prefixed by a - (less than) or + (greater than). Units for
41 -Xtime are d (days, default), h (hours), m (minutes), or s (seconds).
44 !, -a, -o, ( ) not, and, or, group expressions
47 -print Print match with newline -print0 Print match with null
48 -exec Run command with path -execdir Run command in file's dir
49 -ok Ask before exec -okdir Ask before execdir
50 -delete Remove matching file/dir
52 Commands substitute "{}" with matched file. End with ";" to run each file,
53 or "+" (next argument after "{}") to collect and run with multiple files.
61 struct double_list *argdata;
62 int topdir, xdev, depth;
67 struct execdir_data *next;
70 struct double_list *names;
73 // None of this can go in TT because you can have more than one -exec
75 char *next, *prev; // layout compatible with struct double_list
77 int dir, plus, arglen, argsize, curly;
79 struct execdir_data exec, *execdir;
82 // Perform pending -exec (if any)
83 static int flush_exec(struct dirtree *new, struct exec_range *aa)
85 struct execdir_data *bb = aa->execdir ? aa->execdir : &aa->exec;
89 if (!bb->namecount) return 0;
91 dlist_terminate(bb->names);
93 // switch to directory for -execdir, or back to top if we have an -execdir
94 // _and_ a normal -exec, or are at top of tree in -execdir
95 if (TT.topdir != -1) {
96 if (aa->dir && new && new->parent) {
98 rc = fchdir(new->parent->dirfd);
99 } else rc = fchdir(TT.topdir);
101 perror_msg_raw(revert ? new->name : ".");
107 // execdir: accumulated execs in this directory's children.
108 newargs = xmalloc(sizeof(char *)*(aa->arglen+bb->namecount+1));
110 memcpy(newargs, aa->argstart, sizeof(char *)*aa->arglen);
111 newargs[aa->arglen] = 0;
113 int pos = aa->curly, rest = aa->arglen - aa->curly;
114 struct double_list *dl;
116 // Collate argument list
117 memcpy(newargs, aa->argstart, sizeof(char *)*pos);
118 for (dl = bb->names; dl; dl = dl->next) newargs[pos++] = dl->data;
119 rest = aa->arglen - aa->curly - 1;
120 memcpy(newargs+pos, aa->argstart+aa->curly+1, sizeof(char *)*rest);
121 newargs[pos+rest] = 0;
126 llist_traverse(bb->names, llist_free_double);
130 if (revert) revert = fchdir(TT.topdir);
135 // Return numeric value with explicit sign
136 static int compare_numsign(long val, long units, char *str)
141 if (*str == '+' || *str == '-') sign = *(str++);
142 else if (!isdigit(*str)) error_exit("%s not [+-]N", str);
144 if (units && isdigit(str[strlen(str)-1])) myval *= units;
146 if (sign == '+') return val > myval;
147 if (sign == '-') return val < myval;
151 static void do_print(struct dirtree *new, char c)
153 char *s=dirtree_path(new, 0);
155 xprintf("%s%c", s, c);
159 // Descend or ascend -execdir + directory level
160 static void execdir(struct dirtree *new, int flush)
162 struct double_list *dl;
163 struct exec_range *aa;
164 struct execdir_data *bb;
166 if (new && TT.topdir == -1) return;
168 for (dl = TT.argdata; dl; dl = dl->next) {
169 if (dl->prev != (void *)1) continue;
171 if (!aa->plus || (new && !aa->dir)) continue;
175 // Flush pending "-execdir +" instances for this dir
176 // or flush everything for -exec at top
177 toys.exitval |= flush_exec(new, aa);
179 // pop per-directory struct
180 if ((bb = aa->execdir)) {
181 aa->execdir = bb->next;
184 } else if (aa->dir) {
186 // Push new per-directory struct for -execdir/okdir + codepath. (Can't
187 // use new->extra because command line may have multiple -execdir)
188 bb = xzalloc(sizeof(struct execdir_data));
189 bb->next = aa->execdir;
195 // Call this with 0 for first pass argument parsing and syntax checking (which
196 // populates argdata). Later commands traverse argdata (in order) when they
197 // need "do once" results.
198 static int do_find(struct dirtree *new)
200 int pcount = 0, print = 0, not = 0, active = !!new, test = active, recurse;
201 struct double_list *argdata = TT.argdata;
204 recurse = DIRTREE_COMEAGAIN|(DIRTREE_SYMFOLLOW*!!(toys.optflags&FLAG_L));
206 // skip . and .. below topdir, handle -xdev and -depth
209 if (!dirtree_notdotdot(new)) return 0;
210 if (TT.xdev && new->st.st_dev != new->parent->st.st_dev) recurse = 0;
213 if (S_ISDIR(new->st.st_mode)) {
214 // Descending into new directory
218 for (n = new->parent; n; n = n->parent) {
219 if (n->st.st_ino==new->st.st_ino && n->st.st_dev==new->st.st_dev) {
220 error_msg("'%s': loop detected", s = dirtree_path(new, 0));
232 // Done with directory (COMEAGAIN call)
236 if (!TT.depth) return 0;
241 // pcount: parentheses stack depth (using toybuf bytes, 4096 max depth)
242 // test: result of most recent test
243 // active: if 0 don't perform tests
244 // not: a pending ! applies to this test (only set if performing tests)
245 // print: saw one of print/ok/exec, no need for default -print
247 if (TT.filter) for (ss = TT.filter; *ss; ss++) {
248 int check = active && test;
252 // handle ! ( ) using toybuf as a stack
254 if (s[1]) goto error;
257 // Don't invert if we're not making a decision
258 if (check) not = !not;
260 // Save old "not" and "active" on toybuf stack.
261 // Deactivate this parenthetical if !test
262 // Note: test value should never change while !active
263 } else if (*s == '(') {
264 if (pcount == sizeof(toybuf)) goto error;
265 toybuf[pcount++] = not+(active<<1);
266 if (!check) active = 0;
269 // Pop status, apply deferred not to test
270 } else if (*s == ')') {
271 if (--pcount < 0) goto error;
272 // Pop active state, apply deferred not (which was only set if checking)
273 active = (toybuf[pcount]>>1)&1;
274 if (active && (toybuf[pcount]&1)) test = !test;
281 if (!strcmp(s, "xdev")) TT.xdev = 1;
282 else if (!strcmp(s, "delete")) {
283 // Delete forces depth first
286 test = !unlinkat(dirtree_parentfd(new), new->name,
287 S_ISDIR(new->st.st_mode) ? AT_REMOVEDIR : 0);
288 } else if (!strcmp(s, "depth")) TT.depth = 1;
289 else if (!strcmp(s, "o") || !strcmp(s, "or")) {
293 else active = 0; // decision has been made until next ")"
295 } else if (!strcmp(s, "not")) {
296 if (check) not = !not;
298 // Mostly ignore NOP argument
299 } else if (!strcmp(s, "a") || !strcmp(s, "and") || !strcmp(s, "noleaf")) {
302 } else if (!strcmp(s, "print") || !strcmp("print0", s)) {
304 if (check) do_print(new, s[5] ? 0 : '\n');
306 } else if (!strcmp(s, "nouser")) {
307 if (check) if (bufgetpwuid(new->st.st_uid)) test = 0;
308 } else if (!strcmp(s, "nogroup")) {
309 if (check) if (bufgetgrgid(new->st.st_gid)) test = 0;
310 } else if (!strcmp(s, "prune")) {
311 if (check && S_ISDIR(new->st.st_mode) && !TT.depth) recurse = 0;
313 // Remaining filters take an argument
315 if (!strcmp(s, "name") || !strcmp(s, "iname")
316 || !strcmp(s, "path") || !strcmp(s, "ipath"))
319 char *arg = ss[1], *path = 0, *name = new ? new->name : arg;
321 // Handle path expansion and case flattening
322 if (new && s[i] == 'p') name = path = dirtree_path(new, 0);
324 if ((check || !new) && name) name = strlower(name);
325 if (!new) dlist_add(&TT.argdata, name);
326 else arg = ((struct double_list *)llist_pop(&argdata))->data;
330 test = !fnmatch(arg, name, FNM_PATHNAME*(s[i] == 'p'));
334 } else if (!strcmp(s, "perm")) {
337 int match_min = *m == '-',
338 match_any = *m == '/';
339 mode_t m1 = string_to_mode(m+(match_min || match_any), 0),
340 m2 = new->st.st_mode & 07777;
342 if (match_min || match_any) m2 &= m1;
343 test = match_any ? !m1 || m2 : m1 == m2;
345 } else if (!strcmp(s, "type")) {
347 int types[] = {S_IFBLK, S_IFCHR, S_IFDIR, S_IFLNK, S_IFIFO,
348 S_IFREG, S_IFSOCK}, i = stridx("bcdlpfs", *ss[1]);
350 if (i<0) error_exit("bad -type '%c'", *ss[1]);
351 if ((new->st.st_mode & S_IFMT) != types[i]) test = 0;
354 } else if (strchr("acm", *s)
355 && (!strcmp(s+1, "time") || !strcmp(s+1, "min")))
359 time_t thyme = (int []){new->st.st_atime, new->st.st_ctime,
360 new->st.st_mtime}[stridx("acm", *s)];
361 int len = strlen(copy), uu, units = (s[1]=='m') ? 60 : 86400;
363 if (len && -1!=(uu = stridx("dhms",tolower(copy[len-1])))) {
364 copy = xstrdup(copy);
366 units = (int []){86400, 3600, 60, 1}[uu];
368 test = compare_numsign(TT.now - thyme, units, copy);
369 if (copy != ss[1]) free(copy);
371 } else if (!strcmp(s, "size")) {
373 test = compare_numsign(new->st.st_size, 512, ss[1]);
374 } else if (!strcmp(s, "links")) {
375 if (check) test = compare_numsign(new->st.st_nlink, 0, ss[1]);
376 } else if (!strcmp(s, "inum")) {
378 test = compare_numsign(new->st.st_ino, 0, ss[1]);
379 } else if (!strcmp(s, "mindepth") || !strcmp(s, "maxdepth")) {
381 struct dirtree *dt = new;
382 int i = 0, d = atolx(ss[1]);
384 while ((dt = dt->parent)) i++;
387 if (i == d && not) recurse = 0;
390 if (i == d && !not) recurse = 0;
393 } else if (!strcmp(s, "user") || !strcmp(s, "group")
394 || !strcmp(s, "newer"))
407 udl = xmalloc(sizeof(*udl));
408 dlist_add_nomalloc(&TT.argdata, (void *)udl);
410 if (*s == 'u') udl->u.uid = xgetuid(ss[1]);
411 else if (*s == 'g') udl->u.gid = xgetgid(ss[1]);
416 udl->u.tm = st.st_mtim;
420 udl = (void *)llist_pop(&argdata);
422 if (*s == 'u') test = new->st.st_uid == udl->u.uid;
423 else if (*s == 'g') test = new->st.st_gid == udl->u.gid;
425 test = new->st.st_mtim.tv_sec > udl->u.tm.tv_sec;
426 if (new->st.st_mtim.tv_sec == udl->u.tm.tv_sec)
427 test = new->st.st_mtim.tv_nsec > udl->u.tm.tv_nsec;
431 } else if (!strcmp(s, "exec") || !strcmp("ok", s)
432 || !strcmp(s, "execdir") || !strcmp(s, "okdir"))
434 struct exec_range *aa;
438 // Initial argument parsing pass
442 // catch "-exec" with no args and "-exec \;"
443 if (!ss[1] || !strcmp(ss[1], ";")) error_exit("'%s' needs 1 arg", s);
445 dlist_add_nomalloc(&TT.argdata, (void *)(aa = xzalloc(sizeof(*aa))));
449 // Record command line arguments to -exec
450 for (len = 0; ss[len]; len++) {
451 if (!strcmp(ss[len], ";")) break;
452 else if (!strcmp(ss[len], "{}")) {
454 if (ss[len+1] && !strcmp(ss[len+1], "+")) {
459 } else aa->argsize += sizeof(char *) + strlen(ss[len]) + 1;
461 if (!ss[len]) error_exit("-exec without %s",
462 aa->curly!=-1 ? "\\;" : "{}");
465 aa->dir = !!strchr(s, 'd');
466 if (TT.topdir == -1) TT.topdir = xopenro(".");
468 // collect names and execute commands
470 char *name, *ss1 = ss[1];
471 struct execdir_data *bb;
473 // Grab command line exec argument list
474 aa = (void *)llist_pop(&argdata);
475 ss += aa->arglen + 1;
477 if (!check) goto cont;
478 // name is always a new malloc, so we can always free it.
479 name = aa->dir ? xstrdup(new->name) : dirtree_path(new, 0);
482 fprintf(stderr, "[%s] %s", ss1, name);
483 if (!(test = yesno(0))) {
489 // Add next name to list (global list without -dir, local with)
490 bb = aa->execdir ? aa->execdir : &aa->exec;
491 dlist_add(&bb->names, name);
494 // -exec + collates and saves result in exitval
496 // Mark entry so COMEAGAIN can call flush_exec() in parent.
497 // This is never a valid pointer value for prev to have otherwise
498 // Done here vs argument parsing pass so it's after dlist_terminate
499 aa->prev = (void *)1;
501 // Flush if we pass 16 megs of environment space.
502 // An insanely long path (>2 gigs) could wrap the counter and
503 // defeat this test, which could potentially trigger OOM killer.
504 if ((aa->plus += sizeof(char *)+strlen(name)+1) > 1<<24) {
506 toys.exitval |= flush_exec(new, aa);
508 } else test = flush_exec(new, aa);
511 // Argument consumed, skip the check.
515 // This test can go at the end because we do a syntax checking
516 // pass first. Putting it here gets the error message (-unknown
517 // vs -known noarg) right.
518 if (!*++ss) error_exit("'%s' needs 1 arg", --s);
521 // Apply pending "!" to result
522 if (active && not) test = !test;
527 // If there was no action, print
528 if (!print && test) do_print(new, '\n');
530 if (S_ISDIR(new->st.st_mode)) execdir(new, 0);
532 } else dlist_terminate(TT.argdata);
537 error_exit("bad arg '%s'", *ss);
543 char **ss = toys.optargs;
547 // Distinguish paths from filters
548 for (len = 0; toys.optargs[len]; len++)
549 if (strchr("-!(", *toys.optargs[len])) break;
550 TT.filter = toys.optargs+len;
552 // use "." if no paths
554 ss = (char *[]){"."};
558 // first pass argument parsing, verify args match up, handle "evaluate once"
562 // Loop through paths
563 for (i = 0; i < len; i++)
564 dirtree_flagread(ss[i], DIRTREE_SYMFOLLOW*!!(toys.optflags&(FLAG_H|FLAG_L)),
569 if (CFG_TOYBOX_FREE) {
571 llist_traverse(TT.argdata, free);