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