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