function.c revision 1.67.2.4 1 /* $NetBSD: function.c,v 1.67.2.4 2014/05/22 11:42:44 yamt 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.67.2.4 2014/05/22 11:42:44 yamt 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 = 5000;
673 new->e_argv = emalloc((cnt + new->ep_maxargs)
674 * sizeof(*new->e_argv));
675
676 /* We start stuffing arguments after the user's last one. */
677 new->ep_bxp = &new->e_argv[cnt];
678 new->ep_narg = 0;
679
680 /*
681 * Count up the space of the user's arguments, and
682 * subtract that from what we allocate.
683 */
684 #define MAXARG (ARG_MAX - 4 * 1024)
685 for (argv = *argvp, c = 0, cnt = 0;
686 argv < ap;
687 ++argv, ++cnt) {
688 c += strlen(*argv) + 1;
689 if (c >= MAXARG)
690 errx(1, "Arguments too long");
691 new->e_argv[cnt] = *argv;
692 }
693 bufsize = MAXARG - c;
694
695 /*
696 * Allocate, and then initialize current, base, and
697 * end pointers.
698 */
699 new->ep_p = new->ep_bbp = emalloc(bufsize + 1);
700 new->ep_ebp = new->ep_bbp + bufsize - 1;
701 new->ep_rval = 0;
702 } else { /* !F_PLUSSET */
703 cnt = ap - *argvp + 1;
704 new->e_argv = emalloc(cnt * sizeof(*new->e_argv));
705 new->e_orig = emalloc(cnt * sizeof(*new->e_orig));
706 new->e_len = emalloc(cnt * sizeof(*new->e_len));
707
708 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
709 new->e_orig[cnt] = *argv;
710 for (p = *argv; *p; ++p)
711 if (p[0] == '{' && p[1] == '}') {
712 new->e_argv[cnt] =
713 emalloc(MAXPATHLEN);
714 new->e_len[cnt] = MAXPATHLEN;
715 break;
716 }
717 if (!*p) {
718 new->e_argv[cnt] = *argv;
719 new->e_len[cnt] = 0;
720 }
721 }
722 new->e_orig[cnt] = NULL;
723 }
724
725 new->e_argv[cnt] = NULL;
726 *argvp = argv + 1;
727 return (new);
728 }
729
730 /*
731 * -execdir utility [arg ... ] ; functions --
732 *
733 * True if the executed utility returns a zero value as exit status.
734 * The end of the primary expression is delimited by a semicolon. If
735 * "{}" occurs anywhere, it gets replaced by the unqualified pathname.
736 * The current directory for the execution of utility is the same as
737 * the directory where the file lives.
738 */
739 int
740 f_execdir(PLAN *plan, FTSENT *entry)
741 {
742 size_t cnt;
743 pid_t pid;
744 int status;
745 char *file;
746
747 /* XXX - if file/dir ends in '/' this will not work -- can it? */
748 if ((file = strrchr(entry->fts_path, '/')))
749 file++;
750 else
751 file = entry->fts_path;
752
753 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
754 if (plan->e_len[cnt])
755 brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
756 file, &plan->e_len[cnt]);
757
758 /* don't mix output of command with find output */
759 fflush(stdout);
760 fflush(stderr);
761
762 switch (pid = vfork()) {
763 case -1:
764 err(1, "fork");
765 /* NOTREACHED */
766 case 0:
767 execvp(plan->e_argv[0], plan->e_argv);
768 warn("%s", plan->e_argv[0]);
769 _exit(1);
770 }
771 pid = waitpid(pid, &status, 0);
772 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
773 }
774
775 /*
776 * c_execdir --
777 * build three parallel arrays, one with pointers to the strings passed
778 * on the command line, one with (possibly duplicated) pointers to the
779 * argv array, and one with integer values that are lengths of the
780 * strings, but also flags meaning that the string has to be massaged.
781 */
782 PLAN *
783 c_execdir(char ***argvp, int isok)
784 {
785 PLAN *new; /* node returned */
786 size_t cnt;
787 char **argv, **ap, *p;
788
789 ftsoptions &= ~FTS_NOSTAT;
790 isoutput = 1;
791
792 new = palloc(N_EXECDIR, f_execdir);
793
794 for (ap = argv = *argvp;; ++ap) {
795 if (!*ap)
796 errx(1,
797 "-execdir: no terminating \";\"");
798 if (**ap == ';')
799 break;
800 }
801
802 cnt = ap - *argvp + 1;
803 new->e_argv = emalloc(cnt * sizeof(*new->e_argv));
804 new->e_orig = emalloc(cnt * sizeof(*new->e_orig));
805 new->e_len = emalloc(cnt * sizeof(*new->e_len));
806
807 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
808 new->e_orig[cnt] = *argv;
809 for (p = *argv; *p; ++p)
810 if (p[0] == '{' && p[1] == '}') {
811 new->e_argv[cnt] = emalloc(MAXPATHLEN);
812 new->e_len[cnt] = MAXPATHLEN;
813 break;
814 }
815 if (!*p) {
816 new->e_argv[cnt] = *argv;
817 new->e_len[cnt] = 0;
818 }
819 }
820 new->e_argv[cnt] = new->e_orig[cnt] = NULL;
821
822 *argvp = argv + 1;
823 return (new);
824 }
825
826 PLAN *
827 c_exit(char ***argvp, int isok)
828 {
829 char *arg = **argvp;
830 PLAN *new;
831
832 /* not technically true, but otherwise '-print' is implied */
833 isoutput = 1;
834
835 new = palloc(N_EXIT, f_always_true);
836
837 if (arg) {
838 (*argvp)++;
839 new->exit_val = find_parsenum(new, "-exit", arg, NULL);
840 } else
841 new->exit_val = 0;
842
843 return (new);
844 }
845
846
847 /*
848 * -false function
849 */
850 int
851 f_false(PLAN *plan, FTSENT *entry)
852 {
853
854 return (0);
855 }
856
857 PLAN *
858 c_false(char ***argvp, int isok)
859 {
860 return (palloc(N_FALSE, f_false));
861 }
862
863
864 /*
865 * -flags [-]flags functions --
866 */
867 int
868 f_flags(PLAN *plan, FTSENT *entry)
869 {
870 u_int32_t flags;
871
872 flags = entry->fts_statp->st_flags;
873 if (plan->flags == F_ATLEAST)
874 return ((plan->f_data | flags) == flags);
875 else
876 return (flags == plan->f_data);
877 /* NOTREACHED */
878 }
879
880 PLAN *
881 c_flags(char ***argvp, int isok)
882 {
883 char *flags = **argvp;
884 PLAN *new;
885 u_long flagset;
886
887 (*argvp)++;
888 ftsoptions &= ~FTS_NOSTAT;
889
890 new = palloc(N_FLAGS, f_flags);
891
892 if (*flags == '-') {
893 new->flags = F_ATLEAST;
894 ++flags;
895 }
896
897 flagset = 0;
898 if ((strcmp(flags, "none") != 0) &&
899 (string_to_flags(&flags, &flagset, NULL) != 0))
900 errx(1, "-flags: %s: illegal flags string", flags);
901 new->f_data = flagset;
902 return (new);
903 }
904
905 /*
906 * -follow functions --
907 *
908 * Always true, causes symbolic links to be followed on a global
909 * basis.
910 */
911 PLAN *
912 c_follow(char ***argvp, int isok)
913 {
914 ftsoptions &= ~FTS_PHYSICAL;
915 ftsoptions |= FTS_LOGICAL;
916
917 return (palloc(N_FOLLOW, f_always_true));
918 }
919
920 /* -fprint functions --
921 *
922 * Causes the current pathame to be written to the defined output file.
923 */
924 int
925 f_fprint(PLAN *plan, FTSENT *entry)
926 {
927
928 if (-1 == fprintf(plan->fprint_file, "%s\n", entry->fts_path))
929 warn("fprintf");
930
931 return(1);
932
933 /* no descriptors are closed; they will be closed by
934 operating system when this find command exits. */
935 }
936
937 PLAN *
938 c_fprint(char ***argvp, int isok)
939 {
940 PLAN *new;
941
942 isoutput = 1; /* do not assume -print */
943
944 new = palloc(N_FPRINT, f_fprint);
945
946 if (NULL == (new->fprint_file = fopen(**argvp, "w")))
947 err(1, "-fprint: %s: cannot create file", **argvp);
948
949 (*argvp)++;
950 return (new);
951 }
952
953 /*
954 * -fstype functions --
955 *
956 * True if the file is of a certain type.
957 */
958 int
959 f_fstype(PLAN *plan, FTSENT *entry)
960 {
961 static dev_t curdev; /* need a guaranteed illegal dev value */
962 static int first = 1;
963 struct statvfs sb;
964 static short val;
965 static char fstype[sizeof(sb.f_fstypename)];
966 char *p, save[2];
967
968 memset(&save, 0, sizeof save); /* XXX gcc */
969
970 /* Only check when we cross mount point. */
971 if (first || curdev != entry->fts_statp->st_dev) {
972 curdev = entry->fts_statp->st_dev;
973
974 /*
975 * Statfs follows symlinks; find wants the link's file system,
976 * not where it points.
977 */
978 if (entry->fts_info == FTS_SL ||
979 entry->fts_info == FTS_SLNONE) {
980 if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
981 ++p;
982 else
983 p = entry->fts_accpath;
984 save[0] = p[0];
985 p[0] = '.';
986 save[1] = p[1];
987 p[1] = '\0';
988
989 } else
990 p = NULL;
991
992 if (statvfs(entry->fts_accpath, &sb))
993 err(1, "%s", entry->fts_accpath);
994
995 if (p) {
996 p[0] = save[0];
997 p[1] = save[1];
998 }
999
1000 first = 0;
1001
1002 /*
1003 * Further tests may need both of these values, so
1004 * always copy both of them.
1005 */
1006 val = sb.f_flag;
1007 strlcpy(fstype, sb.f_fstypename, sizeof(fstype));
1008 }
1009 switch (plan->flags) {
1010 case F_MTFLAG:
1011 return (val & plan->mt_data);
1012 case F_MTTYPE:
1013 return (strncmp(fstype, plan->c_data, sizeof(fstype)) == 0);
1014 default:
1015 abort();
1016 }
1017 }
1018
1019 PLAN *
1020 c_fstype(char ***argvp, int isok)
1021 {
1022 char *arg = **argvp;
1023 PLAN *new;
1024
1025 (*argvp)++;
1026 ftsoptions &= ~FTS_NOSTAT;
1027
1028 new = palloc(N_FSTYPE, f_fstype);
1029
1030 switch (*arg) {
1031 case 'l':
1032 if (!strcmp(arg, "local")) {
1033 new->flags = F_MTFLAG;
1034 new->mt_data = MNT_LOCAL;
1035 return (new);
1036 }
1037 break;
1038 case 'r':
1039 if (!strcmp(arg, "rdonly")) {
1040 new->flags = F_MTFLAG;
1041 new->mt_data = MNT_RDONLY;
1042 return (new);
1043 }
1044 break;
1045 }
1046
1047 new->flags = F_MTTYPE;
1048 new->c_data = arg;
1049 return (new);
1050 }
1051
1052 /*
1053 * -group gname functions --
1054 *
1055 * True if the file belongs to the group gname. If gname is numeric and
1056 * an equivalent of the getgrnam() function does not return a valid group
1057 * name, gname is taken as a group ID.
1058 */
1059 int
1060 f_group(PLAN *plan, FTSENT *entry)
1061 {
1062
1063 return (entry->fts_statp->st_gid == plan->g_data);
1064 }
1065
1066 PLAN *
1067 c_group(char ***argvp, int isok)
1068 {
1069 char *gname = **argvp;
1070 PLAN *new;
1071 struct group *g;
1072 gid_t gid;
1073
1074 (*argvp)++;
1075 ftsoptions &= ~FTS_NOSTAT;
1076
1077 g = getgrnam(gname);
1078 if (g == NULL) {
1079 gid = atoi(gname);
1080 if (gid == 0 && gname[0] != '0')
1081 errx(1, "-group: %s: no such group", gname);
1082 } else
1083 gid = g->gr_gid;
1084
1085 new = palloc(N_GROUP, f_group);
1086 new->g_data = gid;
1087 return (new);
1088 }
1089
1090 /*
1091 * -inum n functions --
1092 *
1093 * True if the file has inode # n.
1094 */
1095 int
1096 f_inum(PLAN *plan, FTSENT *entry)
1097 {
1098
1099 COMPARE(entry->fts_statp->st_ino, plan->i_data);
1100 }
1101
1102 PLAN *
1103 c_inum(char ***argvp, int isok)
1104 {
1105 char *arg = **argvp;
1106 PLAN *new;
1107
1108 (*argvp)++;
1109 ftsoptions &= ~FTS_NOSTAT;
1110
1111 new = palloc(N_INUM, f_inum);
1112 new->i_data = find_parsenum(new, "-inum", arg, NULL);
1113 return (new);
1114 }
1115
1116 /*
1117 * -links n functions --
1118 *
1119 * True if the file has n links.
1120 */
1121 int
1122 f_links(PLAN *plan, FTSENT *entry)
1123 {
1124
1125 COMPARE(entry->fts_statp->st_nlink, plan->l_data);
1126 }
1127
1128 PLAN *
1129 c_links(char ***argvp, int isok)
1130 {
1131 char *arg = **argvp;
1132 PLAN *new;
1133
1134 (*argvp)++;
1135 ftsoptions &= ~FTS_NOSTAT;
1136
1137 new = palloc(N_LINKS, f_links);
1138 new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL);
1139 return (new);
1140 }
1141
1142 /*
1143 * -ls functions --
1144 *
1145 * Always true - prints the current entry to stdout in "ls" format.
1146 */
1147 int
1148 f_ls(PLAN *plan, FTSENT *entry)
1149 {
1150
1151 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
1152 return (1);
1153 }
1154
1155 PLAN *
1156 c_ls(char ***argvp, int isok)
1157 {
1158
1159 ftsoptions &= ~FTS_NOSTAT;
1160 isoutput = 1;
1161
1162 return (palloc(N_LS, f_ls));
1163 }
1164
1165 /*
1166 * - maxdepth n functions --
1167 *
1168 * True if the current search depth is less than or equal to the
1169 * maximum depth specified
1170 */
1171 int
1172 f_maxdepth(PLAN *plan, FTSENT *entry)
1173 {
1174 extern FTS *tree;
1175
1176 if (entry->fts_level >= plan->max_data)
1177 fts_set(tree, entry, FTS_SKIP);
1178 return (entry->fts_level <= plan->max_data);
1179 }
1180
1181 PLAN *
1182 c_maxdepth(char ***argvp, int isok)
1183 {
1184 char *arg = **argvp;
1185 PLAN *new;
1186
1187 (*argvp)++;
1188 new = palloc(N_MAXDEPTH, f_maxdepth);
1189 new->max_data = atoi(arg);
1190 return (new);
1191 }
1192
1193 /*
1194 * - mindepth n functions --
1195 *
1196 * True if the current search depth is greater than or equal to the
1197 * minimum depth specified
1198 */
1199 int
1200 f_mindepth(PLAN *plan, FTSENT *entry)
1201 {
1202 return (entry->fts_level >= plan->min_data);
1203 }
1204
1205 PLAN *
1206 c_mindepth(char ***argvp, int isok)
1207 {
1208 char *arg = **argvp;
1209 PLAN *new;
1210
1211 (*argvp)++;
1212 new = palloc(N_MINDEPTH, f_mindepth);
1213 new->min_data = atoi(arg);
1214 return (new);
1215 }
1216
1217 /*
1218 * -mmin n functions --
1219 *
1220 * True if the difference between the file modification time and the
1221 * current time is n 24 hour periods.
1222 */
1223 int
1224 f_mmin(PLAN *plan, FTSENT *entry)
1225 {
1226 COMPARE((now - entry->fts_statp->st_mtime + SECSPERMIN - 1) /
1227 SECSPERMIN, plan->t_data);
1228 }
1229
1230 PLAN *
1231 c_mmin(char ***argvp, int isok)
1232 {
1233 char *arg = **argvp;
1234 PLAN *new;
1235
1236 (*argvp)++;
1237 ftsoptions &= ~FTS_NOSTAT;
1238
1239 new = palloc(N_MMIN, f_mmin);
1240 new->t_data = find_parsenum(new, "-mmin", arg, NULL);
1241 TIME_CORRECT(new, N_MMIN);
1242 return (new);
1243 }
1244
1245 /*
1246 * -mtime n functions --
1247 *
1248 * True if the difference between the file modification time and the
1249 * current time is n 24 hour periods.
1250 */
1251 int
1252 f_mtime(PLAN *plan, FTSENT *entry)
1253 {
1254 COMPARE((now - entry->fts_statp->st_mtime + SECSPERDAY - 1) /
1255 SECSPERDAY, plan->t_data);
1256 }
1257
1258 PLAN *
1259 c_mtime(char ***argvp, int isok)
1260 {
1261 char *arg = **argvp;
1262 PLAN *new;
1263
1264 (*argvp)++;
1265 ftsoptions &= ~FTS_NOSTAT;
1266
1267 new = palloc(N_MTIME, f_mtime);
1268 new->t_data = find_parsenum(new, "-mtime", arg, NULL);
1269 TIME_CORRECT(new, N_MTIME);
1270 return (new);
1271 }
1272
1273 /*
1274 * -name functions --
1275 *
1276 * True if the basename of the filename being examined
1277 * matches pattern using Pattern Matching Notation S3.14
1278 */
1279 int
1280 f_name(PLAN *plan, FTSENT *entry)
1281 {
1282
1283 return (!fnmatch(plan->c_data, entry->fts_name, 0));
1284 }
1285
1286 PLAN *
1287 c_name(char ***argvp, int isok)
1288 {
1289 char *pattern = **argvp;
1290 PLAN *new;
1291
1292 (*argvp)++;
1293 new = palloc(N_NAME, f_name);
1294 new->c_data = pattern;
1295 return (new);
1296 }
1297
1298 /*
1299 * -iname functions --
1300 *
1301 * Similar to -name, but does case insensitive matching
1302 *
1303 */
1304 int
1305 f_iname(PLAN *plan, FTSENT *entry)
1306 {
1307 return (!fnmatch(plan->c_data, entry->fts_name, FNM_CASEFOLD));
1308 }
1309
1310 PLAN *
1311 c_iname(char ***argvp, int isok)
1312 {
1313 char *pattern = **argvp;
1314 PLAN *new;
1315
1316 (*argvp)++;
1317 new = palloc(N_INAME, f_iname);
1318 new->c_data = pattern;
1319 return (new);
1320 }
1321
1322 /*
1323 * -newer file functions --
1324 *
1325 * True if the current file has been modified more recently
1326 * than the modification time of the file named by the pathname
1327 * file.
1328 */
1329 int
1330 f_newer(PLAN *plan, FTSENT *entry)
1331 {
1332
1333 return timespeccmp(&entry->fts_statp->st_mtim, &plan->ts_data, >);
1334 }
1335
1336 PLAN *
1337 c_newer(char ***argvp, int isok)
1338 {
1339 char *filename = **argvp;
1340 PLAN *new;
1341 struct stat sb;
1342
1343 (*argvp)++;
1344 ftsoptions &= ~FTS_NOSTAT;
1345
1346 if (stat(filename, &sb))
1347 err(1, "%s", filename);
1348 new = palloc(N_NEWER, f_newer);
1349 new->ts_data = sb.st_mtim;
1350 return (new);
1351 }
1352
1353 /*
1354 * -nogroup functions --
1355 *
1356 * True if file belongs to a user ID for which the equivalent
1357 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1358 */
1359 int
1360 f_nogroup(PLAN *plan, FTSENT *entry)
1361 {
1362
1363 return (group_from_gid(entry->fts_statp->st_gid, 1) ? 0 : 1);
1364 }
1365
1366 PLAN *
1367 c_nogroup(char ***argvp, int isok)
1368 {
1369 ftsoptions &= ~FTS_NOSTAT;
1370
1371 return (palloc(N_NOGROUP, f_nogroup));
1372 }
1373
1374 /*
1375 * -nouser functions --
1376 *
1377 * True if file belongs to a user ID for which the equivalent
1378 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1379 */
1380 int
1381 f_nouser(PLAN *plan, FTSENT *entry)
1382 {
1383
1384 return (user_from_uid(entry->fts_statp->st_uid, 1) ? 0 : 1);
1385 }
1386
1387 PLAN *
1388 c_nouser(char ***argvp, int isok)
1389 {
1390 ftsoptions &= ~FTS_NOSTAT;
1391
1392 return (palloc(N_NOUSER, f_nouser));
1393 }
1394
1395 /*
1396 * -path functions --
1397 *
1398 * True if the path of the filename being examined
1399 * matches pattern using Pattern Matching Notation S3.14
1400 */
1401 int
1402 f_path(PLAN *plan, FTSENT *entry)
1403 {
1404
1405 return (!fnmatch(plan->c_data, entry->fts_path, 0));
1406 }
1407
1408 PLAN *
1409 c_path(char ***argvp, int isok)
1410 {
1411 char *pattern = **argvp;
1412 PLAN *new;
1413
1414 (*argvp)++;
1415 new = palloc(N_NAME, f_path);
1416 new->c_data = pattern;
1417 return (new);
1418 }
1419
1420 /*
1421 * -perm functions --
1422 *
1423 * The mode argument is used to represent file mode bits. If it starts
1424 * with a leading digit, it's treated as an octal mode, otherwise as a
1425 * symbolic mode.
1426 */
1427 int
1428 f_perm(PLAN *plan, FTSENT *entry)
1429 {
1430 mode_t mode;
1431
1432 mode = entry->fts_statp->st_mode &
1433 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1434 if (plan->flags == F_ATLEAST)
1435 return ((plan->m_data | mode) == mode);
1436 else
1437 return (mode == plan->m_data);
1438 /* NOTREACHED */
1439 }
1440
1441 PLAN *
1442 c_perm(char ***argvp, int isok)
1443 {
1444 char *perm = **argvp;
1445 PLAN *new;
1446 mode_t *set;
1447
1448 (*argvp)++;
1449 ftsoptions &= ~FTS_NOSTAT;
1450
1451 new = palloc(N_PERM, f_perm);
1452
1453 if (*perm == '-') {
1454 new->flags = F_ATLEAST;
1455 ++perm;
1456 }
1457
1458 if ((set = setmode(perm)) == NULL)
1459 err(1, "-perm: Cannot set file mode `%s'", perm);
1460
1461 new->m_data = getmode(set, 0);
1462 free(set);
1463 return (new);
1464 }
1465
1466 /*
1467 * -print functions --
1468 *
1469 * Always true, causes the current pathame to be written to
1470 * standard output.
1471 */
1472 int
1473 f_print(PLAN *plan, FTSENT *entry)
1474 {
1475
1476 (void)printf("%s\n", entry->fts_path);
1477 return (1);
1478 }
1479
1480 int
1481 f_print0(PLAN *plan, FTSENT *entry)
1482 {
1483
1484 (void)fputs(entry->fts_path, stdout);
1485 (void)fputc('\0', stdout);
1486 return (1);
1487 }
1488
1489 int
1490 f_printx(PLAN *plan, FTSENT *entry)
1491 {
1492 char *cp;
1493
1494 for (cp = entry->fts_path; *cp; cp++) {
1495 if (*cp == '\'' || *cp == '\"' || *cp == ' ' ||
1496 *cp == '$' || *cp == '`' ||
1497 *cp == '\t' || *cp == '\n' || *cp == '\\')
1498 fputc('\\', stdout);
1499
1500 fputc(*cp, stdout);
1501 }
1502
1503 fputc('\n', stdout);
1504 return (1);
1505 }
1506
1507 PLAN *
1508 c_print(char ***argvp, int isok)
1509 {
1510
1511 isoutput = 1;
1512
1513 return (palloc(N_PRINT, f_print));
1514 }
1515
1516 PLAN *
1517 c_print0(char ***argvp, int isok)
1518 {
1519
1520 isoutput = 1;
1521
1522 return (palloc(N_PRINT0, f_print0));
1523 }
1524
1525 PLAN *
1526 c_printx(char ***argvp, int isok)
1527 {
1528
1529 isoutput = 1;
1530
1531 return (palloc(N_PRINTX, f_printx));
1532 }
1533
1534 /*
1535 * -prune functions --
1536 *
1537 * Prune a portion of the hierarchy.
1538 */
1539 int
1540 f_prune(PLAN *plan, FTSENT *entry)
1541 {
1542 if (fts_set(tree, entry, FTS_SKIP))
1543 err(1, "%s", entry->fts_path);
1544 return (1);
1545 }
1546
1547 PLAN *
1548 c_prune(char ***argvp, int isok)
1549 {
1550
1551 return (palloc(N_PRUNE, f_prune));
1552 }
1553
1554 /*
1555 * -regex regexp (and related) functions --
1556 *
1557 * True if the complete file path matches the regular expression regexp.
1558 * For -regex, regexp is a case-sensitive (basic) regular expression.
1559 * For -iregex, regexp is a case-insensitive (basic) regular expression.
1560 */
1561 int
1562 f_regex(PLAN *plan, FTSENT *entry)
1563 {
1564
1565 return (regexec(&plan->regexp_data, entry->fts_path, 0, NULL, 0) == 0);
1566 }
1567
1568 static PLAN *
1569 c_regex_common(char ***argvp, int isok, enum ntype type, bool icase)
1570 {
1571 char errbuf[LINE_MAX];
1572 regex_t reg;
1573 char *regexp = **argvp;
1574 char *lineregexp;
1575 PLAN *new;
1576 int rv;
1577 size_t len;
1578
1579 (*argvp)++;
1580
1581 len = strlen(regexp) + 1 + 6;
1582 lineregexp = malloc(len); /* max needed */
1583 if (lineregexp == NULL)
1584 err(1, NULL);
1585 snprintf(lineregexp, len, "^%s(%s%s)$",
1586 (regcomp_flags & REG_EXTENDED) ? "" : "\\", regexp,
1587 (regcomp_flags & REG_EXTENDED) ? "" : "\\");
1588 rv = regcomp(®, lineregexp, REG_NOSUB|regcomp_flags|
1589 (icase ? REG_ICASE : 0));
1590 free(lineregexp);
1591 if (rv != 0) {
1592 regerror(rv, ®, errbuf, sizeof errbuf);
1593 errx(1, "regexp %s: %s", regexp, errbuf);
1594 }
1595
1596 new = palloc(type, f_regex);
1597 new->regexp_data = reg;
1598 return (new);
1599 }
1600
1601 PLAN *
1602 c_regex(char ***argvp, int isok)
1603 {
1604
1605 return (c_regex_common(argvp, isok, N_REGEX, false));
1606 }
1607
1608 PLAN *
1609 c_iregex(char ***argvp, int isok)
1610 {
1611
1612 return (c_regex_common(argvp, isok, N_IREGEX, true));
1613 }
1614
1615 /*
1616 * -size n[c] functions --
1617 *
1618 * True if the file size in bytes, divided by an implementation defined
1619 * value and rounded up to the next integer, is n. If n is followed by
1620 * a c, the size is in bytes.
1621 */
1622 #define FIND_SIZE 512
1623 static int divsize = 1;
1624
1625 int
1626 f_size(PLAN *plan, FTSENT *entry)
1627 {
1628 off_t size;
1629
1630 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1631 FIND_SIZE : entry->fts_statp->st_size;
1632 COMPARE(size, plan->o_data);
1633 }
1634
1635 PLAN *
1636 c_size(char ***argvp, int isok)
1637 {
1638 char *arg = **argvp;
1639 PLAN *new;
1640 char endch;
1641
1642 (*argvp)++;
1643 ftsoptions &= ~FTS_NOSTAT;
1644
1645 new = palloc(N_SIZE, f_size);
1646 endch = 'c';
1647 new->o_data = find_parsenum(new, "-size", arg, &endch);
1648 if (endch == 'c')
1649 divsize = 0;
1650 return (new);
1651 }
1652
1653 /*
1654 * -type c functions --
1655 *
1656 * True if the type of the file is c, where c is b, c, d, p, f or w
1657 * for block special file, character special file, directory, FIFO,
1658 * regular file or whiteout respectively.
1659 */
1660 int
1661 f_type(PLAN *plan, FTSENT *entry)
1662 {
1663
1664 return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data);
1665 }
1666
1667 PLAN *
1668 c_type(char ***argvp, int isok)
1669 {
1670 char *typestring = **argvp;
1671 PLAN *new;
1672 mode_t mask = (mode_t)0;
1673
1674 (*argvp)++;
1675 ftsoptions &= ~FTS_NOSTAT;
1676
1677 switch (typestring[0]) {
1678 case 'b':
1679 mask = S_IFBLK;
1680 break;
1681 case 'c':
1682 mask = S_IFCHR;
1683 break;
1684 case 'd':
1685 mask = S_IFDIR;
1686 break;
1687 case 'f':
1688 mask = S_IFREG;
1689 break;
1690 case 'l':
1691 mask = S_IFLNK;
1692 break;
1693 case 'p':
1694 mask = S_IFIFO;
1695 break;
1696 case 's':
1697 mask = S_IFSOCK;
1698 break;
1699 #ifdef S_IFWHT
1700 case 'W':
1701 case 'w':
1702 mask = S_IFWHT;
1703 #ifdef FTS_WHITEOUT
1704 ftsoptions |= FTS_WHITEOUT;
1705 #endif
1706 break;
1707 #endif /* S_IFWHT */
1708 default:
1709 errx(1, "-type: %s: unknown type", typestring);
1710 }
1711
1712 new = palloc(N_TYPE, f_type);
1713 new->m_data = mask;
1714 return (new);
1715 }
1716
1717 /*
1718 * -user uname functions --
1719 *
1720 * True if the file belongs to the user uname. If uname is numeric and
1721 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1722 * return a valid user name, uname is taken as a user ID.
1723 */
1724 int
1725 f_user(PLAN *plan, FTSENT *entry)
1726 {
1727
1728 COMPARE(entry->fts_statp->st_uid, plan->u_data);
1729 }
1730
1731 PLAN *
1732 c_user(char ***argvp, int isok)
1733 {
1734 char *username = **argvp;
1735 PLAN *new;
1736 struct passwd *p;
1737 uid_t uid;
1738
1739 (*argvp)++;
1740 ftsoptions &= ~FTS_NOSTAT;
1741
1742 new = palloc(N_USER, f_user);
1743 p = getpwnam(username);
1744 if (p == NULL) {
1745 if (atoi(username) == 0 && username[0] != '0' &&
1746 strcmp(username, "+0") && strcmp(username, "-0"))
1747 errx(1, "-user: %s: no such user", username);
1748 uid = find_parsenum(new, "-user", username, NULL);
1749
1750 } else {
1751 new->flags = F_EQUAL;
1752 uid = p->pw_uid;
1753 }
1754
1755 new->u_data = uid;
1756 return (new);
1757 }
1758
1759 /*
1760 * -xdev functions --
1761 *
1762 * Always true, causes find not to descend past directories that have a
1763 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1764 */
1765 PLAN *
1766 c_xdev(char ***argvp, int isok)
1767 {
1768 ftsoptions |= FTS_XDEV;
1769
1770 return (palloc(N_XDEV, f_always_true));
1771 }
1772
1773 /*
1774 * ( expression ) functions --
1775 *
1776 * True if expression is true.
1777 */
1778 int
1779 f_expr(PLAN *plan, FTSENT *entry)
1780 {
1781 PLAN *p;
1782 int state;
1783
1784 state = 0;
1785 for (p = plan->p_data[0];
1786 p && (state = (p->eval)(p, entry)); p = p->next);
1787 return (state);
1788 }
1789
1790 /*
1791 * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers. They are
1792 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1793 * to a N_EXPR node containing the expression and the ')' node is discarded.
1794 */
1795 PLAN *
1796 c_openparen(char ***argvp, int isok)
1797 {
1798
1799 return (palloc(N_OPENPAREN, (int (*)(PLAN *, FTSENT *))-1));
1800 }
1801
1802 PLAN *
1803 c_closeparen(char ***argvp, int isok)
1804 {
1805
1806 return (palloc(N_CLOSEPAREN, (int (*)(PLAN *, FTSENT *))-1));
1807 }
1808
1809 /*
1810 * ! expression functions --
1811 *
1812 * Negation of a primary; the unary NOT operator.
1813 */
1814 int
1815 f_not(PLAN *plan, FTSENT *entry)
1816 {
1817 PLAN *p;
1818 int state;
1819
1820 state = 0;
1821 for (p = plan->p_data[0];
1822 p && (state = (p->eval)(p, entry)); p = p->next);
1823 return (!state);
1824 }
1825
1826 PLAN *
1827 c_not(char ***argvp, int isok)
1828 {
1829
1830 return (palloc(N_NOT, f_not));
1831 }
1832
1833 /*
1834 * expression -o expression functions --
1835 *
1836 * Alternation of primaries; the OR operator. The second expression is
1837 * not evaluated if the first expression is true.
1838 */
1839 int
1840 f_or(PLAN *plan, FTSENT *entry)
1841 {
1842 PLAN *p;
1843 int state;
1844
1845 state = 0;
1846 for (p = plan->p_data[0];
1847 p && (state = (p->eval)(p, entry)); p = p->next);
1848
1849 if (state)
1850 return (1);
1851
1852 for (p = plan->p_data[1];
1853 p && (state = (p->eval)(p, entry)); p = p->next);
1854 return (state);
1855 }
1856
1857 PLAN *
1858 c_or(char ***argvp, int isok)
1859 {
1860
1861 return (palloc(N_OR, f_or));
1862 }
1863
1864 PLAN *
1865 c_null(char ***argvp, int isok)
1866 {
1867
1868 return (NULL);
1869 }
1870
1871
1872 /*
1873 * plan_cleanup --
1874 * Check and see if the specified plan has any residual state,
1875 * and if so, clean it up as appropriate.
1876 *
1877 * At the moment, only N_EXEC has state. Two kinds: 1)
1878 * lists of files to feed to subprocesses 2) State on exit
1879 * statusses of past subprocesses.
1880 */
1881 /* ARGSUSED1 */
1882 int
1883 plan_cleanup(PLAN *plan, void *arg)
1884 {
1885 if (plan->type==N_EXEC && plan->ep_narg)
1886 run_f_exec(plan);
1887
1888 return plan->ep_rval; /* Passed save exit-status up chain */
1889 }
1890
1891 static PLAN *
1892 palloc(enum ntype t, int (*f)(PLAN *, FTSENT *))
1893 {
1894 PLAN *new;
1895
1896 if ((new = malloc(sizeof(PLAN))) == NULL)
1897 err(1, NULL);
1898 memset(new, 0, sizeof(PLAN));
1899 new->type = t;
1900 new->eval = f;
1901 return (new);
1902 }
1903