]> git.cameronkatri.com Git - apple_cmds.git/blob - shell_cmds/find/function.c
shell_cmds: use libiosexec
[apple_cmds.git] / shell_cmds / find / function.c
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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #ifndef lint
34 #if 0
35 static const char sccsid[] = "@(#)function.c 8.10 (Berkeley) 5/4/95";
36 #endif
37 #endif /* not lint */
38
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/usr.bin/find/function.c,v 1.71 2011/06/13 05:22:07 avatar Exp $");
41
42 #include <sys/param.h>
43 #include <sys/ucred.h>
44 #include <sys/stat.h>
45 #include <sys/types.h>
46 #include <sys/acl.h>
47 #include <sys/wait.h>
48 #include <sys/mount.h>
49
50 #include <dirent.h>
51 #include <err.h>
52 #include <errno.h>
53 #include <fnmatch.h>
54 #include <fts.h>
55 #include <grp.h>
56 #include <limits.h>
57 #include <pwd.h>
58 #include <regex.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63 #include <ctype.h>
64
65 #ifdef __APPLE__
66 #include <sys/sysctl.h>
67 #include <sys/xattr.h>
68 #include <libgen.h>
69 #include <get_compat.h>
70 #else
71 #define COMPAT_MODE(func, mode) 1
72 #endif
73
74 #include <libiosexec.h>
75
76 #include "find.h"
77
78 static PLAN *palloc(OPTION *);
79 static long long find_parsenum(PLAN *, const char *, char *, char *);
80 static long long find_parsetime(PLAN *, const char *, char *);
81 static char *nextarg(OPTION *, char ***);
82
83 extern char **environ;
84
85 static PLAN *lastexecplus = NULL;
86 int execplus_error;
87
88 #define COMPARE(a, b) do { \
89 switch (plan->flags & F_ELG_MASK) { \
90 case F_EQUAL: \
91 return (a == b); \
92 case F_LESSTHAN: \
93 return (a < b); \
94 case F_GREATER: \
95 return (a > b); \
96 default: \
97 abort(); \
98 } \
99 } while(0)
100
101 static PLAN *
102 palloc(OPTION *option)
103 {
104 PLAN *new;
105
106 if ((new = malloc(sizeof(PLAN))) == NULL)
107 err(1, NULL);
108 new->execute = option->execute;
109 new->flags = option->flags;
110 new->next = NULL;
111 return new;
112 }
113
114 /*
115 * find_parsenum --
116 * Parse a string of the form [+-]# and return the value.
117 */
118 static long long
119 find_parsenum(PLAN *plan, const char *option, char *vp, char *endch)
120 {
121 long long value;
122 char *endchar, *str; /* Pointer to character ending conversion. */
123
124 /* Determine comparison from leading + or -. */
125 str = vp;
126 switch (*str) {
127 case '+':
128 ++str;
129 plan->flags |= F_GREATER;
130 break;
131 case '-':
132 ++str;
133 plan->flags |= F_LESSTHAN;
134 break;
135 default:
136 plan->flags |= F_EQUAL;
137 break;
138 }
139
140 /*
141 * Convert the string with strtoq(). Note, if strtoq() returns zero
142 * and endchar points to the beginning of the string we know we have
143 * a syntax error.
144 */
145 value = strtoq(str, &endchar, 10);
146 if (value == 0 && endchar == str)
147 errx(1, "%s: %s: illegal numeric value", option, vp);
148 if (endchar[0] && endch == NULL)
149 errx(1, "%s: %s: illegal trailing character", option, vp);
150 if (endch)
151 *endch = endchar[0];
152 return value;
153 }
154
155 /*
156 * find_parsetime --
157 * Parse a string of the form [+-]([0-9]+[smhdw]?)+ and return the value.
158 */
159 static long long
160 find_parsetime(PLAN *plan, const char *option, char *vp)
161 {
162 long long secs, value;
163 char *str, *unit; /* Pointer to character ending conversion. */
164
165 /* Determine comparison from leading + or -. */
166 str = vp;
167 switch (*str) {
168 case '+':
169 ++str;
170 plan->flags |= F_GREATER;
171 break;
172 case '-':
173 ++str;
174 plan->flags |= F_LESSTHAN;
175 break;
176 default:
177 plan->flags |= F_EQUAL;
178 break;
179 }
180
181 value = strtoq(str, &unit, 10);
182 if (value == 0 && unit == str) {
183 errx(1, "%s: %s: illegal time value", option, vp);
184 /* NOTREACHED */
185 }
186 if (*unit == '\0')
187 return value;
188
189 /* Units syntax. */
190 secs = 0;
191 for (;;) {
192 switch(*unit) {
193 case 's': /* seconds */
194 secs += value;
195 break;
196 case 'm': /* minutes */
197 secs += value * 60;
198 break;
199 case 'h': /* hours */
200 secs += value * 3600;
201 break;
202 case 'd': /* days */
203 secs += value * 86400;
204 break;
205 case 'w': /* weeks */
206 secs += value * 604800;
207 break;
208 default:
209 errx(1, "%s: %s: bad unit '%c'", option, vp, *unit);
210 /* NOTREACHED */
211 }
212 str = unit + 1;
213 if (*str == '\0') /* EOS */
214 break;
215 value = strtoq(str, &unit, 10);
216 if (value == 0 && unit == str) {
217 errx(1, "%s: %s: illegal time value", option, vp);
218 /* NOTREACHED */
219 }
220 if (*unit == '\0') {
221 errx(1, "%s: %s: missing trailing unit", option, vp);
222 /* NOTREACHED */
223 }
224 }
225 plan->flags |= F_EXACTTIME;
226 return secs;
227 }
228
229 /*
230 * nextarg --
231 * Check that another argument still exists, return a pointer to it,
232 * and increment the argument vector pointer.
233 */
234 static char *
235 nextarg(OPTION *option, char ***argvp)
236 {
237 char *arg;
238
239 if ((arg = **argvp) == 0)
240 errx(1, "%s: requires additional arguments", option->name);
241 (*argvp)++;
242 return arg;
243 } /* nextarg() */
244
245 /*
246 * The value of n for the inode times (atime, birthtime, ctime, mtime) is a
247 * range, i.e. n matches from (n - 1) to n 24 hour periods. This interacts
248 * with -n, such that "-mtime -1" would be less than 0 days, which isn't what
249 * the user wanted. Correct so that -1 is "less than 1".
250 */
251 #define TIME_CORRECT(p) \
252 if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \
253 ++((p)->t_data);
254
255 /*
256 * -[acm]min n functions --
257 *
258 * True if the difference between the
259 * file access time (-amin)
260 * file birth time (-Bmin)
261 * last change of file status information (-cmin)
262 * file modification time (-mmin)
263 * and the current time is n min periods.
264 */
265 int
266 f_Xmin(PLAN *plan, FTSENT *entry)
267 {
268 if (plan->flags & F_TIME_C) {
269 COMPARE((now - entry->fts_statp->st_ctime +
270 60 - 1) / 60, plan->t_data);
271 } else if (plan->flags & F_TIME_A) {
272 COMPARE((now - entry->fts_statp->st_atime +
273 60 - 1) / 60, plan->t_data);
274 } else if (plan->flags & F_TIME_B) {
275 COMPARE((now - entry->fts_statp->st_birthtime +
276 60 - 1) / 60, plan->t_data);
277 } else {
278 COMPARE((now - entry->fts_statp->st_mtime +
279 60 - 1) / 60, plan->t_data);
280 }
281 }
282
283 PLAN *
284 c_Xmin(OPTION *option, char ***argvp)
285 {
286 char *nmins;
287 PLAN *new;
288
289 nmins = nextarg(option, argvp);
290 ftsoptions &= ~FTS_NOSTAT;
291
292 new = palloc(option);
293 new->t_data = find_parsenum(new, option->name, nmins, NULL);
294 TIME_CORRECT(new);
295 return new;
296 }
297
298 /*
299 * -[acm]time n functions --
300 *
301 * True if the difference between the
302 * file access time (-atime)
303 * file birth time (-Btime)
304 * last change of file status information (-ctime)
305 * file modification time (-mtime)
306 * and the current time is n 24 hour periods.
307 */
308
309 int
310 f_Xtime(PLAN *plan, FTSENT *entry)
311 {
312 time_t xtime;
313
314 if (plan->flags & F_TIME_A)
315 xtime = entry->fts_statp->st_atime;
316 else if (plan->flags & F_TIME_B)
317 xtime = entry->fts_statp->st_birthtime;
318 else if (plan->flags & F_TIME_C)
319 xtime = entry->fts_statp->st_ctime;
320 else
321 xtime = entry->fts_statp->st_mtime;
322
323 if (plan->flags & F_EXACTTIME)
324 COMPARE(now - xtime, plan->t_data);
325 else
326 COMPARE((now - xtime + (COMPAT_MODE("bin/find", "unix2003") ? 0 : 86400 - 1)) / 86400, plan->t_data);
327 }
328
329 PLAN *
330 c_Xtime(OPTION *option, char ***argvp)
331 {
332 char *value;
333 PLAN *new;
334
335 value = nextarg(option, argvp);
336 ftsoptions &= ~FTS_NOSTAT;
337
338 new = palloc(option);
339 new->t_data = find_parsetime(new, option->name, value);
340 if (!(new->flags & F_EXACTTIME) && !COMPAT_MODE("bin/find", "unix2003"))
341 TIME_CORRECT(new);
342 return new;
343 }
344
345 /*
346 * -maxdepth/-mindepth n functions --
347 *
348 * Does the same as -prune if the level of the current file is
349 * greater/less than the specified maximum/minimum depth.
350 *
351 * Note that -maxdepth and -mindepth are handled specially in
352 * find_execute() so their f_* functions are set to f_always_true().
353 */
354 PLAN *
355 c_mXXdepth(OPTION *option, char ***argvp)
356 {
357 char *dstr;
358 PLAN *new;
359
360 dstr = nextarg(option, argvp);
361 if (dstr[0] == '-')
362 /* all other errors handled by find_parsenum() */
363 errx(1, "%s: %s: value must be positive", option->name, dstr);
364
365 new = palloc(option);
366 if (option->flags & F_MAXDEPTH)
367 maxdepth = find_parsenum(new, option->name, dstr, NULL);
368 else
369 mindepth = find_parsenum(new, option->name, dstr, NULL);
370 return new;
371 }
372
373 /*
374 * -acl function --
375 *
376 * Show files with EXTENDED ACL attributes.
377 */
378 #ifdef __APPLE__
379 int
380 f_acl(PLAN *plan __unused, FTSENT *entry)
381 {
382 acl_t facl;
383 int match;
384 acl_entry_t ae;
385
386 match = 0;
387 if ((facl = acl_get_link_np(entry->fts_accpath, ACL_TYPE_EXTENDED)) != NULL) {
388 if (acl_get_entry(facl, ACL_FIRST_ENTRY, &ae) == 0) {
389 match = 1;
390 }
391 acl_free(facl);
392 }
393 return match;
394 }
395 #else /* !__APPLE__ */
396 int
397 f_acl(PLAN *plan __unused, FTSENT *entry)
398 {
399 acl_t facl;
400 acl_type_t acl_type;
401 int acl_supported = 0, ret, trivial;
402
403 if (S_ISLNK(entry->fts_statp->st_mode))
404 return 0;
405 ret = pathconf(entry->fts_accpath, _PC_ACL_NFS4);
406 if (ret > 0) {
407 acl_supported = 1;
408 acl_type = ACL_TYPE_NFS4;
409 } else if (ret < 0 && errno != EINVAL) {
410 warn("%s", entry->fts_accpath);
411 return (0);
412 }
413 if (acl_supported == 0) {
414 ret = pathconf(entry->fts_accpath, _PC_ACL_EXTENDED);
415 if (ret > 0) {
416 acl_supported = 1;
417 acl_type = ACL_TYPE_ACCESS;
418 } else if (ret < 0 && errno != EINVAL) {
419 warn("%s", entry->fts_accpath);
420 return (0);
421 }
422 }
423 if (acl_supported == 0)
424 return (0);
425
426 facl = acl_get_file(entry->fts_accpath, acl_type);
427 if (facl == NULL) {
428 warn("%s", entry->fts_accpath);
429 return (0);
430 }
431 ret = acl_is_trivial_np(facl, &trivial);
432 acl_free(facl);
433 if (ret) {
434 warn("%s", entry->fts_accpath);
435 acl_free(facl);
436 return (0);
437 }
438 if (trivial)
439 return (0);
440 return (1);
441 }
442 #endif /* __APPLE__ */
443
444 PLAN *
445 c_acl(OPTION *option, char ***argvp __unused)
446 {
447 #ifndef __APPLE__
448 ftsoptions &= ~FTS_NOSTAT;
449 #endif /* !__APPLE__ */
450 return (palloc(option));
451 }
452
453 #ifdef __APPLE__
454 int
455 f_xattr(PLAN *plan __unused, FTSENT *entry)
456 {
457 ssize_t xattr;
458 int match;
459
460 match = 0;
461 xattr = listxattr(entry->fts_accpath, NULL, 0, XATTR_NOFOLLOW);
462 if (xattr > 0) {
463 match = 1;
464 }
465 return match;
466 }
467
468 int
469 f_xattrname(PLAN *plan, FTSENT *entry)
470 {
471 ssize_t xattr;
472 int match;
473
474 match = 0;
475 xattr = getxattr(entry->fts_accpath, plan->c_data, NULL, 0, 0, XATTR_NOFOLLOW);
476 if (xattr > 0) {
477 match = 1;
478 }
479 return match;
480 }
481 #endif /* __APPLE__ */
482
483 /*
484 * -delete functions --
485 *
486 * True always. Makes its best shot and continues on regardless.
487 */
488 int
489 f_delete(PLAN *plan __unused, FTSENT *entry)
490 {
491 /* ignore these from fts */
492 if (strcmp(entry->fts_accpath, ".") == 0 ||
493 strcmp(entry->fts_accpath, "..") == 0)
494 return 1;
495
496 /* sanity check */
497 if (isdepth == 0 || /* depth off */
498 (ftsoptions & FTS_NOSTAT)) /* not stat()ing */
499 errx(1, "-delete: insecure options got turned on");
500
501 if (!(ftsoptions & FTS_PHYSICAL) || /* physical off */
502 (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */
503 errx(1, "-delete: forbidden when symlinks are followed");
504
505 /* Potentially unsafe - do not accept relative paths whatsoever */
506 if (strchr(entry->fts_accpath, '/') != NULL)
507 errx(1, "-delete: %s: relative path potentially not safe",
508 entry->fts_accpath);
509
510 /* Turn off user immutable bits if running as root */
511 if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
512 !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
513 geteuid() == 0)
514 lchflags(entry->fts_accpath,
515 entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
516
517 /* rmdir directories, unlink everything else */
518 if (S_ISDIR(entry->fts_statp->st_mode)) {
519 if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
520 warn("-delete: rmdir(%s)", entry->fts_path);
521 } else {
522 if (unlink(entry->fts_accpath) < 0)
523 warn("-delete: unlink(%s)", entry->fts_path);
524 }
525
526 /* "succeed" */
527 return 1;
528 }
529
530 PLAN *
531 c_delete(OPTION *option, char ***argvp __unused)
532 {
533
534 ftsoptions &= ~FTS_NOSTAT; /* no optimise */
535 isoutput = 1; /* possible output */
536 isdepth = 1; /* -depth implied */
537
538 return palloc(option);
539 }
540
541
542 /*
543 * always_true --
544 *
545 * Always true, used for -maxdepth, -mindepth, -xdev, -follow, and -true
546 */
547 int
548 f_always_true(PLAN *plan __unused, FTSENT *entry __unused)
549 {
550 return 1;
551 }
552
553 /*
554 * -depth functions --
555 *
556 * With argument: True if the file is at level n.
557 * Without argument: Always true, causes descent of the directory hierarchy
558 * to be done so that all entries in a directory are acted on before the
559 * directory itself.
560 */
561 int
562 f_depth(PLAN *plan, FTSENT *entry)
563 {
564 if (plan->flags & F_DEPTH)
565 COMPARE(entry->fts_level, plan->d_data);
566 else
567 return 1;
568 }
569
570 PLAN *
571 c_depth(OPTION *option, char ***argvp)
572 {
573 PLAN *new;
574 char *str;
575
576 new = palloc(option);
577
578 str = **argvp;
579 if (str && !(new->flags & F_DEPTH)) {
580 /* skip leading + or - */
581 if (*str == '+' || *str == '-')
582 str++;
583 /* skip sign */
584 if (*str == '+' || *str == '-')
585 str++;
586 if (isdigit(*str))
587 new->flags |= F_DEPTH;
588 }
589
590 if (new->flags & F_DEPTH) { /* -depth n */
591 char *ndepth;
592
593 ndepth = nextarg(option, argvp);
594 new->d_data = find_parsenum(new, option->name, ndepth, NULL);
595 } else { /* -d */
596 isdepth = 1;
597 }
598
599 return new;
600 }
601
602 /*
603 * -empty functions --
604 *
605 * True if the file or directory is empty
606 */
607 int
608 f_empty(PLAN *plan __unused, FTSENT *entry)
609 {
610 if (S_ISREG(entry->fts_statp->st_mode) &&
611 entry->fts_statp->st_size == 0)
612 return 1;
613 if (S_ISDIR(entry->fts_statp->st_mode)) {
614 struct dirent *dp;
615 int empty;
616 DIR *dir;
617
618 empty = 1;
619 dir = opendir(entry->fts_accpath);
620 if (dir == NULL)
621 return 0;
622 for (dp = readdir(dir); dp; dp = readdir(dir))
623 if (dp->d_name[0] != '.' ||
624 (dp->d_name[1] != '\0' &&
625 (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
626 empty = 0;
627 break;
628 }
629 closedir(dir);
630 return empty;
631 }
632 return 0;
633 }
634
635 PLAN *
636 c_empty(OPTION *option, char ***argvp __unused)
637 {
638 ftsoptions &= ~FTS_NOSTAT;
639
640 return palloc(option);
641 }
642
643 /*
644 * [-exec | -execdir | -ok] utility [arg ... ] ; functions --
645 *
646 * True if the executed utility returns a zero value as exit status.
647 * The end of the primary expression is delimited by a semicolon. If
648 * "{}" occurs anywhere, it gets replaced by the current pathname,
649 * or, in the case of -execdir, the current basename (filename
650 * without leading directory prefix). For -exec and -ok,
651 * the current directory for the execution of utility is the same as
652 * the current directory when the find utility was started, whereas
653 * for -execdir, it is the directory the file resides in.
654 *
655 * The primary -ok differs from -exec in that it requests affirmation
656 * of the user before executing the utility.
657 */
658 int
659 f_exec(PLAN *plan, FTSENT *entry)
660 {
661 int cnt;
662 pid_t pid;
663 int status;
664 char *file;
665
666 if (entry == NULL && plan->flags & F_EXECPLUS) {
667 if (plan->e_ppos == plan->e_pbnum)
668 return (1);
669 plan->e_argv[plan->e_ppos] = NULL;
670 goto doexec;
671 }
672
673 /* XXX - if file/dir ends in '/' this will not work -- can it? */
674 if ((plan->flags & F_EXECDIR) && \
675 (file = strrchr(entry->fts_path, '/')))
676 file++;
677 else
678 file = entry->fts_path;
679
680 if (plan->flags & F_EXECPLUS) {
681 if ((plan->e_argv[plan->e_ppos] = strdup(file)) == NULL)
682 err(1, NULL);
683 plan->e_len[plan->e_ppos] = strlen(file);
684 plan->e_psize += plan->e_len[plan->e_ppos];
685 if (++plan->e_ppos < plan->e_pnummax &&
686 plan->e_psize < plan->e_psizemax)
687 return (1);
688 plan->e_argv[plan->e_ppos] = NULL;
689 } else {
690 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
691 if (plan->e_len[cnt])
692 brace_subst(plan->e_orig[cnt],
693 &plan->e_argv[cnt], file,
694 plan->e_len[cnt]);
695 }
696
697 doexec: if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv))
698 return 0;
699
700 /* make sure find output is interspersed correctly with subprocesses */
701 fflush(stdout);
702 fflush(stderr);
703
704 switch (pid = fork()) {
705 case -1:
706 err(1, "fork");
707 /* NOTREACHED */
708 case 0:
709 /* change dir back from where we started */
710 if (!(plan->flags & F_EXECDIR) && fchdir(dotfd)) {
711 warn("chdir");
712 _exit(1);
713 }
714 execvp(plan->e_argv[0], plan->e_argv);
715 warn("%s", plan->e_argv[0]);
716 _exit(1);
717 }
718 if (plan->flags & F_EXECPLUS) {
719 while (--plan->e_ppos >= plan->e_pbnum)
720 free(plan->e_argv[plan->e_ppos]);
721 plan->e_ppos = plan->e_pbnum;
722 plan->e_psize = plan->e_pbsize;
723 }
724 pid = waitpid(pid, &status, 0);
725 if (plan->flags & F_EXECPLUS && WIFEXITED(status) && WEXITSTATUS(status) && !execplus_error) {
726 /* Test 140 (8907531, 10656525) */
727 execplus_error = WEXITSTATUS(status);
728 }
729 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
730 }
731
732 /*
733 * c_exec, c_execdir, c_ok --
734 * build three parallel arrays, one with pointers to the strings passed
735 * on the command line, one with (possibly duplicated) pointers to the
736 * argv array, and one with integer values that are lengths of the
737 * strings, but also flags meaning that the string has to be massaged.
738 */
739 PLAN *
740 c_exec(OPTION *option, char ***argvp)
741 {
742 PLAN *new; /* node returned */
743 long argmax;
744 int cnt, i;
745 char **argv, **ap, **ep, *p;
746
747 /* XXX - was in c_execdir, but seems unnecessary!?
748 ftsoptions &= ~FTS_NOSTAT;
749 */
750 isoutput = 1;
751
752 /* XXX - this is a change from the previous coding */
753 new = palloc(option);
754
755 for (ap = argv = *argvp;; ++ap) {
756 if (!*ap)
757 errx(1,
758 "%s: no terminating \";\" or \"+\"", option->name);
759 if (**ap == ';')
760 break;
761 if (**ap == '+' && ap != argv && strcmp(*(ap - 1), "{}") == 0) {
762 new->flags |= F_EXECPLUS;
763 break;
764 }
765 }
766
767 if (ap == argv)
768 errx(1, "%s: no command specified", option->name);
769
770 cnt = ap - *argvp + 1;
771 if (new->flags & F_EXECPLUS) {
772 new->e_ppos = new->e_pbnum = cnt - 2;
773 if ((argmax = sysconf(_SC_ARG_MAX)) == -1) {
774 warn("sysconf(_SC_ARG_MAX)");
775 argmax = _POSIX_ARG_MAX;
776 }
777 argmax -= 1024;
778 for (ep = environ; *ep != NULL; ep++)
779 argmax -= strlen(*ep) + 1 + sizeof(*ep);
780 argmax -= 1 + sizeof(*ep);
781 new->e_pnummax = argmax / 16;
782 argmax -= sizeof(char *) * new->e_pnummax;
783 if (argmax <= 0)
784 errx(1, "no space for arguments");
785 new->e_psizemax = argmax;
786 new->e_pbsize = 0;
787 cnt += new->e_pnummax + 1;
788 new->e_next = lastexecplus;
789 lastexecplus = new;
790 }
791 if ((new->e_argv = malloc(cnt * sizeof(char *))) == NULL)
792 err(1, NULL);
793 if ((new->e_orig = malloc(cnt * sizeof(char *))) == NULL)
794 err(1, NULL);
795 if ((new->e_len = malloc(cnt * sizeof(int))) == NULL)
796 err(1, NULL);
797
798 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
799 new->e_orig[cnt] = *argv;
800 if (new->flags & F_EXECPLUS)
801 new->e_pbsize += strlen(*argv) + 1;
802 for (p = *argv; *p; ++p)
803 if (!(new->flags & F_EXECPLUS) && p[0] == '{' &&
804 p[1] == '}') {
805 if ((new->e_argv[cnt] =
806 malloc(MAXPATHLEN)) == NULL)
807 err(1, NULL);
808 new->e_len[cnt] = MAXPATHLEN;
809 break;
810 }
811 if (!*p) {
812 new->e_argv[cnt] = *argv;
813 new->e_len[cnt] = 0;
814 }
815 }
816 if (new->flags & F_EXECPLUS) {
817 new->e_psize = new->e_pbsize;
818 cnt--;
819 for (i = 0; i < new->e_pnummax; i++) {
820 new->e_argv[cnt] = NULL;
821 new->e_len[cnt] = 0;
822 cnt++;
823 }
824 argv = ap;
825 goto done;
826 }
827 new->e_argv[cnt] = new->e_orig[cnt] = NULL;
828
829 done: *argvp = argv + 1;
830 return new;
831 }
832
833 /* Finish any pending -exec ... {} + functions. */
834 void
835 finish_execplus(void)
836 {
837 PLAN *p;
838
839 p = lastexecplus;
840 while (p != NULL) {
841 (p->execute)(p, NULL);
842 p = p->e_next;
843 }
844 }
845
846 int
847 f_flags(PLAN *plan, FTSENT *entry)
848 {
849 u_long flags;
850
851 flags = entry->fts_statp->st_flags;
852 if (plan->flags & F_ATLEAST)
853 return (flags | plan->fl_flags) == flags &&
854 !(flags & plan->fl_notflags);
855 else if (plan->flags & F_ANY)
856 return (flags & plan->fl_flags) ||
857 (flags | plan->fl_notflags) != flags;
858 else
859 return flags == plan->fl_flags &&
860 !(plan->fl_flags & plan->fl_notflags);
861 }
862
863 PLAN *
864 c_flags(OPTION *option, char ***argvp)
865 {
866 char *flags_str;
867 PLAN *new;
868 u_long flags, notflags;
869
870 flags_str = nextarg(option, argvp);
871 ftsoptions &= ~FTS_NOSTAT;
872
873 new = palloc(option);
874
875 if (*flags_str == '-') {
876 new->flags |= F_ATLEAST;
877 flags_str++;
878 } else if (*flags_str == '+') {
879 new->flags |= F_ANY;
880 flags_str++;
881 }
882 if (strtofflags(&flags_str, &flags, &notflags) == 1)
883 errx(1, "%s: %s: illegal flags string", option->name, flags_str);
884
885 new->fl_flags = flags;
886 new->fl_notflags = notflags;
887 return new;
888 }
889
890 /*
891 * -follow functions --
892 *
893 * Always true, causes symbolic links to be followed on a global
894 * basis.
895 */
896 PLAN *
897 c_follow(OPTION *option, char ***argvp __unused)
898 {
899 ftsoptions &= ~FTS_PHYSICAL;
900 ftsoptions |= FTS_LOGICAL;
901
902 return palloc(option);
903 }
904
905 /*
906 * -fstype functions --
907 *
908 * True if the file is of a certain type.
909 */
910 int
911 f_fstype(PLAN *plan, FTSENT *entry)
912 {
913 static dev_t curdev; /* need a guaranteed illegal dev value */
914 static int first = 1;
915 struct statfs sb;
916 static int val_flags;
917 static char fstype[sizeof(sb.f_fstypename)];
918 char *p, save[2] = {0,0};
919
920 if ((plan->flags & F_MTMASK) == F_MTUNKNOWN)
921 return 0;
922
923 /* Only check when we cross mount point. */
924 if (first || curdev != entry->fts_statp->st_dev) {
925 curdev = entry->fts_statp->st_dev;
926
927 /*
928 * Statfs follows symlinks; find wants the link's filesystem,
929 * not where it points.
930 */
931 if (entry->fts_info == FTS_SL ||
932 entry->fts_info == FTS_SLNONE) {
933 if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
934 ++p;
935 else
936 p = entry->fts_accpath;
937 save[0] = p[0];
938 p[0] = '.';
939 save[1] = p[1];
940 p[1] = '\0';
941 } else
942 p = NULL;
943
944 if (statfs(entry->fts_accpath, &sb))
945 err(1, "%s", entry->fts_accpath);
946
947 if (p) {
948 p[0] = save[0];
949 p[1] = save[1];
950 }
951
952 first = 0;
953
954 /*
955 * Further tests may need both of these values, so
956 * always copy both of them.
957 */
958 val_flags = sb.f_flags;
959 strlcpy(fstype, sb.f_fstypename, sizeof(fstype));
960 }
961 switch (plan->flags & F_MTMASK) {
962 case F_MTFLAG:
963 return val_flags & plan->mt_data;
964 case F_MTTYPE:
965 return (strncmp(fstype, plan->c_data, sizeof(fstype)) == 0);
966 default:
967 abort();
968 }
969 }
970
971 PLAN *
972 c_fstype(OPTION *option, char ***argvp)
973 {
974 char *fsname;
975 PLAN *new;
976
977 fsname = nextarg(option, argvp);
978 ftsoptions &= ~FTS_NOSTAT;
979
980 new = palloc(option);
981 switch (*fsname) {
982 case 'l':
983 if (!strcmp(fsname, "local")) {
984 new->flags |= F_MTFLAG;
985 new->mt_data = MNT_LOCAL;
986 return new;
987 }
988 break;
989 case 'r':
990 if (!strcmp(fsname, "rdonly")) {
991 new->flags |= F_MTFLAG;
992 new->mt_data = MNT_RDONLY;
993 return new;
994 }
995 break;
996 }
997
998 new->flags |= F_MTTYPE;
999 new->c_data = fsname;
1000 return new;
1001 }
1002
1003 /*
1004 * -group gname functions --
1005 *
1006 * True if the file belongs to the group gname. If gname is numeric and
1007 * an equivalent of the getgrnam() function does not return a valid group
1008 * name, gname is taken as a group ID.
1009 */
1010 int
1011 f_group(PLAN *plan, FTSENT *entry)
1012 {
1013 COMPARE(entry->fts_statp->st_gid, plan->g_data);
1014 }
1015
1016 PLAN *
1017 c_group(OPTION *option, char ***argvp)
1018 {
1019 char *gname;
1020 PLAN *new;
1021 struct group *g;
1022 gid_t gid;
1023
1024 gname = nextarg(option, argvp);
1025 ftsoptions &= ~FTS_NOSTAT;
1026
1027 new = palloc(option);
1028 g = getgrnam(gname);
1029 if (g == NULL) {
1030 char* cp = gname;
1031 if (gname[0] == '-' || gname[0] == '+')
1032 gname++;
1033 gid = atoi(gname);
1034 if (gid == 0 && gname[0] != '0')
1035 errx(1, "%s: %s: no such group", option->name, gname);
1036 gid = find_parsenum(new, option->name, cp, NULL);
1037 } else
1038 gid = g->gr_gid;
1039
1040 new->g_data = gid;
1041 return new;
1042 }
1043
1044 /*
1045 * -inum n functions --
1046 *
1047 * True if the file has inode # n.
1048 */
1049 int
1050 f_inum(PLAN *plan, FTSENT *entry)
1051 {
1052 COMPARE(entry->fts_statp->st_ino, plan->i_data);
1053 }
1054
1055 PLAN *
1056 c_inum(OPTION *option, char ***argvp)
1057 {
1058 char *inum_str;
1059 PLAN *new;
1060
1061 inum_str = nextarg(option, argvp);
1062 ftsoptions &= ~FTS_NOSTAT;
1063
1064 new = palloc(option);
1065 new->i_data = find_parsenum(new, option->name, inum_str, NULL);
1066 return new;
1067 }
1068
1069 /*
1070 * -samefile FN
1071 *
1072 * True if the file has the same inode (eg hard link) FN
1073 */
1074
1075 /* f_samefile is just f_inum */
1076 PLAN *
1077 c_samefile(OPTION *option, char ***argvp)
1078 {
1079 char *fn;
1080 PLAN *new;
1081 struct stat sb;
1082
1083 fn = nextarg(option, argvp);
1084 ftsoptions &= ~FTS_NOSTAT;
1085
1086 new = palloc(option);
1087 if (stat(fn, &sb))
1088 err(1, "%s", fn);
1089 new->i_data = sb.st_ino;
1090 return new;
1091 }
1092
1093 /*
1094 * -links n functions --
1095 *
1096 * True if the file has n links.
1097 */
1098 int
1099 f_links(PLAN *plan, FTSENT *entry)
1100 {
1101 COMPARE(entry->fts_statp->st_nlink, plan->l_data);
1102 }
1103
1104 PLAN *
1105 c_links(OPTION *option, char ***argvp)
1106 {
1107 char *nlinks;
1108 PLAN *new;
1109
1110 nlinks = nextarg(option, argvp);
1111 ftsoptions &= ~FTS_NOSTAT;
1112
1113 new = palloc(option);
1114 new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL);
1115 return new;
1116 }
1117
1118 /*
1119 * -ls functions --
1120 *
1121 * Always true - prints the current entry to stdout in "ls" format.
1122 */
1123 int
1124 f_ls(PLAN *plan __unused, FTSENT *entry)
1125 {
1126 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
1127 return 1;
1128 }
1129
1130 PLAN *
1131 c_ls(OPTION *option, char ***argvp __unused)
1132 {
1133 ftsoptions &= ~FTS_NOSTAT;
1134 isoutput = 1;
1135
1136 return palloc(option);
1137 }
1138
1139 /*
1140 * -name functions --
1141 *
1142 * True if the basename of the filename being examined
1143 * matches pattern using Pattern Matching Notation S3.14
1144 */
1145 int
1146 f_name(PLAN *plan, FTSENT *entry)
1147 {
1148 char fn[PATH_MAX];
1149 const char *name;
1150 ssize_t len;
1151
1152 if (plan->flags & F_LINK) {
1153 /*
1154 * The below test both avoids obviously useless readlink()
1155 * calls and ensures that symlinks with existent target do
1156 * not match if symlinks are being followed.
1157 * Assumption: fts will stat all symlinks that are to be
1158 * followed and will return the stat information.
1159 */
1160 if (entry->fts_info != FTS_NSOK && entry->fts_info != FTS_SL &&
1161 entry->fts_info != FTS_SLNONE)
1162 return 0;
1163 len = readlink(entry->fts_accpath, fn, sizeof(fn) - 1);
1164 if (len == -1)
1165 return 0;
1166 fn[len] = '\0';
1167 name = fn;
1168 } else if (entry->fts_namelen == 0) {
1169 name = basename(entry->fts_path);
1170 } else
1171 name = entry->fts_name;
1172 return !fnmatch(plan->c_data, name,
1173 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1174 }
1175
1176 PLAN *
1177 c_name(OPTION *option, char ***argvp)
1178 {
1179 char *pattern;
1180 PLAN *new;
1181
1182 pattern = nextarg(option, argvp);
1183 new = palloc(option);
1184 new->c_data = pattern;
1185 return new;
1186 }
1187
1188 /*
1189 * -newer file functions --
1190 *
1191 * True if the current file has been modified more recently
1192 * then the modification time of the file named by the pathname
1193 * file.
1194 */
1195 int
1196 f_newer(PLAN *plan, FTSENT *entry)
1197 {
1198 if (plan->flags & F_TIME_C)
1199 return entry->fts_statp->st_ctime > plan->t_data;
1200 else if (plan->flags & F_TIME_A)
1201 return entry->fts_statp->st_atime > plan->t_data;
1202 else if (plan->flags & F_TIME_B)
1203 return entry->fts_statp->st_birthtime > plan->t_data;
1204 else
1205 return entry->fts_statp->st_mtime > plan->t_data;
1206 }
1207
1208 PLAN *
1209 c_newer(OPTION *option, char ***argvp)
1210 {
1211 char *fn_or_tspec;
1212 PLAN *new;
1213 struct stat sb;
1214
1215 fn_or_tspec = nextarg(option, argvp);
1216 ftsoptions &= ~FTS_NOSTAT;
1217
1218 new = palloc(option);
1219 /* compare against what */
1220 if (option->flags & F_TIME2_T) {
1221 new->t_data = get_date(fn_or_tspec);
1222 if (new->t_data == (time_t) -1)
1223 errx(1, "Can't parse date/time: %s", fn_or_tspec);
1224 } else {
1225 if (stat(fn_or_tspec, &sb))
1226 err(1, "%s", fn_or_tspec);
1227 if (option->flags & F_TIME2_C)
1228 new->t_data = sb.st_ctime;
1229 else if (option->flags & F_TIME2_A)
1230 new->t_data = sb.st_atime;
1231 else if (option->flags & F_TIME2_B)
1232 new->t_data = sb.st_birthtime;
1233 else
1234 new->t_data = sb.st_mtime;
1235 }
1236 return new;
1237 }
1238
1239 /*
1240 * -nogroup functions --
1241 *
1242 * True if file belongs to a user ID for which the equivalent
1243 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1244 */
1245 int
1246 f_nogroup(PLAN *plan __unused, FTSENT *entry)
1247 {
1248 return group_from_gid(entry->fts_statp->st_gid, 1) == NULL;
1249 }
1250
1251 PLAN *
1252 c_nogroup(OPTION *option, char ***argvp __unused)
1253 {
1254 ftsoptions &= ~FTS_NOSTAT;
1255
1256 return palloc(option);
1257 }
1258
1259 /*
1260 * -nouser functions --
1261 *
1262 * True if file belongs to a user ID for which the equivalent
1263 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1264 */
1265 int
1266 f_nouser(PLAN *plan __unused, FTSENT *entry)
1267 {
1268 return user_from_uid(entry->fts_statp->st_uid, 1) == NULL;
1269 }
1270
1271 PLAN *
1272 c_nouser(OPTION *option, char ***argvp __unused)
1273 {
1274 ftsoptions &= ~FTS_NOSTAT;
1275
1276 return palloc(option);
1277 }
1278
1279 /*
1280 * -path functions --
1281 *
1282 * True if the path of the filename being examined
1283 * matches pattern using Pattern Matching Notation S3.14
1284 */
1285 int
1286 f_path(PLAN *plan, FTSENT *entry)
1287 {
1288 return !fnmatch(plan->c_data, entry->fts_path,
1289 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1290 }
1291
1292 /* c_path is the same as c_name */
1293
1294 /*
1295 * -perm functions --
1296 *
1297 * The mode argument is used to represent file mode bits. If it starts
1298 * with a leading digit, it's treated as an octal mode, otherwise as a
1299 * symbolic mode.
1300 */
1301 int
1302 f_perm(PLAN *plan, FTSENT *entry)
1303 {
1304 mode_t mode;
1305
1306 mode = entry->fts_statp->st_mode &
1307 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1308 if (plan->flags & F_ATLEAST)
1309 return (plan->m_data | mode) == mode;
1310 else if (plan->flags & F_ANY)
1311 return (mode & plan->m_data);
1312 else
1313 return mode == plan->m_data;
1314 /* NOTREACHED */
1315 }
1316
1317 PLAN *
1318 c_perm(OPTION *option, char ***argvp)
1319 {
1320 char *perm;
1321 PLAN *new;
1322 mode_t *set;
1323
1324 perm = nextarg(option, argvp);
1325 ftsoptions &= ~FTS_NOSTAT;
1326
1327 new = palloc(option);
1328
1329 if (*perm == '-') {
1330 new->flags |= F_ATLEAST;
1331 ++perm;
1332 } else if (*perm == '+') {
1333 if ((set = setmode(perm + 1)) != NULL) {
1334 new->flags |= F_ANY;
1335 ++perm;
1336 free(set);
1337 }
1338 }
1339
1340 if ((set = setmode(perm)) == NULL)
1341 errx(1, "%s: %s: illegal mode string", option->name, perm);
1342
1343 new->m_data = getmode(set, 0);
1344 free(set);
1345 return new;
1346 }
1347
1348 /*
1349 * -print functions --
1350 *
1351 * Always true, causes the current pathname to be written to
1352 * standard output.
1353 */
1354 int
1355 f_print(PLAN *plan __unused, FTSENT *entry)
1356 {
1357 (void)puts(entry->fts_path);
1358 return 1;
1359 }
1360
1361 PLAN *
1362 c_print(OPTION *option, char ***argvp __unused)
1363 {
1364 isoutput = 1;
1365
1366 return palloc(option);
1367 }
1368
1369 /*
1370 * -print0 functions --
1371 *
1372 * Always true, causes the current pathname to be written to
1373 * standard output followed by a NUL character
1374 */
1375 int
1376 f_print0(PLAN *plan __unused, FTSENT *entry)
1377 {
1378 fputs(entry->fts_path, stdout);
1379 fputc('\0', stdout);
1380 return 1;
1381 }
1382
1383 /* c_print0 is the same as c_print */
1384
1385 /*
1386 * -prune functions --
1387 *
1388 * Prune a portion of the hierarchy.
1389 */
1390 int
1391 f_prune(PLAN *plan __unused, FTSENT *entry)
1392 {
1393 if (fts_set(tree, entry, FTS_SKIP))
1394 err(1, "%s", entry->fts_path);
1395 return 1;
1396 }
1397
1398 /* c_prune == c_simple */
1399
1400 /*
1401 * -regex functions --
1402 *
1403 * True if the whole path of the file matches pattern using
1404 * regular expression.
1405 */
1406 int
1407 f_regex(PLAN *plan, FTSENT *entry)
1408 {
1409 char *str;
1410 int len;
1411 regex_t *pre;
1412 regmatch_t pmatch;
1413 int errcode;
1414 char errbuf[LINE_MAX];
1415 int matched;
1416
1417 pre = plan->re_data;
1418 str = entry->fts_path;
1419 len = strlen(str);
1420 matched = 0;
1421
1422 pmatch.rm_so = 0;
1423 pmatch.rm_eo = len;
1424
1425 errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND);
1426
1427 if (errcode != 0 && errcode != REG_NOMATCH) {
1428 regerror(errcode, pre, errbuf, sizeof errbuf);
1429 errx(1, "%s: %s",
1430 plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf);
1431 }
1432
1433 if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
1434 matched = 1;
1435
1436 return matched;
1437 }
1438
1439 PLAN *
1440 c_regex(OPTION *option, char ***argvp)
1441 {
1442 PLAN *new;
1443 char *pattern;
1444 regex_t *pre;
1445 int errcode;
1446 char errbuf[LINE_MAX];
1447
1448 if ((pre = malloc(sizeof(regex_t))) == NULL)
1449 err(1, NULL);
1450
1451 pattern = nextarg(option, argvp);
1452
1453 if ((errcode = regcomp(pre, pattern,
1454 regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) {
1455 regerror(errcode, pre, errbuf, sizeof errbuf);
1456 errx(1, "%s: %s: %s",
1457 option->flags & F_IGNCASE ? "-iregex" : "-regex",
1458 pattern, errbuf);
1459 }
1460
1461 new = palloc(option);
1462 new->re_data = pre;
1463
1464 return new;
1465 }
1466
1467 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or, c_true, c_false */
1468
1469 PLAN *
1470 c_simple(OPTION *option, char ***argvp __unused)
1471 {
1472 return palloc(option);
1473 }
1474
1475 /*
1476 * -size n[c] functions --
1477 *
1478 * True if the file size in bytes, divided by an implementation defined
1479 * value and rounded up to the next integer, is n. If n is followed by
1480 * one of c k M G T P, the size is in bytes, kilobytes,
1481 * megabytes, gigabytes, terabytes or petabytes respectively.
1482 */
1483 #define FIND_SIZE 512
1484 static int divsize = 1;
1485
1486 int
1487 f_size(PLAN *plan, FTSENT *entry)
1488 {
1489 off_t size;
1490
1491 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1492 FIND_SIZE : entry->fts_statp->st_size;
1493 COMPARE(size, plan->o_data);
1494 }
1495
1496 PLAN *
1497 c_size(OPTION *option, char ***argvp)
1498 {
1499 char *size_str;
1500 PLAN *new;
1501 char endch;
1502 off_t scale;
1503
1504 size_str = nextarg(option, argvp);
1505 ftsoptions &= ~FTS_NOSTAT;
1506
1507 new = palloc(option);
1508 endch = 'c';
1509 new->o_data = find_parsenum(new, option->name, size_str, &endch);
1510 if (endch != '\0') {
1511 divsize = 0;
1512
1513 switch (endch) {
1514 case 'c': /* characters */
1515 scale = 0x1LL;
1516 break;
1517 case 'k': /* kilobytes 1<<10 */
1518 scale = 0x400LL;
1519 break;
1520 case 'M': /* megabytes 1<<20 */
1521 scale = 0x100000LL;
1522 break;
1523 case 'G': /* gigabytes 1<<30 */
1524 scale = 0x40000000LL;
1525 break;
1526 case 'T': /* terabytes 1<<40 */
1527 scale = 0x1000000000LL;
1528 break;
1529 case 'P': /* petabytes 1<<50 */
1530 scale = 0x4000000000000LL;
1531 break;
1532 default:
1533 errx(1, "%s: %s: illegal trailing character",
1534 option->name, size_str);
1535 break;
1536 }
1537 if (new->o_data > QUAD_MAX / scale)
1538 errx(1, "%s: %s: value too large",
1539 option->name, size_str);
1540 new->o_data *= scale;
1541 }
1542 return new;
1543 }
1544
1545 /*
1546 * -type c functions --
1547 *
1548 * True if the type of the file is c, where c is b, c, d, p, f or w
1549 * for block special file, character special file, directory, FIFO,
1550 * regular file or whiteout respectively.
1551 */
1552 int
1553 f_type(PLAN *plan, FTSENT *entry)
1554 {
1555 return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data;
1556 }
1557
1558 PLAN *
1559 c_type(OPTION *option, char ***argvp)
1560 {
1561 char *typestring;
1562 PLAN *new;
1563 mode_t mask;
1564
1565 typestring = nextarg(option, argvp);
1566 ftsoptions &= ~FTS_NOSTAT;
1567
1568 switch (typestring[0]) {
1569 case 'b':
1570 mask = S_IFBLK;
1571 break;
1572 case 'c':
1573 mask = S_IFCHR;
1574 break;
1575 case 'd':
1576 mask = S_IFDIR;
1577 break;
1578 case 'f':
1579 mask = S_IFREG;
1580 break;
1581 case 'l':
1582 mask = S_IFLNK;
1583 break;
1584 case 'p':
1585 mask = S_IFIFO;
1586 break;
1587 case 's':
1588 mask = S_IFSOCK;
1589 break;
1590 #ifdef FTS_WHITEOUT
1591 case 'w':
1592 mask = S_IFWHT;
1593 ftsoptions |= FTS_WHITEOUT;
1594 break;
1595 #endif /* FTS_WHITEOUT */
1596 default:
1597 errx(1, "%s: %s: unknown type", option->name, typestring);
1598 }
1599
1600 new = palloc(option);
1601 new->m_data = mask;
1602 return new;
1603 }
1604
1605 /*
1606 * -user uname functions --
1607 *
1608 * True if the file belongs to the user uname. If uname is numeric and
1609 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1610 * return a valid user name, uname is taken as a user ID.
1611 */
1612 int
1613 f_user(PLAN *plan, FTSENT *entry)
1614 {
1615 COMPARE(entry->fts_statp->st_uid, plan->u_data);
1616 }
1617
1618 PLAN *
1619 c_user(OPTION *option, char ***argvp)
1620 {
1621 char *username;
1622 PLAN *new;
1623 struct passwd *p;
1624 uid_t uid;
1625
1626 username = nextarg(option, argvp);
1627 ftsoptions &= ~FTS_NOSTAT;
1628
1629 new = palloc(option);
1630 p = getpwnam(username);
1631 if (p == NULL) {
1632 char* cp = username;
1633 if( username[0] == '-' || username[0] == '+' )
1634 username++;
1635 uid = atoi(username);
1636 if (uid == 0 && username[0] != '0')
1637 errx(1, "%s: %s: no such user", option->name, username);
1638 uid = find_parsenum(new, option->name, cp, NULL);
1639 } else
1640 uid = p->pw_uid;
1641
1642 new->u_data = uid;
1643 return new;
1644 }
1645
1646 /*
1647 * -xdev functions --
1648 *
1649 * Always true, causes find not to descend past directories that have a
1650 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1651 */
1652 PLAN *
1653 c_xdev(OPTION *option, char ***argvp __unused)
1654 {
1655 ftsoptions |= FTS_XDEV;
1656
1657 return palloc(option);
1658 }
1659
1660 /*
1661 * ( expression ) functions --
1662 *
1663 * True if expression is true.
1664 */
1665 int
1666 f_expr(PLAN *plan, FTSENT *entry)
1667 {
1668 PLAN *p;
1669 int state = 0;
1670
1671 for (p = plan->p_data[0];
1672 p && (state = (p->execute)(p, entry)); p = p->next);
1673 return state;
1674 }
1675
1676 /*
1677 * f_openparen and f_closeparen nodes are temporary place markers. They are
1678 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1679 * to a f_expr node containing the expression and the ')' node is discarded.
1680 * The functions themselves are only used as constants.
1681 */
1682
1683 int
1684 f_openparen(PLAN *plan __unused, FTSENT *entry __unused)
1685 {
1686 abort();
1687 }
1688
1689 int
1690 f_closeparen(PLAN *plan __unused, FTSENT *entry __unused)
1691 {
1692 abort();
1693 }
1694
1695 /* c_openparen == c_simple */
1696 /* c_closeparen == c_simple */
1697
1698 /*
1699 * AND operator. Since AND is implicit, no node is allocated.
1700 */
1701 PLAN *
1702 c_and(OPTION *option __unused, char ***argvp __unused)
1703 {
1704 return NULL;
1705 }
1706
1707 /*
1708 * ! expression functions --
1709 *
1710 * Negation of a primary; the unary NOT operator.
1711 */
1712 int
1713 f_not(PLAN *plan, FTSENT *entry)
1714 {
1715 PLAN *p;
1716 int state = 0;
1717
1718 for (p = plan->p_data[0];
1719 p && (state = (p->execute)(p, entry)); p = p->next);
1720 return !state;
1721 }
1722
1723 /* c_not == c_simple */
1724
1725 /*
1726 * expression -o expression functions --
1727 *
1728 * Alternation of primaries; the OR operator. The second expression is
1729 * not evaluated if the first expression is true.
1730 */
1731 int
1732 f_or(PLAN *plan, FTSENT *entry)
1733 {
1734 PLAN *p;
1735 int state = 0;
1736
1737 for (p = plan->p_data[0];
1738 p && (state = (p->execute)(p, entry)); p = p->next);
1739
1740 if (state)
1741 return 1;
1742
1743 for (p = plan->p_data[1];
1744 p && (state = (p->execute)(p, entry)); p = p->next);
1745 return state;
1746 }
1747
1748 /* c_or == c_simple */
1749
1750 /*
1751 * -false
1752 *
1753 * Always false.
1754 */
1755 int
1756 f_false(PLAN *plan __unused, FTSENT *entry __unused)
1757 {
1758 return 0;
1759 }
1760
1761 /* c_false == c_simple */
1762
1763 /*
1764 * -quit
1765 *
1766 * Exits the program
1767 */
1768 int
1769 f_quit(PLAN *plan __unused, FTSENT *entry __unused)
1770 {
1771 exit(0);
1772 }
1773
1774 /* c_quit == c_simple */