function.c revision 1.61 1 /* $NetBSD: function.c,v 1.61 2007/02/02 15:48:54 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Cimarron D. Taylor of the University of California, Berkeley.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "from: @(#)function.c 8.10 (Berkeley) 5/4/95";
39 #else
40 __RCSID("$NetBSD: function.c,v 1.61 2007/02/02 15:48:54 christos Exp $");
41 #endif
42 #endif /* not lint */
43
44 #include <sys/param.h>
45 #include <sys/stat.h>
46 #include <sys/wait.h>
47 #include <sys/mount.h>
48
49 #include <dirent.h>
50 #include <err.h>
51 #include <errno.h>
52 #include <fnmatch.h>
53 #include <fts.h>
54 #include <grp.h>
55 #include <inttypes.h>
56 #include <limits.h>
57 #include <pwd.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <tzfile.h>
62 #include <unistd.h>
63 #include <util.h>
64
65 #include "find.h"
66
67 #define COMPARE(a, b) { \
68 switch (plan->flags) { \
69 case F_EQUAL: \
70 return (a == b); \
71 case F_LESSTHAN: \
72 return (a < b); \
73 case F_GREATER: \
74 return (a > b); \
75 default: \
76 abort(); \
77 } \
78 }
79
80 static int64_t find_parsenum(PLAN *, const char *, const char *, char *);
81 static void run_f_exec(PLAN *);
82 int f_always_true(PLAN *, FTSENT *);
83 int f_amin(PLAN *, FTSENT *);
84 int f_anewer(PLAN *, FTSENT *);
85 int f_atime(PLAN *, FTSENT *);
86 int f_cmin(PLAN *, FTSENT *);
87 int f_cnewer(PLAN *, FTSENT *);
88 int f_ctime(PLAN *, FTSENT *);
89 int f_empty(PLAN *, FTSENT *);
90 int f_exec(PLAN *, FTSENT *);
91 int f_execdir(PLAN *, FTSENT *);
92 int f_false(PLAN *, FTSENT *);
93 int f_flags(PLAN *, FTSENT *);
94 int f_fprint(PLAN *, FTSENT *);
95 int f_fstype(PLAN *, FTSENT *);
96 int f_group(PLAN *, FTSENT *);
97 int f_iname(PLAN *, FTSENT *);
98 int f_inum(PLAN *, FTSENT *);
99 int f_links(PLAN *, FTSENT *);
100 int f_ls(PLAN *, FTSENT *);
101 int f_mindepth(PLAN *, FTSENT *);
102 int f_maxdepth(PLAN *, FTSENT *);
103 int f_mmin(PLAN *, FTSENT *);
104 int f_mtime(PLAN *, FTSENT *);
105 int f_name(PLAN *, FTSENT *);
106 int f_newer(PLAN *, FTSENT *);
107 int f_nogroup(PLAN *, FTSENT *);
108 int f_nouser(PLAN *, FTSENT *);
109 int f_path(PLAN *, FTSENT *);
110 int f_perm(PLAN *, FTSENT *);
111 int f_print(PLAN *, FTSENT *);
112 int f_print0(PLAN *, FTSENT *);
113 int f_printx(PLAN *, FTSENT *);
114 int f_prune(PLAN *, FTSENT *);
115 int f_regex(PLAN *, FTSENT *);
116 int f_size(PLAN *, FTSENT *);
117 int f_type(PLAN *, FTSENT *);
118 int f_user(PLAN *, FTSENT *);
119 int f_not(PLAN *, FTSENT *);
120 int f_or(PLAN *, FTSENT *);
121 static PLAN *c_regex_common(char ***, int, enum ntype, int);
122 static PLAN *palloc(enum ntype, int (*)(PLAN *, FTSENT *));
123
124 extern int dotfd;
125 extern FTS *tree;
126 extern time_t now;
127
128 /*
129 * find_parsenum --
130 * Parse a string of the form [+-]# and return the value.
131 */
132 static int64_t
133 find_parsenum(PLAN *plan, const char *option, const char *vp, char *endch)
134 {
135 int64_t value;
136 const char *str;
137 char *endchar; /* Pointer to character ending conversion. */
138
139 /* Determine comparison from leading + or -. */
140 str = vp;
141 switch (*str) {
142 case '+':
143 ++str;
144 plan->flags = F_GREATER;
145 break;
146 case '-':
147 ++str;
148 plan->flags = F_LESSTHAN;
149 break;
150 default:
151 plan->flags = F_EQUAL;
152 break;
153 }
154
155 /*
156 * Convert the string with strtol(). Note, if strtol() returns zero
157 * and endchar points to the beginning of the string we know we have
158 * a syntax error.
159 */
160 value = strtoq(str, &endchar, 10);
161 if (value == 0 && endchar == str)
162 errx(1, "%s: %s: illegal numeric value", option, vp);
163 if (endchar[0] && (endch == NULL || endchar[0] != *endch))
164 errx(1, "%s: %s: illegal trailing character", option, vp);
165 if (endch)
166 *endch = endchar[0];
167 return (value);
168 }
169
170 /*
171 * The value of n for the inode times (atime, ctime, and mtime) is a range,
172 * i.e. n matches from (n - 1) to n 24 hour periods. This interacts with
173 * -n, such that "-mtime -1" would be less than 0 days, which isn't what the
174 * user wanted. Correct so that -1 is "less than 1".
175 */
176 #define TIME_CORRECT(p, ttype) \
177 if ((p)->type == ttype && (p)->flags == F_LESSTHAN) \
178 ++((p)->t_data);
179
180 /*
181 * -amin n functions --
182 *
183 * True if the difference between the file access time and the
184 * current time is n 1 minute periods.
185 */
186 int
187 f_amin(PLAN *plan, FTSENT *entry)
188 {
189 COMPARE((now - entry->fts_statp->st_atime +
190 SECSPERMIN - 1) / SECSPERMIN, plan->t_data);
191 }
192
193 PLAN *
194 c_amin(char ***argvp, int isok)
195 {
196 char *arg = **argvp;
197 PLAN *new;
198
199 (*argvp)++;
200 ftsoptions &= ~FTS_NOSTAT;
201
202 new = palloc(N_AMIN, f_amin);
203 new->t_data = find_parsenum(new, "-amin", arg, NULL);
204 TIME_CORRECT(new, N_AMIN);
205 return (new);
206 }
207
208 /*
209 * -anewer file functions --
210 *
211 * True if the current file has been accessed more recently
212 * than the access time of the file named by the pathname
213 * file.
214 */
215 int
216 f_anewer(plan, entry)
217 PLAN *plan;
218 FTSENT *entry;
219 {
220
221 return (entry->fts_statp->st_atime > plan->t_data);
222 }
223
224 PLAN *
225 c_anewer(char ***argvp, int isok)
226 {
227 char *filename = **argvp;
228 PLAN *new;
229 struct stat sb;
230
231 (*argvp)++;
232 ftsoptions &= ~FTS_NOSTAT;
233
234 if (stat(filename, &sb))
235 err(1, "%s", filename);
236 new = palloc(N_ANEWER, f_anewer);
237 new->t_data = sb.st_atime;
238 return (new);
239 }
240
241 /*
242 * -atime n functions --
243 *
244 * True if the difference between the file access time and the
245 * current time is n 24 hour periods.
246 */
247 int
248 f_atime(PLAN *plan, FTSENT *entry)
249 {
250 COMPARE((now - entry->fts_statp->st_atime +
251 SECSPERDAY - 1) / SECSPERDAY, plan->t_data);
252 }
253
254 PLAN *
255 c_atime(char ***argvp, int isok)
256 {
257 char *arg = **argvp;
258 PLAN *new;
259
260 (*argvp)++;
261 ftsoptions &= ~FTS_NOSTAT;
262
263 new = palloc(N_ATIME, f_atime);
264 new->t_data = find_parsenum(new, "-atime", arg, NULL);
265 TIME_CORRECT(new, N_ATIME);
266 return (new);
267 }
268 /*
269 * -cmin n functions --
270 *
271 * True if the difference between the last change of file
272 * status information and the current time is n 24 hour periods.
273 */
274 int
275 f_cmin(PLAN *plan, FTSENT *entry)
276 {
277 COMPARE((now - entry->fts_statp->st_ctime +
278 SECSPERMIN - 1) / SECSPERMIN, plan->t_data);
279 }
280
281 PLAN *
282 c_cmin(char ***argvp, int isok)
283 {
284 char *arg = **argvp;
285 PLAN *new;
286
287 (*argvp)++;
288 ftsoptions &= ~FTS_NOSTAT;
289
290 new = palloc(N_CMIN, f_cmin);
291 new->t_data = find_parsenum(new, "-cmin", arg, NULL);
292 TIME_CORRECT(new, N_CMIN);
293 return (new);
294 }
295
296 /*
297 * -cnewer file functions --
298 *
299 * True if the current file has been changed more recently
300 * than the changed time of the file named by the pathname
301 * file.
302 */
303 int
304 f_cnewer(PLAN *plan, FTSENT *entry)
305 {
306
307 return (entry->fts_statp->st_ctime > plan->t_data);
308 }
309
310 PLAN *
311 c_cnewer(char ***argvp, int isok)
312 {
313 char *filename = **argvp;
314 PLAN *new;
315 struct stat sb;
316
317 (*argvp)++;
318 ftsoptions &= ~FTS_NOSTAT;
319
320 if (stat(filename, &sb))
321 err(1, "%s", filename);
322 new = palloc(N_CNEWER, f_cnewer);
323 new->t_data = sb.st_ctime;
324 return (new);
325 }
326
327 /*
328 * -ctime n functions --
329 *
330 * True if the difference between the last change of file
331 * status information and the current time is n 24 hour periods.
332 */
333 int
334 f_ctime(PLAN *plan, FTSENT *entry)
335 {
336 COMPARE((now - entry->fts_statp->st_ctime +
337 SECSPERDAY - 1) / SECSPERDAY, plan->t_data);
338 }
339
340 PLAN *
341 c_ctime(char ***argvp, int isok)
342 {
343 char *arg = **argvp;
344 PLAN *new;
345
346 (*argvp)++;
347 ftsoptions &= ~FTS_NOSTAT;
348
349 new = palloc(N_CTIME, f_ctime);
350 new->t_data = find_parsenum(new, "-ctime", arg, NULL);
351 TIME_CORRECT(new, N_CTIME);
352 return (new);
353 }
354
355 /*
356 * -depth functions --
357 *
358 * Always true, causes descent of the directory hierarchy to be done
359 * so that all entries in a directory are acted on before the directory
360 * itself.
361 */
362 int
363 f_always_true(PLAN *plan, FTSENT *entry)
364 {
365
366 return (1);
367 }
368
369 PLAN *
370 c_depth(char ***argvp, int isok)
371 {
372 isdepth = 1;
373
374 return (palloc(N_DEPTH, f_always_true));
375 }
376
377 /*
378 * -empty functions --
379 *
380 * True if the file or directory is empty
381 */
382 int
383 f_empty(PLAN *plan, FTSENT *entry)
384 {
385 if (S_ISREG(entry->fts_statp->st_mode) &&
386 entry->fts_statp->st_size == 0)
387 return (1);
388 if (S_ISDIR(entry->fts_statp->st_mode)) {
389 struct dirent *dp;
390 int empty;
391 DIR *dir;
392
393 empty = 1;
394 dir = opendir(entry->fts_accpath);
395 if (dir == NULL)
396 err(1, "%s", entry->fts_accpath);
397 for (dp = readdir(dir); dp; dp = readdir(dir))
398 if (dp->d_name[0] != '.' ||
399 (dp->d_name[1] != '\0' &&
400 (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
401 empty = 0;
402 break;
403 }
404 closedir(dir);
405 return (empty);
406 }
407 return (0);
408 }
409
410 PLAN *
411 c_empty(char ***argvp, int isok)
412 {
413 ftsoptions &= ~FTS_NOSTAT;
414
415 return (palloc(N_EMPTY, f_empty));
416 }
417
418 /*
419 * [-exec | -ok] utility [arg ... ] ; functions --
420 * [-exec | -ok] utility [arg ... ] {} + functions --
421 *
422 * If the end of the primary expression is delimited by a
423 * semicolon: true if the executed utility returns a zero value
424 * as exit status. If "{}" occurs anywhere, it gets replaced by
425 * the current pathname.
426 *
427 * If the end of the primary expression is delimited by a plus
428 * sign: always true. Pathnames for which the primary is
429 * evaluated shall be aggregated into sets. The utility will be
430 * executed once per set, with "{}" replaced by the entire set of
431 * pathnames (as if xargs). "{}" must appear last.
432 *
433 * The current directory for the execution of utility is the same
434 * as the current directory when the find utility was started.
435 *
436 * The primary -ok is different in that it requests affirmation
437 * of the user before executing the utility.
438 */
439 int
440 f_exec(PLAN *plan, FTSENT *entry)
441 {
442 int cnt, l;
443 pid_t pid;
444 int status;
445
446 if (plan->flags & F_PLUSSET) {
447 /*
448 * Confirm sufficient buffer space, then copy the path
449 * to the buffer.
450 */
451 l = strlen(entry->fts_path);
452 if (plan->ep_p + l < plan->ep_ebp) {
453 plan->ep_bxp[plan->ep_narg++] =
454 strcpy(plan->ep_p, entry->fts_path);
455 plan->ep_p += l + 1;
456
457 if (plan->ep_narg == plan->ep_maxargs)
458 run_f_exec(plan);
459 } else {
460 /*
461 * Without sufficient space to copy in the next
462 * argument, run the command to empty out the
463 * buffer before re-attepting the copy.
464 */
465 run_f_exec(plan);
466 if ((plan->ep_p + l < plan->ep_ebp)) {
467 plan->ep_bxp[plan->ep_narg++]
468 = strcpy(plan->ep_p, entry->fts_path);
469 plan->ep_p += l + 1;
470 } else
471 errx(1, "insufficient space for argument");
472 }
473 return (1);
474 } else {
475 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
476 if (plan->e_len[cnt])
477 brace_subst(plan->e_orig[cnt],
478 &plan->e_argv[cnt],
479 entry->fts_path,
480 &plan->e_len[cnt]);
481 if (plan->flags & F_NEEDOK && !queryuser(plan->e_argv))
482 return (0);
483
484 /* Don't mix output of command with find output. */
485 fflush(stdout);
486 fflush(stderr);
487
488 switch (pid = vfork()) {
489 case -1:
490 err(1, "vfork");
491 /* NOTREACHED */
492 case 0:
493 if (fchdir(dotfd)) {
494 warn("chdir");
495 _exit(1);
496 }
497 execvp(plan->e_argv[0], plan->e_argv);
498 warn("%s", plan->e_argv[0]);
499 _exit(1);
500 }
501 pid = waitpid(pid, &status, 0);
502 return (pid != -1 && WIFEXITED(status)
503 && !WEXITSTATUS(status));
504 }
505 }
506
507 static void
508 run_f_exec(PLAN *plan)
509 {
510 pid_t pid;
511 int rval, status;
512
513 /* Ensure arg list is null terminated. */
514 plan->ep_bxp[plan->ep_narg] = NULL;
515
516 /* Don't mix output of command with find output. */
517 fflush(stdout);
518 fflush(stderr);
519
520 switch (pid = vfork()) {
521 case -1:
522 err(1, "vfork");
523 /* NOTREACHED */
524 case 0:
525 if (fchdir(dotfd)) {
526 warn("chdir");
527 _exit(1);
528 }
529 execvp(plan->e_argv[0], plan->e_argv);
530 warn("%s", plan->e_argv[0]);
531 _exit(1);
532 }
533
534 /* Clear out the argument list. */
535 plan->ep_narg = 0;
536 plan->ep_bxp[plan->ep_narg] = NULL;
537 /* As well as the argument buffer. */
538 plan->ep_p = plan->ep_bbp;
539 *plan->ep_p = '\0';
540
541 pid = waitpid(pid, &status, 0);
542 if (WIFEXITED(status))
543 rval = WEXITSTATUS(status);
544 else
545 rval = -1;
546
547 /*
548 * If we have a non-zero exit status, preserve it so find(1) can
549 * later exit with it.
550 */
551 if (rval)
552 plan->ep_rval = rval;
553 }
554
555 /*
556 * c_exec --
557 * build three parallel arrays, one with pointers to the strings passed
558 * on the command line, one with (possibly duplicated) pointers to the
559 * argv array, and one with integer values that are lengths of the
560 * strings, but also flags meaning that the string has to be massaged.
561 *
562 * If -exec ... {} +, use only the first array, but make it large
563 * enough to hold 5000 args (cf. src/usr.bin/xargs/xargs.c for a
564 * discussion), and then allocate ARG_MAX - 4K of space for args.
565 */
566 PLAN *
567 c_exec(char ***argvp, int isok)
568 {
569 PLAN *new; /* node returned */
570 int cnt, brace, lastbrace;
571 char **argv, **ap, *p;
572
573 isoutput = 1;
574
575 new = palloc(N_EXEC, f_exec);
576 if (isok)
577 new->flags |= F_NEEDOK;
578
579 /*
580 * Terminate if we encounter an arg exacty equal to ";", or an
581 * arg exacty equal to "+" following an arg exacty equal to
582 * "{}".
583 */
584 for (ap = argv = *argvp, brace = 0;; ++ap) {
585 if (!*ap)
586 errx(1, "%s: no terminating \";\" or \"+\"",
587 isok ? "-ok" : "-exec");
588 lastbrace = brace;
589 if (strcmp(*ap, "{}") == 0)
590 brace = 1;
591 if (strcmp(*ap, ";") == 0)
592 break;
593 if (strcmp(*ap, "+") == 0 && lastbrace) {
594 new->flags |= F_PLUSSET;
595 break;
596 }
597 }
598
599 /*
600 * POSIX says -ok ... {} + "need not be supported," and it does
601 * not make much sense anyway.
602 */
603 if (new->flags & F_NEEDOK && new->flags & F_PLUSSET)
604 errx(1, "-ok: terminating \"+\" not permitted.");
605
606 if (new->flags & F_PLUSSET) {
607 u_int c, bufsize;
608
609 cnt = ap - *argvp - 1; /* units are words */
610 new->ep_maxargs = 5000;
611 new->e_argv = (char **)emalloc((u_int)(cnt + new->ep_maxargs)
612 * sizeof(char **));
613
614 /* We start stuffing arguments after the user's last one. */
615 new->ep_bxp = &new->e_argv[cnt];
616 new->ep_narg = 0;
617
618 /*
619 * Count up the space of the user's arguments, and
620 * subtract that from what we allocate.
621 */
622 for (argv = *argvp, c = 0, cnt = 0;
623 argv < ap;
624 ++argv, ++cnt) {
625 c += strlen(*argv) + 1;
626 new->e_argv[cnt] = *argv;
627 }
628 bufsize = ARG_MAX - 4 * 1024 - c;
629
630
631 /*
632 * Allocate, and then initialize current, base, and
633 * end pointers.
634 */
635 new->ep_p = new->ep_bbp = malloc(bufsize + 1);
636 new->ep_ebp = new->ep_bbp + bufsize - 1;
637 new->ep_rval = 0;
638 } else { /* !F_PLUSSET */
639 cnt = ap - *argvp + 1;
640 new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
641 new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
642 new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
643
644 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
645 new->e_orig[cnt] = *argv;
646 for (p = *argv; *p; ++p)
647 if (p[0] == '{' && p[1] == '}') {
648 new->e_argv[cnt] =
649 emalloc((u_int)MAXPATHLEN);
650 new->e_len[cnt] = MAXPATHLEN;
651 break;
652 }
653 if (!*p) {
654 new->e_argv[cnt] = *argv;
655 new->e_len[cnt] = 0;
656 }
657 }
658 new->e_orig[cnt] = NULL;
659 }
660
661 new->e_argv[cnt] = NULL;
662 *argvp = argv + 1;
663 return (new);
664 }
665
666 /*
667 * -execdir utility [arg ... ] ; functions --
668 *
669 * True if the executed utility returns a zero value as exit status.
670 * The end of the primary expression is delimited by a semicolon. If
671 * "{}" occurs anywhere, it gets replaced by the unqualified pathname.
672 * The current directory for the execution of utility is the same as
673 * the directory where the file lives.
674 */
675 int
676 f_execdir(PLAN *plan, FTSENT *entry)
677 {
678 int cnt;
679 pid_t pid;
680 int status;
681 char *file;
682
683 /* XXX - if file/dir ends in '/' this will not work -- can it? */
684 if ((file = strrchr(entry->fts_path, '/')))
685 file++;
686 else
687 file = entry->fts_path;
688
689 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
690 if (plan->e_len[cnt])
691 brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
692 file, &plan->e_len[cnt]);
693
694 /* don't mix output of command with find output */
695 fflush(stdout);
696 fflush(stderr);
697
698 switch (pid = vfork()) {
699 case -1:
700 err(1, "fork");
701 /* NOTREACHED */
702 case 0:
703 execvp(plan->e_argv[0], plan->e_argv);
704 warn("%s", plan->e_argv[0]);
705 _exit(1);
706 }
707 pid = waitpid(pid, &status, 0);
708 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
709 }
710
711 /*
712 * c_execdir --
713 * build three parallel arrays, one with pointers to the strings passed
714 * on the command line, one with (possibly duplicated) pointers to the
715 * argv array, and one with integer values that are lengths of the
716 * strings, but also flags meaning that the string has to be massaged.
717 */
718 PLAN *
719 c_execdir(char ***argvp, int isok)
720 {
721 PLAN *new; /* node returned */
722 int cnt;
723 char **argv, **ap, *p;
724
725 ftsoptions &= ~FTS_NOSTAT;
726 isoutput = 1;
727
728 new = palloc(N_EXECDIR, f_execdir);
729
730 for (ap = argv = *argvp;; ++ap) {
731 if (!*ap)
732 errx(1,
733 "-execdir: no terminating \";\"");
734 if (**ap == ';')
735 break;
736 }
737
738 cnt = ap - *argvp + 1;
739 new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
740 new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
741 new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
742
743 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
744 new->e_orig[cnt] = *argv;
745 for (p = *argv; *p; ++p)
746 if (p[0] == '{' && p[1] == '}') {
747 new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
748 new->e_len[cnt] = MAXPATHLEN;
749 break;
750 }
751 if (!*p) {
752 new->e_argv[cnt] = *argv;
753 new->e_len[cnt] = 0;
754 }
755 }
756 new->e_argv[cnt] = new->e_orig[cnt] = NULL;
757
758 *argvp = argv + 1;
759 return (new);
760 }
761
762 PLAN *
763 c_exit(char ***argvp, int isok)
764 {
765 char *arg = **argvp;
766 PLAN *new;
767
768 /* not technically true, but otherwise '-print' is implied */
769 isoutput = 1;
770
771 new = palloc(N_EXIT, f_always_true);
772
773 if (arg) {
774 (*argvp)++;
775 new->exit_val = find_parsenum(new, "-exit", arg, NULL);
776 } else
777 new->exit_val = 0;
778
779 return (new);
780 }
781
782
783 /*
784 * -false function
785 */
786 int
787 f_false(PLAN *plan, FTSENT *entry)
788 {
789
790 return (0);
791 }
792
793 PLAN *
794 c_false(char ***argvp, int isok)
795 {
796 return (palloc(N_FALSE, f_false));
797 }
798
799
800 /*
801 * -flags [-]flags functions --
802 */
803 int
804 f_flags(PLAN *plan, FTSENT *entry)
805 {
806 u_int32_t flags;
807
808 flags = entry->fts_statp->st_flags;
809 if (plan->flags == F_ATLEAST)
810 return ((plan->f_data | flags) == flags);
811 else
812 return (flags == plan->f_data);
813 /* NOTREACHED */
814 }
815
816 PLAN *
817 c_flags(char ***argvp, int isok)
818 {
819 char *flags = **argvp;
820 PLAN *new;
821 u_long flagset;
822
823 (*argvp)++;
824 ftsoptions &= ~FTS_NOSTAT;
825
826 new = palloc(N_FLAGS, f_flags);
827
828 if (*flags == '-') {
829 new->flags = F_ATLEAST;
830 ++flags;
831 }
832
833 flagset = 0;
834 if ((strcmp(flags, "none") != 0) &&
835 (string_to_flags(&flags, &flagset, NULL) != 0))
836 errx(1, "-flags: %s: illegal flags string", flags);
837 new->f_data = flagset;
838 return (new);
839 }
840
841 /*
842 * -follow functions --
843 *
844 * Always true, causes symbolic links to be followed on a global
845 * basis.
846 */
847 PLAN *
848 c_follow(char ***argvp, int isok)
849 {
850 ftsoptions &= ~FTS_PHYSICAL;
851 ftsoptions |= FTS_LOGICAL;
852
853 return (palloc(N_FOLLOW, f_always_true));
854 }
855
856 /* -fprint functions --
857 *
858 * Causes the current pathame to be written to the defined output file.
859 */
860 int
861 f_fprint(PLAN *plan, FTSENT *entry)
862 {
863
864 if (-1 == fprintf(plan->fprint_file, "%s\n", entry->fts_path))
865 warn("fprintf");
866
867 return(1);
868
869 /* no descriptors are closed; they will be closed by
870 operating system when this find command exits. */
871 }
872
873 PLAN *
874 c_fprint(char ***argvp, int isok)
875 {
876 PLAN *new;
877
878 isoutput = 1; /* do not assume -print */
879
880 new = palloc(N_FPRINT, f_fprint);
881
882 if (NULL == (new->fprint_file = fopen(**argvp, "w")))
883 err(1, "-fprint: %s: cannot create file", **argvp);
884
885 (*argvp)++;
886 return (new);
887 }
888
889 /*
890 * -fstype functions --
891 *
892 * True if the file is of a certain type.
893 */
894 int
895 f_fstype(PLAN *plan, FTSENT *entry)
896 {
897 static dev_t curdev; /* need a guaranteed illegal dev value */
898 static int first = 1;
899 struct statvfs sb;
900 static short val;
901 static char fstype[MFSNAMELEN];
902 char *p, save[2];
903
904 memset(&save, 0, sizeof save); /* XXX gcc */
905
906 /* Only check when we cross mount point. */
907 if (first || curdev != entry->fts_statp->st_dev) {
908 curdev = entry->fts_statp->st_dev;
909
910 /*
911 * Statfs follows symlinks; find wants the link's file system,
912 * not where it points.
913 */
914 if (entry->fts_info == FTS_SL ||
915 entry->fts_info == FTS_SLNONE) {
916 if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
917 ++p;
918 else
919 p = entry->fts_accpath;
920 save[0] = p[0];
921 p[0] = '.';
922 save[1] = p[1];
923 p[1] = '\0';
924
925 } else
926 p = NULL;
927
928 if (statvfs(entry->fts_accpath, &sb))
929 err(1, "%s", entry->fts_accpath);
930
931 if (p) {
932 p[0] = save[0];
933 p[1] = save[1];
934 }
935
936 first = 0;
937
938 /*
939 * Further tests may need both of these values, so
940 * always copy both of them.
941 */
942 val = sb.f_flag;
943 strlcpy(fstype, sb.f_fstypename, sizeof(fstype));
944 }
945 switch (plan->flags) {
946 case F_MTFLAG:
947 return (val & plan->mt_data);
948 case F_MTTYPE:
949 return (strncmp(fstype, plan->c_data, MFSNAMELEN) == 0);
950 default:
951 abort();
952 }
953 }
954
955 PLAN *
956 c_fstype(char ***argvp, int isok)
957 {
958 char *arg = **argvp;
959 PLAN *new;
960
961 (*argvp)++;
962 ftsoptions &= ~FTS_NOSTAT;
963
964 new = palloc(N_FSTYPE, f_fstype);
965
966 switch (*arg) {
967 case 'l':
968 if (!strcmp(arg, "local")) {
969 new->flags = F_MTFLAG;
970 new->mt_data = MNT_LOCAL;
971 return (new);
972 }
973 break;
974 case 'r':
975 if (!strcmp(arg, "rdonly")) {
976 new->flags = F_MTFLAG;
977 new->mt_data = MNT_RDONLY;
978 return (new);
979 }
980 break;
981 }
982
983 new->flags = F_MTTYPE;
984 new->c_data = arg;
985 return (new);
986 }
987
988 /*
989 * -group gname functions --
990 *
991 * True if the file belongs to the group gname. If gname is numeric and
992 * an equivalent of the getgrnam() function does not return a valid group
993 * name, gname is taken as a group ID.
994 */
995 int
996 f_group(PLAN *plan, FTSENT *entry)
997 {
998
999 return (entry->fts_statp->st_gid == plan->g_data);
1000 }
1001
1002 PLAN *
1003 c_group(char ***argvp, int isok)
1004 {
1005 char *gname = **argvp;
1006 PLAN *new;
1007 struct group *g;
1008 gid_t gid;
1009
1010 (*argvp)++;
1011 ftsoptions &= ~FTS_NOSTAT;
1012
1013 g = getgrnam(gname);
1014 if (g == NULL) {
1015 gid = atoi(gname);
1016 if (gid == 0 && gname[0] != '0')
1017 errx(1, "-group: %s: no such group", gname);
1018 } else
1019 gid = g->gr_gid;
1020
1021 new = palloc(N_GROUP, f_group);
1022 new->g_data = gid;
1023 return (new);
1024 }
1025
1026 /*
1027 * -inum n functions --
1028 *
1029 * True if the file has inode # n.
1030 */
1031 int
1032 f_inum(PLAN *plan, FTSENT *entry)
1033 {
1034
1035 COMPARE(entry->fts_statp->st_ino, plan->i_data);
1036 }
1037
1038 PLAN *
1039 c_inum(char ***argvp, int isok)
1040 {
1041 char *arg = **argvp;
1042 PLAN *new;
1043
1044 (*argvp)++;
1045 ftsoptions &= ~FTS_NOSTAT;
1046
1047 new = palloc(N_INUM, f_inum);
1048 new->i_data = find_parsenum(new, "-inum", arg, NULL);
1049 return (new);
1050 }
1051
1052 /*
1053 * -links n functions --
1054 *
1055 * True if the file has n links.
1056 */
1057 int
1058 f_links(PLAN *plan, FTSENT *entry)
1059 {
1060
1061 COMPARE(entry->fts_statp->st_nlink, plan->l_data);
1062 }
1063
1064 PLAN *
1065 c_links(char ***argvp, int isok)
1066 {
1067 char *arg = **argvp;
1068 PLAN *new;
1069
1070 (*argvp)++;
1071 ftsoptions &= ~FTS_NOSTAT;
1072
1073 new = palloc(N_LINKS, f_links);
1074 new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL);
1075 return (new);
1076 }
1077
1078 /*
1079 * -ls functions --
1080 *
1081 * Always true - prints the current entry to stdout in "ls" format.
1082 */
1083 int
1084 f_ls(PLAN *plan, FTSENT *entry)
1085 {
1086
1087 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
1088 return (1);
1089 }
1090
1091 PLAN *
1092 c_ls(char ***argvp, int isok)
1093 {
1094
1095 ftsoptions &= ~FTS_NOSTAT;
1096 isoutput = 1;
1097
1098 return (palloc(N_LS, f_ls));
1099 }
1100
1101 /*
1102 * - maxdepth n functions --
1103 *
1104 * True if the current search depth is less than or equal to the
1105 * maximum depth specified
1106 */
1107 int
1108 f_maxdepth(PLAN *plan, FTSENT *entry)
1109 {
1110 extern FTS *tree;
1111
1112 if (entry->fts_level >= plan->max_data)
1113 fts_set(tree, entry, FTS_SKIP);
1114 return (entry->fts_level <= plan->max_data);
1115 }
1116
1117 PLAN *
1118 c_maxdepth(char ***argvp, int isok)
1119 {
1120 char *arg = **argvp;
1121 PLAN *new;
1122
1123 (*argvp)++;
1124 new = palloc(N_MAXDEPTH, f_maxdepth);
1125 new->max_data = atoi(arg);
1126 return (new);
1127 }
1128
1129 /*
1130 * - mindepth n functions --
1131 *
1132 * True if the current search depth is greater than or equal to the
1133 * minimum depth specified
1134 */
1135 int
1136 f_mindepth(PLAN *plan, FTSENT *entry)
1137 {
1138 return (entry->fts_level >= plan->min_data);
1139 }
1140
1141 PLAN *
1142 c_mindepth(char ***argvp, int isok)
1143 {
1144 char *arg = **argvp;
1145 PLAN *new;
1146
1147 (*argvp)++;
1148 new = palloc(N_MINDEPTH, f_mindepth);
1149 new->min_data = atoi(arg);
1150 return (new);
1151 }
1152 /*
1153 * -mmin n functions --
1154 *
1155 * True if the difference between the file modification time and the
1156 * current time is n 24 hour periods.
1157 */
1158 int
1159 f_mmin(PLAN *plan, FTSENT *entry)
1160 {
1161 COMPARE((now - entry->fts_statp->st_mtime + SECSPERMIN - 1) /
1162 SECSPERMIN, plan->t_data);
1163 }
1164
1165 PLAN *
1166 c_mmin(char ***argvp, int isok)
1167 {
1168 char *arg = **argvp;
1169 PLAN *new;
1170
1171 (*argvp)++;
1172 ftsoptions &= ~FTS_NOSTAT;
1173
1174 new = palloc(N_MMIN, f_mmin);
1175 new->t_data = find_parsenum(new, "-mmin", arg, NULL);
1176 TIME_CORRECT(new, N_MMIN);
1177 return (new);
1178 }
1179 /*
1180 * -mtime n functions --
1181 *
1182 * True if the difference between the file modification time and the
1183 * current time is n 24 hour periods.
1184 */
1185 int
1186 f_mtime(PLAN *plan, FTSENT *entry)
1187 {
1188 COMPARE((now - entry->fts_statp->st_mtime + SECSPERDAY - 1) /
1189 SECSPERDAY, plan->t_data);
1190 }
1191
1192 PLAN *
1193 c_mtime(char ***argvp, int isok)
1194 {
1195 char *arg = **argvp;
1196 PLAN *new;
1197
1198 (*argvp)++;
1199 ftsoptions &= ~FTS_NOSTAT;
1200
1201 new = palloc(N_MTIME, f_mtime);
1202 new->t_data = find_parsenum(new, "-mtime", arg, NULL);
1203 TIME_CORRECT(new, N_MTIME);
1204 return (new);
1205 }
1206
1207 /*
1208 * -name functions --
1209 *
1210 * True if the basename of the filename being examined
1211 * matches pattern using Pattern Matching Notation S3.14
1212 */
1213 int
1214 f_name(PLAN *plan, FTSENT *entry)
1215 {
1216
1217 return (!fnmatch(plan->c_data, entry->fts_name, 0));
1218 }
1219
1220 PLAN *
1221 c_name(char ***argvp, int isok)
1222 {
1223 char *pattern = **argvp;
1224 PLAN *new;
1225
1226 (*argvp)++;
1227 new = palloc(N_NAME, f_name);
1228 new->c_data = pattern;
1229 return (new);
1230 }
1231
1232 /*
1233 * -iname functions --
1234 *
1235 * Similar to -name, but does case insensitive matching
1236 *
1237 */
1238 int
1239 f_iname(PLAN *plan, FTSENT *entry)
1240 {
1241 return (!fnmatch(plan->c_data, entry->fts_name, FNM_CASEFOLD));
1242 }
1243
1244 PLAN *
1245 c_iname(char ***argvp, int isok)
1246 {
1247 char *pattern = **argvp;
1248 PLAN *new;
1249
1250 (*argvp)++;
1251 new = palloc(N_INAME, f_iname);
1252 new->c_data = pattern;
1253 return (new);
1254 }
1255
1256 /*
1257 * -newer file functions --
1258 *
1259 * True if the current file has been modified more recently
1260 * than the modification time of the file named by the pathname
1261 * file.
1262 */
1263 int
1264 f_newer(PLAN *plan, FTSENT *entry)
1265 {
1266
1267 return (entry->fts_statp->st_mtime > plan->t_data);
1268 }
1269
1270 PLAN *
1271 c_newer(char ***argvp, int isok)
1272 {
1273 char *filename = **argvp;
1274 PLAN *new;
1275 struct stat sb;
1276
1277 (*argvp)++;
1278 ftsoptions &= ~FTS_NOSTAT;
1279
1280 if (stat(filename, &sb))
1281 err(1, "%s", filename);
1282 new = palloc(N_NEWER, f_newer);
1283 new->t_data = sb.st_mtime;
1284 return (new);
1285 }
1286
1287 /*
1288 * -nogroup functions --
1289 *
1290 * True if file belongs to a user ID for which the equivalent
1291 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1292 */
1293 int
1294 f_nogroup(PLAN *plan, FTSENT *entry)
1295 {
1296
1297 return (group_from_gid(entry->fts_statp->st_gid, 1) ? 0 : 1);
1298 }
1299
1300 PLAN *
1301 c_nogroup(char ***argvp, int isok)
1302 {
1303 ftsoptions &= ~FTS_NOSTAT;
1304
1305 return (palloc(N_NOGROUP, f_nogroup));
1306 }
1307
1308 /*
1309 * -nouser functions --
1310 *
1311 * True if file belongs to a user ID for which the equivalent
1312 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1313 */
1314 int
1315 f_nouser(PLAN *plan, FTSENT *entry)
1316 {
1317
1318 return (user_from_uid(entry->fts_statp->st_uid, 1) ? 0 : 1);
1319 }
1320
1321 PLAN *
1322 c_nouser(char ***argvp, int isok)
1323 {
1324 ftsoptions &= ~FTS_NOSTAT;
1325
1326 return (palloc(N_NOUSER, f_nouser));
1327 }
1328
1329 /*
1330 * -path functions --
1331 *
1332 * True if the path of the filename being examined
1333 * matches pattern using Pattern Matching Notation S3.14
1334 */
1335 int
1336 f_path(PLAN *plan, FTSENT *entry)
1337 {
1338
1339 return (!fnmatch(plan->c_data, entry->fts_path, 0));
1340 }
1341
1342 PLAN *
1343 c_path(char ***argvp, int isok)
1344 {
1345 char *pattern = **argvp;
1346 PLAN *new;
1347
1348 (*argvp)++;
1349 new = palloc(N_NAME, f_path);
1350 new->c_data = pattern;
1351 return (new);
1352 }
1353
1354 /*
1355 * -perm functions --
1356 *
1357 * The mode argument is used to represent file mode bits. If it starts
1358 * with a leading digit, it's treated as an octal mode, otherwise as a
1359 * symbolic mode.
1360 */
1361 int
1362 f_perm(PLAN *plan, FTSENT *entry)
1363 {
1364 mode_t mode;
1365
1366 mode = entry->fts_statp->st_mode &
1367 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1368 if (plan->flags == F_ATLEAST)
1369 return ((plan->m_data | mode) == mode);
1370 else
1371 return (mode == plan->m_data);
1372 /* NOTREACHED */
1373 }
1374
1375 PLAN *
1376 c_perm(char ***argvp, int isok)
1377 {
1378 char *perm = **argvp;
1379 PLAN *new;
1380 mode_t *set;
1381
1382 (*argvp)++;
1383 ftsoptions &= ~FTS_NOSTAT;
1384
1385 new = palloc(N_PERM, f_perm);
1386
1387 if (*perm == '-') {
1388 new->flags = F_ATLEAST;
1389 ++perm;
1390 }
1391
1392 if ((set = setmode(perm)) == NULL)
1393 err(1, "-perm: Cannot set file mode `%s'", perm);
1394
1395 new->m_data = getmode(set, 0);
1396 free(set);
1397 return (new);
1398 }
1399
1400 /*
1401 * -print functions --
1402 *
1403 * Always true, causes the current pathame to be written to
1404 * standard output.
1405 */
1406 int
1407 f_print(PLAN *plan, FTSENT *entry)
1408 {
1409
1410 (void)printf("%s\n", entry->fts_path);
1411 return (1);
1412 }
1413
1414 int
1415 f_print0(PLAN *plan, FTSENT *entry)
1416 {
1417
1418 (void)fputs(entry->fts_path, stdout);
1419 (void)fputc('\0', stdout);
1420 return (1);
1421 }
1422
1423 int
1424 f_printx(PLAN *plan, FTSENT *entry)
1425 {
1426 char *cp;
1427
1428 for (cp = entry->fts_path; *cp; cp++) {
1429 if (*cp == '\'' || *cp == '\"' || *cp == ' ' ||
1430 *cp == '$' || *cp == '`' ||
1431 *cp == '\t' || *cp == '\n' || *cp == '\\')
1432 fputc('\\', stdout);
1433
1434 fputc(*cp, stdout);
1435 }
1436
1437 fputc('\n', stdout);
1438 return (1);
1439 }
1440
1441 PLAN *
1442 c_print(char ***argvp, int isok)
1443 {
1444
1445 isoutput = 1;
1446
1447 return (palloc(N_PRINT, f_print));
1448 }
1449
1450 PLAN *
1451 c_print0(char ***argvp, int isok)
1452 {
1453
1454 isoutput = 1;
1455
1456 return (palloc(N_PRINT0, f_print0));
1457 }
1458
1459 PLAN *
1460 c_printx(char ***argvp, int isok)
1461 {
1462
1463 isoutput = 1;
1464
1465 return (palloc(N_PRINTX, f_printx));
1466 }
1467
1468 /*
1469 * -prune functions --
1470 *
1471 * Prune a portion of the hierarchy.
1472 */
1473 int
1474 f_prune(PLAN *plan, FTSENT *entry)
1475 {
1476 if (fts_set(tree, entry, FTS_SKIP))
1477 err(1, "%s", entry->fts_path);
1478 return (1);
1479 }
1480
1481 PLAN *
1482 c_prune(char ***argvp, int isok)
1483 {
1484
1485 return (palloc(N_PRUNE, f_prune));
1486 }
1487
1488 /*
1489 * -regex regexp (and related) functions --
1490 *
1491 * True if the complete file path matches the regular expression regexp.
1492 * For -regex, regexp is a case-sensitive (basic) regular expression.
1493 * For -iregex, regexp is a case-insensitive (basic) regular expression.
1494 */
1495 int
1496 f_regex(PLAN *plan, FTSENT *entry)
1497 {
1498
1499 return (regexec(&plan->regexp_data, entry->fts_path, 0, NULL, 0) == 0);
1500 }
1501
1502 static PLAN *
1503 c_regex_common(char ***argvp, int isok, enum ntype type, int regcomp_flags)
1504 {
1505 char errbuf[LINE_MAX];
1506 regex_t reg;
1507 char *regexp = **argvp;
1508 char *lineregexp;
1509 PLAN *new;
1510 int rv;
1511 size_t len;
1512
1513 (*argvp)++;
1514
1515 len = strlen(regexp) + 1 + 6;
1516 lineregexp = malloc(len); /* max needed */
1517 if (lineregexp == NULL)
1518 err(1, NULL);
1519 snprintf(lineregexp, len, "^%s(%s%s)$",
1520 (regcomp_flags & REG_EXTENDED) ? "" : "\\", regexp,
1521 (regcomp_flags & REG_EXTENDED) ? "" : "\\");
1522 rv = regcomp(®, lineregexp, REG_NOSUB|regcomp_flags);
1523 free(lineregexp);
1524 if (rv != 0) {
1525 regerror(rv, ®, errbuf, sizeof errbuf);
1526 errx(1, "regexp %s: %s", regexp, errbuf);
1527 }
1528
1529 new = palloc(type, f_regex);
1530 new->regexp_data = reg;
1531 return (new);
1532 }
1533
1534 PLAN *
1535 c_regex(char ***argvp, int isok)
1536 {
1537
1538 return (c_regex_common(argvp, isok, N_REGEX, REG_BASIC));
1539 }
1540
1541 PLAN *
1542 c_iregex(char ***argvp, int isok)
1543 {
1544
1545 return (c_regex_common(argvp, isok, N_IREGEX, REG_BASIC|REG_ICASE));
1546 }
1547
1548 /*
1549 * -size n[c] functions --
1550 *
1551 * True if the file size in bytes, divided by an implementation defined
1552 * value and rounded up to the next integer, is n. If n is followed by
1553 * a c, the size is in bytes.
1554 */
1555 #define FIND_SIZE 512
1556 static int divsize = 1;
1557
1558 int
1559 f_size(PLAN *plan, FTSENT *entry)
1560 {
1561 off_t size;
1562
1563 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1564 FIND_SIZE : entry->fts_statp->st_size;
1565 COMPARE(size, plan->o_data);
1566 }
1567
1568 PLAN *
1569 c_size(char ***argvp, int isok)
1570 {
1571 char *arg = **argvp;
1572 PLAN *new;
1573 char endch;
1574
1575 (*argvp)++;
1576 ftsoptions &= ~FTS_NOSTAT;
1577
1578 new = palloc(N_SIZE, f_size);
1579 endch = 'c';
1580 new->o_data = find_parsenum(new, "-size", arg, &endch);
1581 if (endch == 'c')
1582 divsize = 0;
1583 return (new);
1584 }
1585
1586 /*
1587 * -type c functions --
1588 *
1589 * True if the type of the file is c, where c is b, c, d, p, f or w
1590 * for block special file, character special file, directory, FIFO,
1591 * regular file or whiteout respectively.
1592 */
1593 int
1594 f_type(PLAN *plan, FTSENT *entry)
1595 {
1596
1597 return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data);
1598 }
1599
1600 PLAN *
1601 c_type(char ***argvp, int isok)
1602 {
1603 char *typestring = **argvp;
1604 PLAN *new;
1605 mode_t mask = (mode_t)0;
1606
1607 (*argvp)++;
1608 ftsoptions &= ~FTS_NOSTAT;
1609
1610 switch (typestring[0]) {
1611 case 'b':
1612 mask = S_IFBLK;
1613 break;
1614 case 'c':
1615 mask = S_IFCHR;
1616 break;
1617 case 'd':
1618 mask = S_IFDIR;
1619 break;
1620 case 'f':
1621 mask = S_IFREG;
1622 break;
1623 case 'l':
1624 mask = S_IFLNK;
1625 break;
1626 case 'p':
1627 mask = S_IFIFO;
1628 break;
1629 case 's':
1630 mask = S_IFSOCK;
1631 break;
1632 #ifdef S_IFWHT
1633 case 'W':
1634 case 'w':
1635 mask = S_IFWHT;
1636 #ifdef FTS_WHITEOUT
1637 ftsoptions |= FTS_WHITEOUT;
1638 #endif
1639 break;
1640 #endif /* S_IFWHT */
1641 default:
1642 errx(1, "-type: %s: unknown type", typestring);
1643 }
1644
1645 new = palloc(N_TYPE, f_type);
1646 new->m_data = mask;
1647 return (new);
1648 }
1649
1650 /*
1651 * -user uname functions --
1652 *
1653 * True if the file belongs to the user uname. If uname is numeric and
1654 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1655 * return a valid user name, uname is taken as a user ID.
1656 */
1657 int
1658 f_user(PLAN *plan, FTSENT *entry)
1659 {
1660
1661 COMPARE(entry->fts_statp->st_uid, plan->u_data);
1662 }
1663
1664 PLAN *
1665 c_user(char ***argvp, int isok)
1666 {
1667 char *username = **argvp;
1668 PLAN *new;
1669 struct passwd *p;
1670 uid_t uid;
1671
1672 (*argvp)++;
1673 ftsoptions &= ~FTS_NOSTAT;
1674
1675 new = palloc(N_USER, f_user);
1676 p = getpwnam(username);
1677 if (p == NULL) {
1678 if (atoi(username) == 0 && username[0] != '0' &&
1679 strcmp(username, "+0") && strcmp(username, "-0"))
1680 errx(1, "-user: %s: no such user", username);
1681 uid = find_parsenum(new, "-user", username, NULL);
1682
1683 } else {
1684 new->flags = F_EQUAL;
1685 uid = p->pw_uid;
1686 }
1687
1688 new->u_data = uid;
1689 return (new);
1690 }
1691
1692 /*
1693 * -xdev functions --
1694 *
1695 * Always true, causes find not to descend past directories that have a
1696 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1697 */
1698 PLAN *
1699 c_xdev(char ***argvp, int isok)
1700 {
1701 ftsoptions |= FTS_XDEV;
1702
1703 return (palloc(N_XDEV, f_always_true));
1704 }
1705
1706 /*
1707 * ( expression ) functions --
1708 *
1709 * True if expression is true.
1710 */
1711 int
1712 f_expr(PLAN *plan, FTSENT *entry)
1713 {
1714 PLAN *p;
1715 int state;
1716
1717 state = 0;
1718 for (p = plan->p_data[0];
1719 p && (state = (p->eval)(p, entry)); p = p->next);
1720 return (state);
1721 }
1722
1723 /*
1724 * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers. They are
1725 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1726 * to a N_EXPR node containing the expression and the ')' node is discarded.
1727 */
1728 PLAN *
1729 c_openparen(char ***argvp, int isok)
1730 {
1731
1732 return (palloc(N_OPENPAREN, (int (*)(PLAN *, FTSENT *))-1));
1733 }
1734
1735 PLAN *
1736 c_closeparen(char ***argvp, int isok)
1737 {
1738
1739 return (palloc(N_CLOSEPAREN, (int (*)(PLAN *, FTSENT *))-1));
1740 }
1741
1742 /*
1743 * ! expression functions --
1744 *
1745 * Negation of a primary; the unary NOT operator.
1746 */
1747 int
1748 f_not(PLAN *plan, FTSENT *entry)
1749 {
1750 PLAN *p;
1751 int state;
1752
1753 state = 0;
1754 for (p = plan->p_data[0];
1755 p && (state = (p->eval)(p, entry)); p = p->next);
1756 return (!state);
1757 }
1758
1759 PLAN *
1760 c_not(char ***argvp, int isok)
1761 {
1762
1763 return (palloc(N_NOT, f_not));
1764 }
1765
1766 /*
1767 * expression -o expression functions --
1768 *
1769 * Alternation of primaries; the OR operator. The second expression is
1770 * not evaluated if the first expression is true.
1771 */
1772 int
1773 f_or(PLAN *plan, FTSENT *entry)
1774 {
1775 PLAN *p;
1776 int state;
1777
1778 state = 0;
1779 for (p = plan->p_data[0];
1780 p && (state = (p->eval)(p, entry)); p = p->next);
1781
1782 if (state)
1783 return (1);
1784
1785 for (p = plan->p_data[1];
1786 p && (state = (p->eval)(p, entry)); p = p->next);
1787 return (state);
1788 }
1789
1790 PLAN *
1791 c_or(char ***argvp, int isok)
1792 {
1793
1794 return (palloc(N_OR, f_or));
1795 }
1796
1797 PLAN *
1798 c_null(char ***argvp, int isok)
1799 {
1800
1801 return (NULL);
1802 }
1803
1804
1805 /*
1806 * plan_cleanup --
1807 * Check and see if the specified plan has any residual state,
1808 * and if so, clean it up as appropriate.
1809 *
1810 * At the moment, only N_EXEC has state. Two kinds: 1)
1811 * lists of files to feed to subprocesses 2) State on exit
1812 * statusses of past subprocesses.
1813 */
1814 /* ARGSUSED1 */
1815 int
1816 plan_cleanup(PLAN *plan, void *arg)
1817 {
1818 if (plan->type==N_EXEC && plan->ep_narg)
1819 run_f_exec(plan);
1820
1821 return plan->ep_rval; /* Passed save exit-status up chain */
1822 }
1823
1824 static PLAN *
1825 palloc(enum ntype t, int (*f)(PLAN *, FTSENT *))
1826 {
1827 PLAN *new;
1828
1829 if ((new = malloc(sizeof(PLAN))) == NULL)
1830 err(1, NULL);
1831 memset(new, 0, sizeof(PLAN));
1832 new->type = t;
1833 new->eval = f;
1834 return (new);
1835 }
1836