Commit | Line | Data |
---|---|---|
45fc66f9 KB |
1 | /*- |
2 | * Copyright (c) 1990 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * This code is derived from software contributed to Berkeley by | |
6 | * Cimarron D. Taylor of the University of California, Berkeley. | |
7 | * | |
8 | * %sccs.include.redist.c% | |
9 | */ | |
c08d5d47 | 10 | |
45fc66f9 KB |
11 | #ifndef lint |
12 | char copyright[] = | |
13 | "@(#) Copyright (c) 1990 The Regents of the University of California.\n\ | |
14 | All rights reserved.\n"; | |
15 | #endif /* not lint */ | |
b98da692 | 16 | |
45fc66f9 | 17 | #ifndef lint |
a476d0ac | 18 | static char sccsid[] = "@(#)find.c 4.31 (Berkeley) %G%"; |
45fc66f9 | 19 | #endif /* not lint */ |
c08d5d47 | 20 | |
45fc66f9 KB |
21 | #include <sys/types.h> |
22 | #include <sys/stat.h> | |
23 | #include <fts.h> | |
24 | #include <stdio.h> | |
38dde0cd | 25 | #include <string.h> |
45fc66f9 KB |
26 | #include <errno.h> |
27 | #include "find.h" | |
28 | ||
29 | FTS *tree; /* pointer to top of FTS hierarchy */ | |
30 | time_t now; /* time find was run */ | |
45fc66f9 | 31 | int ftsoptions; /* options passed to ftsopen() */ |
202dd4ce | 32 | int deprecated; /* old or new syntax */ |
45fc66f9 KB |
33 | int depth; /* set by -depth option */ |
34 | int output_specified; /* one of -print, -ok or -exec was specified */ | |
46b257b1 | 35 | |
46b257b1 KM |
36 | main(argc, argv) |
37 | int argc; | |
45fc66f9 | 38 | char **argv; |
c08d5d47 | 39 | { |
45fc66f9 | 40 | PLAN *plan; |
202dd4ce | 41 | char **p, **paths; |
45fc66f9 KB |
42 | PLAN *find_formplan(); |
43 | time_t time(); | |
44 | ||
202dd4ce | 45 | (void)time(&now); /* initialize the time-of-day */ |
c08d5d47 | 46 | |
45fc66f9 KB |
47 | if (argc < 2) |
48 | usage(); | |
202dd4ce KB |
49 | |
50 | paths = argv; | |
cfbbfaf2 | 51 | ftsoptions = FTS_NOSTAT|FTS_PHYSICAL; |
c08d5d47 | 52 | |
202dd4ce | 53 | /* |
a476d0ac KB |
54 | * if arguments start with an option, treat it like new syntax; |
55 | * otherwise, if has a "-option" anywhere (which isn't an argument | |
56 | * to another command) treat it as old syntax. | |
202dd4ce KB |
57 | */ |
58 | if (argv[1][0] != '-') | |
a476d0ac KB |
59 | for (p = argv + 1; *p; ++p) { |
60 | if (!strcmp(*p, "exec") || !strcmp(*p, "ok")) { | |
61 | while (p[1] && strcmp(*++p, ";")); | |
62 | continue; | |
63 | } | |
202dd4ce KB |
64 | if (**p == '-') { |
65 | deprecated = 1; | |
66 | oldsyntax(&argv); | |
67 | break; | |
68 | } | |
a476d0ac | 69 | } |
202dd4ce KB |
70 | if (!deprecated) |
71 | newsyntax(argc, &argv); | |
72 | ||
45fc66f9 KB |
73 | plan = find_formplan(argv); /* execution plan */ |
74 | find_execute(plan, paths); | |
c08d5d47 | 75 | } |
c08d5d47 | 76 | |
45fc66f9 KB |
77 | /* |
78 | * find_formplan -- | |
79 | * process the command line and create a "plan" corresponding to the | |
80 | * command arguments. | |
81 | */ | |
82 | PLAN * | |
83 | find_formplan(argv) | |
84 | char **argv; | |
85 | { | |
86 | PLAN *plan, *tail, *new; | |
45fc66f9 KB |
87 | PLAN *c_print(), *find_create(), *find_squish_not(), *find_squish_or(); |
88 | PLAN *find_squish_paren(); | |
89 | ||
90 | /* | |
91 | * for each argument in the command line, determine what kind of node | |
92 | * it is, create the appropriate node type and add the new plan node | |
93 | * to the end of the existing plan. The resulting plan is a linked | |
94 | * list of plan nodes. For example, the string: | |
95 | * | |
96 | * % find . -name foo -newer bar -print | |
97 | * | |
98 | * results in the plan: | |
99 | * | |
100 | * [-name foo]--> [-newer bar]--> [-print] | |
101 | * | |
102 | * in this diagram, `[-name foo]' represents the plan node generated | |
103 | * by c_name() with an argument of foo and `-->' represents the | |
104 | * plan->next pointer. | |
105 | */ | |
106 | for (plan = NULL; *argv;) { | |
107 | new = find_create(&argv); | |
108 | if (plan == NULL) | |
109 | tail = plan = new; | |
110 | else { | |
111 | tail->next = new; | |
112 | tail = new; | |
c08d5d47 | 113 | } |
c08d5d47 | 114 | } |
45fc66f9 KB |
115 | |
116 | /* | |
117 | * if the user didn't specify one of -print, -ok or -exec, then -print | |
118 | * is assumed so we add a -print node on the end. It is possible that | |
119 | * the user might want the -print someplace else on the command line, | |
120 | * but there's no way to know that. | |
121 | */ | |
122 | if (!output_specified) { | |
123 | new = c_print(); | |
124 | if (plan == NULL) | |
125 | tail = plan = new; | |
126 | else { | |
127 | tail->next = new; | |
128 | tail = new; | |
c08d5d47 | 129 | } |
c08d5d47 | 130 | } |
45fc66f9 KB |
131 | |
132 | /* | |
133 | * the command line has been completely processed into a search plan | |
134 | * except for the (, ), !, and -o operators. Rearrange the plan so | |
135 | * that the portions of the plan which are affected by the operators | |
136 | * are moved into operator nodes themselves. For example: | |
137 | * | |
138 | * [!]--> [-name foo]--> [-print] | |
139 | * | |
140 | * becomes | |
141 | * | |
142 | * [! [-name foo] ]--> [-print] | |
143 | * | |
144 | * and | |
145 | * | |
146 | * [(]--> [-depth]--> [-name foo]--> [)]--> [-print] | |
147 | * | |
148 | * becomes | |
149 | * | |
150 | * [expr [-depth]-->[-name foo] ]--> [-print] | |
151 | * | |
152 | * operators are handled in order of precedence. | |
153 | */ | |
154 | ||
155 | plan = find_squish_paren(plan); /* ()'s */ | |
156 | plan = find_squish_not(plan); /* !'s */ | |
157 | plan = find_squish_or(plan); /* -o's */ | |
158 | return(plan); | |
159 | } | |
160 | ||
161 | /* | |
162 | * find_execute -- | |
163 | * take a search plan and an array of search paths and executes the plan | |
164 | * over all FTSENT's returned for the given search paths. | |
165 | */ | |
166 | find_execute(plan, paths) | |
167 | PLAN *plan; /* search plan */ | |
168 | char **paths; /* array of pathnames to traverse */ | |
169 | { | |
170 | FTSENT *entry; /* current fts entry */ | |
171 | PLAN *p; | |
172 | ||
173 | if (!(tree = ftsopen(paths, ftsoptions, NULL))) { | |
174 | (void)fprintf(stderr, "find: ftsopen: %s.\n", strerror(errno)); | |
b98da692 | 175 | exit(1); |
365a571b | 176 | } |
45fc66f9 | 177 | while (entry = ftsread(tree)) { |
c26d864e | 178 | switch(entry->fts_info) { |
45fc66f9 KB |
179 | case FTS_DNR: |
180 | (void)fprintf(stderr, | |
c26d864e | 181 | "find: %s: unable to read.\n", entry->fts_path); |
365a571b | 182 | continue; |
45fc66f9 KB |
183 | case FTS_DNX: |
184 | (void)fprintf(stderr, | |
c26d864e | 185 | "find: %s: unable to search.\n", entry->fts_path); |
45fc66f9 KB |
186 | continue; |
187 | case FTS_ERR: | |
188 | (void)fprintf(stderr, | |
c26d864e KB |
189 | "find: %s: %s.\n", entry->fts_path, |
190 | strerror(errno)); | |
45fc66f9 KB |
191 | continue; |
192 | case FTS_D: | |
193 | if (depth) | |
b98da692 | 194 | continue; |
45fc66f9 KB |
195 | break; |
196 | case FTS_DC: | |
197 | (void)fprintf(stderr, | |
c26d864e | 198 | "find: directory cycle: %s.\n", entry->fts_path); |
b98da692 | 199 | continue; |
45fc66f9 KB |
200 | case FTS_DP: |
201 | if (!depth) | |
202 | continue; | |
203 | case FTS_NS: | |
204 | if (!(ftsoptions & FTS_NOSTAT)) { | |
205 | (void)fprintf(stderr, | |
c26d864e | 206 | "find: can't stat: %s.\n", entry->fts_path); |
45fc66f9 KB |
207 | continue; |
208 | } | |
209 | break; | |
b98da692 | 210 | } |
b98da692 | 211 | |
45fc66f9 KB |
212 | /* |
213 | * call all the functions in the execution plan until one is | |
214 | * false or all have been executed. This is where we do all | |
215 | * the work specified by the user on the command line. | |
216 | */ | |
217 | for (p = plan; p && (p->eval)(p, entry); p = p->next); | |
45fc66f9 KB |
218 | } |
219 | (void)ftsclose(tree); | |
b98da692 | 220 | } |