annotate toys/posix/find.c @ 1747:3297b4b490f8 draft

Another bug from David Halls: find -exec wasn't consuming its argument when it didn't activate. test: find . -name README -exec echo one '{}' ';' -or -exec echo two '{}' ';'
author Rob Landley <rob@landley.net>
date Sat, 21 Mar 2015 17:15:09 -0500
parents 9adb0ccb617e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* find.c - Search directories for matching files.
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * Copyright 2014 Rob Landley <rob@landley.net>
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/find.c
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
6 *
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
7 * Our "unspecified" behavior for no paths is to use "."
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
8 * Parentheses can only stack 4096 deep
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
9 * Not treating two {} as an error, but only using last
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
10
1425
2cf6ce96d442 Rereading posix find.c page: "Specifying more than one of the mutually-exclusive options -H and -L shall not be considered an error. The last option specified shall determine the behavior of the utility."
Rob Landley <rob@landley.net>
parents: 1424
diff changeset
11 USE_FIND(NEWTOY(find, "?^HL[-HL]", TOYFLAG_USR|TOYFLAG_BIN))
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
12
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
13 config FIND
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 bool "find"
1420
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
15 default y
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
16 help
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
17 usage: find [-HL] [DIR...] [<options>]
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
18
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
19 Search directories for matching files.
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
20 Default: search "." match all -print all matches.
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
21
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
22 -H Follow command line symlinks -L Follow all symlinks
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
23
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
24 Match filters:
1421
30014454681f find.c: add -mindepth, -maxdepth, and document -newer and -depth.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
25 -name PATTERN filename with wildcards -iname case insensitive -name
30014454681f find.c: add -mindepth, -maxdepth, and document -newer and -depth.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
26 -path PATTERN path name with wildcards -ipath case insensitive -path
30014454681f find.c: add -mindepth, -maxdepth, and document -newer and -depth.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
27 -user UNAME belongs to user UNAME -nouser user not in /etc/passwd
30014454681f find.c: add -mindepth, -maxdepth, and document -newer and -depth.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
28 -group GROUP belongs to group GROUP -nogroup group not in /etc/group
30014454681f find.c: add -mindepth, -maxdepth, and document -newer and -depth.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
29 -perm [-]MODE permissons (-=at least) -prune ignore contents of dir
30014454681f find.c: add -mindepth, -maxdepth, and document -newer and -depth.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
30 -size N[c] 512 byte blocks (c=bytes) -xdev stay in this filesystem
30014454681f find.c: add -mindepth, -maxdepth, and document -newer and -depth.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
31 -links N hardlink count -atime N accessed N days ago
30014454681f find.c: add -mindepth, -maxdepth, and document -newer and -depth.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
32 -ctime N created N days ago -mtime N modified N days ago
30014454681f find.c: add -mindepth, -maxdepth, and document -newer and -depth.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
33 -newer FILE newer mtime than FILE -mindepth # at least # dirs down
30014454681f find.c: add -mindepth, -maxdepth, and document -newer and -depth.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
34 -depth ignore contents of dir -maxdepth # at most # dirs down
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
35 -type [bcdflps] (block, char, dir, file, symlink, pipe, socket)
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
36
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
37 Numbers N may be prefixed by a - (less than) or + (greater than):
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
38
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
39 Combine matches with:
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
40 !, -a, -o, ( ) not, and, or, group expressions
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
41
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
42 Actions:
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
43 -print Print match with newline -print0 Print match with null
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
44 -exec Run command with path -execdir Run command in file's dir
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
45 -ok Ask before exec -okdir Ask before execdir
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
46
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
47 Commands substitute "{}" with matched file. End with ";" to run each file,
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
48 or "+" (next argument after "{}") to collect and run with multiple files.
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
49 */
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
50
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
51 #define FOR_find
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
52 #include "toys.h"
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
53
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
54 GLOBALS(
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
55 char **filter;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
56 struct double_list *argdata;
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
57 int topdir, xdev, depth, envsize;
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
58 time_t now;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
59 )
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
60
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
61 // None of this can go in TT because you can have more than one -exec
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
62 struct exec_range {
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
63 char *next, *prev;
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
64
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
65 int dir, plus, arglen, argsize, curly, namecount, namesize;
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
66 char **argstart;
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
67 struct double_list *names;
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
68 };
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
69
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
70 // Perform pending -exec (if any)
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
71 static int flush_exec(struct dirtree *new, struct exec_range *aa)
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
72 {
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
73 struct double_list **dl;
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
74 char **newargs;
1571
e85e5f3b87c2 As long as Android's going to require fortify, fixup the warnings it generates.
Rob Landley <rob@landley.net>
parents: 1474
diff changeset
75 int rc = 0;
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
76
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
77 if (!aa->namecount) return 0;
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
78
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
79 if (aa->dir && new->parent) dl = (void *)&new->parent->extra;
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
80 else dl = &aa->names;
1414
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
81 dlist_terminate(*dl);
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
82
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
83 // switch to directory for -execdir, or back to top if we have an -execdir
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
84 // _and_ a normal -exec, or are at top of tree in -execdir
1571
e85e5f3b87c2 As long as Android's going to require fortify, fixup the warnings it generates.
Rob Landley <rob@landley.net>
parents: 1474
diff changeset
85 if (aa->dir && new->parent) rc = fchdir(new->parent->data);
e85e5f3b87c2 As long as Android's going to require fortify, fixup the warnings it generates.
Rob Landley <rob@landley.net>
parents: 1474
diff changeset
86 else if (TT.topdir != -1) rc = fchdir(TT.topdir);
e85e5f3b87c2 As long as Android's going to require fortify, fixup the warnings it generates.
Rob Landley <rob@landley.net>
parents: 1474
diff changeset
87 if (rc) {
e85e5f3b87c2 As long as Android's going to require fortify, fixup the warnings it generates.
Rob Landley <rob@landley.net>
parents: 1474
diff changeset
88 perror_msg("%s", new->name);
e85e5f3b87c2 As long as Android's going to require fortify, fixup the warnings it generates.
Rob Landley <rob@landley.net>
parents: 1474
diff changeset
89
e85e5f3b87c2 As long as Android's going to require fortify, fixup the warnings it generates.
Rob Landley <rob@landley.net>
parents: 1474
diff changeset
90 return rc;
e85e5f3b87c2 As long as Android's going to require fortify, fixup the warnings it generates.
Rob Landley <rob@landley.net>
parents: 1474
diff changeset
91 }
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
92
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
93 // execdir: accumulated execs in this directory's children.
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
94 newargs = xmalloc(sizeof(char *)*(aa->arglen+aa->namecount+1));
1414
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
95 if (aa->curly < 0) {
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
96 memcpy(newargs, aa->argstart, sizeof(char *)*aa->arglen);
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
97 newargs[aa->arglen] = 0;
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
98 } else {
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
99 struct double_list *dl2 = *dl;
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
100 int pos = aa->curly, rest = aa->arglen - aa->curly;
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
101
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
102 // Collate argument list
1414
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
103 memcpy(newargs, aa->argstart, sizeof(char *)*pos);
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
104 for (dl2 = *dl; dl2; dl2 = dl2->next) newargs[pos++] = dl2->data;
1414
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
105 rest = aa->arglen - aa->curly - 1;
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
106 memcpy(newargs+pos, aa->argstart+aa->curly+1, sizeof(char *)*rest);
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
107 newargs[pos+rest] = 0;
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
108 }
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
109
1472
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1463
diff changeset
110 rc = xrun(newargs);
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
111
1414
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
112 llist_traverse(*dl, llist_free_double);
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
113 *dl = 0;
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
114 aa->namecount = 0;
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
115
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
116 return rc;
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
117 }
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
118
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
119 // Return numeric value with explicit sign
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
120 static int compare_numsign(long val, long units, char *str)
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
121 {
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
122 char sign = 0;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
123 long myval;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
124
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
125 if (*str == '+' || *str == '-') sign = *(str++);
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
126 else if (!isdigit(*str)) error_exit("%s not [+-]N", str);
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
127 myval = atolx(str);
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
128 if (units && isdigit(str[strlen(str)-1])) myval *= units;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
129
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
130 if (sign == '+') return val > myval;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
131 if (sign == '-') return val < myval;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
132 return val == myval;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
133 }
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
134
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
135 static void do_print(struct dirtree *new, char c)
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
136 {
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
137 char *s=dirtree_path(new, 0);
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
138
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
139 xprintf("%s%c", s, c);
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
140 free(s);
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
141 }
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
142
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
143 char *strlower(char *s)
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
144 {
1409
f477dc852d3b find.c: fix -iname.
Rob Landley <rob@landley.net>
parents: 1407
diff changeset
145 char *try, *new;
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
146
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
147 if (!CFG_TOYBOX_I18N) {
1409
f477dc852d3b find.c: fix -iname.
Rob Landley <rob@landley.net>
parents: 1407
diff changeset
148 try = new = xstrdup(s);
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
149 for (; *s; s++) *(new++) = tolower(*s);
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
150 } else {
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
151 // I can't guarantee the string _won't_ expand during reencoding, so...?
1409
f477dc852d3b find.c: fix -iname.
Rob Landley <rob@landley.net>
parents: 1407
diff changeset
152 try = new = xmalloc(strlen(s)*2+1);
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
153
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
154 while (*s) {
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
155 wchar_t c;
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
156 int len = mbrtowc(&c, s, MB_CUR_MAX, 0);
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
157
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
158 if (len < 1) *(new++) = *(s++);
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
159 else {
1409
f477dc852d3b find.c: fix -iname.
Rob Landley <rob@landley.net>
parents: 1407
diff changeset
160 s += len;
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
161 // squash title case too
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
162 c = towlower(c);
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
163
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
164 // if we had a valid utf8 sequence, convert it to lower case, and can't
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
165 // encode back to utf8, something is wrong with your libc. But just
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
166 // in case somebody finds an exploit...
1409
f477dc852d3b find.c: fix -iname.
Rob Landley <rob@landley.net>
parents: 1407
diff changeset
167 len = wcrtomb(new, c, 0);
1719
848969327d77 On 64 bit, subtracting two pointers produces a long result. On 32 bit, it's an int. Even though long _is_ 32 bits on a 32 bit systems, gcc warns about it because reasons.
Rob Landley <rob@landley.net>
parents: 1571
diff changeset
168 if (len < 1) error_exit("bad utf8 %x", (int)c);
1409
f477dc852d3b find.c: fix -iname.
Rob Landley <rob@landley.net>
parents: 1407
diff changeset
169 new += len;
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
170 }
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
171 }
1409
f477dc852d3b find.c: fix -iname.
Rob Landley <rob@landley.net>
parents: 1407
diff changeset
172 *new = 0;
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
173 }
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
174
1409
f477dc852d3b find.c: fix -iname.
Rob Landley <rob@landley.net>
parents: 1407
diff changeset
175 return try;
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
176 }
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
177
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
178 // Call this with 0 for first pass argument parsing and syntax checking (which
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
179 // populates argdata). Later commands traverse argdata (in order) when they
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
180 // need "do once" results.
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
181 static int do_find(struct dirtree *new)
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
182 {
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
183 int pcount = 0, print = 0, not = 0, active = !!new, test = active, recurse;
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
184 struct double_list *argdata = TT.argdata;
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
185 char *s, **ss;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
186
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
187 recurse = DIRTREE_COMEAGAIN|((toys.optflags&FLAG_L) ? DIRTREE_SYMFOLLOW : 0);
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
188
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
189 // skip . and .. below topdir, handle -xdev and -depth
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
190 if (new) {
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
191 if (new->parent) {
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
192 if (!dirtree_notdotdot(new)) return 0;
1474
715c765cec61 find -xdev should return mount points, just not contents.
Rob Landley <rob@landley.net>
parents: 1472
diff changeset
193 if (TT.xdev && new->st.st_dev != new->parent->st.st_dev) recurse = 0;
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
194 }
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
195 if (S_ISDIR(new->st.st_mode)) {
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
196 if (!new->again) {
1424
c95588a2b7a2 find.c: Posix wants loop detection.
Rob Landley <rob@landley.net>
parents: 1423
diff changeset
197 struct dirtree *n;
c95588a2b7a2 find.c: Posix wants loop detection.
Rob Landley <rob@landley.net>
parents: 1423
diff changeset
198
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
199 if (TT.depth) return recurse;
1424
c95588a2b7a2 find.c: Posix wants loop detection.
Rob Landley <rob@landley.net>
parents: 1423
diff changeset
200 for (n = new->parent; n; n = n->parent) {
c95588a2b7a2 find.c: Posix wants loop detection.
Rob Landley <rob@landley.net>
parents: 1423
diff changeset
201 if (n->st.st_ino==new->st.st_ino && n->st.st_dev==new->st.st_dev) {
c95588a2b7a2 find.c: Posix wants loop detection.
Rob Landley <rob@landley.net>
parents: 1423
diff changeset
202 error_msg("'%s': loop detected", s = dirtree_path(new, 0));
c95588a2b7a2 find.c: Posix wants loop detection.
Rob Landley <rob@landley.net>
parents: 1423
diff changeset
203 free(s);
c95588a2b7a2 find.c: Posix wants loop detection.
Rob Landley <rob@landley.net>
parents: 1423
diff changeset
204
c95588a2b7a2 find.c: Posix wants loop detection.
Rob Landley <rob@landley.net>
parents: 1423
diff changeset
205 return 0;
c95588a2b7a2 find.c: Posix wants loop detection.
Rob Landley <rob@landley.net>
parents: 1423
diff changeset
206 }
c95588a2b7a2 find.c: Posix wants loop detection.
Rob Landley <rob@landley.net>
parents: 1423
diff changeset
207 }
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
208 } else {
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
209 struct double_list *dl;
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
210
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
211 if (TT.topdir != -1)
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
212 for (dl = TT.argdata; dl; dl = dl->next)
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
213 if (dl->prev == (void *)1 || !new->parent)
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
214 toys.exitval |= flush_exec(new, (void *)dl);
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
215
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
216 return 0;
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
217 }
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
218 }
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
219 }
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
220
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
221 // pcount: parentheses stack depth (using toybuf bytes, 4096 max depth)
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
222 // test: result of most recent test
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
223 // active: if 0 don't perform tests
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
224 // not: a pending ! applies to this test (only set if performing tests)
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
225 // print: saw one of print/ok/exec, no need for default -print
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
226
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
227 if (TT.filter) for (ss = TT.filter; *ss; ss++) {
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
228 int check = active && test;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
229
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
230 s = *ss;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
231
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
232 // handle ! ( ) using toybuf as a stack
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
233 if (*s != '-') {
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
234 if (s[1]) goto error;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
235
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
236 if (*s == '!') {
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
237 // Don't invert if we're not making a decision
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
238 if (check) not = !not;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
239
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
240 // Save old "not" and "active" on toybuf stack.
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
241 // Deactivate this parenthetical if !test
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
242 // Note: test value should never change while !active
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
243 } else if (*s == '(') {
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
244 if (pcount == sizeof(toybuf)) goto error;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
245 toybuf[pcount++] = not+(active<<1);
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
246 if (!check) active = 0;
1393
3e8ab37a4b60 Find bugfixes.
Rob Landley <rob@landley.net>
parents: 1390
diff changeset
247 not = 0;
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
248
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
249 // Pop status, apply deferred not to test
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
250 } else if (*s == ')') {
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
251 if (--pcount < 0) goto error;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
252 // Pop active state, apply deferred not (which was only set if checking)
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
253 active = (toybuf[pcount]>>1)&1;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
254 if (active && (toybuf[pcount]&1)) test = !test;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
255 not = 0;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
256 } else goto error;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
257
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
258 continue;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
259 } else s++;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
260
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
261 if (!strcmp(s, "xdev")) TT.xdev = 1;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
262 else if (!strcmp(s, "depth")) TT.depth = 1;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
263 else if (!strcmp(s, "o") || !strcmp(s, "or")) {
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
264 if (not) goto error;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
265 if (active) {
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
266 if (!test) test = 1;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
267 else active = 0; // decision has been made until next ")"
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
268 }
1423
8059cfd67acd Building busybox from source needs find -not (a synonym for posix's "!").
Rob Landley <rob@landley.net>
parents: 1421
diff changeset
269 } else if (!strcmp(s, "not")) {
8059cfd67acd Building busybox from source needs find -not (a synonym for posix's "!").
Rob Landley <rob@landley.net>
parents: 1421
diff changeset
270 if (check) not = !not;
1426
23169562c3ee More find bugfixes.
Rob Landley <rob@landley.net>
parents: 1425
diff changeset
271 continue;
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
272 // Mostly ignore NOP argument
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
273 } else if (!strcmp(s, "a") || !strcmp(s, "and")) {
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
274 if (not) goto error;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
275
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
276 } else if (!strcmp(s, "print") || !strcmp("print0", s)) {
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
277 print++;
1393
3e8ab37a4b60 Find bugfixes.
Rob Landley <rob@landley.net>
parents: 1390
diff changeset
278 if (check) do_print(new, s[5] ? 0 : '\n');
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
279
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
280 } else if (!strcmp(s, "nouser")) {
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
281 if (check) if (getpwuid(new->st.st_uid)) test = 0;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
282 } else if (!strcmp(s, "nogroup")) {
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
283 if (check) if (getgrgid(new->st.st_gid)) test = 0;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
284 } else if (!strcmp(s, "prune")) {
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
285 if (check && S_ISDIR(new->st.st_dev) && !TT.depth) recurse = 0;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
286
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
287 // Remaining filters take an argument
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
288 } else {
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
289 if (!strcmp(s, "name") || !strcmp(s, "iname")
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
290 || !strcmp(s, "path") || !strcmp(s, "ipath"))
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
291 {
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
292 int i = (*s == 'i');
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
293 char *arg = ss[1], *path = 0, *name = new->name;
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
294
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
295 // Handle path expansion and case flattening
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
296 if (new && s[i] == 'p') name = path = dirtree_path(new, 0);
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
297 if (i) {
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
298 if (check || !new) {
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
299 name = strlower(new ? name : arg);
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
300 if (!new) {
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
301 dlist_add(&TT.argdata, name);
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
302 free(path);
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
303 } else arg = ((struct double_list *)llist_pop(&argdata))->data;
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
304 }
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
305 }
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
306
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
307 if (check) {
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
308 test = !fnmatch(arg, name, FNM_PATHNAME*(s[i] == 'p'));
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
309 free(path);
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
310 if (i) free(name);
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
311 }
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
312 } else if (!strcmp(s, "perm")) {
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
313 if (check) {
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
314 char *m = ss[1];
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
315 mode_t m1 = string_to_mode(m+(*m == '-'), 0),
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
316 m2 = new->st.st_dev & 07777;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
317
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
318 if (*m != '-') m2 &= m1;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
319 test = m1 == m2;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
320 }
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
321 } else if (!strcmp(s, "type")) {
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
322 if (check) {
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
323 int types[] = {S_IFBLK, S_IFCHR, S_IFDIR, S_IFLNK, S_IFIFO,
1463
bdd42c1463f8 Add error test and fix memory leak, reported by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 1427
diff changeset
324 S_IFREG, S_IFSOCK}, i = stridx("bcdlpfs", *ss[1]);
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
325
1463
bdd42c1463f8 Add error test and fix memory leak, reported by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 1427
diff changeset
326 if (i<0) error_exit("bad -type '%c'", *ss[1]);
bdd42c1463f8 Add error test and fix memory leak, reported by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 1427
diff changeset
327 if ((new->st.st_mode & S_IFMT) != types[i]) test = 0;
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
328 }
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
329
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
330 } else if (!strcmp(s, "atime")) {
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
331 if (check)
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
332 test = compare_numsign(TT.now - new->st.st_atime, 86400, ss[1]);
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
333 } else if (!strcmp(s, "ctime")) {
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
334 if (check)
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
335 test = compare_numsign(TT.now - new->st.st_ctime, 86400, ss[1]);
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
336 } else if (!strcmp(s, "mtime")) {
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
337 if (check)
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
338 test = compare_numsign(TT.now - new->st.st_mtime, 86400, ss[1]);
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
339 } else if (!strcmp(s, "size")) {
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
340 if (check)
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
341 test = compare_numsign(new->st.st_size, 512, ss[1]);
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
342 } else if (!strcmp(s, "links")) {
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
343 if (check) test = compare_numsign(new->st.st_nlink, 0, ss[1]);
1421
30014454681f find.c: add -mindepth, -maxdepth, and document -newer and -depth.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
344 } else if (!strcmp(s, "mindepth") || !strcmp(s, "maxdepth")) {
30014454681f find.c: add -mindepth, -maxdepth, and document -newer and -depth.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
345 if (check) {
30014454681f find.c: add -mindepth, -maxdepth, and document -newer and -depth.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
346 struct dirtree *dt = new;
30014454681f find.c: add -mindepth, -maxdepth, and document -newer and -depth.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
347 int i = 0, d = atolx(ss[1]);
30014454681f find.c: add -mindepth, -maxdepth, and document -newer and -depth.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
348
30014454681f find.c: add -mindepth, -maxdepth, and document -newer and -depth.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
349 while ((dt = dt->parent)) i++;
1427
0d58119f0bcd Fix -mindepth and -maxdepth to not drill down into excluded directories.
Rob Landley <rob@landley.net>
parents: 1426
diff changeset
350 if (s[1] == 'i') {
0d58119f0bcd Fix -mindepth and -maxdepth to not drill down into excluded directories.
Rob Landley <rob@landley.net>
parents: 1426
diff changeset
351 test = i >= d;
0d58119f0bcd Fix -mindepth and -maxdepth to not drill down into excluded directories.
Rob Landley <rob@landley.net>
parents: 1426
diff changeset
352 if (i == d && not) recurse = 0;
0d58119f0bcd Fix -mindepth and -maxdepth to not drill down into excluded directories.
Rob Landley <rob@landley.net>
parents: 1426
diff changeset
353 } else {
0d58119f0bcd Fix -mindepth and -maxdepth to not drill down into excluded directories.
Rob Landley <rob@landley.net>
parents: 1426
diff changeset
354 test = i <= d;
0d58119f0bcd Fix -mindepth and -maxdepth to not drill down into excluded directories.
Rob Landley <rob@landley.net>
parents: 1426
diff changeset
355 if (i == d && !not) recurse = 0;
0d58119f0bcd Fix -mindepth and -maxdepth to not drill down into excluded directories.
Rob Landley <rob@landley.net>
parents: 1426
diff changeset
356 }
1421
30014454681f find.c: add -mindepth, -maxdepth, and document -newer and -depth.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
357 }
1420
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
358 } else if (!strcmp(s, "user") || !strcmp(s, "group")
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
359 || !strcmp(s, "newer"))
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
360 {
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
361 struct {
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
362 void *next, *prev;
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
363 union {
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
364 uid_t uid;
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
365 gid_t gid;
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
366 struct timespec tm;
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
367 } u;
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
368 } *udl;
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
369
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
370 if (!new && ss[1]) {
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
371 udl = xmalloc(sizeof(*udl));
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
372 dlist_add_nomalloc(&TT.argdata, (void *)udl);
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
373
1732
9adb0ccb617e Make find accept numeric uid/gid, and simplify makedevs using the new infrastructure.
Rob Landley <rob@landley.net>
parents: 1719
diff changeset
374 if (*s == 'u') udl->u.uid = xgetpwnamid(ss[1])->pw_uid;
9adb0ccb617e Make find accept numeric uid/gid, and simplify makedevs using the new infrastructure.
Rob Landley <rob@landley.net>
parents: 1719
diff changeset
375 else if (*s == 'g') udl->u.gid = xgetgrnamid(ss[1])->gr_gid;
1420
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
376 else {
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
377 struct stat st;
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
378
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
379 xstat(ss[1], &st);
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
380 udl->u.tm = st.st_mtim;
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
381 }
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
382 } else if (check) {
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
383 udl = (void *)llist_pop(&argdata);
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
384 if (*s == 'u') test = new->st.st_uid == udl->u.uid;
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
385 else if (*s == 'g') test = new->st.st_gid == udl->u.gid;
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
386 else {
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
387 test = new->st.st_mtim.tv_sec > udl->u.tm.tv_sec;
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
388 if (new->st.st_mtim.tv_sec == udl->u.tm.tv_sec)
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
389 test = new->st.st_mtim.tv_nsec > udl->u.tm.tv_nsec;
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
390 }
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
391 }
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
392 } else if (!strcmp(s, "exec") || !strcmp("ok", s)
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
393 || !strcmp(s, "execdir") || !strcmp(s, "okdir"))
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
394 {
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
395 struct exec_range *aa;
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
396
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
397 print++;
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
398
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
399 // Initial argument parsing pass
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
400 if (!new) {
1414
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
401 int len;
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
402
1414
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
403 // catch "-exec" with no args and "-exec \;"
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
404 if (!ss[1] || !strcmp(ss[1], ";")) error_exit("'%s' needs 1 arg", s);
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
405
1414
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
406 dlist_add_nomalloc(&TT.argdata, (void *)(aa = xzalloc(sizeof(*aa))));
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
407 aa->argstart = ++ss;
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
408 aa->curly = -1;
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
409
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
410 // Record command line arguments to -exec
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
411 for (len = 0; ss[len]; len++) {
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
412 if (!strcmp(ss[len], ";")) break;
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
413 else if (!strcmp(ss[len], "{}")) {
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
414 aa->curly = len;
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
415 if (!strcmp(ss[len+1], "+")) {
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
416
1414
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
417 // Measure environment space
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
418 if (!TT.envsize) {
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
419 char **env;
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
420
1414
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
421 for (env = environ; *env; env++)
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
422 TT.envsize += sizeof(char *) + strlen(*env) + 1;
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
423 TT.envsize += sizeof(char *);
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
424 }
1414
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
425 aa->plus++;
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
426 len++;
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
427 break;
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
428 }
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
429 } else aa->argsize += sizeof(char *) + strlen(ss[len]) + 1;
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
430 }
1414
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
431 if (!ss[len]) error_exit("-exec without \\;");
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
432 ss += len;
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
433 aa->arglen = len;
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
434 aa->dir = !!strchr(s, 'd');
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
435 if (aa->dir && TT.topdir == -1) TT.topdir = xopen(".", 0);
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
436
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
437 // collect names and execute commands
1747
3297b4b490f8 Another bug from David Halls: find -exec wasn't consuming its argument when it didn't activate.
Rob Landley <rob@landley.net>
parents: 1732
diff changeset
438 } else {
1420
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
439 char *name, *ss1 = ss[1];
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
440 struct double_list **ddl;
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
441
1420
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
442 // Grab command line exec argument list
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
443 aa = (void *)llist_pop(&argdata);
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
444 ss += aa->arglen + 1;
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
445
1747
3297b4b490f8 Another bug from David Halls: find -exec wasn't consuming its argument when it didn't activate.
Rob Landley <rob@landley.net>
parents: 1732
diff changeset
446 if (!check) goto cont;
1420
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
447 // name is always a new malloc, so we can always free it.
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
448 name = aa->dir ? xstrdup(new->name) : dirtree_path(new, 0);
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
449
1420
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
450 // Mark entry so COMEAGAIN can call flush_exec() in parent.
1747
3297b4b490f8 Another bug from David Halls: find -exec wasn't consuming its argument when it didn't activate.
Rob Landley <rob@landley.net>
parents: 1732
diff changeset
451 // This is never a valid pointer value for prev to have otherwise
1420
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
452 if (aa->dir) aa->prev = (void *)1;
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
453
1420
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
454 if (*s == 'o') {
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
455 char *prompt = xmprintf("[%s] %s", ss1, name);
1463
bdd42c1463f8 Add error test and fix memory leak, reported by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 1427
diff changeset
456 test = yesno(prompt, 0);
bdd42c1463f8 Add error test and fix memory leak, reported by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 1427
diff changeset
457 free(prompt);
bdd42c1463f8 Add error test and fix memory leak, reported by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 1427
diff changeset
458 if (!test) {
bdd42c1463f8 Add error test and fix memory leak, reported by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 1427
diff changeset
459 free(name);
bdd42c1463f8 Add error test and fix memory leak, reported by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 1427
diff changeset
460 goto cont;
bdd42c1463f8 Add error test and fix memory leak, reported by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 1427
diff changeset
461 }
1420
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
462 }
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
463
1420
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
464 // Add next name to list (global list without -dir, local with)
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
465 if (aa->dir && new->parent)
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
466 ddl = (struct double_list **)&new->parent->extra;
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
467 else ddl = &aa->names;
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
468
1420
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
469 // Is this + mode?
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
470 if (aa->plus) {
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
471 int size = sizeof(char *)+strlen(name)+1;
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
472
1420
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
473 // Linux caps environment space (env vars + args) at 32 4k pages.
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
474 // todo: is there a way to probe this instead of constant here?
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
475
1420
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
476 if (TT.envsize+aa->argsize+aa->namesize+size >= 131072)
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
477 toys.exitval |= flush_exec(new, aa);
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
478 aa->namesize += size;
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
479 }
1420
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
480 dlist_add(ddl, name);
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
481 aa->namecount++;
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1414
diff changeset
482 if (!aa->plus) test = flush_exec(new, aa);
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
483 }
1414
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
484
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
485 // Argument consumed, skip the check.
c37c186409a2 Unbreak find -exec.
Rob Landley <rob@landley.net>
parents: 1409
diff changeset
486 goto cont;
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
487 } else goto error;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
488
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
489 // This test can go at the end because we do a syntax checking
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
490 // pass first. Putting it here gets the error message (-unknown
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
491 // vs -known noarg) right.
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
492 if (!*++ss) error_exit("'%s' needs 1 arg", --s);
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
493 }
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
494 cont:
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
495 // Apply pending "!" to result
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
496 if (active && not) test = !test;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
497 not = 0;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
498 }
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
499
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
500 if (new) {
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
501 // If there was no action, print
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
502 if (!print && test) do_print(new, '\n');
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
503 } else dlist_terminate(TT.argdata);
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
504
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
505 return recurse;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
506
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
507 error:
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
508 error_exit("bad arg '%s'", *ss);
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
509 }
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
510
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
511 void find_main(void)
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
512 {
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
513 int i, len;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
514 char **ss = toys.optargs;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
515
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
516 TT.topdir = -1;
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
517
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
518 // Distinguish paths from filters
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
519 for (len = 0; toys.optargs[len]; len++)
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
520 if (strchr("-!(", *toys.optargs[len])) break;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
521 TT.filter = toys.optargs+len;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
522
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
523 // use "." if no paths
1426
23169562c3ee More find bugfixes.
Rob Landley <rob@landley.net>
parents: 1425
diff changeset
524 if (!len) {
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
525 ss = (char *[]){"."};
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
526 len = 1;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
527 }
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
528
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
529 // first pass argument parsing, verify args match up, handle "evaluate once"
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
530 TT.now = time(0);
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
531 do_find(0);
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
532
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
533 // Loop through paths
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
534 for (i = 0; i < len; i++) {
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
535 struct dirtree *new;
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
536
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
537 new = dirtree_add_node(0, ss[i], toys.optflags&(FLAG_H|FLAG_L));
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
538 if (new) dirtree_handle_callback(new, do_find);
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
539 }
1407
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
540
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
541 if (CFG_TOYBOX_FREE) {
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
542 close(TT.topdir);
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
543 llist_traverse(TT.argdata, free);
434ea531a62c find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents: 1393
diff changeset
544 }
1390
c18ad59ae442 Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff changeset
545 }