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