File: | src/usr.bin/find/find.c |
Warning: | line 97, column 4 Value stored to 'tail' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* $OpenBSD: find.c,v 1.23 2018/08/01 06:39:58 tb Exp $ */ |
2 | |
3 | /*- |
4 | * Copyright (c) 1991, 1993 |
5 | * The Regents of the University of California. All rights reserved. |
6 | * |
7 | * This code is derived from software contributed to Berkeley by |
8 | * Cimarron D. Taylor of the University of California, Berkeley. |
9 | * |
10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions |
12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. |
15 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * notice, this list of conditions and the following disclaimer in the |
17 | * documentation and/or other materials provided with the distribution. |
18 | * 3. Neither the name of the University nor the names of its contributors |
19 | * may be used to endorse or promote products derived from this software |
20 | * without specific prior written permission. |
21 | * |
22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
32 | * SUCH DAMAGE. |
33 | */ |
34 | |
35 | #include <sys/types.h> |
36 | #include <sys/stat.h> |
37 | |
38 | #include <err.h> |
39 | #include <errno(*__errno()).h> |
40 | #include <fts.h> |
41 | #include <signal.h> |
42 | #include <stdio.h> |
43 | #include <string.h> |
44 | #include <stdlib.h> |
45 | #include <unistd.h> |
46 | |
47 | int mayexecve; |
48 | |
49 | #include "find.h" |
50 | |
51 | /* |
52 | * find_formplan -- |
53 | * process the command line and create a "plan" corresponding to the |
54 | * command arguments. |
55 | */ |
56 | PLAN * |
57 | find_formplan(char **argv) |
58 | { |
59 | PLAN *plan, *tail, *new; |
60 | |
61 | /* |
62 | * for each argument in the command line, determine what kind of node |
63 | * it is, create the appropriate node type and add the new plan node |
64 | * to the end of the existing plan. The resulting plan is a linked |
65 | * list of plan nodes. For example, the string: |
66 | * |
67 | * % find . -name foo -newer bar -print |
68 | * |
69 | * results in the plan: |
70 | * |
71 | * [-name foo]--> [-newer bar]--> [-print] |
72 | * |
73 | * in this diagram, `[-name foo]' represents the plan node generated |
74 | * by c_name() with an argument of foo and `-->' represents the |
75 | * plan->next pointer. |
76 | */ |
77 | for (plan = tail = NULL((void *)0); *argv;) { |
78 | if (!(new = find_create(&argv))) |
79 | continue; |
80 | if (plan == NULL((void *)0)) |
81 | tail = plan = new; |
82 | else { |
83 | tail->next = new; |
84 | tail = new; |
85 | } |
86 | } |
87 | |
88 | /* |
89 | * if the user didn't specify one of -delete, -exec, -execdir, |
90 | * -ls, -ok, -print or -print0, then -print is assumed so we |
91 | * bracket the current expression with parens, if necessary, |
92 | * and add a -print node on the end. |
93 | */ |
94 | if (!isoutput) { |
95 | if (plan == NULL((void *)0)) { |
96 | new = c_print(NULL((void *)0), NULL((void *)0), 0); |
97 | tail = plan = new; |
Value stored to 'tail' is never read | |
98 | } else { |
99 | new = c_openparen(NULL((void *)0), NULL((void *)0), 0); |
100 | new->next = plan; |
101 | plan = new; |
102 | new = c_closeparen(NULL((void *)0), NULL((void *)0), 0); |
103 | tail->next = new; |
104 | tail = new; |
105 | new = c_print(NULL((void *)0), NULL((void *)0), 0); |
106 | tail->next = new; |
107 | tail = new; |
108 | } |
109 | } |
110 | |
111 | /* |
112 | * the command line has been completely processed into a search plan |
113 | * except for the (, ), !, and -o operators. Rearrange the plan so |
114 | * that the portions of the plan which are affected by the operators |
115 | * are moved into operator nodes themselves. For example: |
116 | * |
117 | * [!]--> [-name foo]--> [-print] |
118 | * |
119 | * becomes |
120 | * |
121 | * [! [-name foo] ]--> [-print] |
122 | * |
123 | * and |
124 | * |
125 | * [(]--> [-depth]--> [-name foo]--> [)]--> [-print] |
126 | * |
127 | * becomes |
128 | * |
129 | * [expr [-depth]-->[-name foo] ]--> [-print] |
130 | * |
131 | * operators are handled in order of precedence. |
132 | */ |
133 | |
134 | plan = paren_squish(plan); /* ()'s */ |
135 | plan = not_squish(plan); /* !'s */ |
136 | plan = or_squish(plan); /* -o's */ |
137 | return (plan); |
138 | } |
139 | |
140 | FTS *tree; /* pointer to top of FTS hierarchy */ |
141 | |
142 | /* |
143 | * find_execute -- |
144 | * take a search plan and an array of search paths and executes the plan |
145 | * over all FTSENT's returned for the given search paths. |
146 | */ |
147 | |
148 | FTSENT *entry; /* shared with SIGINFO handler */ |
149 | |
150 | int |
151 | find_execute(PLAN *plan, /* search plan */ |
152 | char **paths) /* array of pathnames to traverse */ |
153 | { |
154 | sigset_t fullset, oset; |
155 | int r, rval; |
156 | PLAN *p; |
157 | |
158 | if (mayexecve == 0) { |
159 | if (isdelete) { |
160 | if (pledge("stdio rpath cpath getpw", NULL((void *)0)) == -1) |
161 | err(1, "pledge"); |
162 | } else { |
163 | if (pledge("stdio rpath getpw", NULL((void *)0)) == -1) |
164 | err(1, "pledge"); |
165 | } |
166 | } else { |
167 | if (isdelete) { |
168 | if (pledge("stdio rpath cpath getpw proc exec", NULL((void *)0)) |
169 | == -1) |
170 | err(1, "pledge"); |
171 | } else { |
172 | if (pledge("stdio rpath getpw proc exec", NULL((void *)0)) == -1) |
173 | err(1, "pledge"); |
174 | } |
175 | } |
176 | |
177 | rval = 0; |
178 | |
179 | if (!(tree = fts_open(paths, ftsoptions, NULL((void *)0)))) |
180 | err(1, "fts_open"); |
181 | |
182 | sigfillset(&fullset); |
183 | for (;;) { |
184 | (void)sigprocmask(SIG_BLOCK1, &fullset, &oset); |
185 | entry = fts_read(tree); |
186 | (void)sigprocmask(SIG_SETMASK3, &oset, NULL((void *)0)); |
187 | if (entry == NULL((void *)0)) { |
188 | if (errno(*__errno())) |
189 | err(1, "fts_read"); |
190 | break; |
191 | } |
192 | |
193 | switch (entry->fts_info) { |
194 | case FTS_D1: |
195 | if (isdepth) |
196 | continue; |
197 | break; |
198 | case FTS_DP6: |
199 | if (!isdepth) |
200 | continue; |
201 | break; |
202 | case FTS_DNR4: |
203 | case FTS_ERR7: |
204 | case FTS_NS10: |
205 | (void)fflush(stdout(&__sF[1])); |
206 | warnc(entry->fts_errno, "%s", entry->fts_path); |
207 | rval = 1; |
208 | continue; |
209 | } |
210 | #define BADCH" \t\n\\'\"" " \t\n\\'\"" |
211 | if (isxargs && strpbrk(entry->fts_path, BADCH" \t\n\\'\"")) { |
212 | (void)fflush(stdout(&__sF[1])); |
213 | warnx("%s: illegal path", entry->fts_path); |
214 | rval = 1; |
215 | continue; |
216 | } |
217 | |
218 | /* |
219 | * Call all the functions in the execution plan until one is |
220 | * false or all have been executed. This is where we do all |
221 | * the work specified by the user on the command line. |
222 | */ |
223 | for (p = plan; p && (p->eval)(p, entry); p = p->next) |
224 | ; |
225 | } |
226 | (void)fts_close(tree); |
227 | |
228 | /* |
229 | * Cleanup any plans with leftover state. |
230 | * Keep the last non-zero return value. |
231 | */ |
232 | if ((r = find_traverse(plan, plan_cleanup, NULL((void *)0))) != 0) |
233 | rval = r; |
234 | return (rval); |
235 | } |
236 | |
237 | /* |
238 | * find_traverse -- |
239 | * traverse the plan tree and execute func() on all plans. This |
240 | * does not evaluate each plan's eval() function; it is intended |
241 | * for operations that must run on all plans, such as state |
242 | * cleanup. |
243 | * |
244 | * If any func() returns non-zero, then so will find_traverse(). |
245 | */ |
246 | int |
247 | find_traverse(PLAN *plan, int (*func)(PLAN *, void *), void *arg) |
248 | { |
249 | PLAN *p; |
250 | int r, rval; |
251 | |
252 | rval = 0; |
253 | for (p = plan; p; p = p->next) { |
254 | if ((r = func(p, arg)) != 0) |
255 | rval = r; |
256 | if (p->type == N_EXPR || p->type == N_OR) { |
257 | if (p->p_datap_un._p_data[0]) |
258 | if ((r = find_traverse(p->p_datap_un._p_data[0], |
259 | func, arg)) != 0) |
260 | rval = r; |
261 | if (p->p_datap_un._p_data[1]) |
262 | if ((r = find_traverse(p->p_datap_un._p_data[1], |
263 | func, arg)) != 0) |
264 | rval = r; |
265 | } |
266 | } |
267 | return rval; |
268 | } |