function.c revision 1.10 1 /*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Cimarron D. Taylor of the University of California, Berkeley.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #ifndef lint
38 /*static char sccsid[] = "from: @(#)function.c 8.1 (Berkeley) 6/6/93";*/
39 static char rcsid[] = "$Id: function.c,v 1.10 1993/12/30 21:15:24 jtc Exp $";
40 #endif /* not lint */
41
42 #include <sys/param.h>
43 #include <sys/ucred.h>
44 #include <sys/stat.h>
45 #include <sys/wait.h>
46 #include <sys/mount.h>
47
48 #include <err.h>
49 #include <errno.h>
50 #include <fnmatch.h>
51 #include <fts.h>
52 #include <grp.h>
53 #include <pwd.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <tzfile.h>
58 #include <unistd.h>
59
60 #include "find.h"
61
62 #define COMPARE(a, b) { \
63 switch (plan->flags) { \
64 case F_EQUAL: \
65 return (a == b); \
66 case F_LESSTHAN: \
67 return (a < b); \
68 case F_GREATER: \
69 return (a > b); \
70 default: \
71 abort(); \
72 } \
73 }
74
75 static PLAN *palloc __P((enum ntype, int (*) __P((PLAN *, FTSENT *))));
76
77 /*
78 * find_parsenum --
79 * Parse a string of the form [+-]# and return the value.
80 */
81 static long
82 find_parsenum(plan, option, vp, endch)
83 PLAN *plan;
84 char *option, *vp, *endch;
85 {
86 long value;
87 char *endchar, *str; /* Pointer to character ending conversion. */
88
89 /* Determine comparison from leading + or -. */
90 str = vp;
91 switch (*str) {
92 case '+':
93 ++str;
94 plan->flags = F_GREATER;
95 break;
96 case '-':
97 ++str;
98 plan->flags = F_LESSTHAN;
99 break;
100 default:
101 plan->flags = F_EQUAL;
102 break;
103 }
104
105 /*
106 * Convert the string with strtol(). Note, if strtol() returns zero
107 * and endchar points to the beginning of the string we know we have
108 * a syntax error.
109 */
110 value = strtol(str, &endchar, 10);
111 if (value == 0 && endchar == str)
112 errx(1, "%s: %s: illegal numeric value", option, vp);
113 if (endchar[0] && (endch == NULL || endchar[0] != *endch))
114 errx(1, "%s: %s: illegal trailing character", option, vp);
115 if (endch)
116 *endch = endchar[0];
117 return (value);
118 }
119
120 /*
121 * The value of n for the inode times (atime, ctime, and mtime) is a range,
122 * i.e. n matches from (n - 1) to n 24 hour periods. This interacts with
123 * -n, such that "-mtime -1" would be less than 0 days, which isn't what the
124 * user wanted. Correct so that -1 is "less than 1".
125 */
126 #define TIME_CORRECT(p, ttype) \
127 if ((p)->type == ttype && (p)->flags == F_LESSTHAN) \
128 ++((p)->t_data);
129
130 /*
131 * -atime n functions --
132 *
133 * True if the difference between the file access time and the
134 * current time is n 24 hour periods.
135 */
136 int
137 f_atime(plan, entry)
138 PLAN *plan;
139 FTSENT *entry;
140 {
141 extern time_t now;
142
143 COMPARE((now - entry->fts_statp->st_atime +
144 SECSPERDAY - 1) / SECSPERDAY, plan->t_data);
145 }
146
147 PLAN *
148 c_atime(arg)
149 char *arg;
150 {
151 PLAN *new;
152
153 ftsoptions &= ~FTS_NOSTAT;
154
155 new = palloc(N_ATIME, f_atime);
156 new->t_data = find_parsenum(new, "-atime", arg, NULL);
157 TIME_CORRECT(new, N_ATIME);
158 return (new);
159 }
160 /*
161 * -ctime n functions --
162 *
163 * True if the difference between the last change of file
164 * status information and the current time is n 24 hour periods.
165 */
166 int
167 f_ctime(plan, entry)
168 PLAN *plan;
169 FTSENT *entry;
170 {
171 extern time_t now;
172
173 COMPARE((now - entry->fts_statp->st_ctime +
174 SECSPERDAY - 1) / SECSPERDAY, plan->t_data);
175 }
176
177 PLAN *
178 c_ctime(arg)
179 char *arg;
180 {
181 PLAN *new;
182
183 ftsoptions &= ~FTS_NOSTAT;
184
185 new = palloc(N_CTIME, f_ctime);
186 new->t_data = find_parsenum(new, "-ctime", arg, NULL);
187 TIME_CORRECT(new, N_CTIME);
188 return (new);
189 }
190
191 /*
192 * -depth functions --
193 *
194 * Always true, causes descent of the directory hierarchy to be done
195 * so that all entries in a directory are acted on before the directory
196 * itself.
197 */
198 int
199 f_always_true(plan, entry)
200 PLAN *plan;
201 FTSENT *entry;
202 {
203 return (1);
204 }
205
206 PLAN *
207 c_depth()
208 {
209 isdepth = 1;
210
211 return (palloc(N_DEPTH, f_always_true));
212 }
213
214 /*
215 * [-exec | -ok] utility [arg ... ] ; functions --
216 *
217 * True if the executed utility returns a zero value as exit status.
218 * The end of the primary expression is delimited by a semicolon. If
219 * "{}" occurs anywhere, it gets replaced by the current pathname.
220 * The current directory for the execution of utility is the same as
221 * the current directory when the find utility was started.
222 *
223 * The primary -ok is different in that it requests affirmation of the
224 * user before executing the utility.
225 */
226 int
227 f_exec(plan, entry)
228 register PLAN *plan;
229 FTSENT *entry;
230 {
231 extern int dotfd;
232 register int cnt;
233 pid_t pid;
234 int status;
235
236 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
237 if (plan->e_len[cnt])
238 brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
239 entry->fts_path, plan->e_len[cnt]);
240
241 if (plan->flags == F_NEEDOK && !queryuser(plan->e_argv))
242 return (0);
243
244 switch (pid = vfork()) {
245 case -1:
246 err(1, "fork");
247 /* NOTREACHED */
248 case 0:
249 if (fchdir(dotfd)) {
250 warn("chdir");
251 _exit(1);
252 }
253 execvp(plan->e_argv[0], plan->e_argv);
254 warn("%s", plan->e_argv[0]);
255 _exit(1);
256 }
257 pid = waitpid(pid, &status, 0);
258 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
259 }
260
261 /*
262 * c_exec --
263 * build three parallel arrays, one with pointers to the strings passed
264 * on the command line, one with (possibly duplicated) pointers to the
265 * argv array, and one with integer values that are lengths of the
266 * strings, but also flags meaning that the string has to be massaged.
267 */
268 PLAN *
269 c_exec(argvp, isok)
270 char ***argvp;
271 int isok;
272 {
273 PLAN *new; /* node returned */
274 register int cnt;
275 register char **argv, **ap, *p;
276
277 isoutput = 1;
278
279 new = palloc(N_EXEC, f_exec);
280 if (isok)
281 new->flags = F_NEEDOK;
282
283 for (ap = argv = *argvp;; ++ap) {
284 if (!*ap)
285 errx(1,
286 "%s: no terminating \";\"", isok ? "-ok" : "-exec");
287 if (**ap == ';')
288 break;
289 }
290
291 cnt = ap - *argvp + 1;
292 new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
293 new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
294 new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
295
296 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
297 new->e_orig[cnt] = *argv;
298 for (p = *argv; *p; ++p)
299 if (p[0] == '{' && p[1] == '}') {
300 new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
301 new->e_len[cnt] = MAXPATHLEN;
302 break;
303 }
304 if (!*p) {
305 new->e_argv[cnt] = *argv;
306 new->e_len[cnt] = 0;
307 }
308 }
309 new->e_argv[cnt] = new->e_orig[cnt] = NULL;
310
311 *argvp = argv + 1;
312 return (new);
313 }
314
315 /*
316 * -follow functions --
317 *
318 * Always true, causes symbolic links to be followed on a global
319 * basis.
320 */
321 PLAN *
322 c_follow()
323 {
324 ftsoptions &= ~FTS_PHYSICAL;
325 ftsoptions |= FTS_LOGICAL;
326
327 return (palloc(N_FOLLOW, f_always_true));
328 }
329
330 /*
331 * -fstype functions --
332 *
333 * True if the file is of a certain type.
334 */
335 int
336 f_fstype(plan, entry)
337 PLAN *plan;
338 FTSENT *entry;
339 {
340 static dev_t curdev; /* need a guaranteed illegal dev value */
341 static int first = 1;
342 struct statfs sb;
343 static short val;
344 char *p, save[2];
345
346 /* Only check when we cross mount point. */
347 if (first || curdev != entry->fts_statp->st_dev) {
348 curdev = entry->fts_statp->st_dev;
349
350 /*
351 * Statfs follows symlinks; find wants the link's file system,
352 * not where it points.
353 */
354 if (entry->fts_info == FTS_SL ||
355 entry->fts_info == FTS_SLNONE) {
356 if (p = strrchr(entry->fts_accpath, '/'))
357 ++p;
358 else
359 p = entry->fts_accpath;
360 save[0] = p[0];
361 p[0] = '.';
362 save[1] = p[1];
363 p[1] = '\0';
364
365 } else
366 p = NULL;
367
368 if (statfs(entry->fts_accpath, &sb))
369 err(1, "%s", entry->fts_accpath);
370
371 if (p) {
372 p[0] = save[0];
373 p[1] = save[1];
374 }
375
376 first = 0;
377 switch (plan->flags) {
378 case F_MTFLAG:
379 val = sb.f_flags;
380 break;
381 case F_MTTYPE:
382 val = sb.f_type;
383 break;
384 default:
385 abort();
386 }
387 }
388 switch(plan->flags) {
389 case F_MTFLAG:
390 return (val & plan->mt_data);
391 case F_MTTYPE:
392 return (val == plan->mt_data);
393 default:
394 abort();
395 }
396 }
397
398 PLAN *
399 c_fstype(arg)
400 char *arg;
401 {
402 register PLAN *new;
403
404 ftsoptions &= ~FTS_NOSTAT;
405
406 new = palloc(N_FSTYPE, f_fstype);
407 switch (*arg) {
408 case 'f':
409 #ifdef MOUNT_FDESC
410 if (!strcmp(arg, "fdesc")) {
411 new->flags = F_MTTYPE;
412 new->mt_data = MOUNT_FDESC;
413 return (new);
414 }
415 #endif
416 break;
417 case 'i':
418 #ifdef MOUNT_ISOFS
419 if (!strcmp(arg, "isofs")) {
420 new->flags = F_MTTYPE;
421 new->mt_data = MOUNT_ISOFS;
422 return (new);
423 }
424 #endif
425 break;
426 case 'k':
427 #ifdef MOUNT_KERNFS
428 if (!strcmp(arg, "kernfs")) {
429 new->flags = F_MTTYPE;
430 new->mt_data = MOUNT_KERNFS;
431 return (new);
432 }
433 #endif
434 break;
435 case 'l':
436 if (!strcmp(arg, "local")) {
437 new->flags = F_MTFLAG;
438 new->mt_data = MNT_LOCAL;
439 return (new);
440 }
441 break;
442 case 'm':
443 if (!strcmp(arg, "mfs")) {
444 new->flags = F_MTTYPE;
445 new->mt_data = MOUNT_MFS;
446 return (new);
447 }
448 #ifdef MOUNT_MSDOS
449 if (!strcmp(arg, "msdos")) {
450 new->flags = F_MTTYPE;
451 new->mt_data = MOUNT_MSDOS;
452 }
453 #endif
454 break;
455 case 'n':
456 if (!strcmp(arg, "nfs")) {
457 new->flags = F_MTTYPE;
458 new->mt_data = MOUNT_NFS;
459 return (new);
460 }
461 break;
462 case 'p':
463 #ifdef MOUNT_PC
464 if (!strcmp(arg, "pc")) {
465 new->flags = F_MTTYPE;
466 new->mt_data = MOUNT_PC;
467 return (new);
468 }
469 #endif
470 break;
471 case 'r':
472 if (!strcmp(arg, "rdonly")) {
473 new->flags = F_MTFLAG;
474 new->mt_data = MNT_RDONLY;
475 return (new);
476 }
477 break;
478 case 'u':
479 if (!strcmp(arg, "ufs")) {
480 new->flags = F_MTTYPE;
481 new->mt_data = MOUNT_UFS;
482 return (new);
483 }
484 break;
485 }
486 errx(1, "%s: unknown file type", arg);
487 /* NOTREACHED */
488 }
489
490 /*
491 * -group gname functions --
492 *
493 * True if the file belongs to the group gname. If gname is numeric and
494 * an equivalent of the getgrnam() function does not return a valid group
495 * name, gname is taken as a group ID.
496 */
497 int
498 f_group(plan, entry)
499 PLAN *plan;
500 FTSENT *entry;
501 {
502 return (entry->fts_statp->st_gid == plan->g_data);
503 }
504
505 PLAN *
506 c_group(gname)
507 char *gname;
508 {
509 PLAN *new;
510 struct group *g;
511 gid_t gid;
512
513 ftsoptions &= ~FTS_NOSTAT;
514
515 g = getgrnam(gname);
516 if (g == NULL) {
517 gid = atoi(gname);
518 if (gid == 0 && gname[0] != '0')
519 errx(1, "-group: %s: no such group", gname);
520 } else
521 gid = g->gr_gid;
522
523 new = palloc(N_GROUP, f_group);
524 new->g_data = gid;
525 return (new);
526 }
527
528 /*
529 * -inum n functions --
530 *
531 * True if the file has inode # n.
532 */
533 int
534 f_inum(plan, entry)
535 PLAN *plan;
536 FTSENT *entry;
537 {
538 COMPARE(entry->fts_statp->st_ino, plan->i_data);
539 }
540
541 PLAN *
542 c_inum(arg)
543 char *arg;
544 {
545 PLAN *new;
546
547 ftsoptions &= ~FTS_NOSTAT;
548
549 new = palloc(N_INUM, f_inum);
550 new->i_data = find_parsenum(new, "-inum", arg, NULL);
551 return (new);
552 }
553
554 /*
555 * -links n functions --
556 *
557 * True if the file has n links.
558 */
559 int
560 f_links(plan, entry)
561 PLAN *plan;
562 FTSENT *entry;
563 {
564 COMPARE(entry->fts_statp->st_nlink, plan->l_data);
565 }
566
567 PLAN *
568 c_links(arg)
569 char *arg;
570 {
571 PLAN *new;
572
573 ftsoptions &= ~FTS_NOSTAT;
574
575 new = palloc(N_LINKS, f_links);
576 new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL);
577 return (new);
578 }
579
580 /*
581 * -ls functions --
582 *
583 * Always true - prints the current entry to stdout in "ls" format.
584 */
585 int
586 f_ls(plan, entry)
587 PLAN *plan;
588 FTSENT *entry;
589 {
590 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
591 return (1);
592 }
593
594 PLAN *
595 c_ls()
596 {
597 ftsoptions &= ~FTS_NOSTAT;
598 isoutput = 1;
599
600 return (palloc(N_LS, f_ls));
601 }
602
603 /*
604 * -mtime n functions --
605 *
606 * True if the difference between the file modification time and the
607 * current time is n 24 hour periods.
608 */
609 int
610 f_mtime(plan, entry)
611 PLAN *plan;
612 FTSENT *entry;
613 {
614 extern time_t now;
615
616 COMPARE((now - entry->fts_statp->st_mtime + SECSPERDAY - 1) /
617 SECSPERDAY, plan->t_data);
618 }
619
620 PLAN *
621 c_mtime(arg)
622 char *arg;
623 {
624 PLAN *new;
625
626 ftsoptions &= ~FTS_NOSTAT;
627
628 new = palloc(N_MTIME, f_mtime);
629 new->t_data = find_parsenum(new, "-mtime", arg, NULL);
630 TIME_CORRECT(new, N_MTIME);
631 return (new);
632 }
633
634 /*
635 * -name functions --
636 *
637 * True if the basename of the filename being examined
638 * matches pattern using Pattern Matching Notation S3.14
639 */
640 int
641 f_name(plan, entry)
642 PLAN *plan;
643 FTSENT *entry;
644 {
645 return (!fnmatch(plan->c_data, entry->fts_name, 0));
646 }
647
648 PLAN *
649 c_name(pattern)
650 char *pattern;
651 {
652 PLAN *new;
653
654 new = palloc(N_NAME, f_name);
655 new->c_data = pattern;
656 return (new);
657 }
658
659 /*
660 * -newer file functions --
661 *
662 * True if the current file has been modified more recently
663 * then the modification time of the file named by the pathname
664 * file.
665 */
666 int
667 f_newer(plan, entry)
668 PLAN *plan;
669 FTSENT *entry;
670 {
671 return (entry->fts_statp->st_mtime > plan->t_data);
672 }
673
674 PLAN *
675 c_newer(filename)
676 char *filename;
677 {
678 PLAN *new;
679 struct stat sb;
680
681 ftsoptions &= ~FTS_NOSTAT;
682
683 if (stat(filename, &sb))
684 err(1, "%s", filename);
685 new = palloc(N_NEWER, f_newer);
686 new->t_data = sb.st_mtime;
687 return (new);
688 }
689
690 /*
691 * -nogroup functions --
692 *
693 * True if file belongs to a user ID for which the equivalent
694 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
695 */
696 int
697 f_nogroup(plan, entry)
698 PLAN *plan;
699 FTSENT *entry;
700 {
701 char *group_from_gid();
702
703 return (group_from_gid(entry->fts_statp->st_gid, 1) ? 1 : 0);
704 }
705
706 PLAN *
707 c_nogroup()
708 {
709 ftsoptions &= ~FTS_NOSTAT;
710
711 return (palloc(N_NOGROUP, f_nogroup));
712 }
713
714 /*
715 * -nouser functions --
716 *
717 * True if file belongs to a user ID for which the equivalent
718 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
719 */
720 int
721 f_nouser(plan, entry)
722 PLAN *plan;
723 FTSENT *entry;
724 {
725 char *user_from_uid();
726
727 return (user_from_uid(entry->fts_statp->st_uid, 1) ? 1 : 0);
728 }
729
730 PLAN *
731 c_nouser()
732 {
733 ftsoptions &= ~FTS_NOSTAT;
734
735 return (palloc(N_NOUSER, f_nouser));
736 }
737
738 /*
739 * -path functions --
740 *
741 * True if the path of the filename being examined
742 * matches pattern using Pattern Matching Notation S3.14
743 */
744 int
745 f_path(plan, entry)
746 PLAN *plan;
747 FTSENT *entry;
748 {
749 return (!fnmatch(plan->c_data, entry->fts_path, 0));
750 }
751
752 PLAN *
753 c_path(pattern)
754 char *pattern;
755 {
756 PLAN *new;
757
758 new = palloc(N_NAME, f_path);
759 new->c_data = pattern;
760 return (new);
761 }
762
763 /*
764 * -perm functions --
765 *
766 * The mode argument is used to represent file mode bits. If it starts
767 * with a leading digit, it's treated as an octal mode, otherwise as a
768 * symbolic mode.
769 */
770 int
771 f_perm(plan, entry)
772 PLAN *plan;
773 FTSENT *entry;
774 {
775 mode_t mode;
776
777 mode = entry->fts_statp->st_mode &
778 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
779 if (plan->flags == F_ATLEAST)
780 return ((plan->m_data | mode) == mode);
781 else
782 return (mode == plan->m_data);
783 /* NOTREACHED */
784 }
785
786 PLAN *
787 c_perm(perm)
788 char *perm;
789 {
790 PLAN *new;
791 mode_t *set;
792
793 ftsoptions &= ~FTS_NOSTAT;
794
795 new = palloc(N_PERM, f_perm);
796
797 if (*perm == '-') {
798 new->flags = F_ATLEAST;
799 ++perm;
800 }
801
802 if ((set = setmode(perm)) == NULL)
803 err(1, "-perm: %s: illegal mode string", perm);
804
805 new->m_data = getmode(set, 0);
806 return (new);
807 }
808
809 /*
810 * -print functions --
811 *
812 * Always true, causes the current pathame to be written to
813 * standard output.
814 */
815 int
816 f_print(plan, entry)
817 PLAN *plan;
818 FTSENT *entry;
819 {
820 (void)printf("%s\n", entry->fts_path);
821 return(1);
822 }
823
824 /* ARGSUSED */
825 f_print0(plan, entry)
826 PLAN *plan;
827 FTSENT *entry;
828 {
829 (void)fputs(entry->fts_path, stdout);
830 (void)fputc('\0', stdout);
831 return(1);
832 }
833
834 PLAN *
835 c_print()
836 {
837 isoutput = 1;
838
839 return(palloc(N_PRINT, f_print));
840 }
841
842 PLAN *
843 c_print0()
844 {
845 isoutput = 1;
846
847 return(palloc(N_PRINT0, f_print0));
848 }
849
850 /*
851 * -prune functions --
852 *
853 * Prune a portion of the hierarchy.
854 */
855 int
856 f_prune(plan, entry)
857 PLAN *plan;
858 FTSENT *entry;
859 {
860 extern FTS *tree;
861
862 if (fts_set(tree, entry, FTS_SKIP))
863 err(1, "%s", entry->fts_path);
864 return (1);
865 }
866
867 PLAN *
868 c_prune()
869 {
870 return (palloc(N_PRUNE, f_prune));
871 }
872
873 /*
874 * -size n[c] functions --
875 *
876 * True if the file size in bytes, divided by an implementation defined
877 * value and rounded up to the next integer, is n. If n is followed by
878 * a c, the size is in bytes.
879 */
880 #define FIND_SIZE 512
881 static int divsize = 1;
882
883 int
884 f_size(plan, entry)
885 PLAN *plan;
886 FTSENT *entry;
887 {
888 off_t size;
889
890 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
891 FIND_SIZE : entry->fts_statp->st_size;
892 COMPARE(size, plan->o_data);
893 }
894
895 PLAN *
896 c_size(arg)
897 char *arg;
898 {
899 PLAN *new;
900 char endch;
901
902 ftsoptions &= ~FTS_NOSTAT;
903
904 new = palloc(N_SIZE, f_size);
905 endch = 'c';
906 new->o_data = find_parsenum(new, "-size", arg, &endch);
907 if (endch == 'c')
908 divsize = 0;
909 return (new);
910 }
911
912 /*
913 * -type c functions --
914 *
915 * True if the type of the file is c, where c is b, c, d, p, or f for
916 * block special file, character special file, directory, FIFO, or
917 * regular file, respectively.
918 */
919 int
920 f_type(plan, entry)
921 PLAN *plan;
922 FTSENT *entry;
923 {
924 return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data);
925 }
926
927 PLAN *
928 c_type(typestring)
929 char *typestring;
930 {
931 PLAN *new;
932 mode_t mask;
933
934 ftsoptions &= ~FTS_NOSTAT;
935
936 switch (typestring[0]) {
937 case 'b':
938 mask = S_IFBLK;
939 break;
940 case 'c':
941 mask = S_IFCHR;
942 break;
943 case 'd':
944 mask = S_IFDIR;
945 break;
946 case 'f':
947 mask = S_IFREG;
948 break;
949 case 'l':
950 mask = S_IFLNK;
951 break;
952 case 'p':
953 mask = S_IFIFO;
954 break;
955 case 's':
956 mask = S_IFSOCK;
957 break;
958 default:
959 errx(1, "-type: %s: unknown type", typestring);
960 }
961
962 new = palloc(N_TYPE, f_type);
963 new->m_data = mask;
964 return (new);
965 }
966
967 /*
968 * -user uname functions --
969 *
970 * True if the file belongs to the user uname. If uname is numeric and
971 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
972 * return a valid user name, uname is taken as a user ID.
973 */
974 int
975 f_user(plan, entry)
976 PLAN *plan;
977 FTSENT *entry;
978 {
979 return (entry->fts_statp->st_uid == plan->u_data);
980 }
981
982 PLAN *
983 c_user(username)
984 char *username;
985 {
986 PLAN *new;
987 struct passwd *p;
988 uid_t uid;
989
990 ftsoptions &= ~FTS_NOSTAT;
991
992 p = getpwnam(username);
993 if (p == NULL) {
994 uid = atoi(username);
995 if (uid == 0 && username[0] != '0')
996 errx(1, "-user: %s: no such user", username);
997 } else
998 uid = p->pw_uid;
999
1000 new = palloc(N_USER, f_user);
1001 new->u_data = uid;
1002 return (new);
1003 }
1004
1005 /*
1006 * -xdev functions --
1007 *
1008 * Always true, causes find not to decend past directories that have a
1009 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1010 */
1011 PLAN *
1012 c_xdev()
1013 {
1014 ftsoptions |= FTS_XDEV;
1015
1016 return (palloc(N_XDEV, f_always_true));
1017 }
1018
1019 /*
1020 * ( expression ) functions --
1021 *
1022 * True if expression is true.
1023 */
1024 int
1025 f_expr(plan, entry)
1026 PLAN *plan;
1027 FTSENT *entry;
1028 {
1029 register PLAN *p;
1030 register int state;
1031
1032 for (p = plan->p_data[0];
1033 p && (state = (p->eval)(p, entry)); p = p->next);
1034 return (state);
1035 }
1036
1037 /*
1038 * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers. They are
1039 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1040 * to a N_EXPR node containing the expression and the ')' node is discarded.
1041 */
1042 PLAN *
1043 c_openparen()
1044 {
1045 return (palloc(N_OPENPAREN, (int (*)())-1));
1046 }
1047
1048 PLAN *
1049 c_closeparen()
1050 {
1051 return (palloc(N_CLOSEPAREN, (int (*)())-1));
1052 }
1053
1054 /*
1055 * ! expression functions --
1056 *
1057 * Negation of a primary; the unary NOT operator.
1058 */
1059 int
1060 f_not(plan, entry)
1061 PLAN *plan;
1062 FTSENT *entry;
1063 {
1064 register PLAN *p;
1065 register int state;
1066
1067 for (p = plan->p_data[0];
1068 p && (state = (p->eval)(p, entry)); p = p->next);
1069 return (!state);
1070 }
1071
1072 PLAN *
1073 c_not()
1074 {
1075 return (palloc(N_NOT, f_not));
1076 }
1077
1078 /*
1079 * expression -o expression functions --
1080 *
1081 * Alternation of primaries; the OR operator. The second expression is
1082 * not evaluated if the first expression is true.
1083 */
1084 int
1085 f_or(plan, entry)
1086 PLAN *plan;
1087 FTSENT *entry;
1088 {
1089 register PLAN *p;
1090 register int state;
1091
1092 for (p = plan->p_data[0];
1093 p && (state = (p->eval)(p, entry)); p = p->next);
1094
1095 if (state)
1096 return (1);
1097
1098 for (p = plan->p_data[1];
1099 p && (state = (p->eval)(p, entry)); p = p->next);
1100 return (state);
1101 }
1102
1103 PLAN *
1104 c_or()
1105 {
1106 return (palloc(N_OR, f_or));
1107 }
1108
1109 static PLAN *
1110 palloc(t, f)
1111 enum ntype t;
1112 int (*f) __P((PLAN *, FTSENT *));
1113 {
1114 PLAN *new;
1115
1116 if (new = malloc(sizeof(PLAN))) {
1117 new->type = t;
1118 new->eval = f;
1119 new->flags = 0;
1120 new->next = NULL;
1121 return (new);
1122 }
1123 err(1, NULL);
1124 /* NOTREACHED */
1125 }
1126