2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Cimarron D. Taylor of the University of California, Berkeley.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
39 static const char sccsid[] = "@(#)function.c 8.10 (Berkeley) 5/4/95";
41 static const char rcsid[] =
46 #include <sys/param.h>
47 #include <sys/ucred.h>
50 #include <sys/mount.h>
67 #define COMPARE(a, b) { \
68 switch (plan->flags) { \
80 static int do_f_regex __P((PLAN *, FTSENT *, int));
81 static PLAN *do_c_regex __P((char *, int));
82 static PLAN *palloc __P((enum ntype, int (*) __P((PLAN *, FTSENT *))));
86 * Parse a string of the form [+-]# and return the value.
89 find_parsenum(plan, option, vp, endch)
91 char *option, *vp, *endch;
94 char *endchar, *str; /* Pointer to character ending conversion. */
96 /* Determine comparison from leading + or -. */
101 plan->flags = F_GREATER;
105 plan->flags = F_LESSTHAN;
108 plan->flags = F_EQUAL;
113 * Convert the string with strtoq(). Note, if strtoq() returns zero
114 * and endchar points to the beginning of the string we know we have
117 value = strtoq(str, &endchar, 10);
118 if (value == 0 && endchar == str)
119 errx(1, "%s: %s: illegal numeric value", option, vp);
120 if (endchar[0] && (endch == NULL || endchar[0] != *endch))
121 errx(1, "%s: %s: illegal trailing character", option, vp);
128 * The value of n for the inode times (atime, ctime, and mtime) is a range,
129 * i.e. n matches from (n - 1) to n 24 hour periods. This interacts with
130 * -n, such that "-mtime -1" would be less than 0 days, which isn't what the
131 * user wanted. Correct so that -1 is "less than 1".
133 #define TIME_CORRECT(p, ttype) \
134 if ((p)->type == ttype && (p)->flags == F_LESSTHAN) \
138 * -amin n functions --
140 * True if the difference between the file access time and the
141 * current time is n min periods.
150 COMPARE((now - entry->fts_statp->st_atime +
151 60 - 1) / 60, plan->t_data);
160 ftsoptions &= ~FTS_NOSTAT;
162 new = palloc(N_AMIN, f_amin);
163 new->t_data = find_parsenum(new, "-amin", arg, NULL);
164 TIME_CORRECT(new, N_AMIN);
170 * -atime n functions --
172 * True if the difference between the file access time and the
173 * current time is n 24 hour periods.
182 COMPARE((now - entry->fts_statp->st_atime +
183 86400 - 1) / 86400, plan->t_data);
192 ftsoptions &= ~FTS_NOSTAT;
194 new = palloc(N_ATIME, f_atime);
195 new->t_data = find_parsenum(new, "-atime", arg, NULL);
196 TIME_CORRECT(new, N_ATIME);
202 * -cmin n functions --
204 * True if the difference between the last change of file
205 * status information and the current time is n min periods.
214 COMPARE((now - entry->fts_statp->st_ctime +
215 60 - 1) / 60, plan->t_data);
224 ftsoptions &= ~FTS_NOSTAT;
226 new = palloc(N_CMIN, f_cmin);
227 new->t_data = find_parsenum(new, "-cmin", arg, NULL);
228 TIME_CORRECT(new, N_CMIN);
233 * -ctime n functions --
235 * True if the difference between the last change of file
236 * status information and the current time is n 24 hour periods.
245 COMPARE((now - entry->fts_statp->st_ctime +
246 86400 - 1) / 86400, plan->t_data);
255 ftsoptions &= ~FTS_NOSTAT;
257 new = palloc(N_CTIME, f_ctime);
258 new->t_data = find_parsenum(new, "-ctime", arg, NULL);
259 TIME_CORRECT(new, N_CTIME);
265 * -depth functions --
267 * Always true, causes descent of the directory hierarchy to be done
268 * so that all entries in a directory are acted on before the directory
272 f_always_true(plan, entry)
284 return (palloc(N_DEPTH, f_always_true));
288 * [-exec | -ok] utility [arg ... ] ; functions --
290 * True if the executed utility returns a zero value as exit status.
291 * The end of the primary expression is delimited by a semicolon. If
292 * "{}" occurs anywhere, it gets replaced by the current pathname.
293 * The current directory for the execution of utility is the same as
294 * the current directory when the find utility was started.
296 * The primary -ok is different in that it requests affirmation of the
297 * user before executing the utility.
309 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
310 if (plan->e_len[cnt])
311 brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
312 entry->fts_path, plan->e_len[cnt]);
314 if (plan->flags == F_NEEDOK && !queryuser(plan->e_argv))
317 /* make sure find output is interspersed correctly with subprocesses */
320 switch (pid = fork()) {
329 execvp(plan->e_argv[0], plan->e_argv);
330 warn("%s", plan->e_argv[0]);
333 pid = waitpid(pid, &status, 0);
334 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
339 * build three parallel arrays, one with pointers to the strings passed
340 * on the command line, one with (possibly duplicated) pointers to the
341 * argv array, and one with integer values that are lengths of the
342 * strings, but also flags meaning that the string has to be massaged.
349 PLAN *new; /* node returned */
351 register char **argv, **ap, *p;
355 new = palloc(N_EXEC, f_exec);
357 new->flags = F_NEEDOK;
359 for (ap = argv = *argvp;; ++ap) {
362 "%s: no terminating \";\"", isok ? "-ok" : "-exec");
367 cnt = ap - *argvp + 1;
368 new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
369 new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
370 new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
372 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
373 new->e_orig[cnt] = *argv;
374 for (p = *argv; *p; ++p)
375 if (p[0] == '{' && p[1] == '}') {
376 new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
377 new->e_len[cnt] = MAXPATHLEN;
381 new->e_argv[cnt] = *argv;
385 new->e_argv[cnt] = new->e_orig[cnt] = NULL;
392 * -empty functions --
394 * True if the file or directory is empty
401 if (S_ISREG(entry->fts_statp->st_mode) && entry->fts_statp->st_size == 0)
403 if (S_ISDIR(entry->fts_statp->st_mode)) {
409 dir = opendir(entry->fts_accpath);
411 err(1, "%s", entry->fts_accpath);
412 for (dp = readdir(dir); dp; dp = readdir(dir))
413 if (dp->d_name[0] != '.' ||
414 (dp->d_name[1] != '\0' &&
415 (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
428 ftsoptions &= ~FTS_NOSTAT;
430 return (palloc(N_EMPTY, f_empty));
434 * -execdir utility [arg ... ] ; functions --
436 * True if the executed utility returns a zero value as exit status.
437 * The end of the primary expression is delimited by a semicolon. If
438 * "{}" occurs anywhere, it gets replaced by the unqualified pathname.
439 * The current directory for the execution of utility is the same as
440 * the directory where the file lives.
443 f_execdir(plan, entry)
452 /* XXX - if file/dir ends in '/' this will not work -- can it? */
453 if ((file = strrchr(entry->fts_path, '/')))
456 file = entry->fts_path;
458 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
459 if (plan->e_len[cnt])
460 brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
461 file, plan->e_len[cnt]);
463 /* don't mix output of command with find output */
467 switch (pid = fork()) {
472 execvp(plan->e_argv[0], plan->e_argv);
473 warn("%s", plan->e_argv[0]);
476 pid = waitpid(pid, &status, 0);
477 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
482 * build three parallel arrays, one with pointers to the strings passed
483 * on the command line, one with (possibly duplicated) pointers to the
484 * argv array, and one with integer values that are lengths of the
485 * strings, but also flags meaning that the string has to be massaged.
491 PLAN *new; /* node returned */
493 register char **argv, **ap, *p;
495 ftsoptions &= ~FTS_NOSTAT;
498 new = palloc(N_EXECDIR, f_execdir);
500 for (ap = argv = *argvp;; ++ap) {
503 "-execdir: no terminating \";\"");
508 cnt = ap - *argvp + 1;
509 new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
510 new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
511 new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
513 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
514 new->e_orig[cnt] = *argv;
515 for (p = *argv; *p; ++p)
516 if (p[0] == '{' && p[1] == '}') {
517 new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
518 new->e_len[cnt] = MAXPATHLEN;
522 new->e_argv[cnt] = *argv;
526 new->e_argv[cnt] = new->e_orig[cnt] = NULL;
533 * -follow functions --
535 * Always true, causes symbolic links to be followed on a global
541 ftsoptions &= ~FTS_PHYSICAL;
542 ftsoptions |= FTS_LOGICAL;
544 return (palloc(N_FOLLOW, f_always_true));
548 * -fstype functions --
550 * True if the file is of a certain type.
553 f_fstype(plan, entry)
557 static dev_t curdev; /* need a guaranteed illegal dev value */
558 static int first = 1;
560 static int val_type, val_flags;
563 /* Only check when we cross mount point. */
564 if (first || curdev != entry->fts_statp->st_dev) {
565 curdev = entry->fts_statp->st_dev;
568 * Statfs follows symlinks; find wants the link's file system,
569 * not where it points.
571 if (entry->fts_info == FTS_SL ||
572 entry->fts_info == FTS_SLNONE) {
573 if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
576 p = entry->fts_accpath;
585 if (statfs(entry->fts_accpath, &sb))
586 err(1, "%s", entry->fts_accpath);
596 * Further tests may need both of these values, so
597 * always copy both of them.
599 val_flags = sb.f_flags;
600 val_type = sb.f_type;
602 switch (plan->flags) {
604 return (val_flags & plan->mt_data) != 0;
606 return (val_type == plan->mt_data);
612 #if !defined(__NetBSD__)
614 f_always_false(plan, entry)
628 ftsoptions &= ~FTS_NOSTAT;
630 new = palloc(N_FSTYPE, f_fstype);
633 * Check first for a filesystem name.
635 if (getvfsbyname(arg, &vfc) == 0) {
636 new->flags = F_MTTYPE;
637 new->mt_data = vfc.vfc_typenum;
643 if (!strcmp(arg, "local")) {
644 new->flags = F_MTFLAG;
645 new->mt_data = MNT_LOCAL;
650 if (!strcmp(arg, "rdonly")) {
651 new->flags = F_MTFLAG;
652 new->mt_data = MNT_RDONLY;
658 * We need to make filesystem checks for filesystems
659 * that exists but aren't in the kernel work.
661 fprintf(stderr, "Warning: Unknown filesystem type %s\n", arg);
664 return (palloc(N_FSTYPE, f_always_false));
669 * -group gname functions --
671 * True if the file belongs to the group gname. If gname is numeric and
672 * an equivalent of the getgrnam() function does not return a valid group
673 * name, gname is taken as a group ID.
680 return (entry->fts_statp->st_gid == plan->g_data);
691 ftsoptions &= ~FTS_NOSTAT;
696 if (gid == 0 && gname[0] != '0')
697 errx(1, "-group: %s: no such group", gname);
701 new = palloc(N_GROUP, f_group);
707 * -inum n functions --
709 * True if the file has inode # n.
716 COMPARE(entry->fts_statp->st_ino, plan->i_data);
725 ftsoptions &= ~FTS_NOSTAT;
727 new = palloc(N_INUM, f_inum);
728 new->i_data = find_parsenum(new, "-inum", arg, NULL);
733 * -links n functions --
735 * True if the file has n links.
742 COMPARE(entry->fts_statp->st_nlink, plan->l_data);
751 ftsoptions &= ~FTS_NOSTAT;
753 new = palloc(N_LINKS, f_links);
754 new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL);
761 * Always true - prints the current entry to stdout in "ls" format.
768 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
775 ftsoptions &= ~FTS_NOSTAT;
778 return (palloc(N_LS, f_ls));
782 * -maxdepth n functions --
784 * Does the same as -prune if the level of the current file is greater
785 * than the specified maximum depth.
787 * Note that -maxdepth and -mindepth are handled specially in
788 * find_execute() so their f_* functions here do nothing.
791 f_maxdepth(plan, entry)
805 /* all other errors handled by find_parsenum() */
806 errx(1, "-maxdepth: %s: value must be positive", arg);
808 new = palloc(N_MAXDEPTH, f_maxdepth);
809 maxdepth = find_parsenum(new, "-maxdepth", arg, NULL);
814 * -mindepth n functions --
816 * True if the current file is at or deeper than the specified minimum
820 f_mindepth(plan, entry)
834 /* all other errors handled by find_parsenum() */
835 errx(1, "-maxdepth: %s: value must be positive", arg);
837 new = palloc(N_MINDEPTH, f_mindepth);
838 mindepth = find_parsenum(new, "-mindepth", arg, NULL);
843 * -mtime n functions --
845 * True if the difference between the file modification time and the
846 * current time is n 24 hour periods.
855 COMPARE((now - entry->fts_statp->st_mtime + 86400 - 1) /
856 86400, plan->t_data);
865 ftsoptions &= ~FTS_NOSTAT;
867 new = palloc(N_MTIME, f_mtime);
868 new->t_data = find_parsenum(new, "-mtime", arg, NULL);
869 TIME_CORRECT(new, N_MTIME);
874 * -mmin n functions --
876 * True if the difference between the file modification time and the
877 * current time is n min periods.
886 COMPARE((now - entry->fts_statp->st_mtime + 60 - 1) /
896 ftsoptions &= ~FTS_NOSTAT;
898 new = palloc(N_MMIN, f_mmin);
899 new->t_data = find_parsenum(new, "-mmin", arg, NULL);
900 TIME_CORRECT(new, N_MMIN);
908 * True if the basename of the filename being examined
909 * matches pattern using Pattern Matching Notation S3.14
916 return (!fnmatch(plan->c_data, entry->fts_name, 0));
925 new = palloc(N_NAME, f_name);
926 new->c_data = pattern;
932 * -iname functions --
934 * Like -iname, but the match is case insensitive.
941 return (!fnmatch(plan->c_data, entry->fts_name, FNM_CASEFOLD));
950 new = palloc(N_INAME, f_iname);
951 new->c_data = pattern;
957 * -regex functions --
959 * True if the whole path of the file matches pattern using
960 * regular expression.
967 return (do_f_regex(plan, entry, 0));
974 return (do_c_regex(pattern, 0));
978 * -iregex functions --
980 * Like -regex, but the match is case insensitive.
983 f_iregex(plan, entry)
987 return (do_f_regex(plan, entry, REG_ICASE));
994 return (do_c_regex(pattern, REG_ICASE));
998 do_f_regex(plan, entry, icase)
1008 char errbuf[LINE_MAX];
1011 pre = plan->re_data;
1012 str = entry->fts_path;
1019 errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND);
1021 if (errcode != 0 && errcode != REG_NOMATCH) {
1022 regerror(errcode, pre, errbuf, sizeof errbuf);
1024 icase == 0 ? "-regex" : "-iregex", errbuf);
1027 if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
1034 do_c_regex(pattern, icase)
1041 char errbuf[LINE_MAX];
1043 if ((pre = malloc(sizeof(regex_t))) == NULL)
1046 if ((errcode = regcomp(pre, pattern, regexp_flags | icase)) != 0) {
1047 regerror(errcode, pre, errbuf, sizeof errbuf);
1048 errx(1, "%s: %s: %s",
1049 icase == 0 ? "-regex" : "-iregex", pattern, errbuf);
1052 new = icase == 0 ? palloc(N_REGEX, f_regex) : palloc(N_IREGEX, f_iregex);
1058 * -newer file functions --
1060 * True if the current file has been modified more recently
1061 * then the modification time of the file named by the pathname
1065 f_newer(plan, entry)
1069 return (entry->fts_statp->st_mtime > plan->t_data);
1079 ftsoptions &= ~FTS_NOSTAT;
1081 if (stat(filename, &sb))
1082 err(1, "%s", filename);
1083 new = palloc(N_NEWER, f_newer);
1084 new->t_data = sb.st_mtime;
1089 * -nogroup functions --
1091 * True if file belongs to a user ID for which the equivalent
1092 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1095 f_nogroup(plan, entry)
1099 return (group_from_gid(entry->fts_statp->st_gid, 1) ? 0 : 1);
1105 ftsoptions &= ~FTS_NOSTAT;
1107 return (palloc(N_NOGROUP, f_nogroup));
1111 * -nouser functions --
1113 * True if file belongs to a user ID for which the equivalent
1114 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1117 f_nouser(plan, entry)
1121 return (user_from_uid(entry->fts_statp->st_uid, 1) ? 0 : 1);
1127 ftsoptions &= ~FTS_NOSTAT;
1129 return (palloc(N_NOUSER, f_nouser));
1133 * -path functions --
1135 * True if the path of the filename being examined
1136 * matches pattern using Pattern Matching Notation S3.14
1143 return (!fnmatch(plan->c_data, entry->fts_path, 0));
1152 new = palloc(N_PATH, f_path);
1153 new->c_data = pattern;
1158 * -ipath functions --
1160 * Like -path, but the match is case insensitive.
1163 f_ipath(plan, entry)
1167 return (!fnmatch(plan->c_data, entry->fts_path, FNM_CASEFOLD));
1176 new = palloc(N_IPATH, f_ipath);
1177 new->c_data = pattern;
1182 * -perm functions --
1184 * The mode argument is used to represent file mode bits. If it starts
1185 * with a leading digit, it's treated as an octal mode, otherwise as a
1195 mode = entry->fts_statp->st_mode &
1196 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1197 if (plan->flags == F_ATLEAST)
1198 return ((plan->m_data | mode) == mode);
1199 else if (plan->flags == F_ANY )
1200 return (plan->m_data & mode);
1202 return (mode == plan->m_data);
1213 ftsoptions &= ~FTS_NOSTAT;
1215 new = palloc(N_PERM, f_perm);
1218 new->flags = F_ATLEAST;
1220 } else if (*perm == '+') {
1225 if ((set = setmode(perm)) == NULL)
1226 errx(1, "-perm: %s: illegal mode string", perm);
1228 new->m_data = getmode(set, 0);
1234 * -flags functions --
1236 * The flags argument is used to represent file flags bits.
1239 f_flags(plan, entry)
1245 flags = entry->fts_statp->st_flags &
1246 (UF_NODUMP | UF_IMMUTABLE | UF_APPEND | UF_OPAQUE |
1247 SF_ARCHIVED | SF_IMMUTABLE | SF_APPEND);
1248 if (plan->flags == F_ATLEAST)
1249 /* note that plan->fl_flags always is a subset of
1251 return (flags & plan->fl_mask) == plan->fl_flags;
1253 return flags == plan->fl_flags;
1262 u_long flags, notflags;
1264 ftsoptions &= ~FTS_NOSTAT;
1266 new = palloc(N_FLAGS, f_flags);
1268 if (*flags_str == '-') {
1269 new->flags = F_ATLEAST;
1272 if (strtofflags(&flags_str, &flags, ¬flags) == 1)
1273 errx(1, "-flags: %s: illegal flags string", flags_str);
1275 new->fl_flags = flags;
1276 new->fl_mask = flags | notflags;
1278 printf("flags = %08x, mask = %08x (%08x, %08x)\n",
1279 new->fl_flags, new->fl_mask, flags, notflags);
1285 * -print functions --
1287 * Always true, causes the current pathame to be written to
1291 f_print(plan, entry)
1295 (void)puts(entry->fts_path);
1304 return (palloc(N_PRINT, f_print));
1308 * -print0 functions --
1310 * Always true, causes the current pathame to be written to
1311 * standard output followed by a NUL character
1314 f_print0(plan, entry)
1318 fputs(entry->fts_path, stdout);
1319 fputc('\0', stdout);
1328 return (palloc(N_PRINT0, f_print0));
1332 * -prune functions --
1334 * Prune a portion of the hierarchy.
1337 f_prune(plan, entry)
1343 if (fts_set(tree, entry, FTS_SKIP))
1344 err(1, "%s", entry->fts_path);
1351 return (palloc(N_PRUNE, f_prune));
1355 * -size n[c] functions --
1357 * True if the file size in bytes, divided by an implementation defined
1358 * value and rounded up to the next integer, is n. If n is followed by
1359 * a c, the size is in bytes.
1361 #define FIND_SIZE 512
1362 static int divsize = 1;
1371 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1372 FIND_SIZE : entry->fts_statp->st_size;
1373 COMPARE(size, plan->o_data);
1383 ftsoptions &= ~FTS_NOSTAT;
1385 new = palloc(N_SIZE, f_size);
1387 new->o_data = find_parsenum(new, "-size", arg, &endch);
1394 * -type c functions --
1396 * True if the type of the file is c, where c is b, c, d, p, f or w
1397 * for block special file, character special file, directory, FIFO,
1398 * regular file or whiteout respectively.
1405 return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data);
1415 ftsoptions &= ~FTS_NOSTAT;
1417 switch (typestring[0]) {
1442 ftsoptions |= FTS_WHITEOUT;
1444 #endif /* FTS_WHITEOUT */
1446 errx(1, "-type: %s: unknown type", typestring);
1449 new = palloc(N_TYPE, f_type);
1455 * -delete functions --
1457 * True always. Makes it's best shot and continues on regardless.
1460 f_delete(plan, entry)
1464 /* ignore these from fts */
1465 if (strcmp(entry->fts_accpath, ".") == 0 ||
1466 strcmp(entry->fts_accpath, "..") == 0)
1470 if (isdepth == 0 || /* depth off */
1471 (ftsoptions & FTS_NOSTAT) || /* not stat()ing */
1472 !(ftsoptions & FTS_PHYSICAL) || /* physical off */
1473 (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */
1474 errx(1, "-delete: insecure options got turned on");
1476 /* Potentially unsafe - do not accept relative paths whatsoever */
1477 if (strchr(entry->fts_accpath, '/') != NULL)
1478 errx(1, "-delete: %s: relative path potentially not safe",
1479 entry->fts_accpath);
1481 /* Turn off user immutable bits if running as root */
1482 if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
1483 !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
1485 chflags(entry->fts_accpath,
1486 entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
1488 /* rmdir directories, unlink everything else */
1489 if (S_ISDIR(entry->fts_statp->st_mode)) {
1490 if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
1491 warn("-delete: rmdir(%s)", entry->fts_path);
1493 if (unlink(entry->fts_accpath) < 0)
1494 warn("-delete: unlink(%s)", entry->fts_path);
1505 ftsoptions &= ~FTS_NOSTAT; /* no optimise */
1506 ftsoptions |= FTS_PHYSICAL; /* disable -follow */
1507 ftsoptions &= ~FTS_LOGICAL; /* disable -follow */
1508 isoutput = 1; /* possible output */
1509 isdepth = 1; /* -depth implied */
1511 return (palloc(N_DELETE, f_delete));
1515 * -user uname functions --
1517 * True if the file belongs to the user uname. If uname is numeric and
1518 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1519 * return a valid user name, uname is taken as a user ID.
1526 return (entry->fts_statp->st_uid == plan->u_data);
1537 ftsoptions &= ~FTS_NOSTAT;
1539 p = getpwnam(username);
1541 uid = atoi(username);
1542 if (uid == 0 && username[0] != '0')
1543 errx(1, "-user: %s: no such user", username);
1547 new = palloc(N_USER, f_user);
1553 * -xdev functions --
1555 * Always true, causes find not to decend past directories that have a
1556 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1561 ftsoptions |= FTS_XDEV;
1563 return (palloc(N_XDEV, f_always_true));
1567 * ( expression ) functions --
1569 * True if expression is true.
1580 for (p = plan->p_data[0];
1581 p && (state = (p->eval)(p, entry)); p = p->next);
1586 * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers. They are
1587 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1588 * to a N_EXPR node containing the expression and the ')' node is discarded.
1593 return (palloc(N_OPENPAREN, (int (*)())-1));
1599 return (palloc(N_CLOSEPAREN, (int (*)())-1));
1603 * ! expression functions --
1605 * Negation of a primary; the unary NOT operator.
1616 for (p = plan->p_data[0];
1617 p && (state = (p->eval)(p, entry)); p = p->next);
1624 return (palloc(N_NOT, f_not));
1628 * expression -o expression functions --
1630 * Alternation of primaries; the OR operator. The second expression is
1631 * not evaluated if the first expression is true.
1642 for (p = plan->p_data[0];
1643 p && (state = (p->eval)(p, entry)); p = p->next);
1648 for (p = plan->p_data[1];
1649 p && (state = (p->eval)(p, entry)); p = p->next);
1656 return (palloc(N_OR, f_or));
1662 int (*f) __P((PLAN *, FTSENT *));
1666 if ((new = malloc(sizeof(PLAN))) == NULL)