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