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 |
cfbbfaf2 | 18 | static char sccsid[] = "@(#)find.c 4.30 (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 KB |
53 | /* |
54 | * if arguments start with an option, it's new syntax; otherwise, | |
55 | * if has a "-option" anywhere it must be old syntax. | |
56 | */ | |
57 | if (argv[1][0] != '-') | |
58 | for (p = argv + 1; *p; ++p) | |
59 | if (**p == '-') { | |
60 | deprecated = 1; | |
61 | oldsyntax(&argv); | |
62 | break; | |
63 | } | |
64 | if (!deprecated) | |
65 | newsyntax(argc, &argv); | |
66 | ||
45fc66f9 KB |
67 | plan = find_formplan(argv); /* execution plan */ |
68 | find_execute(plan, paths); | |
c08d5d47 | 69 | } |
c08d5d47 | 70 | |
45fc66f9 KB |
71 | /* |
72 | * find_formplan -- | |
73 | * process the command line and create a "plan" corresponding to the | |
74 | * command arguments. | |
75 | */ | |
76 | PLAN * | |
77 | find_formplan(argv) | |
78 | char **argv; | |
79 | { | |
80 | PLAN *plan, *tail, *new; | |
45fc66f9 KB |
81 | PLAN *c_print(), *find_create(), *find_squish_not(), *find_squish_or(); |
82 | PLAN *find_squish_paren(); | |
83 | ||
84 | /* | |
85 | * for each argument in the command line, determine what kind of node | |
86 | * it is, create the appropriate node type and add the new plan node | |
87 | * to the end of the existing plan. The resulting plan is a linked | |
88 | * list of plan nodes. For example, the string: | |
89 | * | |
90 | * % find . -name foo -newer bar -print | |
91 | * | |
92 | * results in the plan: | |
93 | * | |
94 | * [-name foo]--> [-newer bar]--> [-print] | |
95 | * | |
96 | * in this diagram, `[-name foo]' represents the plan node generated | |
97 | * by c_name() with an argument of foo and `-->' represents the | |
98 | * plan->next pointer. | |
99 | */ | |
100 | for (plan = NULL; *argv;) { | |
101 | new = find_create(&argv); | |
102 | if (plan == NULL) | |
103 | tail = plan = new; | |
104 | else { | |
105 | tail->next = new; | |
106 | tail = new; | |
c08d5d47 | 107 | } |
c08d5d47 | 108 | } |
45fc66f9 KB |
109 | |
110 | /* | |
111 | * if the user didn't specify one of -print, -ok or -exec, then -print | |
112 | * is assumed so we add a -print node on the end. It is possible that | |
113 | * the user might want the -print someplace else on the command line, | |
114 | * but there's no way to know that. | |
115 | */ | |
116 | if (!output_specified) { | |
117 | new = c_print(); | |
118 | if (plan == NULL) | |
119 | tail = plan = new; | |
120 | else { | |
121 | tail->next = new; | |
122 | tail = new; | |
c08d5d47 | 123 | } |
c08d5d47 | 124 | } |
45fc66f9 KB |
125 | |
126 | /* | |
127 | * the command line has been completely processed into a search plan | |
128 | * except for the (, ), !, and -o operators. Rearrange the plan so | |
129 | * that the portions of the plan which are affected by the operators | |
130 | * are moved into operator nodes themselves. For example: | |
131 | * | |
132 | * [!]--> [-name foo]--> [-print] | |
133 | * | |
134 | * becomes | |
135 | * | |
136 | * [! [-name foo] ]--> [-print] | |
137 | * | |
138 | * and | |
139 | * | |
140 | * [(]--> [-depth]--> [-name foo]--> [)]--> [-print] | |
141 | * | |
142 | * becomes | |
143 | * | |
144 | * [expr [-depth]-->[-name foo] ]--> [-print] | |
145 | * | |
146 | * operators are handled in order of precedence. | |
147 | */ | |
148 | ||
149 | plan = find_squish_paren(plan); /* ()'s */ | |
150 | plan = find_squish_not(plan); /* !'s */ | |
151 | plan = find_squish_or(plan); /* -o's */ | |
152 | return(plan); | |
153 | } | |
154 | ||
155 | /* | |
156 | * find_execute -- | |
157 | * take a search plan and an array of search paths and executes the plan | |
158 | * over all FTSENT's returned for the given search paths. | |
159 | */ | |
160 | find_execute(plan, paths) | |
161 | PLAN *plan; /* search plan */ | |
162 | char **paths; /* array of pathnames to traverse */ | |
163 | { | |
164 | FTSENT *entry; /* current fts entry */ | |
165 | PLAN *p; | |
166 | ||
167 | if (!(tree = ftsopen(paths, ftsoptions, NULL))) { | |
168 | (void)fprintf(stderr, "find: ftsopen: %s.\n", strerror(errno)); | |
b98da692 | 169 | exit(1); |
365a571b | 170 | } |
45fc66f9 | 171 | while (entry = ftsread(tree)) { |
c26d864e | 172 | switch(entry->fts_info) { |
45fc66f9 KB |
173 | case FTS_DNR: |
174 | (void)fprintf(stderr, | |
c26d864e | 175 | "find: %s: unable to read.\n", entry->fts_path); |
365a571b | 176 | continue; |
45fc66f9 KB |
177 | case FTS_DNX: |
178 | (void)fprintf(stderr, | |
c26d864e | 179 | "find: %s: unable to search.\n", entry->fts_path); |
45fc66f9 KB |
180 | continue; |
181 | case FTS_ERR: | |
182 | (void)fprintf(stderr, | |
c26d864e KB |
183 | "find: %s: %s.\n", entry->fts_path, |
184 | strerror(errno)); | |
45fc66f9 KB |
185 | continue; |
186 | case FTS_D: | |
187 | if (depth) | |
b98da692 | 188 | continue; |
45fc66f9 KB |
189 | break; |
190 | case FTS_DC: | |
191 | (void)fprintf(stderr, | |
c26d864e | 192 | "find: directory cycle: %s.\n", entry->fts_path); |
b98da692 | 193 | continue; |
45fc66f9 KB |
194 | case FTS_DP: |
195 | if (!depth) | |
196 | continue; | |
197 | case FTS_NS: | |
198 | if (!(ftsoptions & FTS_NOSTAT)) { | |
199 | (void)fprintf(stderr, | |
c26d864e | 200 | "find: can't stat: %s.\n", entry->fts_path); |
45fc66f9 KB |
201 | continue; |
202 | } | |
203 | break; | |
b98da692 | 204 | } |
b98da692 | 205 | |
45fc66f9 KB |
206 | /* |
207 | * call all the functions in the execution plan until one is | |
208 | * false or all have been executed. This is where we do all | |
209 | * the work specified by the user on the command line. | |
210 | */ | |
211 | for (p = plan; p && (p->eval)(p, entry); p = p->next); | |
45fc66f9 KB |
212 | } |
213 | (void)ftsclose(tree); | |
b98da692 | 214 | } |