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 USE_FIND(NEWTOY(find, "?^HL[-HL]", TOYFLAG_USR|TOYFLAG_BIN))
17 usage: find [-HL] [DIR...] [<options>]
19 Search directories for matching files.
20 Default: search "." match all -print all matches.
22 -H Follow command line symlinks -L Follow all symlinks
25 -name PATTERN filename with wildcards -iname case insensitive -name
26 -path PATTERN path name with wildcards -ipath case insensitive -path
27 -user UNAME belongs to user UNAME -nouser user not in /etc/passwd
28 -group GROUP belongs to group GROUP -nogroup group not in /etc/group
29 -perm [-]MODE permissons (-=at least) -prune ignore contents of dir
30 -size N[c] 512 byte blocks (c=bytes) -xdev stay in this filesystem
31 -links N hardlink count -atime N accessed N days ago
32 -ctime N created N days ago -mtime N modified N days ago
33 -newer FILE newer mtime than FILE -mindepth # at least # dirs down
34 -depth ignore contents of dir -maxdepth # at most # dirs down
35 -inum N inode number N
36 -type [bcdflps] (block, char, dir, file, symlink, pipe, socket)
38 Numbers N may be prefixed by a - (less than) or + (greater than):
41 !, -a, -o, ( ) not, and, or, group expressions
44 -print Print match with newline -print0 Print match with null
45 -exec Run command with path -execdir Run command in file's dir
46 -ok Ask before exec -okdir Ask before execdir
48 Commands substitute "{}" with matched file. End with ";" to run each file,
49 or "+" (next argument after "{}") to collect and run with multiple files.
57 struct double_list *argdata;
58 int topdir, xdev, depth, envsize;
62 // None of this can go in TT because you can have more than one -exec
66 int dir, plus, arglen, argsize, curly, namecount, namesize;
68 struct double_list *names;
71 // Perform pending -exec (if any)
72 static int flush_exec(struct dirtree *new, struct exec_range *aa)
74 struct double_list **dl;
78 if (!aa->namecount) return 0;
80 if (aa->dir && new->parent) dl = (void *)&new->parent->extra;
84 // switch to directory for -execdir, or back to top if we have an -execdir
85 // _and_ a normal -exec, or are at top of tree in -execdir
86 if (aa->dir && new->parent) rc = fchdir(new->parent->data);
87 else if (TT.topdir != -1) rc = fchdir(TT.topdir);
89 perror_msg("%s", new->name);
94 // execdir: accumulated execs in this directory's children.
95 newargs = xmalloc(sizeof(char *)*(aa->arglen+aa->namecount+1));
97 memcpy(newargs, aa->argstart, sizeof(char *)*aa->arglen);
98 newargs[aa->arglen] = 0;
100 struct double_list *dl2 = *dl;
101 int pos = aa->curly, rest = aa->arglen - aa->curly;
103 // Collate argument list
104 memcpy(newargs, aa->argstart, sizeof(char *)*pos);
105 for (dl2 = *dl; dl2; dl2 = dl2->next) newargs[pos++] = dl2->data;
106 rest = aa->arglen - aa->curly - 1;
107 memcpy(newargs+pos, aa->argstart+aa->curly+1, sizeof(char *)*rest);
108 newargs[pos+rest] = 0;
113 llist_traverse(*dl, llist_free_double);
120 // Return numeric value with explicit sign
121 static int compare_numsign(long val, long units, char *str)
126 if (*str == '+' || *str == '-') sign = *(str++);
127 else if (!isdigit(*str)) error_exit("%s not [+-]N", str);
129 if (units && isdigit(str[strlen(str)-1])) myval *= units;
131 if (sign == '+') return val > myval;
132 if (sign == '-') return val < myval;
136 static void do_print(struct dirtree *new, char c)
138 char *s=dirtree_path(new, 0);
140 xprintf("%s%c", s, c);
144 // Call this with 0 for first pass argument parsing and syntax checking (which
145 // populates argdata). Later commands traverse argdata (in order) when they
146 // need "do once" results.
147 static int do_find(struct dirtree *new)
149 int pcount = 0, print = 0, not = 0, active = !!new, test = active, recurse;
150 struct double_list *argdata = TT.argdata;
153 recurse = DIRTREE_COMEAGAIN|(DIRTREE_SYMFOLLOW*!!(toys.optflags&FLAG_L));
155 // skip . and .. below topdir, handle -xdev and -depth
158 if (!dirtree_notdotdot(new)) return 0;
159 if (TT.xdev && new->st.st_dev != new->parent->st.st_dev) recurse = 0;
161 if (S_ISDIR(new->st.st_mode)) {
165 if (TT.depth) return recurse;
166 for (n = new->parent; n; n = n->parent) {
167 if (n->st.st_ino==new->st.st_ino && n->st.st_dev==new->st.st_dev) {
168 error_msg("'%s': loop detected", s = dirtree_path(new, 0));
175 struct double_list *dl;
178 for (dl = TT.argdata; dl; dl = dl->next)
179 if (dl->prev == (void *)1 || !new->parent)
180 toys.exitval |= flush_exec(new, (void *)dl);
187 // pcount: parentheses stack depth (using toybuf bytes, 4096 max depth)
188 // test: result of most recent test
189 // active: if 0 don't perform tests
190 // not: a pending ! applies to this test (only set if performing tests)
191 // print: saw one of print/ok/exec, no need for default -print
193 if (TT.filter) for (ss = TT.filter; *ss; ss++) {
194 int check = active && test;
198 // handle ! ( ) using toybuf as a stack
200 if (s[1]) goto error;
203 // Don't invert if we're not making a decision
204 if (check) not = !not;
206 // Save old "not" and "active" on toybuf stack.
207 // Deactivate this parenthetical if !test
208 // Note: test value should never change while !active
209 } else if (*s == '(') {
210 if (pcount == sizeof(toybuf)) goto error;
211 toybuf[pcount++] = not+(active<<1);
212 if (!check) active = 0;
215 // Pop status, apply deferred not to test
216 } else if (*s == ')') {
217 if (--pcount < 0) goto error;
218 // Pop active state, apply deferred not (which was only set if checking)
219 active = (toybuf[pcount]>>1)&1;
220 if (active && (toybuf[pcount]&1)) test = !test;
227 if (!strcmp(s, "xdev")) TT.xdev = 1;
228 else if (!strcmp(s, "depth")) TT.depth = 1;
229 else if (!strcmp(s, "o") || !strcmp(s, "or")) {
233 else active = 0; // decision has been made until next ")"
235 } else if (!strcmp(s, "not")) {
236 if (check) not = !not;
238 // Mostly ignore NOP argument
239 } else if (!strcmp(s, "a") || !strcmp(s, "and")) {
242 } else if (!strcmp(s, "print") || !strcmp("print0", s)) {
244 if (check) do_print(new, s[5] ? 0 : '\n');
246 } else if (!strcmp(s, "nouser")) {
247 if (check) if (getpwuid(new->st.st_uid)) test = 0;
248 } else if (!strcmp(s, "nogroup")) {
249 if (check) if (getgrgid(new->st.st_gid)) test = 0;
250 } else if (!strcmp(s, "prune")) {
251 if (check && S_ISDIR(new->st.st_dev) && !TT.depth) recurse = 0;
253 // Remaining filters take an argument
255 if (!strcmp(s, "name") || !strcmp(s, "iname")
256 || !strcmp(s, "path") || !strcmp(s, "ipath"))
259 char *arg = ss[1], *path = 0, *name = new->name;
261 // Handle path expansion and case flattening
262 if (new && s[i] == 'p') name = path = dirtree_path(new, 0);
265 name = strlower(new ? name : arg);
267 dlist_add(&TT.argdata, name);
269 } else arg = ((struct double_list *)llist_pop(&argdata))->data;
274 test = !fnmatch(arg, name, FNM_PATHNAME*(s[i] == 'p'));
278 } else if (!strcmp(s, "perm")) {
281 mode_t m1 = string_to_mode(m+(*m == '-'), 0),
282 m2 = new->st.st_dev & 07777;
284 if (*m != '-') m2 &= m1;
287 } else if (!strcmp(s, "type")) {
289 int types[] = {S_IFBLK, S_IFCHR, S_IFDIR, S_IFLNK, S_IFIFO,
290 S_IFREG, S_IFSOCK}, i = stridx("bcdlpfs", *ss[1]);
292 if (i<0) error_exit("bad -type '%c'", *ss[1]);
293 if ((new->st.st_mode & S_IFMT) != types[i]) test = 0;
296 } else if (!strcmp(s, "atime")) {
298 test = compare_numsign(TT.now - new->st.st_atime, 86400, ss[1]);
299 } else if (!strcmp(s, "ctime")) {
301 test = compare_numsign(TT.now - new->st.st_ctime, 86400, ss[1]);
302 } else if (!strcmp(s, "mtime")) {
304 test = compare_numsign(TT.now - new->st.st_mtime, 86400, ss[1]);
305 } else if (!strcmp(s, "size")) {
307 test = compare_numsign(new->st.st_size, 512, ss[1]);
308 } else if (!strcmp(s, "links")) {
309 if (check) test = compare_numsign(new->st.st_nlink, 0, ss[1]);
310 } else if (!strcmp(s, "inum")) {
312 test = compare_numsign(new->st.st_ino, 0, ss[1]);
313 } else if (!strcmp(s, "mindepth") || !strcmp(s, "maxdepth")) {
315 struct dirtree *dt = new;
316 int i = 0, d = atolx(ss[1]);
318 while ((dt = dt->parent)) i++;
321 if (i == d && not) recurse = 0;
324 if (i == d && !not) recurse = 0;
327 } else if (!strcmp(s, "user") || !strcmp(s, "group")
328 || !strcmp(s, "newer"))
341 udl = xmalloc(sizeof(*udl));
342 dlist_add_nomalloc(&TT.argdata, (void *)udl);
344 if (*s == 'u') udl->u.uid = xgetpwnamid(ss[1])->pw_uid;
345 else if (*s == 'g') udl->u.gid = xgetgrnamid(ss[1])->gr_gid;
350 udl->u.tm = st.st_mtim;
354 udl = (void *)llist_pop(&argdata);
356 if (*s == 'u') test = new->st.st_uid == udl->u.uid;
357 else if (*s == 'g') test = new->st.st_gid == udl->u.gid;
359 test = new->st.st_mtim.tv_sec > udl->u.tm.tv_sec;
360 if (new->st.st_mtim.tv_sec == udl->u.tm.tv_sec)
361 test = new->st.st_mtim.tv_nsec > udl->u.tm.tv_nsec;
365 } else if (!strcmp(s, "exec") || !strcmp("ok", s)
366 || !strcmp(s, "execdir") || !strcmp(s, "okdir"))
368 struct exec_range *aa;
372 // Initial argument parsing pass
376 // catch "-exec" with no args and "-exec \;"
377 if (!ss[1] || !strcmp(ss[1], ";")) error_exit("'%s' needs 1 arg", s);
379 dlist_add_nomalloc(&TT.argdata, (void *)(aa = xzalloc(sizeof(*aa))));
383 // Record command line arguments to -exec
384 for (len = 0; ss[len]; len++) {
385 if (!strcmp(ss[len], ";")) break;
386 else if (!strcmp(ss[len], "{}")) {
388 if (!strcmp(ss[len+1], "+")) {
390 // Measure environment space
394 for (env = environ; *env; env++)
395 TT.envsize += sizeof(char *) + strlen(*env) + 1;
396 TT.envsize += sizeof(char *);
402 } else aa->argsize += sizeof(char *) + strlen(ss[len]) + 1;
404 if (!ss[len]) error_exit("-exec without \\;");
407 aa->dir = !!strchr(s, 'd');
408 if (aa->dir && TT.topdir == -1) TT.topdir = xopen(".", 0);
410 // collect names and execute commands
412 char *name, *ss1 = ss[1];
413 struct double_list **ddl;
415 // Grab command line exec argument list
416 aa = (void *)llist_pop(&argdata);
417 ss += aa->arglen + 1;
419 if (!check) goto cont;
420 // name is always a new malloc, so we can always free it.
421 name = aa->dir ? xstrdup(new->name) : dirtree_path(new, 0);
423 // Mark entry so COMEAGAIN can call flush_exec() in parent.
424 // This is never a valid pointer value for prev to have otherwise
425 if (aa->dir) aa->prev = (void *)1;
428 fprintf(stderr, "[%s] %s", ss1, name);
429 if (!(test = yesno(0))) {
435 // Add next name to list (global list without -dir, local with)
436 if (aa->dir && new->parent)
437 ddl = (struct double_list **)&new->parent->extra;
438 else ddl = &aa->names;
442 int size = sizeof(char *)+strlen(name)+1;
444 // Linux caps environment space (env vars + args) at 32 4k pages.
445 // todo: is there a way to probe this instead of constant here?
447 if (TT.envsize+aa->argsize+aa->namesize+size >= 131072)
448 toys.exitval |= flush_exec(new, aa);
449 aa->namesize += size;
451 dlist_add(ddl, name);
453 if (!aa->plus) test = flush_exec(new, aa);
456 // Argument consumed, skip the check.
460 // This test can go at the end because we do a syntax checking
461 // pass first. Putting it here gets the error message (-unknown
462 // vs -known noarg) right.
463 if (!*++ss) error_exit("'%s' needs 1 arg", --s);
466 // Apply pending "!" to result
467 if (active && not) test = !test;
472 // If there was no action, print
473 if (!print && test) do_print(new, '\n');
474 } else dlist_terminate(TT.argdata);
479 error_exit("bad arg '%s'", *ss);
485 char **ss = toys.optargs;
489 // Distinguish paths from filters
490 for (len = 0; toys.optargs[len]; len++)
491 if (strchr("-!(", *toys.optargs[len])) break;
492 TT.filter = toys.optargs+len;
494 // use "." if no paths
496 ss = (char *[]){"."};
500 // first pass argument parsing, verify args match up, handle "evaluate once"
504 // Loop through paths
505 for (i = 0; i < len; i++)
506 dirtree_handle_callback(dirtree_start(ss[i], toys.optflags&(FLAG_H|FLAG_L)),
509 if (CFG_TOYBOX_FREE) {
511 llist_traverse(TT.argdata, free);