Deleted Added
full compact
function.c (32402) function.c (40301)
1/*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. 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 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
38static char sccsid[] = "@(#)function.c 8.10 (Berkeley) 5/4/95";
39#endif /* not lint */
40
41#include <sys/param.h>
42#include <sys/ucred.h>
43#include <sys/stat.h>
44#include <sys/wait.h>
45#include <sys/mount.h>
46
47#include <err.h>
48#include <errno.h>
49#include <fnmatch.h>
50#include <fts.h>
51#include <grp.h>
52#include <pwd.h>
53#include <stdio.h>
54#include <stdlib.h>
55#include <string.h>
56#include <unistd.h>
57
58#include "find.h"
59
60#define COMPARE(a, b) { \
61 switch (plan->flags) { \
62 case F_EQUAL: \
63 return (a == b); \
64 case F_LESSTHAN: \
65 return (a < b); \
66 case F_GREATER: \
67 return (a > b); \
68 default: \
69 abort(); \
70 } \
71}
72
73static PLAN *palloc __P((enum ntype, int (*) __P((PLAN *, FTSENT *))));
74
75/*
76 * find_parsenum --
77 * Parse a string of the form [+-]# and return the value.
78 */
79static long long
80find_parsenum(plan, option, vp, endch)
81 PLAN *plan;
82 char *option, *vp, *endch;
83{
84 long long value;
85 char *endchar, *str; /* Pointer to character ending conversion. */
86
87 /* Determine comparison from leading + or -. */
88 str = vp;
89 switch (*str) {
90 case '+':
91 ++str;
92 plan->flags = F_GREATER;
93 break;
94 case '-':
95 ++str;
96 plan->flags = F_LESSTHAN;
97 break;
98 default:
99 plan->flags = F_EQUAL;
100 break;
101 }
102
103 /*
104 * Convert the string with strtoq(). Note, if strtoq() returns zero
105 * and endchar points to the beginning of the string we know we have
106 * a syntax error.
107 */
108 value = strtoq(str, &endchar, 10);
109 if (value == 0 && endchar == str)
110 errx(1, "%s: %s: illegal numeric value", option, vp);
111 if (endchar[0] && (endch == NULL || endchar[0] != *endch))
112 errx(1, "%s: %s: illegal trailing character", option, vp);
113 if (endch)
114 *endch = endchar[0];
115 return (value);
116}
117
118/*
119 * The value of n for the inode times (atime, ctime, and mtime) is a range,
120 * i.e. n matches from (n - 1) to n 24 hour periods. This interacts with
121 * -n, such that "-mtime -1" would be less than 0 days, which isn't what the
122 * user wanted. Correct so that -1 is "less than 1".
123 */
124#define TIME_CORRECT(p, ttype) \
125 if ((p)->type == ttype && (p)->flags == F_LESSTHAN) \
126 ++((p)->t_data);
127
128/*
129 * -amin n functions --
130 *
131 * True if the difference between the file access time and the
132 * current time is n min periods.
133 */
134int
135f_amin(plan, entry)
136 PLAN *plan;
137 FTSENT *entry;
138{
139 extern time_t now;
140
141 COMPARE((now - entry->fts_statp->st_atime +
142 60 - 1) / 60, plan->t_data);
143}
144
145PLAN *
146c_amin(arg)
147 char *arg;
148{
149 PLAN *new;
150
151 ftsoptions &= ~FTS_NOSTAT;
152
153 new = palloc(N_AMIN, f_amin);
154 new->t_data = find_parsenum(new, "-amin", arg, NULL);
155 TIME_CORRECT(new, N_AMIN);
156 return (new);
157}
158
159
160/*
161 * -atime n functions --
162 *
163 * True if the difference between the file access time and the
164 * current time is n 24 hour periods.
165 */
166int
167f_atime(plan, entry)
168 PLAN *plan;
169 FTSENT *entry;
170{
171 extern time_t now;
172
173 COMPARE((now - entry->fts_statp->st_atime +
174 86400 - 1) / 86400, plan->t_data);
175}
176
177PLAN *
178c_atime(arg)
179 char *arg;
180{
181 PLAN *new;
182
183 ftsoptions &= ~FTS_NOSTAT;
184
185 new = palloc(N_ATIME, f_atime);
186 new->t_data = find_parsenum(new, "-atime", arg, NULL);
187 TIME_CORRECT(new, N_ATIME);
188 return (new);
189}
190
191
192/*
193 * -cmin n functions --
194 *
195 * True if the difference between the last change of file
196 * status information and the current time is n min periods.
197 */
198int
199f_cmin(plan, entry)
200 PLAN *plan;
201 FTSENT *entry;
202{
203 extern time_t now;
204
205 COMPARE((now - entry->fts_statp->st_ctime +
206 60 - 1) / 60, plan->t_data);
207}
208
209PLAN *
210c_cmin(arg)
211 char *arg;
212{
213 PLAN *new;
214
215 ftsoptions &= ~FTS_NOSTAT;
216
217 new = palloc(N_CMIN, f_cmin);
218 new->t_data = find_parsenum(new, "-cmin", arg, NULL);
219 TIME_CORRECT(new, N_CMIN);
220 return (new);
221}
222
223/*
224 * -ctime n functions --
225 *
226 * True if the difference between the last change of file
227 * status information and the current time is n 24 hour periods.
228 */
229int
230f_ctime(plan, entry)
231 PLAN *plan;
232 FTSENT *entry;
233{
234 extern time_t now;
235
236 COMPARE((now - entry->fts_statp->st_ctime +
237 86400 - 1) / 86400, plan->t_data);
238}
239
240PLAN *
241c_ctime(arg)
242 char *arg;
243{
244 PLAN *new;
245
246 ftsoptions &= ~FTS_NOSTAT;
247
248 new = palloc(N_CTIME, f_ctime);
249 new->t_data = find_parsenum(new, "-ctime", arg, NULL);
250 TIME_CORRECT(new, N_CTIME);
251 return (new);
252}
253
254
255/*
256 * -depth functions --
257 *
258 * Always true, causes descent of the directory hierarchy to be done
259 * so that all entries in a directory are acted on before the directory
260 * itself.
261 */
262int
263f_always_true(plan, entry)
264 PLAN *plan;
265 FTSENT *entry;
266{
267 return (1);
268}
269
270PLAN *
271c_depth()
272{
273 isdepth = 1;
274
275 return (palloc(N_DEPTH, f_always_true));
276}
277
278/*
279 * [-exec | -ok] utility [arg ... ] ; functions --
280 *
281 * True if the executed utility returns a zero value as exit status.
282 * The end of the primary expression is delimited by a semicolon. If
283 * "{}" occurs anywhere, it gets replaced by the current pathname.
284 * The current directory for the execution of utility is the same as
285 * the current directory when the find utility was started.
286 *
287 * The primary -ok is different in that it requests affirmation of the
288 * user before executing the utility.
289 */
290int
291f_exec(plan, entry)
292 register PLAN *plan;
293 FTSENT *entry;
294{
295 extern int dotfd;
296 register int cnt;
297 pid_t pid;
298 int status;
299
300 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
301 if (plan->e_len[cnt])
302 brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
303 entry->fts_path, plan->e_len[cnt]);
304
305 if (plan->flags == F_NEEDOK && !queryuser(plan->e_argv))
306 return (0);
307
308 /* make sure find output is interspersed correctly with subprocesses */
309 fflush(stdout);
310
1/*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. 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 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
38static char sccsid[] = "@(#)function.c 8.10 (Berkeley) 5/4/95";
39#endif /* not lint */
40
41#include <sys/param.h>
42#include <sys/ucred.h>
43#include <sys/stat.h>
44#include <sys/wait.h>
45#include <sys/mount.h>
46
47#include <err.h>
48#include <errno.h>
49#include <fnmatch.h>
50#include <fts.h>
51#include <grp.h>
52#include <pwd.h>
53#include <stdio.h>
54#include <stdlib.h>
55#include <string.h>
56#include <unistd.h>
57
58#include "find.h"
59
60#define COMPARE(a, b) { \
61 switch (plan->flags) { \
62 case F_EQUAL: \
63 return (a == b); \
64 case F_LESSTHAN: \
65 return (a < b); \
66 case F_GREATER: \
67 return (a > b); \
68 default: \
69 abort(); \
70 } \
71}
72
73static PLAN *palloc __P((enum ntype, int (*) __P((PLAN *, FTSENT *))));
74
75/*
76 * find_parsenum --
77 * Parse a string of the form [+-]# and return the value.
78 */
79static long long
80find_parsenum(plan, option, vp, endch)
81 PLAN *plan;
82 char *option, *vp, *endch;
83{
84 long long value;
85 char *endchar, *str; /* Pointer to character ending conversion. */
86
87 /* Determine comparison from leading + or -. */
88 str = vp;
89 switch (*str) {
90 case '+':
91 ++str;
92 plan->flags = F_GREATER;
93 break;
94 case '-':
95 ++str;
96 plan->flags = F_LESSTHAN;
97 break;
98 default:
99 plan->flags = F_EQUAL;
100 break;
101 }
102
103 /*
104 * Convert the string with strtoq(). Note, if strtoq() returns zero
105 * and endchar points to the beginning of the string we know we have
106 * a syntax error.
107 */
108 value = strtoq(str, &endchar, 10);
109 if (value == 0 && endchar == str)
110 errx(1, "%s: %s: illegal numeric value", option, vp);
111 if (endchar[0] && (endch == NULL || endchar[0] != *endch))
112 errx(1, "%s: %s: illegal trailing character", option, vp);
113 if (endch)
114 *endch = endchar[0];
115 return (value);
116}
117
118/*
119 * The value of n for the inode times (atime, ctime, and mtime) is a range,
120 * i.e. n matches from (n - 1) to n 24 hour periods. This interacts with
121 * -n, such that "-mtime -1" would be less than 0 days, which isn't what the
122 * user wanted. Correct so that -1 is "less than 1".
123 */
124#define TIME_CORRECT(p, ttype) \
125 if ((p)->type == ttype && (p)->flags == F_LESSTHAN) \
126 ++((p)->t_data);
127
128/*
129 * -amin n functions --
130 *
131 * True if the difference between the file access time and the
132 * current time is n min periods.
133 */
134int
135f_amin(plan, entry)
136 PLAN *plan;
137 FTSENT *entry;
138{
139 extern time_t now;
140
141 COMPARE((now - entry->fts_statp->st_atime +
142 60 - 1) / 60, plan->t_data);
143}
144
145PLAN *
146c_amin(arg)
147 char *arg;
148{
149 PLAN *new;
150
151 ftsoptions &= ~FTS_NOSTAT;
152
153 new = palloc(N_AMIN, f_amin);
154 new->t_data = find_parsenum(new, "-amin", arg, NULL);
155 TIME_CORRECT(new, N_AMIN);
156 return (new);
157}
158
159
160/*
161 * -atime n functions --
162 *
163 * True if the difference between the file access time and the
164 * current time is n 24 hour periods.
165 */
166int
167f_atime(plan, entry)
168 PLAN *plan;
169 FTSENT *entry;
170{
171 extern time_t now;
172
173 COMPARE((now - entry->fts_statp->st_atime +
174 86400 - 1) / 86400, plan->t_data);
175}
176
177PLAN *
178c_atime(arg)
179 char *arg;
180{
181 PLAN *new;
182
183 ftsoptions &= ~FTS_NOSTAT;
184
185 new = palloc(N_ATIME, f_atime);
186 new->t_data = find_parsenum(new, "-atime", arg, NULL);
187 TIME_CORRECT(new, N_ATIME);
188 return (new);
189}
190
191
192/*
193 * -cmin n functions --
194 *
195 * True if the difference between the last change of file
196 * status information and the current time is n min periods.
197 */
198int
199f_cmin(plan, entry)
200 PLAN *plan;
201 FTSENT *entry;
202{
203 extern time_t now;
204
205 COMPARE((now - entry->fts_statp->st_ctime +
206 60 - 1) / 60, plan->t_data);
207}
208
209PLAN *
210c_cmin(arg)
211 char *arg;
212{
213 PLAN *new;
214
215 ftsoptions &= ~FTS_NOSTAT;
216
217 new = palloc(N_CMIN, f_cmin);
218 new->t_data = find_parsenum(new, "-cmin", arg, NULL);
219 TIME_CORRECT(new, N_CMIN);
220 return (new);
221}
222
223/*
224 * -ctime n functions --
225 *
226 * True if the difference between the last change of file
227 * status information and the current time is n 24 hour periods.
228 */
229int
230f_ctime(plan, entry)
231 PLAN *plan;
232 FTSENT *entry;
233{
234 extern time_t now;
235
236 COMPARE((now - entry->fts_statp->st_ctime +
237 86400 - 1) / 86400, plan->t_data);
238}
239
240PLAN *
241c_ctime(arg)
242 char *arg;
243{
244 PLAN *new;
245
246 ftsoptions &= ~FTS_NOSTAT;
247
248 new = palloc(N_CTIME, f_ctime);
249 new->t_data = find_parsenum(new, "-ctime", arg, NULL);
250 TIME_CORRECT(new, N_CTIME);
251 return (new);
252}
253
254
255/*
256 * -depth functions --
257 *
258 * Always true, causes descent of the directory hierarchy to be done
259 * so that all entries in a directory are acted on before the directory
260 * itself.
261 */
262int
263f_always_true(plan, entry)
264 PLAN *plan;
265 FTSENT *entry;
266{
267 return (1);
268}
269
270PLAN *
271c_depth()
272{
273 isdepth = 1;
274
275 return (palloc(N_DEPTH, f_always_true));
276}
277
278/*
279 * [-exec | -ok] utility [arg ... ] ; functions --
280 *
281 * True if the executed utility returns a zero value as exit status.
282 * The end of the primary expression is delimited by a semicolon. If
283 * "{}" occurs anywhere, it gets replaced by the current pathname.
284 * The current directory for the execution of utility is the same as
285 * the current directory when the find utility was started.
286 *
287 * The primary -ok is different in that it requests affirmation of the
288 * user before executing the utility.
289 */
290int
291f_exec(plan, entry)
292 register PLAN *plan;
293 FTSENT *entry;
294{
295 extern int dotfd;
296 register int cnt;
297 pid_t pid;
298 int status;
299
300 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
301 if (plan->e_len[cnt])
302 brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
303 entry->fts_path, plan->e_len[cnt]);
304
305 if (plan->flags == F_NEEDOK && !queryuser(plan->e_argv))
306 return (0);
307
308 /* make sure find output is interspersed correctly with subprocesses */
309 fflush(stdout);
310
311 switch (pid = vfork()) {
311 switch (pid = fork()) {
312 case -1:
313 err(1, "fork");
314 /* NOTREACHED */
315 case 0:
316 if (fchdir(dotfd)) {
317 warn("chdir");
318 _exit(1);
319 }
320 execvp(plan->e_argv[0], plan->e_argv);
321 warn("%s", plan->e_argv[0]);
322 _exit(1);
323 }
324 pid = waitpid(pid, &status, 0);
325 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
326}
327
328/*
329 * c_exec --
330 * build three parallel arrays, one with pointers to the strings passed
331 * on the command line, one with (possibly duplicated) pointers to the
332 * argv array, and one with integer values that are lengths of the
333 * strings, but also flags meaning that the string has to be massaged.
334 */
335PLAN *
336c_exec(argvp, isok)
337 char ***argvp;
338 int isok;
339{
340 PLAN *new; /* node returned */
341 register int cnt;
342 register char **argv, **ap, *p;
343
344 isoutput = 1;
345
346 new = palloc(N_EXEC, f_exec);
347 if (isok)
348 new->flags = F_NEEDOK;
349
350 for (ap = argv = *argvp;; ++ap) {
351 if (!*ap)
352 errx(1,
353 "%s: no terminating \";\"", isok ? "-ok" : "-exec");
354 if (**ap == ';')
355 break;
356 }
357
358 cnt = ap - *argvp + 1;
359 new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
360 new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
361 new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
362
363 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
364 new->e_orig[cnt] = *argv;
365 for (p = *argv; *p; ++p)
366 if (p[0] == '{' && p[1] == '}') {
367 new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
368 new->e_len[cnt] = MAXPATHLEN;
369 break;
370 }
371 if (!*p) {
372 new->e_argv[cnt] = *argv;
373 new->e_len[cnt] = 0;
374 }
375 }
376 new->e_argv[cnt] = new->e_orig[cnt] = NULL;
377
378 *argvp = argv + 1;
379 return (new);
380}
381
382/*
383 * -execdir utility [arg ... ] ; functions --
384 *
385 * True if the executed utility returns a zero value as exit status.
386 * The end of the primary expression is delimited by a semicolon. If
387 * "{}" occurs anywhere, it gets replaced by the unqualified pathname.
388 * The current directory for the execution of utility is the same as
389 * the directory where the file lives.
390 */
391int
392f_execdir(plan, entry)
393 register PLAN *plan;
394 FTSENT *entry;
395{
396 extern int dotfd;
397 register int cnt;
398 pid_t pid;
399 int status;
400 char *file;
401
402 /* XXX - if file/dir ends in '/' this will not work -- can it? */
403 if ((file = strrchr(entry->fts_path, '/')))
404 file++;
405 else
406 file = entry->fts_path;
407
408 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
409 if (plan->e_len[cnt])
410 brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
411 file, plan->e_len[cnt]);
412
413 /* don't mix output of command with find output */
414 fflush(stdout);
415 fflush(stderr);
416
312 case -1:
313 err(1, "fork");
314 /* NOTREACHED */
315 case 0:
316 if (fchdir(dotfd)) {
317 warn("chdir");
318 _exit(1);
319 }
320 execvp(plan->e_argv[0], plan->e_argv);
321 warn("%s", plan->e_argv[0]);
322 _exit(1);
323 }
324 pid = waitpid(pid, &status, 0);
325 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
326}
327
328/*
329 * c_exec --
330 * build three parallel arrays, one with pointers to the strings passed
331 * on the command line, one with (possibly duplicated) pointers to the
332 * argv array, and one with integer values that are lengths of the
333 * strings, but also flags meaning that the string has to be massaged.
334 */
335PLAN *
336c_exec(argvp, isok)
337 char ***argvp;
338 int isok;
339{
340 PLAN *new; /* node returned */
341 register int cnt;
342 register char **argv, **ap, *p;
343
344 isoutput = 1;
345
346 new = palloc(N_EXEC, f_exec);
347 if (isok)
348 new->flags = F_NEEDOK;
349
350 for (ap = argv = *argvp;; ++ap) {
351 if (!*ap)
352 errx(1,
353 "%s: no terminating \";\"", isok ? "-ok" : "-exec");
354 if (**ap == ';')
355 break;
356 }
357
358 cnt = ap - *argvp + 1;
359 new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
360 new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
361 new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
362
363 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
364 new->e_orig[cnt] = *argv;
365 for (p = *argv; *p; ++p)
366 if (p[0] == '{' && p[1] == '}') {
367 new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
368 new->e_len[cnt] = MAXPATHLEN;
369 break;
370 }
371 if (!*p) {
372 new->e_argv[cnt] = *argv;
373 new->e_len[cnt] = 0;
374 }
375 }
376 new->e_argv[cnt] = new->e_orig[cnt] = NULL;
377
378 *argvp = argv + 1;
379 return (new);
380}
381
382/*
383 * -execdir utility [arg ... ] ; functions --
384 *
385 * True if the executed utility returns a zero value as exit status.
386 * The end of the primary expression is delimited by a semicolon. If
387 * "{}" occurs anywhere, it gets replaced by the unqualified pathname.
388 * The current directory for the execution of utility is the same as
389 * the directory where the file lives.
390 */
391int
392f_execdir(plan, entry)
393 register PLAN *plan;
394 FTSENT *entry;
395{
396 extern int dotfd;
397 register int cnt;
398 pid_t pid;
399 int status;
400 char *file;
401
402 /* XXX - if file/dir ends in '/' this will not work -- can it? */
403 if ((file = strrchr(entry->fts_path, '/')))
404 file++;
405 else
406 file = entry->fts_path;
407
408 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
409 if (plan->e_len[cnt])
410 brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
411 file, plan->e_len[cnt]);
412
413 /* don't mix output of command with find output */
414 fflush(stdout);
415 fflush(stderr);
416
417 switch (pid = vfork()) {
417 switch (pid = fork()) {
418 case -1:
419 err(1, "fork");
420 /* NOTREACHED */
421 case 0:
422 execvp(plan->e_argv[0], plan->e_argv);
423 warn("%s", plan->e_argv[0]);
424 _exit(1);
425 }
426 pid = waitpid(pid, &status, 0);
427 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
428}
429
430/*
431 * c_execdir --
432 * build three parallel arrays, one with pointers to the strings passed
433 * on the command line, one with (possibly duplicated) pointers to the
434 * argv array, and one with integer values that are lengths of the
435 * strings, but also flags meaning that the string has to be massaged.
436 */
437PLAN *
438c_execdir(argvp)
439 char ***argvp;
440{
441 PLAN *new; /* node returned */
442 register int cnt;
443 register char **argv, **ap, *p;
444
445 ftsoptions &= ~FTS_NOSTAT;
446 isoutput = 1;
447
448 new = palloc(N_EXECDIR, f_execdir);
449
450 for (ap = argv = *argvp;; ++ap) {
451 if (!*ap)
452 errx(1,
453 "-execdir: no terminating \";\"");
454 if (**ap == ';')
455 break;
456 }
457
458 cnt = ap - *argvp + 1;
459 new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
460 new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
461 new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
462
463 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
464 new->e_orig[cnt] = *argv;
465 for (p = *argv; *p; ++p)
466 if (p[0] == '{' && p[1] == '}') {
467 new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
468 new->e_len[cnt] = MAXPATHLEN;
469 break;
470 }
471 if (!*p) {
472 new->e_argv[cnt] = *argv;
473 new->e_len[cnt] = 0;
474 }
475 }
476 new->e_argv[cnt] = new->e_orig[cnt] = NULL;
477
478 *argvp = argv + 1;
479 return (new);
480}
481
482/*
483 * -follow functions --
484 *
485 * Always true, causes symbolic links to be followed on a global
486 * basis.
487 */
488PLAN *
489c_follow()
490{
491 ftsoptions &= ~FTS_PHYSICAL;
492 ftsoptions |= FTS_LOGICAL;
493
494 return (palloc(N_FOLLOW, f_always_true));
495}
496
497/*
498 * -fstype functions --
499 *
500 * True if the file is of a certain type.
501 */
502int
503f_fstype(plan, entry)
504 PLAN *plan;
505 FTSENT *entry;
506{
507 static dev_t curdev; /* need a guaranteed illegal dev value */
508 static int first = 1;
509 struct statfs sb;
510 static int val_type, val_flags;
511 char *p, save[2];
512
513 /* Only check when we cross mount point. */
514 if (first || curdev != entry->fts_statp->st_dev) {
515 curdev = entry->fts_statp->st_dev;
516
517 /*
518 * Statfs follows symlinks; find wants the link's file system,
519 * not where it points.
520 */
521 if (entry->fts_info == FTS_SL ||
522 entry->fts_info == FTS_SLNONE) {
523 if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
524 ++p;
525 else
526 p = entry->fts_accpath;
527 save[0] = p[0];
528 p[0] = '.';
529 save[1] = p[1];
530 p[1] = '\0';
531
532 } else
533 p = NULL;
534
535 if (statfs(entry->fts_accpath, &sb))
536 err(1, "%s", entry->fts_accpath);
537
538 if (p) {
539 p[0] = save[0];
540 p[1] = save[1];
541 }
542
543 first = 0;
544
545 /*
546 * Further tests may need both of these values, so
547 * always copy both of them.
548 */
549 val_flags = sb.f_flags;
550 val_type = sb.f_type;
551 }
552 switch (plan->flags) {
553 case F_MTFLAG:
554 return (val_flags & plan->mt_data) != 0;
555 case F_MTTYPE:
556 return (val_type == plan->mt_data);
557 default:
558 abort();
559 }
560}
561
562#if !defined(__NetBSD__)
563PLAN *
564c_fstype(arg)
565 char *arg;
566{
567 register PLAN *new;
568 struct vfsconf vfc;
569
570 ftsoptions &= ~FTS_NOSTAT;
571
572 new = palloc(N_FSTYPE, f_fstype);
573
574 /*
575 * Check first for a filesystem name.
576 */
577 if (getvfsbyname(arg, &vfc) == 0) {
578 new->flags = F_MTTYPE;
579 new->mt_data = vfc.vfc_typenum;
580 return (new);
581 }
582
583 switch (*arg) {
584 case 'l':
585 if (!strcmp(arg, "local")) {
586 new->flags = F_MTFLAG;
587 new->mt_data = MNT_LOCAL;
588 return (new);
589 }
590 break;
591 case 'r':
592 if (!strcmp(arg, "rdonly")) {
593 new->flags = F_MTFLAG;
594 new->mt_data = MNT_RDONLY;
595 return (new);
596 }
597 break;
598 }
599 errx(1, "%s: unknown file type", arg);
600 /* NOTREACHED */
601}
602#endif
603
604/*
605 * -group gname functions --
606 *
607 * True if the file belongs to the group gname. If gname is numeric and
608 * an equivalent of the getgrnam() function does not return a valid group
609 * name, gname is taken as a group ID.
610 */
611int
612f_group(plan, entry)
613 PLAN *plan;
614 FTSENT *entry;
615{
616 return (entry->fts_statp->st_gid == plan->g_data);
617}
618
619PLAN *
620c_group(gname)
621 char *gname;
622{
623 PLAN *new;
624 struct group *g;
625 gid_t gid;
626
627 ftsoptions &= ~FTS_NOSTAT;
628
629 g = getgrnam(gname);
630 if (g == NULL) {
631 gid = atoi(gname);
632 if (gid == 0 && gname[0] != '0')
633 errx(1, "-group: %s: no such group", gname);
634 } else
635 gid = g->gr_gid;
636
637 new = palloc(N_GROUP, f_group);
638 new->g_data = gid;
639 return (new);
640}
641
642/*
643 * -inum n functions --
644 *
645 * True if the file has inode # n.
646 */
647int
648f_inum(plan, entry)
649 PLAN *plan;
650 FTSENT *entry;
651{
652 COMPARE(entry->fts_statp->st_ino, plan->i_data);
653}
654
655PLAN *
656c_inum(arg)
657 char *arg;
658{
659 PLAN *new;
660
661 ftsoptions &= ~FTS_NOSTAT;
662
663 new = palloc(N_INUM, f_inum);
664 new->i_data = find_parsenum(new, "-inum", arg, NULL);
665 return (new);
666}
667
668/*
669 * -links n functions --
670 *
671 * True if the file has n links.
672 */
673int
674f_links(plan, entry)
675 PLAN *plan;
676 FTSENT *entry;
677{
678 COMPARE(entry->fts_statp->st_nlink, plan->l_data);
679}
680
681PLAN *
682c_links(arg)
683 char *arg;
684{
685 PLAN *new;
686
687 ftsoptions &= ~FTS_NOSTAT;
688
689 new = palloc(N_LINKS, f_links);
690 new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL);
691 return (new);
692}
693
694/*
695 * -ls functions --
696 *
697 * Always true - prints the current entry to stdout in "ls" format.
698 */
699int
700f_ls(plan, entry)
701 PLAN *plan;
702 FTSENT *entry;
703{
704 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
705 return (1);
706}
707
708PLAN *
709c_ls()
710{
711 ftsoptions &= ~FTS_NOSTAT;
712 isoutput = 1;
713
714 return (palloc(N_LS, f_ls));
715}
716
717/*
718 * -mtime n functions --
719 *
720 * True if the difference between the file modification time and the
721 * current time is n 24 hour periods.
722 */
723int
724f_mtime(plan, entry)
725 PLAN *plan;
726 FTSENT *entry;
727{
728 extern time_t now;
729
730 COMPARE((now - entry->fts_statp->st_mtime + 86400 - 1) /
731 86400, plan->t_data);
732}
733
734PLAN *
735c_mtime(arg)
736 char *arg;
737{
738 PLAN *new;
739
740 ftsoptions &= ~FTS_NOSTAT;
741
742 new = palloc(N_MTIME, f_mtime);
743 new->t_data = find_parsenum(new, "-mtime", arg, NULL);
744 TIME_CORRECT(new, N_MTIME);
745 return (new);
746}
747
748/*
749 * -mmin n functions --
750 *
751 * True if the difference between the file modification time and the
752 * current time is n min periods.
753 */
754int
755f_mmin(plan, entry)
756 PLAN *plan;
757 FTSENT *entry;
758{
759 extern time_t now;
760
761 COMPARE((now - entry->fts_statp->st_mtime + 60 - 1) /
762 60, plan->t_data);
763}
764
765PLAN *
766c_mmin(arg)
767 char *arg;
768{
769 PLAN *new;
770
771 ftsoptions &= ~FTS_NOSTAT;
772
773 new = palloc(N_MMIN, f_mmin);
774 new->t_data = find_parsenum(new, "-mmin", arg, NULL);
775 TIME_CORRECT(new, N_MMIN);
776 return (new);
777}
778
779
780/*
781 * -name functions --
782 *
783 * True if the basename of the filename being examined
784 * matches pattern using Pattern Matching Notation S3.14
785 */
786int
787f_name(plan, entry)
788 PLAN *plan;
789 FTSENT *entry;
790{
791 return (!fnmatch(plan->c_data, entry->fts_name, 0));
792}
793
794PLAN *
795c_name(pattern)
796 char *pattern;
797{
798 PLAN *new;
799
800 new = palloc(N_NAME, f_name);
801 new->c_data = pattern;
802 return (new);
803}
804
805/*
806 * -newer file functions --
807 *
808 * True if the current file has been modified more recently
809 * then the modification time of the file named by the pathname
810 * file.
811 */
812int
813f_newer(plan, entry)
814 PLAN *plan;
815 FTSENT *entry;
816{
817 return (entry->fts_statp->st_mtime > plan->t_data);
818}
819
820PLAN *
821c_newer(filename)
822 char *filename;
823{
824 PLAN *new;
825 struct stat sb;
826
827 ftsoptions &= ~FTS_NOSTAT;
828
829 if (stat(filename, &sb))
830 err(1, "%s", filename);
831 new = palloc(N_NEWER, f_newer);
832 new->t_data = sb.st_mtime;
833 return (new);
834}
835
836/*
837 * -nogroup functions --
838 *
839 * True if file belongs to a user ID for which the equivalent
840 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
841 */
842int
843f_nogroup(plan, entry)
844 PLAN *plan;
845 FTSENT *entry;
846{
847 char *group_from_gid();
848
849 return (group_from_gid(entry->fts_statp->st_gid, 1) ? 0 : 1);
850}
851
852PLAN *
853c_nogroup()
854{
855 ftsoptions &= ~FTS_NOSTAT;
856
857 return (palloc(N_NOGROUP, f_nogroup));
858}
859
860/*
861 * -nouser functions --
862 *
863 * True if file belongs to a user ID for which the equivalent
864 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
865 */
866int
867f_nouser(plan, entry)
868 PLAN *plan;
869 FTSENT *entry;
870{
871 char *user_from_uid();
872
873 return (user_from_uid(entry->fts_statp->st_uid, 1) ? 0 : 1);
874}
875
876PLAN *
877c_nouser()
878{
879 ftsoptions &= ~FTS_NOSTAT;
880
881 return (palloc(N_NOUSER, f_nouser));
882}
883
884/*
885 * -path functions --
886 *
887 * True if the path of the filename being examined
888 * matches pattern using Pattern Matching Notation S3.14
889 */
890int
891f_path(plan, entry)
892 PLAN *plan;
893 FTSENT *entry;
894{
895 return (!fnmatch(plan->c_data, entry->fts_path, 0));
896}
897
898PLAN *
899c_path(pattern)
900 char *pattern;
901{
902 PLAN *new;
903
904 new = palloc(N_NAME, f_path);
905 new->c_data = pattern;
906 return (new);
907}
908
909/*
910 * -perm functions --
911 *
912 * The mode argument is used to represent file mode bits. If it starts
913 * with a leading digit, it's treated as an octal mode, otherwise as a
914 * symbolic mode.
915 */
916int
917f_perm(plan, entry)
918 PLAN *plan;
919 FTSENT *entry;
920{
921 mode_t mode;
922
923 mode = entry->fts_statp->st_mode &
924 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
925 if (plan->flags == F_ATLEAST)
926 return ((plan->m_data | mode) == mode);
927 else
928 return (mode == plan->m_data);
929 /* NOTREACHED */
930}
931
932PLAN *
933c_perm(perm)
934 char *perm;
935{
936 PLAN *new;
937 mode_t *set;
938
939 ftsoptions &= ~FTS_NOSTAT;
940
941 new = palloc(N_PERM, f_perm);
942
943 if (*perm == '-') {
944 new->flags = F_ATLEAST;
945 ++perm;
946 }
947
948 if ((set = setmode(perm)) == NULL)
949 err(1, "-perm: %s: illegal mode string", perm);
950
951 new->m_data = getmode(set, 0);
952 return (new);
953}
954
955/*
956 * -print functions --
957 *
958 * Always true, causes the current pathame to be written to
959 * standard output.
960 */
961int
962f_print(plan, entry)
963 PLAN *plan;
964 FTSENT *entry;
965{
966 (void)puts(entry->fts_path);
967 return (1);
968}
969
970PLAN *
971c_print()
972{
973 isoutput = 1;
974
975 return (palloc(N_PRINT, f_print));
976}
977
978/*
979 * -print0 functions --
980 *
981 * Always true, causes the current pathame to be written to
982 * standard output followed by a NUL character
983 */
984int
985f_print0(plan, entry)
986 PLAN *plan;
987 FTSENT *entry;
988{
989 fputs(entry->fts_path, stdout);
990 fputc('\0', stdout);
991 return (1);
992}
993
994PLAN *
995c_print0()
996{
997 isoutput = 1;
998
999 return (palloc(N_PRINT0, f_print0));
1000}
1001
1002/*
1003 * -prune functions --
1004 *
1005 * Prune a portion of the hierarchy.
1006 */
1007int
1008f_prune(plan, entry)
1009 PLAN *plan;
1010 FTSENT *entry;
1011{
1012 extern FTS *tree;
1013
1014 if (fts_set(tree, entry, FTS_SKIP))
1015 err(1, "%s", entry->fts_path);
1016 return (1);
1017}
1018
1019PLAN *
1020c_prune()
1021{
1022 return (palloc(N_PRUNE, f_prune));
1023}
1024
1025/*
1026 * -size n[c] functions --
1027 *
1028 * True if the file size in bytes, divided by an implementation defined
1029 * value and rounded up to the next integer, is n. If n is followed by
1030 * a c, the size is in bytes.
1031 */
1032#define FIND_SIZE 512
1033static int divsize = 1;
1034
1035int
1036f_size(plan, entry)
1037 PLAN *plan;
1038 FTSENT *entry;
1039{
1040 off_t size;
1041
1042 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1043 FIND_SIZE : entry->fts_statp->st_size;
1044 COMPARE(size, plan->o_data);
1045}
1046
1047PLAN *
1048c_size(arg)
1049 char *arg;
1050{
1051 PLAN *new;
1052 char endch;
1053
1054 ftsoptions &= ~FTS_NOSTAT;
1055
1056 new = palloc(N_SIZE, f_size);
1057 endch = 'c';
1058 new->o_data = find_parsenum(new, "-size", arg, &endch);
1059 if (endch == 'c')
1060 divsize = 0;
1061 return (new);
1062}
1063
1064/*
1065 * -type c functions --
1066 *
1067 * True if the type of the file is c, where c is b, c, d, p, f or w
1068 * for block special file, character special file, directory, FIFO,
1069 * regular file or whiteout respectively.
1070 */
1071int
1072f_type(plan, entry)
1073 PLAN *plan;
1074 FTSENT *entry;
1075{
1076 return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data);
1077}
1078
1079PLAN *
1080c_type(typestring)
1081 char *typestring;
1082{
1083 PLAN *new;
1084 mode_t mask;
1085
1086 ftsoptions &= ~FTS_NOSTAT;
1087
1088 switch (typestring[0]) {
1089 case 'b':
1090 mask = S_IFBLK;
1091 break;
1092 case 'c':
1093 mask = S_IFCHR;
1094 break;
1095 case 'd':
1096 mask = S_IFDIR;
1097 break;
1098 case 'f':
1099 mask = S_IFREG;
1100 break;
1101 case 'l':
1102 mask = S_IFLNK;
1103 break;
1104 case 'p':
1105 mask = S_IFIFO;
1106 break;
1107 case 's':
1108 mask = S_IFSOCK;
1109 break;
1110#ifdef FTS_WHITEOUT
1111 case 'w':
1112 mask = S_IFWHT;
1113 ftsoptions |= FTS_WHITEOUT;
1114 break;
1115#endif /* FTS_WHITEOUT */
1116 default:
1117 errx(1, "-type: %s: unknown type", typestring);
1118 }
1119
1120 new = palloc(N_TYPE, f_type);
1121 new->m_data = mask;
1122 return (new);
1123}
1124
1125/*
1126 * -delete functions --
1127 *
1128 * True always. Makes it's best shot and continues on regardless.
1129 */
1130int
1131f_delete(plan, entry)
1132 PLAN *plan;
1133 FTSENT *entry;
1134{
1135 /* ignore these from fts */
1136 if (strcmp(entry->fts_accpath, ".") == 0 ||
1137 strcmp(entry->fts_accpath, "..") == 0)
1138 return (1);
1139
1140 /* sanity check */
1141 if (isdepth == 0 || /* depth off */
1142 (ftsoptions & FTS_NOSTAT) || /* not stat()ing */
1143 !(ftsoptions & FTS_PHYSICAL) || /* physical off */
1144 (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */
1145 errx(1, "-delete: insecure options got turned on");
1146
1147 /* Potentially unsafe - do not accept relative paths whatsoever */
1148 if (strchr(entry->fts_accpath, '/') != NULL)
1149 errx(1, "-delete: %s: relative path potentially not safe",
1150 entry->fts_accpath);
1151
1152 /* Turn off user immutable bits if running as root */
1153 if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
1154 !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
1155 geteuid() == 0)
1156 chflags(entry->fts_accpath,
1157 entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
1158
1159 /* rmdir directories, unlink everything else */
1160 if (S_ISDIR(entry->fts_statp->st_mode)) {
1161 if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
1162 warn("-delete: rmdir(%s)", entry->fts_path);
1163 } else {
1164 if (unlink(entry->fts_accpath) < 0)
1165 warn("-delete: unlink(%s)", entry->fts_path);
1166 }
1167
1168 /* "succeed" */
1169 return (1);
1170}
1171
1172PLAN *
1173c_delete()
1174{
1175
1176 ftsoptions &= ~FTS_NOSTAT; /* no optimise */
1177 ftsoptions |= FTS_PHYSICAL; /* disable -follow */
1178 ftsoptions &= ~FTS_LOGICAL; /* disable -follow */
1179 isoutput = 1; /* possible output */
1180 isdepth = 1; /* -depth implied */
1181
1182 return (palloc(N_DELETE, f_delete));
1183}
1184
1185/*
1186 * -user uname functions --
1187 *
1188 * True if the file belongs to the user uname. If uname is numeric and
1189 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1190 * return a valid user name, uname is taken as a user ID.
1191 */
1192int
1193f_user(plan, entry)
1194 PLAN *plan;
1195 FTSENT *entry;
1196{
1197 return (entry->fts_statp->st_uid == plan->u_data);
1198}
1199
1200PLAN *
1201c_user(username)
1202 char *username;
1203{
1204 PLAN *new;
1205 struct passwd *p;
1206 uid_t uid;
1207
1208 ftsoptions &= ~FTS_NOSTAT;
1209
1210 p = getpwnam(username);
1211 if (p == NULL) {
1212 uid = atoi(username);
1213 if (uid == 0 && username[0] != '0')
1214 errx(1, "-user: %s: no such user", username);
1215 } else
1216 uid = p->pw_uid;
1217
1218 new = palloc(N_USER, f_user);
1219 new->u_data = uid;
1220 return (new);
1221}
1222
1223/*
1224 * -xdev functions --
1225 *
1226 * Always true, causes find not to decend past directories that have a
1227 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1228 */
1229PLAN *
1230c_xdev()
1231{
1232 ftsoptions |= FTS_XDEV;
1233
1234 return (palloc(N_XDEV, f_always_true));
1235}
1236
1237/*
1238 * ( expression ) functions --
1239 *
1240 * True if expression is true.
1241 */
1242int
1243f_expr(plan, entry)
1244 PLAN *plan;
1245 FTSENT *entry;
1246{
1247 register PLAN *p;
1248 register int state;
1249
1250 for (p = plan->p_data[0];
1251 p && (state = (p->eval)(p, entry)); p = p->next);
1252 return (state);
1253}
1254
1255/*
1256 * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers. They are
1257 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1258 * to a N_EXPR node containing the expression and the ')' node is discarded.
1259 */
1260PLAN *
1261c_openparen()
1262{
1263 return (palloc(N_OPENPAREN, (int (*)())-1));
1264}
1265
1266PLAN *
1267c_closeparen()
1268{
1269 return (palloc(N_CLOSEPAREN, (int (*)())-1));
1270}
1271
1272/*
1273 * ! expression functions --
1274 *
1275 * Negation of a primary; the unary NOT operator.
1276 */
1277int
1278f_not(plan, entry)
1279 PLAN *plan;
1280 FTSENT *entry;
1281{
1282 register PLAN *p;
1283 register int state;
1284
1285 for (p = plan->p_data[0];
1286 p && (state = (p->eval)(p, entry)); p = p->next);
1287 return (!state);
1288}
1289
1290PLAN *
1291c_not()
1292{
1293 return (palloc(N_NOT, f_not));
1294}
1295
1296/*
1297 * expression -o expression functions --
1298 *
1299 * Alternation of primaries; the OR operator. The second expression is
1300 * not evaluated if the first expression is true.
1301 */
1302int
1303f_or(plan, entry)
1304 PLAN *plan;
1305 FTSENT *entry;
1306{
1307 register PLAN *p;
1308 register int state;
1309
1310 for (p = plan->p_data[0];
1311 p && (state = (p->eval)(p, entry)); p = p->next);
1312
1313 if (state)
1314 return (1);
1315
1316 for (p = plan->p_data[1];
1317 p && (state = (p->eval)(p, entry)); p = p->next);
1318 return (state);
1319}
1320
1321PLAN *
1322c_or()
1323{
1324 return (palloc(N_OR, f_or));
1325}
1326
1327static PLAN *
1328palloc(t, f)
1329 enum ntype t;
1330 int (*f) __P((PLAN *, FTSENT *));
1331{
1332 PLAN *new;
1333
1334 if ((new = malloc(sizeof(PLAN))) == NULL)
1335 err(1, NULL);
1336 new->type = t;
1337 new->eval = f;
1338 new->flags = 0;
1339 new->next = NULL;
1340 return (new);
1341}
418 case -1:
419 err(1, "fork");
420 /* NOTREACHED */
421 case 0:
422 execvp(plan->e_argv[0], plan->e_argv);
423 warn("%s", plan->e_argv[0]);
424 _exit(1);
425 }
426 pid = waitpid(pid, &status, 0);
427 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
428}
429
430/*
431 * c_execdir --
432 * build three parallel arrays, one with pointers to the strings passed
433 * on the command line, one with (possibly duplicated) pointers to the
434 * argv array, and one with integer values that are lengths of the
435 * strings, but also flags meaning that the string has to be massaged.
436 */
437PLAN *
438c_execdir(argvp)
439 char ***argvp;
440{
441 PLAN *new; /* node returned */
442 register int cnt;
443 register char **argv, **ap, *p;
444
445 ftsoptions &= ~FTS_NOSTAT;
446 isoutput = 1;
447
448 new = palloc(N_EXECDIR, f_execdir);
449
450 for (ap = argv = *argvp;; ++ap) {
451 if (!*ap)
452 errx(1,
453 "-execdir: no terminating \";\"");
454 if (**ap == ';')
455 break;
456 }
457
458 cnt = ap - *argvp + 1;
459 new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
460 new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
461 new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
462
463 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
464 new->e_orig[cnt] = *argv;
465 for (p = *argv; *p; ++p)
466 if (p[0] == '{' && p[1] == '}') {
467 new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
468 new->e_len[cnt] = MAXPATHLEN;
469 break;
470 }
471 if (!*p) {
472 new->e_argv[cnt] = *argv;
473 new->e_len[cnt] = 0;
474 }
475 }
476 new->e_argv[cnt] = new->e_orig[cnt] = NULL;
477
478 *argvp = argv + 1;
479 return (new);
480}
481
482/*
483 * -follow functions --
484 *
485 * Always true, causes symbolic links to be followed on a global
486 * basis.
487 */
488PLAN *
489c_follow()
490{
491 ftsoptions &= ~FTS_PHYSICAL;
492 ftsoptions |= FTS_LOGICAL;
493
494 return (palloc(N_FOLLOW, f_always_true));
495}
496
497/*
498 * -fstype functions --
499 *
500 * True if the file is of a certain type.
501 */
502int
503f_fstype(plan, entry)
504 PLAN *plan;
505 FTSENT *entry;
506{
507 static dev_t curdev; /* need a guaranteed illegal dev value */
508 static int first = 1;
509 struct statfs sb;
510 static int val_type, val_flags;
511 char *p, save[2];
512
513 /* Only check when we cross mount point. */
514 if (first || curdev != entry->fts_statp->st_dev) {
515 curdev = entry->fts_statp->st_dev;
516
517 /*
518 * Statfs follows symlinks; find wants the link's file system,
519 * not where it points.
520 */
521 if (entry->fts_info == FTS_SL ||
522 entry->fts_info == FTS_SLNONE) {
523 if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
524 ++p;
525 else
526 p = entry->fts_accpath;
527 save[0] = p[0];
528 p[0] = '.';
529 save[1] = p[1];
530 p[1] = '\0';
531
532 } else
533 p = NULL;
534
535 if (statfs(entry->fts_accpath, &sb))
536 err(1, "%s", entry->fts_accpath);
537
538 if (p) {
539 p[0] = save[0];
540 p[1] = save[1];
541 }
542
543 first = 0;
544
545 /*
546 * Further tests may need both of these values, so
547 * always copy both of them.
548 */
549 val_flags = sb.f_flags;
550 val_type = sb.f_type;
551 }
552 switch (plan->flags) {
553 case F_MTFLAG:
554 return (val_flags & plan->mt_data) != 0;
555 case F_MTTYPE:
556 return (val_type == plan->mt_data);
557 default:
558 abort();
559 }
560}
561
562#if !defined(__NetBSD__)
563PLAN *
564c_fstype(arg)
565 char *arg;
566{
567 register PLAN *new;
568 struct vfsconf vfc;
569
570 ftsoptions &= ~FTS_NOSTAT;
571
572 new = palloc(N_FSTYPE, f_fstype);
573
574 /*
575 * Check first for a filesystem name.
576 */
577 if (getvfsbyname(arg, &vfc) == 0) {
578 new->flags = F_MTTYPE;
579 new->mt_data = vfc.vfc_typenum;
580 return (new);
581 }
582
583 switch (*arg) {
584 case 'l':
585 if (!strcmp(arg, "local")) {
586 new->flags = F_MTFLAG;
587 new->mt_data = MNT_LOCAL;
588 return (new);
589 }
590 break;
591 case 'r':
592 if (!strcmp(arg, "rdonly")) {
593 new->flags = F_MTFLAG;
594 new->mt_data = MNT_RDONLY;
595 return (new);
596 }
597 break;
598 }
599 errx(1, "%s: unknown file type", arg);
600 /* NOTREACHED */
601}
602#endif
603
604/*
605 * -group gname functions --
606 *
607 * True if the file belongs to the group gname. If gname is numeric and
608 * an equivalent of the getgrnam() function does not return a valid group
609 * name, gname is taken as a group ID.
610 */
611int
612f_group(plan, entry)
613 PLAN *plan;
614 FTSENT *entry;
615{
616 return (entry->fts_statp->st_gid == plan->g_data);
617}
618
619PLAN *
620c_group(gname)
621 char *gname;
622{
623 PLAN *new;
624 struct group *g;
625 gid_t gid;
626
627 ftsoptions &= ~FTS_NOSTAT;
628
629 g = getgrnam(gname);
630 if (g == NULL) {
631 gid = atoi(gname);
632 if (gid == 0 && gname[0] != '0')
633 errx(1, "-group: %s: no such group", gname);
634 } else
635 gid = g->gr_gid;
636
637 new = palloc(N_GROUP, f_group);
638 new->g_data = gid;
639 return (new);
640}
641
642/*
643 * -inum n functions --
644 *
645 * True if the file has inode # n.
646 */
647int
648f_inum(plan, entry)
649 PLAN *plan;
650 FTSENT *entry;
651{
652 COMPARE(entry->fts_statp->st_ino, plan->i_data);
653}
654
655PLAN *
656c_inum(arg)
657 char *arg;
658{
659 PLAN *new;
660
661 ftsoptions &= ~FTS_NOSTAT;
662
663 new = palloc(N_INUM, f_inum);
664 new->i_data = find_parsenum(new, "-inum", arg, NULL);
665 return (new);
666}
667
668/*
669 * -links n functions --
670 *
671 * True if the file has n links.
672 */
673int
674f_links(plan, entry)
675 PLAN *plan;
676 FTSENT *entry;
677{
678 COMPARE(entry->fts_statp->st_nlink, plan->l_data);
679}
680
681PLAN *
682c_links(arg)
683 char *arg;
684{
685 PLAN *new;
686
687 ftsoptions &= ~FTS_NOSTAT;
688
689 new = palloc(N_LINKS, f_links);
690 new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL);
691 return (new);
692}
693
694/*
695 * -ls functions --
696 *
697 * Always true - prints the current entry to stdout in "ls" format.
698 */
699int
700f_ls(plan, entry)
701 PLAN *plan;
702 FTSENT *entry;
703{
704 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
705 return (1);
706}
707
708PLAN *
709c_ls()
710{
711 ftsoptions &= ~FTS_NOSTAT;
712 isoutput = 1;
713
714 return (palloc(N_LS, f_ls));
715}
716
717/*
718 * -mtime n functions --
719 *
720 * True if the difference between the file modification time and the
721 * current time is n 24 hour periods.
722 */
723int
724f_mtime(plan, entry)
725 PLAN *plan;
726 FTSENT *entry;
727{
728 extern time_t now;
729
730 COMPARE((now - entry->fts_statp->st_mtime + 86400 - 1) /
731 86400, plan->t_data);
732}
733
734PLAN *
735c_mtime(arg)
736 char *arg;
737{
738 PLAN *new;
739
740 ftsoptions &= ~FTS_NOSTAT;
741
742 new = palloc(N_MTIME, f_mtime);
743 new->t_data = find_parsenum(new, "-mtime", arg, NULL);
744 TIME_CORRECT(new, N_MTIME);
745 return (new);
746}
747
748/*
749 * -mmin n functions --
750 *
751 * True if the difference between the file modification time and the
752 * current time is n min periods.
753 */
754int
755f_mmin(plan, entry)
756 PLAN *plan;
757 FTSENT *entry;
758{
759 extern time_t now;
760
761 COMPARE((now - entry->fts_statp->st_mtime + 60 - 1) /
762 60, plan->t_data);
763}
764
765PLAN *
766c_mmin(arg)
767 char *arg;
768{
769 PLAN *new;
770
771 ftsoptions &= ~FTS_NOSTAT;
772
773 new = palloc(N_MMIN, f_mmin);
774 new->t_data = find_parsenum(new, "-mmin", arg, NULL);
775 TIME_CORRECT(new, N_MMIN);
776 return (new);
777}
778
779
780/*
781 * -name functions --
782 *
783 * True if the basename of the filename being examined
784 * matches pattern using Pattern Matching Notation S3.14
785 */
786int
787f_name(plan, entry)
788 PLAN *plan;
789 FTSENT *entry;
790{
791 return (!fnmatch(plan->c_data, entry->fts_name, 0));
792}
793
794PLAN *
795c_name(pattern)
796 char *pattern;
797{
798 PLAN *new;
799
800 new = palloc(N_NAME, f_name);
801 new->c_data = pattern;
802 return (new);
803}
804
805/*
806 * -newer file functions --
807 *
808 * True if the current file has been modified more recently
809 * then the modification time of the file named by the pathname
810 * file.
811 */
812int
813f_newer(plan, entry)
814 PLAN *plan;
815 FTSENT *entry;
816{
817 return (entry->fts_statp->st_mtime > plan->t_data);
818}
819
820PLAN *
821c_newer(filename)
822 char *filename;
823{
824 PLAN *new;
825 struct stat sb;
826
827 ftsoptions &= ~FTS_NOSTAT;
828
829 if (stat(filename, &sb))
830 err(1, "%s", filename);
831 new = palloc(N_NEWER, f_newer);
832 new->t_data = sb.st_mtime;
833 return (new);
834}
835
836/*
837 * -nogroup functions --
838 *
839 * True if file belongs to a user ID for which the equivalent
840 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
841 */
842int
843f_nogroup(plan, entry)
844 PLAN *plan;
845 FTSENT *entry;
846{
847 char *group_from_gid();
848
849 return (group_from_gid(entry->fts_statp->st_gid, 1) ? 0 : 1);
850}
851
852PLAN *
853c_nogroup()
854{
855 ftsoptions &= ~FTS_NOSTAT;
856
857 return (palloc(N_NOGROUP, f_nogroup));
858}
859
860/*
861 * -nouser functions --
862 *
863 * True if file belongs to a user ID for which the equivalent
864 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
865 */
866int
867f_nouser(plan, entry)
868 PLAN *plan;
869 FTSENT *entry;
870{
871 char *user_from_uid();
872
873 return (user_from_uid(entry->fts_statp->st_uid, 1) ? 0 : 1);
874}
875
876PLAN *
877c_nouser()
878{
879 ftsoptions &= ~FTS_NOSTAT;
880
881 return (palloc(N_NOUSER, f_nouser));
882}
883
884/*
885 * -path functions --
886 *
887 * True if the path of the filename being examined
888 * matches pattern using Pattern Matching Notation S3.14
889 */
890int
891f_path(plan, entry)
892 PLAN *plan;
893 FTSENT *entry;
894{
895 return (!fnmatch(plan->c_data, entry->fts_path, 0));
896}
897
898PLAN *
899c_path(pattern)
900 char *pattern;
901{
902 PLAN *new;
903
904 new = palloc(N_NAME, f_path);
905 new->c_data = pattern;
906 return (new);
907}
908
909/*
910 * -perm functions --
911 *
912 * The mode argument is used to represent file mode bits. If it starts
913 * with a leading digit, it's treated as an octal mode, otherwise as a
914 * symbolic mode.
915 */
916int
917f_perm(plan, entry)
918 PLAN *plan;
919 FTSENT *entry;
920{
921 mode_t mode;
922
923 mode = entry->fts_statp->st_mode &
924 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
925 if (plan->flags == F_ATLEAST)
926 return ((plan->m_data | mode) == mode);
927 else
928 return (mode == plan->m_data);
929 /* NOTREACHED */
930}
931
932PLAN *
933c_perm(perm)
934 char *perm;
935{
936 PLAN *new;
937 mode_t *set;
938
939 ftsoptions &= ~FTS_NOSTAT;
940
941 new = palloc(N_PERM, f_perm);
942
943 if (*perm == '-') {
944 new->flags = F_ATLEAST;
945 ++perm;
946 }
947
948 if ((set = setmode(perm)) == NULL)
949 err(1, "-perm: %s: illegal mode string", perm);
950
951 new->m_data = getmode(set, 0);
952 return (new);
953}
954
955/*
956 * -print functions --
957 *
958 * Always true, causes the current pathame to be written to
959 * standard output.
960 */
961int
962f_print(plan, entry)
963 PLAN *plan;
964 FTSENT *entry;
965{
966 (void)puts(entry->fts_path);
967 return (1);
968}
969
970PLAN *
971c_print()
972{
973 isoutput = 1;
974
975 return (palloc(N_PRINT, f_print));
976}
977
978/*
979 * -print0 functions --
980 *
981 * Always true, causes the current pathame to be written to
982 * standard output followed by a NUL character
983 */
984int
985f_print0(plan, entry)
986 PLAN *plan;
987 FTSENT *entry;
988{
989 fputs(entry->fts_path, stdout);
990 fputc('\0', stdout);
991 return (1);
992}
993
994PLAN *
995c_print0()
996{
997 isoutput = 1;
998
999 return (palloc(N_PRINT0, f_print0));
1000}
1001
1002/*
1003 * -prune functions --
1004 *
1005 * Prune a portion of the hierarchy.
1006 */
1007int
1008f_prune(plan, entry)
1009 PLAN *plan;
1010 FTSENT *entry;
1011{
1012 extern FTS *tree;
1013
1014 if (fts_set(tree, entry, FTS_SKIP))
1015 err(1, "%s", entry->fts_path);
1016 return (1);
1017}
1018
1019PLAN *
1020c_prune()
1021{
1022 return (palloc(N_PRUNE, f_prune));
1023}
1024
1025/*
1026 * -size n[c] functions --
1027 *
1028 * True if the file size in bytes, divided by an implementation defined
1029 * value and rounded up to the next integer, is n. If n is followed by
1030 * a c, the size is in bytes.
1031 */
1032#define FIND_SIZE 512
1033static int divsize = 1;
1034
1035int
1036f_size(plan, entry)
1037 PLAN *plan;
1038 FTSENT *entry;
1039{
1040 off_t size;
1041
1042 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1043 FIND_SIZE : entry->fts_statp->st_size;
1044 COMPARE(size, plan->o_data);
1045}
1046
1047PLAN *
1048c_size(arg)
1049 char *arg;
1050{
1051 PLAN *new;
1052 char endch;
1053
1054 ftsoptions &= ~FTS_NOSTAT;
1055
1056 new = palloc(N_SIZE, f_size);
1057 endch = 'c';
1058 new->o_data = find_parsenum(new, "-size", arg, &endch);
1059 if (endch == 'c')
1060 divsize = 0;
1061 return (new);
1062}
1063
1064/*
1065 * -type c functions --
1066 *
1067 * True if the type of the file is c, where c is b, c, d, p, f or w
1068 * for block special file, character special file, directory, FIFO,
1069 * regular file or whiteout respectively.
1070 */
1071int
1072f_type(plan, entry)
1073 PLAN *plan;
1074 FTSENT *entry;
1075{
1076 return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data);
1077}
1078
1079PLAN *
1080c_type(typestring)
1081 char *typestring;
1082{
1083 PLAN *new;
1084 mode_t mask;
1085
1086 ftsoptions &= ~FTS_NOSTAT;
1087
1088 switch (typestring[0]) {
1089 case 'b':
1090 mask = S_IFBLK;
1091 break;
1092 case 'c':
1093 mask = S_IFCHR;
1094 break;
1095 case 'd':
1096 mask = S_IFDIR;
1097 break;
1098 case 'f':
1099 mask = S_IFREG;
1100 break;
1101 case 'l':
1102 mask = S_IFLNK;
1103 break;
1104 case 'p':
1105 mask = S_IFIFO;
1106 break;
1107 case 's':
1108 mask = S_IFSOCK;
1109 break;
1110#ifdef FTS_WHITEOUT
1111 case 'w':
1112 mask = S_IFWHT;
1113 ftsoptions |= FTS_WHITEOUT;
1114 break;
1115#endif /* FTS_WHITEOUT */
1116 default:
1117 errx(1, "-type: %s: unknown type", typestring);
1118 }
1119
1120 new = palloc(N_TYPE, f_type);
1121 new->m_data = mask;
1122 return (new);
1123}
1124
1125/*
1126 * -delete functions --
1127 *
1128 * True always. Makes it's best shot and continues on regardless.
1129 */
1130int
1131f_delete(plan, entry)
1132 PLAN *plan;
1133 FTSENT *entry;
1134{
1135 /* ignore these from fts */
1136 if (strcmp(entry->fts_accpath, ".") == 0 ||
1137 strcmp(entry->fts_accpath, "..") == 0)
1138 return (1);
1139
1140 /* sanity check */
1141 if (isdepth == 0 || /* depth off */
1142 (ftsoptions & FTS_NOSTAT) || /* not stat()ing */
1143 !(ftsoptions & FTS_PHYSICAL) || /* physical off */
1144 (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */
1145 errx(1, "-delete: insecure options got turned on");
1146
1147 /* Potentially unsafe - do not accept relative paths whatsoever */
1148 if (strchr(entry->fts_accpath, '/') != NULL)
1149 errx(1, "-delete: %s: relative path potentially not safe",
1150 entry->fts_accpath);
1151
1152 /* Turn off user immutable bits if running as root */
1153 if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
1154 !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
1155 geteuid() == 0)
1156 chflags(entry->fts_accpath,
1157 entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
1158
1159 /* rmdir directories, unlink everything else */
1160 if (S_ISDIR(entry->fts_statp->st_mode)) {
1161 if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
1162 warn("-delete: rmdir(%s)", entry->fts_path);
1163 } else {
1164 if (unlink(entry->fts_accpath) < 0)
1165 warn("-delete: unlink(%s)", entry->fts_path);
1166 }
1167
1168 /* "succeed" */
1169 return (1);
1170}
1171
1172PLAN *
1173c_delete()
1174{
1175
1176 ftsoptions &= ~FTS_NOSTAT; /* no optimise */
1177 ftsoptions |= FTS_PHYSICAL; /* disable -follow */
1178 ftsoptions &= ~FTS_LOGICAL; /* disable -follow */
1179 isoutput = 1; /* possible output */
1180 isdepth = 1; /* -depth implied */
1181
1182 return (palloc(N_DELETE, f_delete));
1183}
1184
1185/*
1186 * -user uname functions --
1187 *
1188 * True if the file belongs to the user uname. If uname is numeric and
1189 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1190 * return a valid user name, uname is taken as a user ID.
1191 */
1192int
1193f_user(plan, entry)
1194 PLAN *plan;
1195 FTSENT *entry;
1196{
1197 return (entry->fts_statp->st_uid == plan->u_data);
1198}
1199
1200PLAN *
1201c_user(username)
1202 char *username;
1203{
1204 PLAN *new;
1205 struct passwd *p;
1206 uid_t uid;
1207
1208 ftsoptions &= ~FTS_NOSTAT;
1209
1210 p = getpwnam(username);
1211 if (p == NULL) {
1212 uid = atoi(username);
1213 if (uid == 0 && username[0] != '0')
1214 errx(1, "-user: %s: no such user", username);
1215 } else
1216 uid = p->pw_uid;
1217
1218 new = palloc(N_USER, f_user);
1219 new->u_data = uid;
1220 return (new);
1221}
1222
1223/*
1224 * -xdev functions --
1225 *
1226 * Always true, causes find not to decend past directories that have a
1227 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1228 */
1229PLAN *
1230c_xdev()
1231{
1232 ftsoptions |= FTS_XDEV;
1233
1234 return (palloc(N_XDEV, f_always_true));
1235}
1236
1237/*
1238 * ( expression ) functions --
1239 *
1240 * True if expression is true.
1241 */
1242int
1243f_expr(plan, entry)
1244 PLAN *plan;
1245 FTSENT *entry;
1246{
1247 register PLAN *p;
1248 register int state;
1249
1250 for (p = plan->p_data[0];
1251 p && (state = (p->eval)(p, entry)); p = p->next);
1252 return (state);
1253}
1254
1255/*
1256 * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers. They are
1257 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1258 * to a N_EXPR node containing the expression and the ')' node is discarded.
1259 */
1260PLAN *
1261c_openparen()
1262{
1263 return (palloc(N_OPENPAREN, (int (*)())-1));
1264}
1265
1266PLAN *
1267c_closeparen()
1268{
1269 return (palloc(N_CLOSEPAREN, (int (*)())-1));
1270}
1271
1272/*
1273 * ! expression functions --
1274 *
1275 * Negation of a primary; the unary NOT operator.
1276 */
1277int
1278f_not(plan, entry)
1279 PLAN *plan;
1280 FTSENT *entry;
1281{
1282 register PLAN *p;
1283 register int state;
1284
1285 for (p = plan->p_data[0];
1286 p && (state = (p->eval)(p, entry)); p = p->next);
1287 return (!state);
1288}
1289
1290PLAN *
1291c_not()
1292{
1293 return (palloc(N_NOT, f_not));
1294}
1295
1296/*
1297 * expression -o expression functions --
1298 *
1299 * Alternation of primaries; the OR operator. The second expression is
1300 * not evaluated if the first expression is true.
1301 */
1302int
1303f_or(plan, entry)
1304 PLAN *plan;
1305 FTSENT *entry;
1306{
1307 register PLAN *p;
1308 register int state;
1309
1310 for (p = plan->p_data[0];
1311 p && (state = (p->eval)(p, entry)); p = p->next);
1312
1313 if (state)
1314 return (1);
1315
1316 for (p = plan->p_data[1];
1317 p && (state = (p->eval)(p, entry)); p = p->next);
1318 return (state);
1319}
1320
1321PLAN *
1322c_or()
1323{
1324 return (palloc(N_OR, f_or));
1325}
1326
1327static PLAN *
1328palloc(t, f)
1329 enum ntype t;
1330 int (*f) __P((PLAN *, FTSENT *));
1331{
1332 PLAN *new;
1333
1334 if ((new = malloc(sizeof(PLAN))) == NULL)
1335 err(1, NULL);
1336 new->type = t;
1337 new->eval = f;
1338 new->flags = 0;
1339 new->next = NULL;
1340 return (new);
1341}