Mercurial > hg > toybox
annotate toys/posix/find.c @ 1407:434ea531a62c draft
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
author | Rob Landley <rob@landley.net> |
---|---|
date | Tue, 29 Jul 2014 20:04:38 -0500 |
parents | 3e8ab37a4b60 |
children | f477dc852d3b |
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 |
1407
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
11 USE_FIND(NEWTOY(find, "?^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" |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
15 default n |
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: |
1407
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
25 -name PATTERN filename with wildcards -iname case insensitive -name |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
26 -path PATTERN path name with wildcards -ipath case insensitive -path |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
27 -user UNAME belongs to user -nouser belongs to unknown user |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
28 -group GROUP belongs to group -nogroup belongs to unknown group |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
29 -perm [-]MODE permissons (-=at least) -prune ignore contents of dir |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
30 -size N[c] 512 byte blocks (c=bytes) -xdev stay in this filesystem |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
31 -links N hardlink count -atime N accessed N days ago |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
32 -ctime N created N days ago -mtime N modified N days ago |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
33 -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
|
34 |
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 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
|
36 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
37 Combine matches with: |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
38 !, -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
|
39 |
1407
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
40 Actions: |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
41 -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
|
42 -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
|
43 -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
|
44 |
1407
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
45 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
|
46 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
|
47 */ |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
48 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
49 // find . ! \( -name blah -print \) |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
50 // find . -o |
1407
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
51 // find -type f |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
52 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
53 // pending issues: |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
54 // old false -a ! new false does not yield true. |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
55 // |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
56 // -user -group -newer evaluate once and save result (where?) |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
57 // add -print if no action (-exec, -ok, -print) |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
58 // find . -print -xdev (should xdev before print) |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
59 // -exec {} + accepts any + after {}, not just immediately after. ";" optional |
1390
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
60 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
61 #define FOR_find |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
62 #include "toys.h" |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
63 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
64 GLOBALS( |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
65 char **filter; |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
66 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
|
67 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
|
68 time_t now; |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
69 ) |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
70 |
1407
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
71 // 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
|
72 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
|
73 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
|
74 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
75 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
|
76 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
|
77 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
|
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 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
80 // 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
|
81 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
|
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 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
|
84 char **newargs; |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
85 int rc; |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
86 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
87 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
|
88 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
89 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
|
90 else dl = &aa->names; |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
91 dlist_terminate(dl); |
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 // 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
|
94 // _and_ a normal -exec, or are at top of tree in -execdir |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
95 if (aa->dir && new->parent) fchdir(new->parent->data); |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
96 else if (TT.topdir != -1) fchdir(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
|
97 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
98 // 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
|
99 newargs = xmalloc(sizeof(char *)*(aa->arglen+aa->namecount+1)); |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
100 if (!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 memcpy(newargs, aa->argstart+1, sizeof(char *)*aa->arglen); |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
102 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
|
103 } else { |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
104 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
|
105 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
|
106 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
107 // Collate argument list |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
108 memcpy(newargs, aa->argstart+1, sizeof(char *)*pos); |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
109 for (dl2 = *dl; dl2; dl2 = dl2->next) newargs[pos++] = dl2->data; |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
110 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
|
111 memcpy(newargs+pos, aa->argstart+aa->curly+1, |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
112 sizeof(char *)*(rest-1)); |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
113 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
|
114 } |
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 rc = xpclose(xpopen(newargs, 0), 0); |
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 llist_traverse(dl, llist_free_double); |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
119 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
120 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
|
121 } |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
122 |
1390
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
123 // 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
|
124 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
|
125 { |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
126 char sign = 0; |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
127 long myval; |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
128 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
129 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
|
130 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
|
131 myval = atolx(str); |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
132 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
|
133 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
134 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
|
135 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
|
136 return val == myval; |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
137 } |
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 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
|
140 { |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
141 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
|
142 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
143 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
|
144 free(s); |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
145 } |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
146 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
147 void todo_store_argument(void) |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
148 { |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
149 error_exit("NOP"); |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
150 } |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
151 |
1407
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
152 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
|
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 char *new; |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
155 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
156 if (!CFG_TOYBOX_I18N) { |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
157 new = xstrdup(s); |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
158 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
|
159 } else { |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
160 // I can't guarantee the string _won't_ expand during reencoding, so...? |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
161 new = xmalloc(strlen(s)*2+1); |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
162 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
163 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
|
164 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
|
165 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
|
166 |
1407
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
167 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
|
168 else { |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
169 // 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
|
170 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
|
171 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
172 // 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
|
173 // 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
|
174 // in case somebody finds an exploit... |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
175 len = wcrtomb(s, c, 0); |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
176 if (len < 1) error_exit("bad utf8 %x", c); |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
177 s += len; |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
178 } |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
179 } |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
180 } |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
181 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
182 return new; |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
183 } |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
184 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
185 // 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
|
186 // 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
|
187 // 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
|
188 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
|
189 { |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
190 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
|
191 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
|
192 char *s, **ss; |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
193 |
1407
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
194 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
|
195 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
196 // 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
|
197 if (new) { |
1390
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
198 if (new->parent) { |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
199 if (!dirtree_notdotdot(new)) return 0; |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
200 if (TT.xdev && new->st.st_dev != new->parent->st.st_dev) return 0; |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
201 } |
1407
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
202 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
|
203 if (!new->again) { |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
204 if (TT.depth) return recurse; |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
205 } else { |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
206 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
|
207 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
208 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
|
209 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
|
210 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
|
211 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
|
212 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
213 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
|
214 } |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
215 } |
1390
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
216 } |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
217 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
218 // 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
|
219 // 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
|
220 // 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
|
221 // 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
|
222 // 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
|
223 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
224 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
|
225 int check = active && test; |
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 s = *ss; |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
228 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
229 // 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
|
230 if (*s != '-') { |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
231 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
|
232 |
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 // 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
|
235 if (check) not = !not; |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
236 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
237 // 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
|
238 // 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
|
239 // 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
|
240 } else if (*s == '(') { |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
241 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
|
242 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
|
243 if (!check) active = 0; |
1393 | 244 not = 0; |
1390
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
245 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
246 // 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
|
247 } else if (*s == ')') { |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
248 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
|
249 // 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
|
250 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
|
251 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
|
252 not = 0; |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
253 } else goto error; |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
254 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
255 continue; |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
256 } else s++; |
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 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
|
259 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
|
260 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
|
261 if (not) goto error; |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
262 if (active) { |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
263 if (!test) test = 1; |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
264 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
|
265 } |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
266 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
267 // Mostly ignore NOP argument |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
268 } 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
|
269 if (not) goto error; |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
270 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
271 } 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
|
272 print++; |
1393 | 273 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
|
274 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
275 } 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
|
276 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
|
277 } 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
|
278 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
|
279 } 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
|
280 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
|
281 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
282 // 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
|
283 } 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
|
284 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
|
285 || !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
|
286 { |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
287 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
|
288 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
|
289 |
1407
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
290 // 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
|
291 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
|
292 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
|
293 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
|
294 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
|
295 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
|
296 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
|
297 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
|
298 } 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
|
299 } |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
300 } |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
301 |
1390
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
302 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
|
303 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
|
304 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
|
305 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
|
306 } |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
307 } 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
|
308 if (check) { |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
309 char *m = ss[1]; |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
310 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
|
311 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
|
312 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
313 if (*m != '-') m2 &= m1; |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
314 test = m1 == m2; |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
315 } |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
316 } 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
|
317 if (check) { |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
318 char c = stridx("bcdlpfs", *ss[1]); |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
319 int types[] = {S_IFBLK, S_IFCHR, S_IFDIR, S_IFLNK, S_IFIFO, |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
320 S_IFREG, S_IFSOCK}; |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
321 |
1407
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
322 if ((new->st.st_mode & S_IFMT) != types[c]) test = 0; |
1390
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
323 } |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
324 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
325 } 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
|
326 if (check) |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
327 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
|
328 } 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
|
329 if (check) |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
330 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
|
331 } 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
|
332 if (check) |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
333 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
|
334 } 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
|
335 if (check) |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
336 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
|
337 } 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
|
338 if (check) test = compare_numsign(new->st.st_nlink, 0, 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, "user")) { |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
340 todo_store_argument(); |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
341 } else if (!strcmp(s, "group")) { |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
342 todo_store_argument(); |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
343 } else if (!strcmp(s, "newer")) { |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
344 todo_store_argument(); |
1407
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
345 } 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
|
346 || !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
|
347 { |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
348 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
|
349 |
1390
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
350 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
|
351 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
352 // 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
|
353 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
|
354 // special case "-exec \;" to fall through to "needs 1 arg" error. |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
355 if (!strcmp(ss[1], ";")) { |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
356 int len; |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
357 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
358 dlist_add_nomalloc(&TT.argdata,(void *)(aa = xzalloc(sizeof(*aa)))); |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
359 aa->argstart = ++ss; |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
360 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
361 // Record command line arguments to -exec |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
362 for (len = 0; ss[len]; len++) { |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
363 if (!strcmp(ss[len], ";")) break; |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
364 else if (!strcmp(ss[len], "{}")) { |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
365 aa->curly = len; |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
366 if (!strcmp(ss[len+1], "+")) { |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
367 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
368 // Measure environment space |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
369 if (!TT.envsize) { |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
370 char **env; |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
371 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
372 for (env = environ; *env; env++) |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
373 TT.envsize += sizeof(char *) + strlen(*env) + 1; |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
374 TT.envsize += sizeof(char *); |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
375 } |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
376 aa->plus++; |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
377 len++; |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
378 break; |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
379 } |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
380 } else aa->argsize += sizeof(char *) + strlen(ss[len]) + 1; |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
381 } |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
382 if (!ss[len]) error_exit("-exec without \\;"); |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
383 ss += len-1; |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
384 aa->arglen = len; |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
385 aa->dir = !!strchr(s, 'd'); |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
386 if (aa->dir && TT.topdir == -1) TT.topdir = xopen(".", 0); |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
387 } |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
388 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
389 // collect names and execute commands |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
390 } else { |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
391 if (check) { |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
392 char *name; |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
393 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
|
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 // Grab command line exec argument list |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
396 aa = (void *)llist_pop(&argdata); |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
397 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
398 // name is always a new malloc, so we can always free it. |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
399 name = aa->dir ? xstrdup(new->name) : 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
|
400 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
401 // Mark entry so COMEAGAIN can call flush_exec() in parent. |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
402 // This is never a valid pointer valud for prev to have otherwise |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
403 if (aa->dir) aa->prev = (void *)1; |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
404 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
405 if (*s == 'o') { |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
406 char *prompt = xmprintf("[%s] %s", ss[1], name); |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
407 if(!(test = yesno(prompt, 0))) goto cont; |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
408 } |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
409 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
410 // Add next name to list (global list without -dir, local with) |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
411 if (aa->dir && 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
|
412 dl = (struct double_list **)&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
|
413 else dl = &aa->names; |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
414 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
415 // Is this + mode? |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
416 if (aa->plus) { |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
417 int size = sizeof(char *)+strlen(name)+1; |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
418 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
419 // Linux caps environment space (env vars + args) at 32 4k pages. |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
420 // todo: is there a way to probe this instead of constant here? |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
421 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
422 if (TT.envsize+aa->argsize+aa->namesize+size >= 131072) |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
423 toys.exitval |= flush_exec(new, aa); |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
424 aa->namesize += size; |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
425 } |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
426 dlist_add(dl, name); |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
427 if (!aa->plus) test = flush_exec(new, aa); |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
428 } |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
429 } |
1390
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
430 } else goto error; |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
431 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
432 // 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
|
433 // 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
|
434 // vs -known noarg) right. |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
435 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
|
436 } |
1407
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
437 cont: |
1390
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
438 // Apply pending "!" to result |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
439 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
|
440 not = 0; |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
441 } |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
442 |
1407
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
443 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
|
444 // 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
|
445 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
|
446 } 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
|
447 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
448 return recurse; |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
449 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
450 error: |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
451 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
|
452 } |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
453 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
454 void find_main(void) |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
455 { |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
456 int i, len; |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
457 char **ss = toys.optargs; |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
458 |
1407
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
459 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
|
460 |
1390
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
461 // Distinguish paths from filters |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
462 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
|
463 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
|
464 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
|
465 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
466 // use "." if no paths |
1407
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
467 if (!*ss || **ss == '-') { |
1390
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
468 ss = (char *[]){"."}; |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
469 len = 1; |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
470 } |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
471 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
472 // 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
|
473 TT.now = time(0); |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
474 do_find(0); |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
475 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
476 // Loop through paths |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
477 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
|
478 struct dirtree *new; |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
479 |
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
480 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
|
481 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
|
482 } |
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 |
434ea531a62c
find.c: first pass at case case insensitivity and exec. (Needs more debugging.)
Rob Landley <rob@landley.net>
parents:
1393
diff
changeset
|
484 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
|
485 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
|
486 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
|
487 } |
1390
c18ad59ae442
Write a new find. Not quite done, but the basics work.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
488 } |