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