function.c revision 1.34 1 /* $NetBSD: function.c,v 1.34 2000/10/10 14:30:41 enami 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.34 2000/10/10 14:30:41 enami 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
315 return (1);
316 }
317
318 PLAN *
319 c_depth(argvp, isok)
320 char ***argvp;
321 int isok;
322 {
323 isdepth = 1;
324
325 return (palloc(N_DEPTH, f_always_true));
326 }
327
328 /*
329 * [-exec | -ok] utility [arg ... ] ; functions --
330 *
331 * True if the executed utility returns a zero value as exit status.
332 * The end of the primary expression is delimited by a semicolon. If
333 * "{}" occurs anywhere, it gets replaced by the current pathname.
334 * The current directory for the execution of utility is the same as
335 * the current directory when the find utility was started.
336 *
337 * The primary -ok is different in that it requests affirmation of the
338 * user before executing the utility.
339 */
340 int
341 f_exec(plan, entry)
342 PLAN *plan;
343 FTSENT *entry;
344 {
345 extern int dotfd;
346 int cnt;
347 pid_t pid;
348 int status;
349
350 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
351 if (plan->e_len[cnt])
352 brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
353 entry->fts_path, plan->e_len[cnt]);
354
355 if (plan->flags == F_NEEDOK && !queryuser(plan->e_argv))
356 return (0);
357
358 /* don't mix output of command with find output */
359 fflush(stdout);
360 fflush(stderr);
361
362 switch (pid = vfork()) {
363 case -1:
364 err(1, "fork");
365 /* NOTREACHED */
366 case 0:
367 if (fchdir(dotfd)) {
368 warn("chdir");
369 _exit(1);
370 }
371 execvp(plan->e_argv[0], plan->e_argv);
372 warn("%s", plan->e_argv[0]);
373 _exit(1);
374 }
375 pid = waitpid(pid, &status, 0);
376 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
377 }
378
379 /*
380 * c_exec --
381 * build three parallel arrays, one with pointers to the strings passed
382 * on the command line, one with (possibly duplicated) pointers to the
383 * argv array, and one with integer values that are lengths of the
384 * strings, but also flags meaning that the string has to be massaged.
385 */
386 PLAN *
387 c_exec(argvp, isok)
388 char ***argvp;
389 int isok;
390 {
391 PLAN *new; /* node returned */
392 int cnt;
393 char **argv, **ap, *p;
394
395 isoutput = 1;
396
397 new = palloc(N_EXEC, f_exec);
398 if (isok)
399 new->flags = F_NEEDOK;
400
401 for (ap = argv = *argvp;; ++ap) {
402 if (!*ap)
403 errx(1,
404 "%s: no terminating \";\"", isok ? "-ok" : "-exec");
405 if (**ap == ';')
406 break;
407 }
408
409 cnt = ap - *argvp + 1;
410 new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
411 new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
412 new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
413
414 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
415 new->e_orig[cnt] = *argv;
416 for (p = *argv; *p; ++p)
417 if (p[0] == '{' && p[1] == '}') {
418 new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
419 new->e_len[cnt] = MAXPATHLEN;
420 break;
421 }
422 if (!*p) {
423 new->e_argv[cnt] = *argv;
424 new->e_len[cnt] = 0;
425 }
426 }
427 new->e_argv[cnt] = new->e_orig[cnt] = NULL;
428
429 *argvp = argv + 1;
430 return (new);
431 }
432
433 /*
434 * -flags [-]flags functions --
435 */
436 int
437 f_flags(plan, entry)
438 PLAN *plan;
439 FTSENT *entry;
440 {
441 u_int32_t flags;
442
443 flags = entry->fts_statp->st_flags;
444 if (plan->flags == F_ATLEAST)
445 return ((plan->f_data | flags) == flags);
446 else
447 return (flags == plan->f_data);
448 /* NOTREACHED */
449 }
450
451 PLAN *
452 c_flags(argvp, isok)
453 char ***argvp;
454 int isok;
455 {
456 char *flags = **argvp;
457 PLAN *new;
458 u_long flagset;
459
460 (*argvp)++;
461 ftsoptions &= ~FTS_NOSTAT;
462
463 new = palloc(N_FLAGS, f_flags);
464
465 if (*flags == '-') {
466 new->flags = F_ATLEAST;
467 ++flags;
468 }
469
470 flagset = 0;
471 if ((strcmp(flags, "none") != 0) &&
472 (string_to_flags(&flags, &flagset, NULL) != 0))
473 errx(1, "-flags: %s: illegal flags string", flags);
474 new->f_data = flagset;
475 return (new);
476 }
477
478
479 /*
480 * -follow functions --
481 *
482 * Always true, causes symbolic links to be followed on a global
483 * basis.
484 */
485 PLAN *
486 c_follow(argvp, isok)
487 char ***argvp;
488 int isok;
489 {
490 ftsoptions &= ~FTS_PHYSICAL;
491 ftsoptions |= FTS_LOGICAL;
492
493 return (palloc(N_FOLLOW, f_always_true));
494 }
495
496 /*
497 * -fstype functions --
498 *
499 * True if the file is of a certain type.
500 */
501 int
502 f_fstype(plan, entry)
503 PLAN *plan;
504 FTSENT *entry;
505 {
506 static dev_t curdev; /* need a guaranteed illegal dev value */
507 static int first = 1;
508 struct statfs sb;
509 static short val;
510 static char fstype[MFSNAMELEN];
511 char *p, save[2];
512
513 /* Only check when we cross mount point. */
514 if (first || curdev != entry->fts_statp->st_dev) {
515 curdev = entry->fts_statp->st_dev;
516
517 /*
518 * Statfs follows symlinks; find wants the link's file system,
519 * not where it points.
520 */
521 if (entry->fts_info == FTS_SL ||
522 entry->fts_info == FTS_SLNONE) {
523 if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
524 ++p;
525 else
526 p = entry->fts_accpath;
527 save[0] = p[0];
528 p[0] = '.';
529 save[1] = p[1];
530 p[1] = '\0';
531
532 } else
533 p = NULL;
534
535 if (statfs(entry->fts_accpath, &sb))
536 err(1, "%s", entry->fts_accpath);
537
538 if (p) {
539 p[0] = save[0];
540 p[1] = save[1];
541 }
542
543 first = 0;
544
545 /*
546 * Further tests may need both of these values, so
547 * always copy both of them.
548 */
549 val = sb.f_flags;
550 strncpy(fstype, sb.f_fstypename, MFSNAMELEN);
551 }
552 switch (plan->flags) {
553 case F_MTFLAG:
554 return (val & plan->mt_data);
555 case F_MTTYPE:
556 return (strncmp(fstype, plan->c_data, MFSNAMELEN) == 0);
557 default:
558 abort();
559 }
560 }
561
562 PLAN *
563 c_fstype(argvp, isok)
564 char ***argvp;
565 int isok;
566 {
567 char *arg = **argvp;
568 PLAN *new;
569
570 (*argvp)++;
571 ftsoptions &= ~FTS_NOSTAT;
572
573 new = palloc(N_FSTYPE, f_fstype);
574
575 switch (*arg) {
576 case 'l':
577 if (!strcmp(arg, "local")) {
578 new->flags = F_MTFLAG;
579 new->mt_data = MNT_LOCAL;
580 return (new);
581 }
582 break;
583 case 'r':
584 if (!strcmp(arg, "rdonly")) {
585 new->flags = F_MTFLAG;
586 new->mt_data = MNT_RDONLY;
587 return (new);
588 }
589 break;
590 }
591
592 new->flags = F_MTTYPE;
593 new->c_data = arg;
594 return (new);
595 }
596
597 /*
598 * -group gname functions --
599 *
600 * True if the file belongs to the group gname. If gname is numeric and
601 * an equivalent of the getgrnam() function does not return a valid group
602 * name, gname is taken as a group ID.
603 */
604 int
605 f_group(plan, entry)
606 PLAN *plan;
607 FTSENT *entry;
608 {
609
610 return (entry->fts_statp->st_gid == plan->g_data);
611 }
612
613 PLAN *
614 c_group(argvp, isok)
615 char ***argvp;
616 int isok;
617 {
618 char *gname = **argvp;
619 PLAN *new;
620 struct group *g;
621 gid_t gid;
622
623 (*argvp)++;
624 ftsoptions &= ~FTS_NOSTAT;
625
626 g = getgrnam(gname);
627 if (g == NULL) {
628 gid = atoi(gname);
629 if (gid == 0 && gname[0] != '0')
630 errx(1, "-group: %s: no such group", gname);
631 } else
632 gid = g->gr_gid;
633
634 new = palloc(N_GROUP, f_group);
635 new->g_data = gid;
636 return (new);
637 }
638
639 /*
640 * -inum n functions --
641 *
642 * True if the file has inode # n.
643 */
644 int
645 f_inum(plan, entry)
646 PLAN *plan;
647 FTSENT *entry;
648 {
649
650 COMPARE(entry->fts_statp->st_ino, plan->i_data);
651 }
652
653 PLAN *
654 c_inum(argvp, isok)
655 char ***argvp;
656 int isok;
657 {
658 char *arg = **argvp;
659 PLAN *new;
660
661 (*argvp)++;
662 ftsoptions &= ~FTS_NOSTAT;
663
664 new = palloc(N_INUM, f_inum);
665 new->i_data = find_parsenum(new, "-inum", arg, NULL);
666 return (new);
667 }
668
669 /*
670 * -links n functions --
671 *
672 * True if the file has n links.
673 */
674 int
675 f_links(plan, entry)
676 PLAN *plan;
677 FTSENT *entry;
678 {
679
680 COMPARE(entry->fts_statp->st_nlink, plan->l_data);
681 }
682
683 PLAN *
684 c_links(argvp, isok)
685 char ***argvp;
686 int isok;
687 {
688 char *arg = **argvp;
689 PLAN *new;
690
691 (*argvp)++;
692 ftsoptions &= ~FTS_NOSTAT;
693
694 new = palloc(N_LINKS, f_links);
695 new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL);
696 return (new);
697 }
698
699 /*
700 * -ls functions --
701 *
702 * Always true - prints the current entry to stdout in "ls" format.
703 */
704 int
705 f_ls(plan, entry)
706 PLAN *plan;
707 FTSENT *entry;
708 {
709
710 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
711 return (1);
712 }
713
714 PLAN *
715 c_ls(argvp, isok)
716 char ***argvp;
717 int isok;
718 {
719
720 ftsoptions &= ~FTS_NOSTAT;
721 isoutput = 1;
722
723 return (palloc(N_LS, f_ls));
724 }
725
726 /*
727 * -mmin n functions --
728 *
729 * True if the difference between the file modification time and the
730 * current time is n 24 hour periods.
731 */
732 int
733 f_mmin(plan, entry)
734 PLAN *plan;
735 FTSENT *entry;
736 {
737 extern time_t now;
738
739 COMPARE((now - entry->fts_statp->st_mtime + SECSPERMIN - 1) /
740 SECSPERMIN, plan->t_data);
741 }
742
743 PLAN *
744 c_mmin(argvp, isok)
745 char ***argvp;
746 int isok;
747 {
748 char *arg = **argvp;
749 PLAN *new;
750
751 (*argvp)++;
752 ftsoptions &= ~FTS_NOSTAT;
753
754 new = palloc(N_MMIN, f_mmin);
755 new->t_data = find_parsenum(new, "-mmin", arg, NULL);
756 TIME_CORRECT(new, N_MMIN);
757 return (new);
758 }
759 /*
760 * -mtime n functions --
761 *
762 * True if the difference between the file modification time and the
763 * current time is n 24 hour periods.
764 */
765 int
766 f_mtime(plan, entry)
767 PLAN *plan;
768 FTSENT *entry;
769 {
770 extern time_t now;
771
772 COMPARE((now - entry->fts_statp->st_mtime + SECSPERDAY - 1) /
773 SECSPERDAY, plan->t_data);
774 }
775
776 PLAN *
777 c_mtime(argvp, isok)
778 char ***argvp;
779 int isok;
780 {
781 char *arg = **argvp;
782 PLAN *new;
783
784 (*argvp)++;
785 ftsoptions &= ~FTS_NOSTAT;
786
787 new = palloc(N_MTIME, f_mtime);
788 new->t_data = find_parsenum(new, "-mtime", arg, NULL);
789 TIME_CORRECT(new, N_MTIME);
790 return (new);
791 }
792
793 /*
794 * -name functions --
795 *
796 * True if the basename of the filename being examined
797 * matches pattern using Pattern Matching Notation S3.14
798 */
799 int
800 f_name(plan, entry)
801 PLAN *plan;
802 FTSENT *entry;
803 {
804
805 return (!fnmatch(plan->c_data, entry->fts_name, 0));
806 }
807
808 PLAN *
809 c_name(argvp, isok)
810 char ***argvp;
811 int isok;
812 {
813 char *pattern = **argvp;
814 PLAN *new;
815
816 (*argvp)++;
817 new = palloc(N_NAME, f_name);
818 new->c_data = pattern;
819 return (new);
820 }
821
822 /*
823 * -newer file functions --
824 *
825 * True if the current file has been modified more recently
826 * then the modification time of the file named by the pathname
827 * file.
828 */
829 int
830 f_newer(plan, entry)
831 PLAN *plan;
832 FTSENT *entry;
833 {
834
835 return (entry->fts_statp->st_mtime > plan->t_data);
836 }
837
838 PLAN *
839 c_newer(argvp, isok)
840 char ***argvp;
841 int isok;
842 {
843 char *filename = **argvp;
844 PLAN *new;
845 struct stat sb;
846
847 (*argvp)++;
848 ftsoptions &= ~FTS_NOSTAT;
849
850 if (stat(filename, &sb))
851 err(1, "%s", filename);
852 new = palloc(N_NEWER, f_newer);
853 new->t_data = sb.st_mtime;
854 return (new);
855 }
856
857 /*
858 * -nogroup functions --
859 *
860 * True if file belongs to a user ID for which the equivalent
861 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
862 */
863 int
864 f_nogroup(plan, entry)
865 PLAN *plan;
866 FTSENT *entry;
867 {
868
869 return (group_from_gid(entry->fts_statp->st_gid, 1) ? 0 : 1);
870 }
871
872 PLAN *
873 c_nogroup(argvp, isok)
874 char ***argvp;
875 int isok;
876 {
877 ftsoptions &= ~FTS_NOSTAT;
878
879 return (palloc(N_NOGROUP, f_nogroup));
880 }
881
882 /*
883 * -nouser functions --
884 *
885 * True if file belongs to a user ID for which the equivalent
886 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
887 */
888 int
889 f_nouser(plan, entry)
890 PLAN *plan;
891 FTSENT *entry;
892 {
893
894 return (user_from_uid(entry->fts_statp->st_uid, 1) ? 0 : 1);
895 }
896
897 PLAN *
898 c_nouser(argvp, isok)
899 char ***argvp;
900 int isok;
901 {
902 ftsoptions &= ~FTS_NOSTAT;
903
904 return (palloc(N_NOUSER, f_nouser));
905 }
906
907 /*
908 * -path functions --
909 *
910 * True if the path of the filename being examined
911 * matches pattern using Pattern Matching Notation S3.14
912 */
913 int
914 f_path(plan, entry)
915 PLAN *plan;
916 FTSENT *entry;
917 {
918
919 return (!fnmatch(plan->c_data, entry->fts_path, 0));
920 }
921
922 PLAN *
923 c_path(argvp, isok)
924 char ***argvp;
925 int isok;
926 {
927 char *pattern = **argvp;
928 PLAN *new;
929
930 (*argvp)++;
931 new = palloc(N_NAME, f_path);
932 new->c_data = pattern;
933 return (new);
934 }
935
936 /*
937 * -perm functions --
938 *
939 * The mode argument is used to represent file mode bits. If it starts
940 * with a leading digit, it's treated as an octal mode, otherwise as a
941 * symbolic mode.
942 */
943 int
944 f_perm(plan, entry)
945 PLAN *plan;
946 FTSENT *entry;
947 {
948 mode_t mode;
949
950 mode = entry->fts_statp->st_mode &
951 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
952 if (plan->flags == F_ATLEAST)
953 return ((plan->m_data | mode) == mode);
954 else
955 return (mode == plan->m_data);
956 /* NOTREACHED */
957 }
958
959 PLAN *
960 c_perm(argvp, isok)
961 char ***argvp;
962 int isok;
963 {
964 char *perm = **argvp;
965 PLAN *new;
966 mode_t *set;
967
968 (*argvp)++;
969 ftsoptions &= ~FTS_NOSTAT;
970
971 new = palloc(N_PERM, f_perm);
972
973 if (*perm == '-') {
974 new->flags = F_ATLEAST;
975 ++perm;
976 }
977
978 if ((set = setmode(perm)) == NULL)
979 err(1, "-perm: %s: illegal mode string", perm);
980
981 new->m_data = getmode(set, 0);
982 free(set);
983 return (new);
984 }
985
986 /*
987 * -print functions --
988 *
989 * Always true, causes the current pathame to be written to
990 * standard output.
991 */
992 int
993 f_print(plan, entry)
994 PLAN *plan;
995 FTSENT *entry;
996 {
997
998 (void)printf("%s\n", entry->fts_path);
999 return (1);
1000 }
1001
1002 int
1003 f_print0(plan, entry)
1004 PLAN *plan;
1005 FTSENT *entry;
1006 {
1007
1008 (void)fputs(entry->fts_path, stdout);
1009 (void)fputc('\0', stdout);
1010 return (1);
1011 }
1012
1013 int
1014 f_printx(plan, entry)
1015 PLAN *plan;
1016 FTSENT *entry;
1017 {
1018 char *cp;
1019
1020 for (cp = entry->fts_path; *cp; cp++) {
1021 if (*cp == '\'' || *cp == '\"' || *cp == ' ' ||
1022 *cp == '\t' || *cp == '\n' || *cp == '\\')
1023 fputc('\\', stdout);
1024
1025 fputc(*cp, stdout);
1026 }
1027
1028 fputc('\n', stdout);
1029 return (1);
1030 }
1031
1032 PLAN *
1033 c_print(argvp, isok)
1034 char ***argvp;
1035 int isok;
1036 {
1037
1038 isoutput = 1;
1039
1040 return (palloc(N_PRINT, f_print));
1041 }
1042
1043 PLAN *
1044 c_print0(argvp, isok)
1045 char ***argvp;
1046 int isok;
1047 {
1048
1049 isoutput = 1;
1050
1051 return (palloc(N_PRINT0, f_print0));
1052 }
1053
1054 PLAN *
1055 c_printx(argvp, isok)
1056 char ***argvp;
1057 int isok;
1058 {
1059
1060 isoutput = 1;
1061
1062 return (palloc(N_PRINTX, f_printx));
1063 }
1064
1065 /*
1066 * -prune functions --
1067 *
1068 * Prune a portion of the hierarchy.
1069 */
1070 int
1071 f_prune(plan, entry)
1072 PLAN *plan;
1073 FTSENT *entry;
1074 {
1075 extern FTS *tree;
1076
1077 if (fts_set(tree, entry, FTS_SKIP))
1078 err(1, "%s", entry->fts_path);
1079 return (1);
1080 }
1081
1082 PLAN *
1083 c_prune(argvp, isok)
1084 char ***argvp;
1085 int isok;
1086 {
1087
1088 return (palloc(N_PRUNE, f_prune));
1089 }
1090
1091 /*
1092 * -regex regexp (and related) functions --
1093 *
1094 * True if the complete file path matches the regular expression regexp.
1095 * For -regex, regexp is a case-sensitive (basic) regular expression.
1096 * For -iregex, regexp is a case-insensitive (basic) regular expression.
1097 */
1098 int
1099 f_regex(plan, entry)
1100 PLAN *plan;
1101 FTSENT *entry;
1102 {
1103
1104 return (regexec(&plan->regexp_data, entry->fts_path, 0, NULL, 0) == 0);
1105 }
1106
1107 static PLAN *
1108 c_regex_common(argvp, isok, type, regcomp_flags)
1109 char ***argvp;
1110 int isok, regcomp_flags;
1111 enum ntype type;
1112 {
1113 char errbuf[LINE_MAX];
1114 regex_t reg;
1115 char *regexp = **argvp;
1116 char *lineregexp;
1117 PLAN *new;
1118 int rv;
1119
1120 (*argvp)++;
1121
1122 lineregexp = alloca(strlen(regexp) + 1 + 6); /* max needed */
1123 sprintf(lineregexp, "^%s(%s%s)$",
1124 (regcomp_flags & REG_EXTENDED) ? "" : "\\", regexp,
1125 (regcomp_flags & REG_EXTENDED) ? "" : "\\");
1126 rv = regcomp(®, lineregexp, REG_NOSUB|regcomp_flags);
1127 if (rv != 0) {
1128 regerror(rv, ®, errbuf, sizeof errbuf);
1129 errx(1, "regexp %s: %s", regexp, errbuf);
1130 }
1131
1132 new = palloc(type, f_regex);
1133 new->regexp_data = reg;
1134 return (new);
1135 }
1136
1137 PLAN *
1138 c_regex(argvp, isok)
1139 char ***argvp;
1140 int isok;
1141 {
1142
1143 return (c_regex_common(argvp, isok, N_REGEX, REG_BASIC));
1144 }
1145
1146 PLAN *
1147 c_iregex(argvp, isok)
1148 char ***argvp;
1149 int isok;
1150 {
1151
1152 return (c_regex_common(argvp, isok, N_IREGEX, REG_BASIC|REG_ICASE));
1153 }
1154
1155 /*
1156 * -size n[c] functions --
1157 *
1158 * True if the file size in bytes, divided by an implementation defined
1159 * value and rounded up to the next integer, is n. If n is followed by
1160 * a c, the size is in bytes.
1161 */
1162 #define FIND_SIZE 512
1163 static int divsize = 1;
1164
1165 int
1166 f_size(plan, entry)
1167 PLAN *plan;
1168 FTSENT *entry;
1169 {
1170 off_t size;
1171
1172 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1173 FIND_SIZE : entry->fts_statp->st_size;
1174 COMPARE(size, plan->o_data);
1175 }
1176
1177 PLAN *
1178 c_size(argvp, isok)
1179 char ***argvp;
1180 int isok;
1181 {
1182 char *arg = **argvp;
1183 PLAN *new;
1184 char endch;
1185
1186 (*argvp)++;
1187 ftsoptions &= ~FTS_NOSTAT;
1188
1189 new = palloc(N_SIZE, f_size);
1190 endch = 'c';
1191 new->o_data = find_parsenum(new, "-size", arg, &endch);
1192 if (endch == 'c')
1193 divsize = 0;
1194 return (new);
1195 }
1196
1197 /*
1198 * -type c functions --
1199 *
1200 * True if the type of the file is c, where c is b, c, d, p, f or w
1201 * for block special file, character special file, directory, FIFO,
1202 * regular file or whiteout respectively.
1203 */
1204 int
1205 f_type(plan, entry)
1206 PLAN *plan;
1207 FTSENT *entry;
1208 {
1209
1210 return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data);
1211 }
1212
1213 PLAN *
1214 c_type(argvp, isok)
1215 char ***argvp;
1216 int isok;
1217 {
1218 char *typestring = **argvp;
1219 PLAN *new;
1220 mode_t mask = (mode_t)0;
1221
1222 (*argvp)++;
1223 ftsoptions &= ~FTS_NOSTAT;
1224
1225 switch (typestring[0]) {
1226 #ifdef S_IFWHT
1227 case 'W':
1228 #ifdef FTS_WHITEOUT
1229 ftsoptions |= FTS_WHITEOUT;
1230 #endif
1231 mask = S_IFWHT;
1232 break;
1233 #endif
1234 case 'b':
1235 mask = S_IFBLK;
1236 break;
1237 case 'c':
1238 mask = S_IFCHR;
1239 break;
1240 case 'd':
1241 mask = S_IFDIR;
1242 break;
1243 case 'f':
1244 mask = S_IFREG;
1245 break;
1246 case 'l':
1247 mask = S_IFLNK;
1248 break;
1249 case 'p':
1250 mask = S_IFIFO;
1251 break;
1252 case 's':
1253 mask = S_IFSOCK;
1254 break;
1255 #ifdef FTS_WHITEOUT
1256 case 'w':
1257 mask = S_IFWHT;
1258 ftsoptions |= FTS_WHITEOUT;
1259 break;
1260 #endif /* FTS_WHITEOUT */
1261 default:
1262 errx(1, "-type: %s: unknown type", typestring);
1263 }
1264
1265 new = palloc(N_TYPE, f_type);
1266 new->m_data = mask;
1267 return (new);
1268 }
1269
1270 /*
1271 * -user uname functions --
1272 *
1273 * True if the file belongs to the user uname. If uname is numeric and
1274 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1275 * return a valid user name, uname is taken as a user ID.
1276 */
1277 int
1278 f_user(plan, entry)
1279 PLAN *plan;
1280 FTSENT *entry;
1281 {
1282
1283 return (entry->fts_statp->st_uid == plan->u_data);
1284 }
1285
1286 PLAN *
1287 c_user(argvp, isok)
1288 char ***argvp;
1289 int isok;
1290 {
1291 char *username = **argvp;
1292 PLAN *new;
1293 struct passwd *p;
1294 uid_t uid;
1295
1296 (*argvp)++;
1297 ftsoptions &= ~FTS_NOSTAT;
1298
1299 p = getpwnam(username);
1300 if (p == NULL) {
1301 uid = atoi(username);
1302 if (uid == 0 && username[0] != '0')
1303 errx(1, "-user: %s: no such user", username);
1304 } else
1305 uid = p->pw_uid;
1306
1307 new = palloc(N_USER, f_user);
1308 new->u_data = uid;
1309 return (new);
1310 }
1311
1312 /*
1313 * -xdev functions --
1314 *
1315 * Always true, causes find not to decend past directories that have a
1316 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1317 */
1318 PLAN *
1319 c_xdev(argvp, isok)
1320 char ***argvp;
1321 int isok;
1322 {
1323 ftsoptions |= FTS_XDEV;
1324
1325 return (palloc(N_XDEV, f_always_true));
1326 }
1327
1328 /*
1329 * ( expression ) functions --
1330 *
1331 * True if expression is true.
1332 */
1333 int
1334 f_expr(plan, entry)
1335 PLAN *plan;
1336 FTSENT *entry;
1337 {
1338 PLAN *p;
1339 int state;
1340
1341 state = 0;
1342 for (p = plan->p_data[0];
1343 p && (state = (p->eval)(p, entry)); p = p->next);
1344 return (state);
1345 }
1346
1347 /*
1348 * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers. They are
1349 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1350 * to a N_EXPR node containing the expression and the ')' node is discarded.
1351 */
1352 PLAN *
1353 c_openparen(argvp, isok)
1354 char ***argvp;
1355 int isok;
1356 {
1357
1358 return (palloc(N_OPENPAREN, (int (*) __P((PLAN *, FTSENT *)))-1));
1359 }
1360
1361 PLAN *
1362 c_closeparen(argvp, isok)
1363 char ***argvp;
1364 int isok;
1365 {
1366
1367 return (palloc(N_CLOSEPAREN, (int (*) __P((PLAN *, FTSENT *)))-1));
1368 }
1369
1370 /*
1371 * ! expression functions --
1372 *
1373 * Negation of a primary; the unary NOT operator.
1374 */
1375 int
1376 f_not(plan, entry)
1377 PLAN *plan;
1378 FTSENT *entry;
1379 {
1380 PLAN *p;
1381 int state;
1382
1383 state = 0;
1384 for (p = plan->p_data[0];
1385 p && (state = (p->eval)(p, entry)); p = p->next);
1386 return (!state);
1387 }
1388
1389 PLAN *
1390 c_not(argvp, isok)
1391 char ***argvp;
1392 int isok;
1393 {
1394
1395 return (palloc(N_NOT, f_not));
1396 }
1397
1398 /*
1399 * expression -o expression functions --
1400 *
1401 * Alternation of primaries; the OR operator. The second expression is
1402 * not evaluated if the first expression is true.
1403 */
1404 int
1405 f_or(plan, entry)
1406 PLAN *plan;
1407 FTSENT *entry;
1408 {
1409 PLAN *p;
1410 int state;
1411
1412 state = 0;
1413 for (p = plan->p_data[0];
1414 p && (state = (p->eval)(p, entry)); p = p->next);
1415
1416 if (state)
1417 return (1);
1418
1419 for (p = plan->p_data[1];
1420 p && (state = (p->eval)(p, entry)); p = p->next);
1421 return (state);
1422 }
1423
1424 PLAN *
1425 c_or(argvp, isok)
1426 char ***argvp;
1427 int isok;
1428 {
1429
1430 return (palloc(N_OR, f_or));
1431 }
1432
1433 PLAN *
1434 c_null(argvp, isok)
1435 char ***argvp;
1436 int isok;
1437 {
1438
1439 return (NULL);
1440 }
1441
1442 static PLAN *
1443 palloc(t, f)
1444 enum ntype t;
1445 int (*f) __P((PLAN *, FTSENT *));
1446 {
1447 PLAN *new;
1448
1449 if ((new = malloc(sizeof(PLAN))) == NULL)
1450 err(1, NULL);
1451 new->type = t;
1452 new->eval = f;
1453 new->flags = 0;
1454 new->next = NULL;
1455 return (new);
1456 }
1457