19b50d902SRodney W. Grimes /*- 29b50d902SRodney W. Grimes * Copyright (c) 1990, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * This code is derived from software contributed to Berkeley by 69b50d902SRodney W. Grimes * Cimarron D. Taylor of the University of California, Berkeley. 79b50d902SRodney W. Grimes * 89b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 99b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 109b50d902SRodney W. Grimes * are met: 119b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 129b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 139b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 149b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 159b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 169b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 179b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 189b50d902SRodney W. Grimes * without specific prior written permission. 199b50d902SRodney W. Grimes * 209b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 219b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 229b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 239b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 249b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 259b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 269b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 279b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 289b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 299b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 309b50d902SRodney W. Grimes * SUCH DAMAGE. 319b50d902SRodney W. Grimes */ 329b50d902SRodney W. Grimes 333549ef2fSXin LI #ifndef lint 343549ef2fSXin LI #if 0 353549ef2fSXin LI static const char sccsid[] = "@(#)function.c 8.10 (Berkeley) 5/4/95"; 363549ef2fSXin LI #endif 373549ef2fSXin LI #endif /* not lint */ 383549ef2fSXin LI 393077469eSDavid E. O'Brien #include <sys/cdefs.h> 403077469eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 419b50d902SRodney W. Grimes 429b50d902SRodney W. Grimes #include <sys/param.h> 439b50d902SRodney W. Grimes #include <sys/ucred.h> 449b50d902SRodney W. Grimes #include <sys/stat.h> 459c5d31dfSBosko Milekic #include <sys/types.h> 469c5d31dfSBosko Milekic #include <sys/acl.h> 479b50d902SRodney W. Grimes #include <sys/wait.h> 489b50d902SRodney W. Grimes #include <sys/mount.h> 499b50d902SRodney W. Grimes 50ed1a4621SPeter Wemm #include <dirent.h> 519b50d902SRodney W. Grimes #include <err.h> 529b50d902SRodney W. Grimes #include <errno.h> 539b50d902SRodney W. Grimes #include <fnmatch.h> 549b50d902SRodney W. Grimes #include <fts.h> 559b50d902SRodney W. Grimes #include <grp.h> 565e25d888STim J. Robbins #include <limits.h> 579b50d902SRodney W. Grimes #include <pwd.h> 587c1d4b3aSAkinori MUSHA #include <regex.h> 599b50d902SRodney W. Grimes #include <stdio.h> 609b50d902SRodney W. Grimes #include <stdlib.h> 619b50d902SRodney W. Grimes #include <string.h> 629b50d902SRodney W. Grimes #include <unistd.h> 631c832963SOliver Eikemeier #include <ctype.h> 649b50d902SRodney W. Grimes 659b50d902SRodney W. Grimes #include "find.h" 669b50d902SRodney W. Grimes 67ecca1f1cSMark Murray static PLAN *palloc(OPTION *); 68ecca1f1cSMark Murray static long long find_parsenum(PLAN *, const char *, char *, char *); 69ecca1f1cSMark Murray static long long find_parsetime(PLAN *, const char *, char *); 70ecca1f1cSMark Murray static char *nextarg(OPTION *, char ***); 71ea92232aSPoul-Henning Kamp 72a07af811STim J. Robbins extern char **environ; 73a07af811STim J. Robbins 7422170420SKirill Ponomarev static PLAN *lastexecplus = NULL; 7522170420SKirill Ponomarev 76adff4fcaSRuslan Ermilov #define COMPARE(a, b) do { \ 77ea92232aSPoul-Henning Kamp switch (plan->flags & F_ELG_MASK) { \ 789b50d902SRodney W. Grimes case F_EQUAL: \ 799b50d902SRodney W. Grimes return (a == b); \ 809b50d902SRodney W. Grimes case F_LESSTHAN: \ 819b50d902SRodney W. Grimes return (a < b); \ 829b50d902SRodney W. Grimes case F_GREATER: \ 839b50d902SRodney W. Grimes return (a > b); \ 849b50d902SRodney W. Grimes default: \ 859b50d902SRodney W. Grimes abort(); \ 869b50d902SRodney W. Grimes } \ 87adff4fcaSRuslan Ermilov } while(0) 889b50d902SRodney W. Grimes 89ea92232aSPoul-Henning Kamp static PLAN * 90ef646f18SMark Murray palloc(OPTION *option) 91ea92232aSPoul-Henning Kamp { 92ea92232aSPoul-Henning Kamp PLAN *new; 93ea92232aSPoul-Henning Kamp 94ea92232aSPoul-Henning Kamp if ((new = malloc(sizeof(PLAN))) == NULL) 95ea92232aSPoul-Henning Kamp err(1, NULL); 96ea92232aSPoul-Henning Kamp new->execute = option->execute; 97ea92232aSPoul-Henning Kamp new->flags = option->flags; 98ea92232aSPoul-Henning Kamp new->next = NULL; 99ea92232aSPoul-Henning Kamp return new; 100ea92232aSPoul-Henning Kamp } 1019b50d902SRodney W. Grimes 1029b50d902SRodney W. Grimes /* 1039b50d902SRodney W. Grimes * find_parsenum -- 1049b50d902SRodney W. Grimes * Parse a string of the form [+-]# and return the value. 1059b50d902SRodney W. Grimes */ 1069192bbf4SBruce Evans static long long 107ef646f18SMark Murray find_parsenum(PLAN *plan, const char *option, char *vp, char *endch) 1089b50d902SRodney W. Grimes { 1099192bbf4SBruce Evans long long value; 1109b50d902SRodney W. Grimes char *endchar, *str; /* Pointer to character ending conversion. */ 1119b50d902SRodney W. Grimes 1129b50d902SRodney W. Grimes /* Determine comparison from leading + or -. */ 1139b50d902SRodney W. Grimes str = vp; 1149b50d902SRodney W. Grimes switch (*str) { 1159b50d902SRodney W. Grimes case '+': 1169b50d902SRodney W. Grimes ++str; 117ea92232aSPoul-Henning Kamp plan->flags |= F_GREATER; 1189b50d902SRodney W. Grimes break; 1199b50d902SRodney W. Grimes case '-': 1209b50d902SRodney W. Grimes ++str; 121ea92232aSPoul-Henning Kamp plan->flags |= F_LESSTHAN; 1229b50d902SRodney W. Grimes break; 1239b50d902SRodney W. Grimes default: 124ea92232aSPoul-Henning Kamp plan->flags |= F_EQUAL; 1259b50d902SRodney W. Grimes break; 1269b50d902SRodney W. Grimes } 1279b50d902SRodney W. Grimes 1289b50d902SRodney W. Grimes /* 1299192bbf4SBruce Evans * Convert the string with strtoq(). Note, if strtoq() returns zero 1309b50d902SRodney W. Grimes * and endchar points to the beginning of the string we know we have 1319b50d902SRodney W. Grimes * a syntax error. 1329b50d902SRodney W. Grimes */ 1339192bbf4SBruce Evans value = strtoq(str, &endchar, 10); 1349b50d902SRodney W. Grimes if (value == 0 && endchar == str) 1359b50d902SRodney W. Grimes errx(1, "%s: %s: illegal numeric value", option, vp); 1365a890aacSKirill Ponomarev if (endchar[0] && endch == NULL) 1379b50d902SRodney W. Grimes errx(1, "%s: %s: illegal trailing character", option, vp); 1389b50d902SRodney W. Grimes if (endch) 1399b50d902SRodney W. Grimes *endch = endchar[0]; 140ea92232aSPoul-Henning Kamp return value; 1419b50d902SRodney W. Grimes } 1429b50d902SRodney W. Grimes 1439b50d902SRodney W. Grimes /* 144adff4fcaSRuslan Ermilov * find_parsetime -- 145adff4fcaSRuslan Ermilov * Parse a string of the form [+-]([0-9]+[smhdw]?)+ and return the value. 146adff4fcaSRuslan Ermilov */ 147adff4fcaSRuslan Ermilov static long long 148ef646f18SMark Murray find_parsetime(PLAN *plan, const char *option, char *vp) 149adff4fcaSRuslan Ermilov { 150adff4fcaSRuslan Ermilov long long secs, value; 151adff4fcaSRuslan Ermilov char *str, *unit; /* Pointer to character ending conversion. */ 152adff4fcaSRuslan Ermilov 153adff4fcaSRuslan Ermilov /* Determine comparison from leading + or -. */ 154adff4fcaSRuslan Ermilov str = vp; 155adff4fcaSRuslan Ermilov switch (*str) { 156adff4fcaSRuslan Ermilov case '+': 157adff4fcaSRuslan Ermilov ++str; 158adff4fcaSRuslan Ermilov plan->flags |= F_GREATER; 159adff4fcaSRuslan Ermilov break; 160adff4fcaSRuslan Ermilov case '-': 161adff4fcaSRuslan Ermilov ++str; 162adff4fcaSRuslan Ermilov plan->flags |= F_LESSTHAN; 163adff4fcaSRuslan Ermilov break; 164adff4fcaSRuslan Ermilov default: 165adff4fcaSRuslan Ermilov plan->flags |= F_EQUAL; 166adff4fcaSRuslan Ermilov break; 167adff4fcaSRuslan Ermilov } 168adff4fcaSRuslan Ermilov 169adff4fcaSRuslan Ermilov value = strtoq(str, &unit, 10); 170adff4fcaSRuslan Ermilov if (value == 0 && unit == str) { 171adff4fcaSRuslan Ermilov errx(1, "%s: %s: illegal time value", option, vp); 172adff4fcaSRuslan Ermilov /* NOTREACHED */ 173adff4fcaSRuslan Ermilov } 174adff4fcaSRuslan Ermilov if (*unit == '\0') 175adff4fcaSRuslan Ermilov return value; 176adff4fcaSRuslan Ermilov 177adff4fcaSRuslan Ermilov /* Units syntax. */ 178adff4fcaSRuslan Ermilov secs = 0; 179adff4fcaSRuslan Ermilov for (;;) { 180adff4fcaSRuslan Ermilov switch(*unit) { 181adff4fcaSRuslan Ermilov case 's': /* seconds */ 182adff4fcaSRuslan Ermilov secs += value; 183adff4fcaSRuslan Ermilov break; 184adff4fcaSRuslan Ermilov case 'm': /* minutes */ 185adff4fcaSRuslan Ermilov secs += value * 60; 186adff4fcaSRuslan Ermilov break; 187adff4fcaSRuslan Ermilov case 'h': /* hours */ 188adff4fcaSRuslan Ermilov secs += value * 3600; 189adff4fcaSRuslan Ermilov break; 190adff4fcaSRuslan Ermilov case 'd': /* days */ 191adff4fcaSRuslan Ermilov secs += value * 86400; 192adff4fcaSRuslan Ermilov break; 193adff4fcaSRuslan Ermilov case 'w': /* weeks */ 194adff4fcaSRuslan Ermilov secs += value * 604800; 195adff4fcaSRuslan Ermilov break; 196adff4fcaSRuslan Ermilov default: 197adff4fcaSRuslan Ermilov errx(1, "%s: %s: bad unit '%c'", option, vp, *unit); 198adff4fcaSRuslan Ermilov /* NOTREACHED */ 199adff4fcaSRuslan Ermilov } 200adff4fcaSRuslan Ermilov str = unit + 1; 201adff4fcaSRuslan Ermilov if (*str == '\0') /* EOS */ 202adff4fcaSRuslan Ermilov break; 203adff4fcaSRuslan Ermilov value = strtoq(str, &unit, 10); 204adff4fcaSRuslan Ermilov if (value == 0 && unit == str) { 205adff4fcaSRuslan Ermilov errx(1, "%s: %s: illegal time value", option, vp); 206adff4fcaSRuslan Ermilov /* NOTREACHED */ 207adff4fcaSRuslan Ermilov } 208adff4fcaSRuslan Ermilov if (*unit == '\0') { 209adff4fcaSRuslan Ermilov errx(1, "%s: %s: missing trailing unit", option, vp); 210adff4fcaSRuslan Ermilov /* NOTREACHED */ 211adff4fcaSRuslan Ermilov } 212adff4fcaSRuslan Ermilov } 213adff4fcaSRuslan Ermilov plan->flags |= F_EXACTTIME; 214adff4fcaSRuslan Ermilov return secs; 215adff4fcaSRuslan Ermilov } 216adff4fcaSRuslan Ermilov 217adff4fcaSRuslan Ermilov /* 218ea92232aSPoul-Henning Kamp * nextarg -- 219ea92232aSPoul-Henning Kamp * Check that another argument still exists, return a pointer to it, 220ea92232aSPoul-Henning Kamp * and increment the argument vector pointer. 221ea92232aSPoul-Henning Kamp */ 222ea92232aSPoul-Henning Kamp static char * 223ef646f18SMark Murray nextarg(OPTION *option, char ***argvp) 224ea92232aSPoul-Henning Kamp { 225ea92232aSPoul-Henning Kamp char *arg; 226ea92232aSPoul-Henning Kamp 227ea92232aSPoul-Henning Kamp if ((arg = **argvp) == 0) 228ea92232aSPoul-Henning Kamp errx(1, "%s: requires additional arguments", option->name); 229ea92232aSPoul-Henning Kamp (*argvp)++; 230ea92232aSPoul-Henning Kamp return arg; 231ea92232aSPoul-Henning Kamp } /* nextarg() */ 232ea92232aSPoul-Henning Kamp 233ea92232aSPoul-Henning Kamp /* 23431d53425SCeri Davies * The value of n for the inode times (atime, birthtime, ctime, mtime) is a 23531d53425SCeri Davies * range, i.e. n matches from (n - 1) to n 24 hour periods. This interacts 23631d53425SCeri Davies * with -n, such that "-mtime -1" would be less than 0 days, which isn't what 23731d53425SCeri Davies * the user wanted. Correct so that -1 is "less than 1". 2389b50d902SRodney W. Grimes */ 239ea92232aSPoul-Henning Kamp #define TIME_CORRECT(p) \ 240ea92232aSPoul-Henning Kamp if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \ 2419b50d902SRodney W. Grimes ++((p)->t_data); 2429b50d902SRodney W. Grimes 2439b50d902SRodney W. Grimes /* 244ea92232aSPoul-Henning Kamp * -[acm]min n functions -- 2453f5223f8SWolfram Schneider * 246ea92232aSPoul-Henning Kamp * True if the difference between the 247ea92232aSPoul-Henning Kamp * file access time (-amin) 24831d53425SCeri Davies * file birth time (-Bmin) 249ea92232aSPoul-Henning Kamp * last change of file status information (-cmin) 250ea92232aSPoul-Henning Kamp * file modification time (-mmin) 251ea92232aSPoul-Henning Kamp * and the current time is n min periods. 2523f5223f8SWolfram Schneider */ 2533f5223f8SWolfram Schneider int 254ef646f18SMark Murray f_Xmin(PLAN *plan, FTSENT *entry) 2553f5223f8SWolfram Schneider { 256ea92232aSPoul-Henning Kamp if (plan->flags & F_TIME_C) { 2573f5223f8SWolfram Schneider COMPARE((now - entry->fts_statp->st_ctime + 2583f5223f8SWolfram Schneider 60 - 1) / 60, plan->t_data); 259ea92232aSPoul-Henning Kamp } else if (plan->flags & F_TIME_A) { 260ea92232aSPoul-Henning Kamp COMPARE((now - entry->fts_statp->st_atime + 261ea92232aSPoul-Henning Kamp 60 - 1) / 60, plan->t_data); 26231d53425SCeri Davies } else if (plan->flags & F_TIME_B) { 26331d53425SCeri Davies COMPARE((now - entry->fts_statp->st_birthtime + 26431d53425SCeri Davies 60 - 1) / 60, plan->t_data); 265ea92232aSPoul-Henning Kamp } else { 266ea92232aSPoul-Henning Kamp COMPARE((now - entry->fts_statp->st_mtime + 267ea92232aSPoul-Henning Kamp 60 - 1) / 60, plan->t_data); 268ea92232aSPoul-Henning Kamp } 2693f5223f8SWolfram Schneider } 2703f5223f8SWolfram Schneider 2713f5223f8SWolfram Schneider PLAN * 272ef646f18SMark Murray c_Xmin(OPTION *option, char ***argvp) 2733f5223f8SWolfram Schneider { 274ea92232aSPoul-Henning Kamp char *nmins; 2753f5223f8SWolfram Schneider PLAN *new; 2763f5223f8SWolfram Schneider 277ea92232aSPoul-Henning Kamp nmins = nextarg(option, argvp); 2783f5223f8SWolfram Schneider ftsoptions &= ~FTS_NOSTAT; 2793f5223f8SWolfram Schneider 280ea92232aSPoul-Henning Kamp new = palloc(option); 281ea92232aSPoul-Henning Kamp new->t_data = find_parsenum(new, option->name, nmins, NULL); 282ea92232aSPoul-Henning Kamp TIME_CORRECT(new); 283ea92232aSPoul-Henning Kamp return new; 2843f5223f8SWolfram Schneider } 2853f5223f8SWolfram Schneider 2869b50d902SRodney W. Grimes /* 287ea92232aSPoul-Henning Kamp * -[acm]time n functions -- 2889b50d902SRodney W. Grimes * 289ea92232aSPoul-Henning Kamp * True if the difference between the 290ea92232aSPoul-Henning Kamp * file access time (-atime) 29131d53425SCeri Davies * file birth time (-Btime) 292ea92232aSPoul-Henning Kamp * last change of file status information (-ctime) 293ea92232aSPoul-Henning Kamp * file modification time (-mtime) 294ea92232aSPoul-Henning Kamp * and the current time is n 24 hour periods. 2959b50d902SRodney W. Grimes */ 296ea92232aSPoul-Henning Kamp 2979b50d902SRodney W. Grimes int 298ef646f18SMark Murray f_Xtime(PLAN *plan, FTSENT *entry) 2999b50d902SRodney W. Grimes { 300631a8765SRuslan Ermilov time_t xtime; 301adff4fcaSRuslan Ermilov 302631a8765SRuslan Ermilov if (plan->flags & F_TIME_A) 303631a8765SRuslan Ermilov xtime = entry->fts_statp->st_atime; 30431d53425SCeri Davies else if (plan->flags & F_TIME_B) 30531d53425SCeri Davies xtime = entry->fts_statp->st_birthtime; 306631a8765SRuslan Ermilov else if (plan->flags & F_TIME_C) 307631a8765SRuslan Ermilov xtime = entry->fts_statp->st_ctime; 308631a8765SRuslan Ermilov else 309631a8765SRuslan Ermilov xtime = entry->fts_statp->st_mtime; 3109b50d902SRodney W. Grimes 311631a8765SRuslan Ermilov if (plan->flags & F_EXACTTIME) 312631a8765SRuslan Ermilov COMPARE(now - xtime, plan->t_data); 313adff4fcaSRuslan Ermilov else 314631a8765SRuslan Ermilov COMPARE((now - xtime + 86400 - 1) / 86400, plan->t_data); 3159b50d902SRodney W. Grimes } 3169b50d902SRodney W. Grimes 3179b50d902SRodney W. Grimes PLAN * 318ef646f18SMark Murray c_Xtime(OPTION *option, char ***argvp) 3199b50d902SRodney W. Grimes { 320adff4fcaSRuslan Ermilov char *value; 3219b50d902SRodney W. Grimes PLAN *new; 3229b50d902SRodney W. Grimes 323adff4fcaSRuslan Ermilov value = nextarg(option, argvp); 3249b50d902SRodney W. Grimes ftsoptions &= ~FTS_NOSTAT; 3259b50d902SRodney W. Grimes 326ea92232aSPoul-Henning Kamp new = palloc(option); 327adff4fcaSRuslan Ermilov new->t_data = find_parsetime(new, option->name, value); 328adff4fcaSRuslan Ermilov if (!(new->flags & F_EXACTTIME)) 329ea92232aSPoul-Henning Kamp TIME_CORRECT(new); 330ea92232aSPoul-Henning Kamp return new; 331ea92232aSPoul-Henning Kamp } 332ea92232aSPoul-Henning Kamp 333ea92232aSPoul-Henning Kamp /* 334ea92232aSPoul-Henning Kamp * -maxdepth/-mindepth n functions -- 335ea92232aSPoul-Henning Kamp * 336ea92232aSPoul-Henning Kamp * Does the same as -prune if the level of the current file is 337ea92232aSPoul-Henning Kamp * greater/less than the specified maximum/minimum depth. 338ea92232aSPoul-Henning Kamp * 339ea92232aSPoul-Henning Kamp * Note that -maxdepth and -mindepth are handled specially in 340ea92232aSPoul-Henning Kamp * find_execute() so their f_* functions are set to f_always_true(). 341ea92232aSPoul-Henning Kamp */ 342ea92232aSPoul-Henning Kamp PLAN * 343ef646f18SMark Murray c_mXXdepth(OPTION *option, char ***argvp) 344ea92232aSPoul-Henning Kamp { 345ea92232aSPoul-Henning Kamp char *dstr; 346ea92232aSPoul-Henning Kamp PLAN *new; 347ea92232aSPoul-Henning Kamp 348ea92232aSPoul-Henning Kamp dstr = nextarg(option, argvp); 349ea92232aSPoul-Henning Kamp if (dstr[0] == '-') 350ea92232aSPoul-Henning Kamp /* all other errors handled by find_parsenum() */ 351ea92232aSPoul-Henning Kamp errx(1, "%s: %s: value must be positive", option->name, dstr); 352ea92232aSPoul-Henning Kamp 353ea92232aSPoul-Henning Kamp new = palloc(option); 354ea92232aSPoul-Henning Kamp if (option->flags & F_MAXDEPTH) 355ea92232aSPoul-Henning Kamp maxdepth = find_parsenum(new, option->name, dstr, NULL); 356ea92232aSPoul-Henning Kamp else 357ea92232aSPoul-Henning Kamp mindepth = find_parsenum(new, option->name, dstr, NULL); 358ea92232aSPoul-Henning Kamp return new; 359ea92232aSPoul-Henning Kamp } 360ea92232aSPoul-Henning Kamp 361ea92232aSPoul-Henning Kamp /* 3629c5d31dfSBosko Milekic * -acl function -- 3639c5d31dfSBosko Milekic * 3649c5d31dfSBosko Milekic * Show files with EXTENDED ACL attributes. 3659c5d31dfSBosko Milekic */ 3669c5d31dfSBosko Milekic int 3679c5d31dfSBosko Milekic f_acl(PLAN *plan __unused, FTSENT *entry) 3689c5d31dfSBosko Milekic { 3699c5d31dfSBosko Milekic acl_t facl; 370fa2db914SEdward Tomasz Napierala acl_type_t acl_type; 371fa2db914SEdward Tomasz Napierala int acl_supported = 0, ret, trivial; 3729c5d31dfSBosko Milekic 3739c5d31dfSBosko Milekic if (S_ISLNK(entry->fts_statp->st_mode)) 3749c5d31dfSBosko Milekic return 0; 375fa2db914SEdward Tomasz Napierala ret = pathconf(entry->fts_accpath, _PC_ACL_NFS4); 376fa2db914SEdward Tomasz Napierala if (ret > 0) { 377fa2db914SEdward Tomasz Napierala acl_supported = 1; 378fa2db914SEdward Tomasz Napierala acl_type = ACL_TYPE_NFS4; 379fa2db914SEdward Tomasz Napierala } else if (ret < 0 && errno != EINVAL) { 3809c5d31dfSBosko Milekic warn("%s", entry->fts_accpath); 381fa2db914SEdward Tomasz Napierala return (0); 3829c5d31dfSBosko Milekic } 383fa2db914SEdward Tomasz Napierala if (acl_supported == 0) { 384fa2db914SEdward Tomasz Napierala ret = pathconf(entry->fts_accpath, _PC_ACL_EXTENDED); 385fa2db914SEdward Tomasz Napierala if (ret > 0) { 386fa2db914SEdward Tomasz Napierala acl_supported = 1; 387fa2db914SEdward Tomasz Napierala acl_type = ACL_TYPE_ACCESS; 388fa2db914SEdward Tomasz Napierala } else if (ret < 0 && errno != EINVAL) { 389fa2db914SEdward Tomasz Napierala warn("%s", entry->fts_accpath); 390fa2db914SEdward Tomasz Napierala return (0); 3919c5d31dfSBosko Milekic } 3929c5d31dfSBosko Milekic } 393fa2db914SEdward Tomasz Napierala if (acl_supported == 0) 394fa2db914SEdward Tomasz Napierala return (0); 395fa2db914SEdward Tomasz Napierala 396fa2db914SEdward Tomasz Napierala facl = acl_get_file(entry->fts_accpath, acl_type); 397fa2db914SEdward Tomasz Napierala if (facl == NULL) { 398fa2db914SEdward Tomasz Napierala warn("%s", entry->fts_accpath); 399fa2db914SEdward Tomasz Napierala return (0); 4009c5d31dfSBosko Milekic } 401fa2db914SEdward Tomasz Napierala ret = acl_is_trivial_np(facl, &trivial); 4029c5d31dfSBosko Milekic acl_free(facl); 403fa2db914SEdward Tomasz Napierala if (ret) { 4049c5d31dfSBosko Milekic warn("%s", entry->fts_accpath); 405fa2db914SEdward Tomasz Napierala acl_free(facl); 406fa2db914SEdward Tomasz Napierala return (0); 407fa2db914SEdward Tomasz Napierala } 408fa2db914SEdward Tomasz Napierala if (trivial) 409fa2db914SEdward Tomasz Napierala return (0); 410fa2db914SEdward Tomasz Napierala return (1); 4119c5d31dfSBosko Milekic } 4129c5d31dfSBosko Milekic 4139c5d31dfSBosko Milekic PLAN * 4149c5d31dfSBosko Milekic c_acl(OPTION *option, char ***argvp __unused) 4159c5d31dfSBosko Milekic { 4169c5d31dfSBosko Milekic ftsoptions &= ~FTS_NOSTAT; 4179c5d31dfSBosko Milekic return (palloc(option)); 4189c5d31dfSBosko Milekic } 4199c5d31dfSBosko Milekic 4209c5d31dfSBosko Milekic /* 421ea92232aSPoul-Henning Kamp * -delete functions -- 422ea92232aSPoul-Henning Kamp * 423ea92232aSPoul-Henning Kamp * True always. Makes its best shot and continues on regardless. 424ea92232aSPoul-Henning Kamp */ 425ea92232aSPoul-Henning Kamp int 426ef646f18SMark Murray f_delete(PLAN *plan __unused, FTSENT *entry) 427ea92232aSPoul-Henning Kamp { 428ea92232aSPoul-Henning Kamp /* ignore these from fts */ 429ea92232aSPoul-Henning Kamp if (strcmp(entry->fts_accpath, ".") == 0 || 430ea92232aSPoul-Henning Kamp strcmp(entry->fts_accpath, "..") == 0) 431ea92232aSPoul-Henning Kamp return 1; 432ea92232aSPoul-Henning Kamp 433ea92232aSPoul-Henning Kamp /* sanity check */ 434ea92232aSPoul-Henning Kamp if (isdepth == 0 || /* depth off */ 43505e605b7SAndriy Gapon (ftsoptions & FTS_NOSTAT)) /* not stat()ing */ 436ea92232aSPoul-Henning Kamp errx(1, "-delete: insecure options got turned on"); 437ea92232aSPoul-Henning Kamp 43805e605b7SAndriy Gapon if (!(ftsoptions & FTS_PHYSICAL) || /* physical off */ 43905e605b7SAndriy Gapon (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */ 44005e605b7SAndriy Gapon errx(1, "-delete: forbidden when symlinks are followed"); 44105e605b7SAndriy Gapon 442ea92232aSPoul-Henning Kamp /* Potentially unsafe - do not accept relative paths whatsoever */ 443ea92232aSPoul-Henning Kamp if (strchr(entry->fts_accpath, '/') != NULL) 444ea92232aSPoul-Henning Kamp errx(1, "-delete: %s: relative path potentially not safe", 445ea92232aSPoul-Henning Kamp entry->fts_accpath); 446ea92232aSPoul-Henning Kamp 447ea92232aSPoul-Henning Kamp /* Turn off user immutable bits if running as root */ 448ea92232aSPoul-Henning Kamp if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) && 449ea92232aSPoul-Henning Kamp !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) && 450ea92232aSPoul-Henning Kamp geteuid() == 0) 4516911f596SJilles Tjoelker lchflags(entry->fts_accpath, 452ea92232aSPoul-Henning Kamp entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)); 453ea92232aSPoul-Henning Kamp 454ea92232aSPoul-Henning Kamp /* rmdir directories, unlink everything else */ 455ea92232aSPoul-Henning Kamp if (S_ISDIR(entry->fts_statp->st_mode)) { 456ea92232aSPoul-Henning Kamp if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY) 457ea92232aSPoul-Henning Kamp warn("-delete: rmdir(%s)", entry->fts_path); 458ea92232aSPoul-Henning Kamp } else { 459ea92232aSPoul-Henning Kamp if (unlink(entry->fts_accpath) < 0) 460ea92232aSPoul-Henning Kamp warn("-delete: unlink(%s)", entry->fts_path); 461ea92232aSPoul-Henning Kamp } 462ea92232aSPoul-Henning Kamp 463ea92232aSPoul-Henning Kamp /* "succeed" */ 464ea92232aSPoul-Henning Kamp return 1; 465ea92232aSPoul-Henning Kamp } 466ea92232aSPoul-Henning Kamp 467ea92232aSPoul-Henning Kamp PLAN * 468ef646f18SMark Murray c_delete(OPTION *option, char ***argvp __unused) 469ea92232aSPoul-Henning Kamp { 470ea92232aSPoul-Henning Kamp 471ea92232aSPoul-Henning Kamp ftsoptions &= ~FTS_NOSTAT; /* no optimise */ 472ea92232aSPoul-Henning Kamp isoutput = 1; /* possible output */ 473ea92232aSPoul-Henning Kamp isdepth = 1; /* -depth implied */ 474ea92232aSPoul-Henning Kamp 475*17ef6d3aSJilles Tjoelker /* 476*17ef6d3aSJilles Tjoelker * Try to avoid the confusing error message about relative paths 477*17ef6d3aSJilles Tjoelker * being potentially not safe. 478*17ef6d3aSJilles Tjoelker */ 479*17ef6d3aSJilles Tjoelker if (ftsoptions & FTS_NOCHDIR) 480*17ef6d3aSJilles Tjoelker errx(1, "%s: forbidden when the current directory cannot be opened", 481*17ef6d3aSJilles Tjoelker "-delete"); 482*17ef6d3aSJilles Tjoelker 483ea92232aSPoul-Henning Kamp return palloc(option); 4849b50d902SRodney W. Grimes } 4859b50d902SRodney W. Grimes 4863f5223f8SWolfram Schneider 4879b50d902SRodney W. Grimes /* 4881c832963SOliver Eikemeier * always_true -- 4899b50d902SRodney W. Grimes * 49046b993ffSWarner Losh * Always true, used for -maxdepth, -mindepth, -xdev, -follow, and -true 4919b50d902SRodney W. Grimes */ 4929b50d902SRodney W. Grimes int 493ef646f18SMark Murray f_always_true(PLAN *plan __unused, FTSENT *entry __unused) 4949b50d902SRodney W. Grimes { 495ea92232aSPoul-Henning Kamp return 1; 4969b50d902SRodney W. Grimes } 4979b50d902SRodney W. Grimes 4981c832963SOliver Eikemeier /* 4991c832963SOliver Eikemeier * -depth functions -- 5001c832963SOliver Eikemeier * 5011c832963SOliver Eikemeier * With argument: True if the file is at level n. 5021c832963SOliver Eikemeier * Without argument: Always true, causes descent of the directory hierarchy 5031c832963SOliver Eikemeier * to be done so that all entries in a directory are acted on before the 5041c832963SOliver Eikemeier * directory itself. 5051c832963SOliver Eikemeier */ 5061c832963SOliver Eikemeier int 5071c832963SOliver Eikemeier f_depth(PLAN *plan, FTSENT *entry) 5089b50d902SRodney W. Grimes { 5091c832963SOliver Eikemeier if (plan->flags & F_DEPTH) 5101c832963SOliver Eikemeier COMPARE(entry->fts_level, plan->d_data); 5111c832963SOliver Eikemeier else 5121c832963SOliver Eikemeier return 1; 5131c832963SOliver Eikemeier } 5149b50d902SRodney W. Grimes 5151c832963SOliver Eikemeier PLAN * 5161c832963SOliver Eikemeier c_depth(OPTION *option, char ***argvp) 5171c832963SOliver Eikemeier { 5181c832963SOliver Eikemeier PLAN *new; 5191c832963SOliver Eikemeier char *str; 5201c832963SOliver Eikemeier 5211c832963SOliver Eikemeier new = palloc(option); 5221c832963SOliver Eikemeier 5231c832963SOliver Eikemeier str = **argvp; 5241c832963SOliver Eikemeier if (str && !(new->flags & F_DEPTH)) { 5251c832963SOliver Eikemeier /* skip leading + or - */ 5261c832963SOliver Eikemeier if (*str == '+' || *str == '-') 5271c832963SOliver Eikemeier str++; 5281c832963SOliver Eikemeier /* skip sign */ 5291c832963SOliver Eikemeier if (*str == '+' || *str == '-') 5301c832963SOliver Eikemeier str++; 5311c832963SOliver Eikemeier if (isdigit(*str)) 5321c832963SOliver Eikemeier new->flags |= F_DEPTH; 5331c832963SOliver Eikemeier } 5341c832963SOliver Eikemeier 5351c832963SOliver Eikemeier if (new->flags & F_DEPTH) { /* -depth n */ 5361c832963SOliver Eikemeier char *ndepth; 5371c832963SOliver Eikemeier 5381c832963SOliver Eikemeier ndepth = nextarg(option, argvp); 5391c832963SOliver Eikemeier new->d_data = find_parsenum(new, option->name, ndepth, NULL); 5401c832963SOliver Eikemeier } else { /* -d */ 5411c832963SOliver Eikemeier isdepth = 1; 5421c832963SOliver Eikemeier } 5431c832963SOliver Eikemeier 5441c832963SOliver Eikemeier return new; 5459b50d902SRodney W. Grimes } 546127d7563SWarner Losh 547127d7563SWarner Losh /* 548ed1a4621SPeter Wemm * -empty functions -- 549ed1a4621SPeter Wemm * 550ed1a4621SPeter Wemm * True if the file or directory is empty 551ed1a4621SPeter Wemm */ 552ed1a4621SPeter Wemm int 553ef646f18SMark Murray f_empty(PLAN *plan __unused, FTSENT *entry) 554ed1a4621SPeter Wemm { 555ea92232aSPoul-Henning Kamp if (S_ISREG(entry->fts_statp->st_mode) && 556ea92232aSPoul-Henning Kamp entry->fts_statp->st_size == 0) 557ea92232aSPoul-Henning Kamp return 1; 558ed1a4621SPeter Wemm if (S_ISDIR(entry->fts_statp->st_mode)) { 559ed1a4621SPeter Wemm struct dirent *dp; 560ed1a4621SPeter Wemm int empty; 561ed1a4621SPeter Wemm DIR *dir; 562ed1a4621SPeter Wemm 563ed1a4621SPeter Wemm empty = 1; 564ed1a4621SPeter Wemm dir = opendir(entry->fts_accpath); 565ed1a4621SPeter Wemm if (dir == NULL) 566e66a677bSKevin Lo return 0; 567ed1a4621SPeter Wemm for (dp = readdir(dir); dp; dp = readdir(dir)) 568ed1a4621SPeter Wemm if (dp->d_name[0] != '.' || 569ed1a4621SPeter Wemm (dp->d_name[1] != '\0' && 570ed1a4621SPeter Wemm (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) { 571ed1a4621SPeter Wemm empty = 0; 572ed1a4621SPeter Wemm break; 573ed1a4621SPeter Wemm } 574ed1a4621SPeter Wemm closedir(dir); 575ea92232aSPoul-Henning Kamp return empty; 576ed1a4621SPeter Wemm } 577ea92232aSPoul-Henning Kamp return 0; 578ed1a4621SPeter Wemm } 579ed1a4621SPeter Wemm 580ed1a4621SPeter Wemm PLAN * 581ef646f18SMark Murray c_empty(OPTION *option, char ***argvp __unused) 582ed1a4621SPeter Wemm { 583ed1a4621SPeter Wemm ftsoptions &= ~FTS_NOSTAT; 584ed1a4621SPeter Wemm 585ea92232aSPoul-Henning Kamp return palloc(option); 586ed1a4621SPeter Wemm } 587ed1a4621SPeter Wemm 588ed1a4621SPeter Wemm /* 589ea92232aSPoul-Henning Kamp * [-exec | -execdir | -ok] utility [arg ... ] ; functions -- 590127d7563SWarner Losh * 591127d7563SWarner Losh * True if the executed utility returns a zero value as exit status. 592127d7563SWarner Losh * The end of the primary expression is delimited by a semicolon. If 593ea92232aSPoul-Henning Kamp * "{}" occurs anywhere, it gets replaced by the current pathname, 594ea92232aSPoul-Henning Kamp * or, in the case of -execdir, the current basename (filename 595ea92232aSPoul-Henning Kamp * without leading directory prefix). For -exec and -ok, 596ea92232aSPoul-Henning Kamp * the current directory for the execution of utility is the same as 597ea92232aSPoul-Henning Kamp * the current directory when the find utility was started, whereas 598ea92232aSPoul-Henning Kamp * for -execdir, it is the directory the file resides in. 599ea92232aSPoul-Henning Kamp * 600ea92232aSPoul-Henning Kamp * The primary -ok differs from -exec in that it requests affirmation 601ea92232aSPoul-Henning Kamp * of the user before executing the utility. 602127d7563SWarner Losh */ 603127d7563SWarner Losh int 604ef646f18SMark Murray f_exec(PLAN *plan, FTSENT *entry) 605127d7563SWarner Losh { 606e98080b1SDavid Malone int cnt; 607127d7563SWarner Losh pid_t pid; 608127d7563SWarner Losh int status; 609127d7563SWarner Losh char *file; 610127d7563SWarner Losh 6115e25d888STim J. Robbins if (entry == NULL && plan->flags & F_EXECPLUS) { 6125e25d888STim J. Robbins if (plan->e_ppos == plan->e_pbnum) 6135e25d888STim J. Robbins return (1); 6145e25d888STim J. Robbins plan->e_argv[plan->e_ppos] = NULL; 6155e25d888STim J. Robbins goto doexec; 6165e25d888STim J. Robbins } 6175e25d888STim J. Robbins 618127d7563SWarner Losh /* XXX - if file/dir ends in '/' this will not work -- can it? */ 619ea92232aSPoul-Henning Kamp if ((plan->flags & F_EXECDIR) && \ 620ea92232aSPoul-Henning Kamp (file = strrchr(entry->fts_path, '/'))) 621127d7563SWarner Losh file++; 622127d7563SWarner Losh else 623127d7563SWarner Losh file = entry->fts_path; 624127d7563SWarner Losh 6255e25d888STim J. Robbins if (plan->flags & F_EXECPLUS) { 6265e25d888STim J. Robbins if ((plan->e_argv[plan->e_ppos] = strdup(file)) == NULL) 6275e25d888STim J. Robbins err(1, NULL); 6285e25d888STim J. Robbins plan->e_len[plan->e_ppos] = strlen(file); 6295e25d888STim J. Robbins plan->e_psize += plan->e_len[plan->e_ppos]; 6305e25d888STim J. Robbins if (++plan->e_ppos < plan->e_pnummax && 6315e25d888STim J. Robbins plan->e_psize < plan->e_psizemax) 6325e25d888STim J. Robbins return (1); 6335e25d888STim J. Robbins plan->e_argv[plan->e_ppos] = NULL; 6345e25d888STim J. Robbins } else { 635127d7563SWarner Losh for (cnt = 0; plan->e_argv[cnt]; ++cnt) 636127d7563SWarner Losh if (plan->e_len[cnt]) 6375e25d888STim J. Robbins brace_subst(plan->e_orig[cnt], 6385e25d888STim J. Robbins &plan->e_argv[cnt], file, 6395e25d888STim J. Robbins plan->e_len[cnt]); 6405e25d888STim J. Robbins } 641127d7563SWarner Losh 6425e25d888STim J. Robbins doexec: if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv)) 643ea92232aSPoul-Henning Kamp return 0; 644ea92232aSPoul-Henning Kamp 645ea92232aSPoul-Henning Kamp /* make sure find output is interspersed correctly with subprocesses */ 646127d7563SWarner Losh fflush(stdout); 647127d7563SWarner Losh fflush(stderr); 648127d7563SWarner Losh 6491fd98d7dSDag-Erling Smørgrav switch (pid = fork()) { 650127d7563SWarner Losh case -1: 651127d7563SWarner Losh err(1, "fork"); 652127d7563SWarner Losh /* NOTREACHED */ 653127d7563SWarner Losh case 0: 654ea92232aSPoul-Henning Kamp /* change dir back from where we started */ 655*17ef6d3aSJilles Tjoelker if (!(plan->flags & F_EXECDIR) && 656*17ef6d3aSJilles Tjoelker !(ftsoptions & FTS_NOCHDIR) && fchdir(dotfd)) { 657ea92232aSPoul-Henning Kamp warn("chdir"); 658ea92232aSPoul-Henning Kamp _exit(1); 659ea92232aSPoul-Henning Kamp } 660127d7563SWarner Losh execvp(plan->e_argv[0], plan->e_argv); 661127d7563SWarner Losh warn("%s", plan->e_argv[0]); 662127d7563SWarner Losh _exit(1); 663127d7563SWarner Losh } 6645e25d888STim J. Robbins if (plan->flags & F_EXECPLUS) { 6655e25d888STim J. Robbins while (--plan->e_ppos >= plan->e_pbnum) 6665e25d888STim J. Robbins free(plan->e_argv[plan->e_ppos]); 6675e25d888STim J. Robbins plan->e_ppos = plan->e_pbnum; 6685e25d888STim J. Robbins plan->e_psize = plan->e_pbsize; 6695e25d888STim J. Robbins } 670127d7563SWarner Losh pid = waitpid(pid, &status, 0); 671127d7563SWarner Losh return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status)); 672127d7563SWarner Losh } 673127d7563SWarner Losh 674127d7563SWarner Losh /* 675ea92232aSPoul-Henning Kamp * c_exec, c_execdir, c_ok -- 676127d7563SWarner Losh * build three parallel arrays, one with pointers to the strings passed 677127d7563SWarner Losh * on the command line, one with (possibly duplicated) pointers to the 678127d7563SWarner Losh * argv array, and one with integer values that are lengths of the 679127d7563SWarner Losh * strings, but also flags meaning that the string has to be massaged. 680127d7563SWarner Losh */ 681127d7563SWarner Losh PLAN * 682ef646f18SMark Murray c_exec(OPTION *option, char ***argvp) 683127d7563SWarner Losh { 684127d7563SWarner Losh PLAN *new; /* node returned */ 6855e25d888STim J. Robbins long argmax; 6865e25d888STim J. Robbins int cnt, i; 687a07af811STim J. Robbins char **argv, **ap, **ep, *p; 688127d7563SWarner Losh 689*17ef6d3aSJilles Tjoelker /* This would defeat -execdir's intended security. */ 690*17ef6d3aSJilles Tjoelker if (option->flags & F_EXECDIR && ftsoptions & FTS_NOCHDIR) 691*17ef6d3aSJilles Tjoelker errx(1, "%s: forbidden when the current directory cannot be opened", 692*17ef6d3aSJilles Tjoelker "-execdir"); 693*17ef6d3aSJilles Tjoelker 694ea92232aSPoul-Henning Kamp /* XXX - was in c_execdir, but seems unnecessary!? 695127d7563SWarner Losh ftsoptions &= ~FTS_NOSTAT; 696ea92232aSPoul-Henning Kamp */ 697127d7563SWarner Losh isoutput = 1; 698127d7563SWarner Losh 699ea92232aSPoul-Henning Kamp /* XXX - this is a change from the previous coding */ 700ea92232aSPoul-Henning Kamp new = palloc(option); 701127d7563SWarner Losh 702127d7563SWarner Losh for (ap = argv = *argvp;; ++ap) { 703127d7563SWarner Losh if (!*ap) 704127d7563SWarner Losh errx(1, 705e22bb9dbSTim J. Robbins "%s: no terminating \";\" or \"+\"", option->name); 706127d7563SWarner Losh if (**ap == ';') 707127d7563SWarner Losh break; 7085e25d888STim J. Robbins if (**ap == '+' && ap != argv && strcmp(*(ap - 1), "{}") == 0) { 7095e25d888STim J. Robbins new->flags |= F_EXECPLUS; 7105e25d888STim J. Robbins break; 7115e25d888STim J. Robbins } 712127d7563SWarner Losh } 713127d7563SWarner Losh 71451b0534fSJuli Mallett if (ap == argv) 71551b0534fSJuli Mallett errx(1, "%s: no command specified", option->name); 71651b0534fSJuli Mallett 717127d7563SWarner Losh cnt = ap - *argvp + 1; 7185e25d888STim J. Robbins if (new->flags & F_EXECPLUS) { 7195e25d888STim J. Robbins new->e_ppos = new->e_pbnum = cnt - 2; 7205e25d888STim J. Robbins if ((argmax = sysconf(_SC_ARG_MAX)) == -1) { 7215e25d888STim J. Robbins warn("sysconf(_SC_ARG_MAX)"); 7225e25d888STim J. Robbins argmax = _POSIX_ARG_MAX; 7235e25d888STim J. Robbins } 724a07af811STim J. Robbins argmax -= 1024; 725a07af811STim J. Robbins for (ep = environ; *ep != NULL; ep++) 726a07af811STim J. Robbins argmax -= strlen(*ep) + 1 + sizeof(*ep); 727a07af811STim J. Robbins argmax -= 1 + sizeof(*ep); 728bc626176SJilles Tjoelker /* 729bc626176SJilles Tjoelker * Ensure that -execdir ... {} + does not mix files 730bc626176SJilles Tjoelker * from different directories in one invocation. 731bc626176SJilles Tjoelker * Files from the same directory should be handled 732bc626176SJilles Tjoelker * in one invocation but there is no code for it. 733bc626176SJilles Tjoelker */ 734bc626176SJilles Tjoelker new->e_pnummax = new->flags & F_EXECDIR ? 1 : argmax / 16; 735a07af811STim J. Robbins argmax -= sizeof(char *) * new->e_pnummax; 736a07af811STim J. Robbins if (argmax <= 0) 737a07af811STim J. Robbins errx(1, "no space for arguments"); 738a07af811STim J. Robbins new->e_psizemax = argmax; 7395e25d888STim J. Robbins new->e_pbsize = 0; 7405e25d888STim J. Robbins cnt += new->e_pnummax + 1; 74122170420SKirill Ponomarev new->e_next = lastexecplus; 74222170420SKirill Ponomarev lastexecplus = new; 7435e25d888STim J. Robbins } 74447bca8b0SJuli Mallett if ((new->e_argv = malloc(cnt * sizeof(char *))) == NULL) 74547bca8b0SJuli Mallett err(1, NULL); 74647bca8b0SJuli Mallett if ((new->e_orig = malloc(cnt * sizeof(char *))) == NULL) 74747bca8b0SJuli Mallett err(1, NULL); 74847bca8b0SJuli Mallett if ((new->e_len = malloc(cnt * sizeof(int))) == NULL) 74947bca8b0SJuli Mallett err(1, NULL); 750127d7563SWarner Losh 751127d7563SWarner Losh for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) { 752127d7563SWarner Losh new->e_orig[cnt] = *argv; 7535e25d888STim J. Robbins if (new->flags & F_EXECPLUS) 7545e25d888STim J. Robbins new->e_pbsize += strlen(*argv) + 1; 755127d7563SWarner Losh for (p = *argv; *p; ++p) 7565e25d888STim J. Robbins if (!(new->flags & F_EXECPLUS) && p[0] == '{' && 7575e25d888STim J. Robbins p[1] == '}') { 758f0cb9537SDavid E. O'Brien if ((new->e_argv[cnt] = 75947bca8b0SJuli Mallett malloc(MAXPATHLEN)) == NULL) 76047bca8b0SJuli Mallett err(1, NULL); 761127d7563SWarner Losh new->e_len[cnt] = MAXPATHLEN; 762127d7563SWarner Losh break; 763127d7563SWarner Losh } 764127d7563SWarner Losh if (!*p) { 765127d7563SWarner Losh new->e_argv[cnt] = *argv; 766127d7563SWarner Losh new->e_len[cnt] = 0; 767127d7563SWarner Losh } 768127d7563SWarner Losh } 7695e25d888STim J. Robbins if (new->flags & F_EXECPLUS) { 7705e25d888STim J. Robbins new->e_psize = new->e_pbsize; 7715e25d888STim J. Robbins cnt--; 7725e25d888STim J. Robbins for (i = 0; i < new->e_pnummax; i++) { 7735e25d888STim J. Robbins new->e_argv[cnt] = NULL; 7745e25d888STim J. Robbins new->e_len[cnt] = 0; 7755e25d888STim J. Robbins cnt++; 7765e25d888STim J. Robbins } 7775e25d888STim J. Robbins argv = ap; 7785e25d888STim J. Robbins goto done; 7795e25d888STim J. Robbins } 780127d7563SWarner Losh new->e_argv[cnt] = new->e_orig[cnt] = NULL; 781127d7563SWarner Losh 7825e25d888STim J. Robbins done: *argvp = argv + 1; 783ea92232aSPoul-Henning Kamp return new; 784ea92232aSPoul-Henning Kamp } 785ea92232aSPoul-Henning Kamp 78622170420SKirill Ponomarev /* Finish any pending -exec ... {} + functions. */ 78722170420SKirill Ponomarev void 78810bc3a7fSEd Schouten finish_execplus(void) 78922170420SKirill Ponomarev { 79022170420SKirill Ponomarev PLAN *p; 79122170420SKirill Ponomarev 79222170420SKirill Ponomarev p = lastexecplus; 79322170420SKirill Ponomarev while (p != NULL) { 79422170420SKirill Ponomarev (p->execute)(p, NULL); 79522170420SKirill Ponomarev p = p->e_next; 79622170420SKirill Ponomarev } 79722170420SKirill Ponomarev } 79822170420SKirill Ponomarev 799ea92232aSPoul-Henning Kamp int 800ef646f18SMark Murray f_flags(PLAN *plan, FTSENT *entry) 801ea92232aSPoul-Henning Kamp { 802ea92232aSPoul-Henning Kamp u_long flags; 803ea92232aSPoul-Henning Kamp 8047fd5ee41SRuslan Ermilov flags = entry->fts_statp->st_flags; 805ea92232aSPoul-Henning Kamp if (plan->flags & F_ATLEAST) 8067fd5ee41SRuslan Ermilov return (flags | plan->fl_flags) == flags && 8077fd5ee41SRuslan Ermilov !(flags & plan->fl_notflags); 8087fd5ee41SRuslan Ermilov else if (plan->flags & F_ANY) 8097fd5ee41SRuslan Ermilov return (flags & plan->fl_flags) || 8107fd5ee41SRuslan Ermilov (flags | plan->fl_notflags) != flags; 811ea92232aSPoul-Henning Kamp else 8127fd5ee41SRuslan Ermilov return flags == plan->fl_flags && 8137fd5ee41SRuslan Ermilov !(plan->fl_flags & plan->fl_notflags); 814ea92232aSPoul-Henning Kamp } 815ea92232aSPoul-Henning Kamp 816ea92232aSPoul-Henning Kamp PLAN * 817ef646f18SMark Murray c_flags(OPTION *option, char ***argvp) 818ea92232aSPoul-Henning Kamp { 819ea92232aSPoul-Henning Kamp char *flags_str; 820ea92232aSPoul-Henning Kamp PLAN *new; 821ea92232aSPoul-Henning Kamp u_long flags, notflags; 822ea92232aSPoul-Henning Kamp 823ea92232aSPoul-Henning Kamp flags_str = nextarg(option, argvp); 824ea92232aSPoul-Henning Kamp ftsoptions &= ~FTS_NOSTAT; 825ea92232aSPoul-Henning Kamp 826ea92232aSPoul-Henning Kamp new = palloc(option); 827ea92232aSPoul-Henning Kamp 828ea92232aSPoul-Henning Kamp if (*flags_str == '-') { 829ea92232aSPoul-Henning Kamp new->flags |= F_ATLEAST; 830ea92232aSPoul-Henning Kamp flags_str++; 8317fd5ee41SRuslan Ermilov } else if (*flags_str == '+') { 8327fd5ee41SRuslan Ermilov new->flags |= F_ANY; 8337fd5ee41SRuslan Ermilov flags_str++; 834ea92232aSPoul-Henning Kamp } 835ea92232aSPoul-Henning Kamp if (strtofflags(&flags_str, &flags, ¬flags) == 1) 836ea92232aSPoul-Henning Kamp errx(1, "%s: %s: illegal flags string", option->name, flags_str); 837ea92232aSPoul-Henning Kamp 838ea92232aSPoul-Henning Kamp new->fl_flags = flags; 8397fd5ee41SRuslan Ermilov new->fl_notflags = notflags; 840ea92232aSPoul-Henning Kamp return new; 841127d7563SWarner Losh } 8429b50d902SRodney W. Grimes 8439b50d902SRodney W. Grimes /* 8449b50d902SRodney W. Grimes * -follow functions -- 8459b50d902SRodney W. Grimes * 8469b50d902SRodney W. Grimes * Always true, causes symbolic links to be followed on a global 8479b50d902SRodney W. Grimes * basis. 8489b50d902SRodney W. Grimes */ 8499b50d902SRodney W. Grimes PLAN * 850ef646f18SMark Murray c_follow(OPTION *option, char ***argvp __unused) 8519b50d902SRodney W. Grimes { 8529b50d902SRodney W. Grimes ftsoptions &= ~FTS_PHYSICAL; 8539b50d902SRodney W. Grimes ftsoptions |= FTS_LOGICAL; 8549b50d902SRodney W. Grimes 855ea92232aSPoul-Henning Kamp return palloc(option); 8569b50d902SRodney W. Grimes } 8579b50d902SRodney W. Grimes 8589b50d902SRodney W. Grimes /* 8599b50d902SRodney W. Grimes * -fstype functions -- 8609b50d902SRodney W. Grimes * 8619b50d902SRodney W. Grimes * True if the file is of a certain type. 8629b50d902SRodney W. Grimes */ 8639b50d902SRodney W. Grimes int 864ef646f18SMark Murray f_fstype(PLAN *plan, FTSENT *entry) 8659b50d902SRodney W. Grimes { 8669b50d902SRodney W. Grimes static dev_t curdev; /* need a guaranteed illegal dev value */ 8679b50d902SRodney W. Grimes static int first = 1; 8689b50d902SRodney W. Grimes struct statfs sb; 8696ab780e5STai-hwa Liang static int val_flags; 8706ab780e5STai-hwa Liang static char fstype[sizeof(sb.f_fstypename)]; 8718a0a76b8SOllivier Robert char *p, save[2] = {0,0}; 8729b50d902SRodney W. Grimes 873ea92232aSPoul-Henning Kamp if ((plan->flags & F_MTMASK) == F_MTUNKNOWN) 874ea92232aSPoul-Henning Kamp return 0; 875ea92232aSPoul-Henning Kamp 8769b50d902SRodney W. Grimes /* Only check when we cross mount point. */ 8779b50d902SRodney W. Grimes if (first || curdev != entry->fts_statp->st_dev) { 8789b50d902SRodney W. Grimes curdev = entry->fts_statp->st_dev; 8799b50d902SRodney W. Grimes 8809b50d902SRodney W. Grimes /* 8819b50d902SRodney W. Grimes * Statfs follows symlinks; find wants the link's filesystem, 8829b50d902SRodney W. Grimes * not where it points. 8839b50d902SRodney W. Grimes */ 8849b50d902SRodney W. Grimes if (entry->fts_info == FTS_SL || 8859b50d902SRodney W. Grimes entry->fts_info == FTS_SLNONE) { 8869b50d902SRodney W. Grimes if ((p = strrchr(entry->fts_accpath, '/')) != NULL) 8879b50d902SRodney W. Grimes ++p; 8889b50d902SRodney W. Grimes else 8899b50d902SRodney W. Grimes p = entry->fts_accpath; 8909b50d902SRodney W. Grimes save[0] = p[0]; 8919b50d902SRodney W. Grimes p[0] = '.'; 8929b50d902SRodney W. Grimes save[1] = p[1]; 8939b50d902SRodney W. Grimes p[1] = '\0'; 8949b50d902SRodney W. Grimes } else 8959b50d902SRodney W. Grimes p = NULL; 8969b50d902SRodney W. Grimes 8979b50d902SRodney W. Grimes if (statfs(entry->fts_accpath, &sb)) 8989b50d902SRodney W. Grimes err(1, "%s", entry->fts_accpath); 8999b50d902SRodney W. Grimes 9009b50d902SRodney W. Grimes if (p) { 9019b50d902SRodney W. Grimes p[0] = save[0]; 9029b50d902SRodney W. Grimes p[1] = save[1]; 9039b50d902SRodney W. Grimes } 9049b50d902SRodney W. Grimes 9059b50d902SRodney W. Grimes first = 0; 906841484cdSPeter Wemm 907841484cdSPeter Wemm /* 908841484cdSPeter Wemm * Further tests may need both of these values, so 909841484cdSPeter Wemm * always copy both of them. 910841484cdSPeter Wemm */ 9119d08e419SPeter Wemm val_flags = sb.f_flags; 9126ab780e5STai-hwa Liang strlcpy(fstype, sb.f_fstypename, sizeof(fstype)); 9139b50d902SRodney W. Grimes } 914ea92232aSPoul-Henning Kamp switch (plan->flags & F_MTMASK) { 9159b50d902SRodney W. Grimes case F_MTFLAG: 916ea92232aSPoul-Henning Kamp return val_flags & plan->mt_data; 9179b50d902SRodney W. Grimes case F_MTTYPE: 9186ab780e5STai-hwa Liang return (strncmp(fstype, plan->c_data, sizeof(fstype)) == 0); 9199b50d902SRodney W. Grimes default: 9209b50d902SRodney W. Grimes abort(); 9219b50d902SRodney W. Grimes } 9229b50d902SRodney W. Grimes } 9239b50d902SRodney W. Grimes 9249b50d902SRodney W. Grimes PLAN * 925ef646f18SMark Murray c_fstype(OPTION *option, char ***argvp) 9269b50d902SRodney W. Grimes { 927ea92232aSPoul-Henning Kamp char *fsname; 928e98080b1SDavid Malone PLAN *new; 9299b50d902SRodney W. Grimes 930ea92232aSPoul-Henning Kamp fsname = nextarg(option, argvp); 9319b50d902SRodney W. Grimes ftsoptions &= ~FTS_NOSTAT; 9329b50d902SRodney W. Grimes 933ea92232aSPoul-Henning Kamp new = palloc(option); 934ea92232aSPoul-Henning Kamp switch (*fsname) { 9359b50d902SRodney W. Grimes case 'l': 936ea92232aSPoul-Henning Kamp if (!strcmp(fsname, "local")) { 937ea92232aSPoul-Henning Kamp new->flags |= F_MTFLAG; 9389b50d902SRodney W. Grimes new->mt_data = MNT_LOCAL; 939ea92232aSPoul-Henning Kamp return new; 9409b50d902SRodney W. Grimes } 9419b50d902SRodney W. Grimes break; 9429b50d902SRodney W. Grimes case 'r': 943ea92232aSPoul-Henning Kamp if (!strcmp(fsname, "rdonly")) { 944ea92232aSPoul-Henning Kamp new->flags |= F_MTFLAG; 9459b50d902SRodney W. Grimes new->mt_data = MNT_RDONLY; 946ea92232aSPoul-Henning Kamp return new; 9479b50d902SRodney W. Grimes } 9489b50d902SRodney W. Grimes break; 9499b50d902SRodney W. Grimes } 950ea92232aSPoul-Henning Kamp 9516ab780e5STai-hwa Liang new->flags |= F_MTTYPE; 9526ab780e5STai-hwa Liang new->c_data = fsname; 953ea92232aSPoul-Henning Kamp return new; 9549b50d902SRodney W. Grimes } 9559b50d902SRodney W. Grimes 9569b50d902SRodney W. Grimes /* 9579b50d902SRodney W. Grimes * -group gname functions -- 9589b50d902SRodney W. Grimes * 9599b50d902SRodney W. Grimes * True if the file belongs to the group gname. If gname is numeric and 9609b50d902SRodney W. Grimes * an equivalent of the getgrnam() function does not return a valid group 9619b50d902SRodney W. Grimes * name, gname is taken as a group ID. 9629b50d902SRodney W. Grimes */ 9639b50d902SRodney W. Grimes int 964ef646f18SMark Murray f_group(PLAN *plan, FTSENT *entry) 9659b50d902SRodney W. Grimes { 9664ba3b38bSKirill Ponomarev COMPARE(entry->fts_statp->st_gid, plan->g_data); 9679b50d902SRodney W. Grimes } 9689b50d902SRodney W. Grimes 9699b50d902SRodney W. Grimes PLAN * 970ef646f18SMark Murray c_group(OPTION *option, char ***argvp) 9719b50d902SRodney W. Grimes { 972ea92232aSPoul-Henning Kamp char *gname; 9739b50d902SRodney W. Grimes PLAN *new; 9749b50d902SRodney W. Grimes struct group *g; 9759b50d902SRodney W. Grimes gid_t gid; 9769b50d902SRodney W. Grimes 977ea92232aSPoul-Henning Kamp gname = nextarg(option, argvp); 9789b50d902SRodney W. Grimes ftsoptions &= ~FTS_NOSTAT; 9799b50d902SRodney W. Grimes 9804ba3b38bSKirill Ponomarev new = palloc(option); 9819b50d902SRodney W. Grimes g = getgrnam(gname); 9829b50d902SRodney W. Grimes if (g == NULL) { 9834ba3b38bSKirill Ponomarev char* cp = gname; 9844ba3b38bSKirill Ponomarev if (gname[0] == '-' || gname[0] == '+') 9854ba3b38bSKirill Ponomarev gname++; 9869b50d902SRodney W. Grimes gid = atoi(gname); 9879b50d902SRodney W. Grimes if (gid == 0 && gname[0] != '0') 988ea92232aSPoul-Henning Kamp errx(1, "%s: %s: no such group", option->name, gname); 9894ba3b38bSKirill Ponomarev gid = find_parsenum(new, option->name, cp, NULL); 9909b50d902SRodney W. Grimes } else 9919b50d902SRodney W. Grimes gid = g->gr_gid; 9929b50d902SRodney W. Grimes 9939b50d902SRodney W. Grimes new->g_data = gid; 994ea92232aSPoul-Henning Kamp return new; 9959b50d902SRodney W. Grimes } 9969b50d902SRodney W. Grimes 9979b50d902SRodney W. Grimes /* 99840072dc2SJilles Tjoelker * -ignore_readdir_race functions -- 99940072dc2SJilles Tjoelker * 100040072dc2SJilles Tjoelker * Always true. Ignore errors which occur if a file or a directory 100140072dc2SJilles Tjoelker * in a starting point gets deleted between reading the name and calling 100240072dc2SJilles Tjoelker * stat on it while find is traversing the starting point. 100340072dc2SJilles Tjoelker */ 100440072dc2SJilles Tjoelker 100540072dc2SJilles Tjoelker PLAN * 100640072dc2SJilles Tjoelker c_ignore_readdir_race(OPTION *option, char ***argvp __unused) 100740072dc2SJilles Tjoelker { 100840072dc2SJilles Tjoelker if (strcmp(option->name, "-ignore_readdir_race") == 0) 100940072dc2SJilles Tjoelker ignore_readdir_race = 1; 101040072dc2SJilles Tjoelker else 101140072dc2SJilles Tjoelker ignore_readdir_race = 0; 101240072dc2SJilles Tjoelker 101340072dc2SJilles Tjoelker return palloc(option); 101440072dc2SJilles Tjoelker } 101540072dc2SJilles Tjoelker 101640072dc2SJilles Tjoelker /* 10179b50d902SRodney W. Grimes * -inum n functions -- 10189b50d902SRodney W. Grimes * 10199b50d902SRodney W. Grimes * True if the file has inode # n. 10209b50d902SRodney W. Grimes */ 10219b50d902SRodney W. Grimes int 1022ef646f18SMark Murray f_inum(PLAN *plan, FTSENT *entry) 10239b50d902SRodney W. Grimes { 10249b50d902SRodney W. Grimes COMPARE(entry->fts_statp->st_ino, plan->i_data); 10259b50d902SRodney W. Grimes } 10269b50d902SRodney W. Grimes 10279b50d902SRodney W. Grimes PLAN * 1028ef646f18SMark Murray c_inum(OPTION *option, char ***argvp) 10299b50d902SRodney W. Grimes { 1030ea92232aSPoul-Henning Kamp char *inum_str; 10319b50d902SRodney W. Grimes PLAN *new; 10329b50d902SRodney W. Grimes 1033ea92232aSPoul-Henning Kamp inum_str = nextarg(option, argvp); 10349b50d902SRodney W. Grimes ftsoptions &= ~FTS_NOSTAT; 10359b50d902SRodney W. Grimes 1036ea92232aSPoul-Henning Kamp new = palloc(option); 1037ea92232aSPoul-Henning Kamp new->i_data = find_parsenum(new, option->name, inum_str, NULL); 1038ea92232aSPoul-Henning Kamp return new; 10399b50d902SRodney W. Grimes } 10409b50d902SRodney W. Grimes 10419b50d902SRodney W. Grimes /* 104246b993ffSWarner Losh * -samefile FN 104346b993ffSWarner Losh * 104446b993ffSWarner Losh * True if the file has the same inode (eg hard link) FN 104546b993ffSWarner Losh */ 104646b993ffSWarner Losh 104746b993ffSWarner Losh /* f_samefile is just f_inum */ 104846b993ffSWarner Losh PLAN * 104946b993ffSWarner Losh c_samefile(OPTION *option, char ***argvp) 105046b993ffSWarner Losh { 105146b993ffSWarner Losh char *fn; 105246b993ffSWarner Losh PLAN *new; 105346b993ffSWarner Losh struct stat sb; 105446b993ffSWarner Losh 105546b993ffSWarner Losh fn = nextarg(option, argvp); 105646b993ffSWarner Losh ftsoptions &= ~FTS_NOSTAT; 105746b993ffSWarner Losh 105846b993ffSWarner Losh new = palloc(option); 105946b993ffSWarner Losh if (stat(fn, &sb)) 106046b993ffSWarner Losh err(1, "%s", fn); 106146b993ffSWarner Losh new->i_data = sb.st_ino; 106246b993ffSWarner Losh return new; 106346b993ffSWarner Losh } 106446b993ffSWarner Losh 106546b993ffSWarner Losh /* 10669b50d902SRodney W. Grimes * -links n functions -- 10679b50d902SRodney W. Grimes * 10689b50d902SRodney W. Grimes * True if the file has n links. 10699b50d902SRodney W. Grimes */ 10709b50d902SRodney W. Grimes int 1071ef646f18SMark Murray f_links(PLAN *plan, FTSENT *entry) 10729b50d902SRodney W. Grimes { 10739b50d902SRodney W. Grimes COMPARE(entry->fts_statp->st_nlink, plan->l_data); 10749b50d902SRodney W. Grimes } 10759b50d902SRodney W. Grimes 10769b50d902SRodney W. Grimes PLAN * 1077ef646f18SMark Murray c_links(OPTION *option, char ***argvp) 10789b50d902SRodney W. Grimes { 1079ea92232aSPoul-Henning Kamp char *nlinks; 10809b50d902SRodney W. Grimes PLAN *new; 10819b50d902SRodney W. Grimes 1082ea92232aSPoul-Henning Kamp nlinks = nextarg(option, argvp); 10839b50d902SRodney W. Grimes ftsoptions &= ~FTS_NOSTAT; 10849b50d902SRodney W. Grimes 1085ea92232aSPoul-Henning Kamp new = palloc(option); 1086ea92232aSPoul-Henning Kamp new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL); 1087ea92232aSPoul-Henning Kamp return new; 10889b50d902SRodney W. Grimes } 10899b50d902SRodney W. Grimes 10909b50d902SRodney W. Grimes /* 10919b50d902SRodney W. Grimes * -ls functions -- 10929b50d902SRodney W. Grimes * 10939b50d902SRodney W. Grimes * Always true - prints the current entry to stdout in "ls" format. 10949b50d902SRodney W. Grimes */ 10959b50d902SRodney W. Grimes int 1096ef646f18SMark Murray f_ls(PLAN *plan __unused, FTSENT *entry) 10979b50d902SRodney W. Grimes { 10989b50d902SRodney W. Grimes printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp); 1099ea92232aSPoul-Henning Kamp return 1; 11009b50d902SRodney W. Grimes } 11019b50d902SRodney W. Grimes 11029b50d902SRodney W. Grimes PLAN * 1103ef646f18SMark Murray c_ls(OPTION *option, char ***argvp __unused) 11049b50d902SRodney W. Grimes { 11059b50d902SRodney W. Grimes ftsoptions &= ~FTS_NOSTAT; 11069b50d902SRodney W. Grimes isoutput = 1; 11079b50d902SRodney W. Grimes 1108ea92232aSPoul-Henning Kamp return palloc(option); 11099b50d902SRodney W. Grimes } 11109b50d902SRodney W. Grimes 11119b50d902SRodney W. Grimes /* 11129b50d902SRodney W. Grimes * -name functions -- 11139b50d902SRodney W. Grimes * 11149b50d902SRodney W. Grimes * True if the basename of the filename being examined 11159b50d902SRodney W. Grimes * matches pattern using Pattern Matching Notation S3.14 11169b50d902SRodney W. Grimes */ 11179b50d902SRodney W. Grimes int 1118ef646f18SMark Murray f_name(PLAN *plan, FTSENT *entry) 11199b50d902SRodney W. Grimes { 1120acebb585SWarner Losh char fn[PATH_MAX]; 1121acebb585SWarner Losh const char *name; 1122acebb585SWarner Losh 1123acebb585SWarner Losh if (plan->flags & F_LINK) { 1124acebb585SWarner Losh name = fn; 1125acebb585SWarner Losh if (readlink(entry->fts_path, fn, sizeof(fn)) == -1) 112646b993ffSWarner Losh return 0; 1127acebb585SWarner Losh } else 1128acebb585SWarner Losh name = entry->fts_name; 1129acebb585SWarner Losh return !fnmatch(plan->c_data, name, 1130ea92232aSPoul-Henning Kamp plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0); 11319b50d902SRodney W. Grimes } 11329b50d902SRodney W. Grimes 11339b50d902SRodney W. Grimes PLAN * 1134ef646f18SMark Murray c_name(OPTION *option, char ***argvp) 11359b50d902SRodney W. Grimes { 1136ea92232aSPoul-Henning Kamp char *pattern; 11379b50d902SRodney W. Grimes PLAN *new; 11389b50d902SRodney W. Grimes 1139ea92232aSPoul-Henning Kamp pattern = nextarg(option, argvp); 1140ea92232aSPoul-Henning Kamp new = palloc(option); 11419b50d902SRodney W. Grimes new->c_data = pattern; 1142ea92232aSPoul-Henning Kamp return new; 11439b50d902SRodney W. Grimes } 11449b50d902SRodney W. Grimes 11457c1d4b3aSAkinori MUSHA /* 1146ea92232aSPoul-Henning Kamp * -newer file functions -- 11477c1d4b3aSAkinori MUSHA * 1148ea92232aSPoul-Henning Kamp * True if the current file has been modified more recently 1149ea92232aSPoul-Henning Kamp * then the modification time of the file named by the pathname 1150ea92232aSPoul-Henning Kamp * file. 11517c1d4b3aSAkinori MUSHA */ 11527c1d4b3aSAkinori MUSHA int 1153ef646f18SMark Murray f_newer(PLAN *plan, FTSENT *entry) 11547c1d4b3aSAkinori MUSHA { 1155ea92232aSPoul-Henning Kamp if (plan->flags & F_TIME_C) 1156ea92232aSPoul-Henning Kamp return entry->fts_statp->st_ctime > plan->t_data; 1157ea92232aSPoul-Henning Kamp else if (plan->flags & F_TIME_A) 1158ea92232aSPoul-Henning Kamp return entry->fts_statp->st_atime > plan->t_data; 115931d53425SCeri Davies else if (plan->flags & F_TIME_B) 116031d53425SCeri Davies return entry->fts_statp->st_birthtime > plan->t_data; 1161ea92232aSPoul-Henning Kamp else 1162ea92232aSPoul-Henning Kamp return entry->fts_statp->st_mtime > plan->t_data; 11637c1d4b3aSAkinori MUSHA } 11647c1d4b3aSAkinori MUSHA 11657c1d4b3aSAkinori MUSHA PLAN * 1166ef646f18SMark Murray c_newer(OPTION *option, char ***argvp) 11677c1d4b3aSAkinori MUSHA { 1168ea92232aSPoul-Henning Kamp char *fn_or_tspec; 11697c1d4b3aSAkinori MUSHA PLAN *new; 1170ea92232aSPoul-Henning Kamp struct stat sb; 11717c1d4b3aSAkinori MUSHA 1172ea92232aSPoul-Henning Kamp fn_or_tspec = nextarg(option, argvp); 1173ea92232aSPoul-Henning Kamp ftsoptions &= ~FTS_NOSTAT; 1174ea92232aSPoul-Henning Kamp 1175ea92232aSPoul-Henning Kamp new = palloc(option); 1176ea92232aSPoul-Henning Kamp /* compare against what */ 1177ea92232aSPoul-Henning Kamp if (option->flags & F_TIME2_T) { 117865bcaaecSEd Schouten new->t_data = get_date(fn_or_tspec); 1179ea92232aSPoul-Henning Kamp if (new->t_data == (time_t) -1) 1180ea92232aSPoul-Henning Kamp errx(1, "Can't parse date/time: %s", fn_or_tspec); 1181ea92232aSPoul-Henning Kamp } else { 1182ea92232aSPoul-Henning Kamp if (stat(fn_or_tspec, &sb)) 1183ea92232aSPoul-Henning Kamp err(1, "%s", fn_or_tspec); 1184ea92232aSPoul-Henning Kamp if (option->flags & F_TIME2_C) 1185ea92232aSPoul-Henning Kamp new->t_data = sb.st_ctime; 1186ea92232aSPoul-Henning Kamp else if (option->flags & F_TIME2_A) 1187ea92232aSPoul-Henning Kamp new->t_data = sb.st_atime; 11888310a1a2SGavin Atkinson else if (option->flags & F_TIME2_B) 11898310a1a2SGavin Atkinson new->t_data = sb.st_birthtime; 1190ea92232aSPoul-Henning Kamp else 1191ea92232aSPoul-Henning Kamp new->t_data = sb.st_mtime; 1192ea92232aSPoul-Henning Kamp } 1193ea92232aSPoul-Henning Kamp return new; 11947c1d4b3aSAkinori MUSHA } 11957c1d4b3aSAkinori MUSHA 1196ea92232aSPoul-Henning Kamp /* 1197ea92232aSPoul-Henning Kamp * -nogroup functions -- 1198ea92232aSPoul-Henning Kamp * 1199ea92232aSPoul-Henning Kamp * True if file belongs to a user ID for which the equivalent 1200ea92232aSPoul-Henning Kamp * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL. 1201ea92232aSPoul-Henning Kamp */ 1202ea92232aSPoul-Henning Kamp int 1203ef646f18SMark Murray f_nogroup(PLAN *plan __unused, FTSENT *entry) 1204ea92232aSPoul-Henning Kamp { 1205ea92232aSPoul-Henning Kamp return group_from_gid(entry->fts_statp->st_gid, 1) == NULL; 1206ea92232aSPoul-Henning Kamp } 1207ea92232aSPoul-Henning Kamp 1208ea92232aSPoul-Henning Kamp PLAN * 1209ef646f18SMark Murray c_nogroup(OPTION *option, char ***argvp __unused) 1210ea92232aSPoul-Henning Kamp { 1211ea92232aSPoul-Henning Kamp ftsoptions &= ~FTS_NOSTAT; 1212ea92232aSPoul-Henning Kamp 1213ea92232aSPoul-Henning Kamp return palloc(option); 1214ea92232aSPoul-Henning Kamp } 1215ea92232aSPoul-Henning Kamp 1216ea92232aSPoul-Henning Kamp /* 1217ea92232aSPoul-Henning Kamp * -nouser functions -- 1218ea92232aSPoul-Henning Kamp * 1219ea92232aSPoul-Henning Kamp * True if file belongs to a user ID for which the equivalent 1220ea92232aSPoul-Henning Kamp * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL. 1221ea92232aSPoul-Henning Kamp */ 1222ea92232aSPoul-Henning Kamp int 1223ef646f18SMark Murray f_nouser(PLAN *plan __unused, FTSENT *entry) 1224ea92232aSPoul-Henning Kamp { 1225ea92232aSPoul-Henning Kamp return user_from_uid(entry->fts_statp->st_uid, 1) == NULL; 1226ea92232aSPoul-Henning Kamp } 1227ea92232aSPoul-Henning Kamp 1228ea92232aSPoul-Henning Kamp PLAN * 1229ef646f18SMark Murray c_nouser(OPTION *option, char ***argvp __unused) 1230ea92232aSPoul-Henning Kamp { 1231ea92232aSPoul-Henning Kamp ftsoptions &= ~FTS_NOSTAT; 1232ea92232aSPoul-Henning Kamp 1233ea92232aSPoul-Henning Kamp return palloc(option); 1234ea92232aSPoul-Henning Kamp } 1235ea92232aSPoul-Henning Kamp 1236ea92232aSPoul-Henning Kamp /* 1237ea92232aSPoul-Henning Kamp * -path functions -- 1238ea92232aSPoul-Henning Kamp * 1239ea92232aSPoul-Henning Kamp * True if the path of the filename being examined 1240ea92232aSPoul-Henning Kamp * matches pattern using Pattern Matching Notation S3.14 1241ea92232aSPoul-Henning Kamp */ 1242ea92232aSPoul-Henning Kamp int 1243ef646f18SMark Murray f_path(PLAN *plan, FTSENT *entry) 1244ea92232aSPoul-Henning Kamp { 1245ea92232aSPoul-Henning Kamp return !fnmatch(plan->c_data, entry->fts_path, 1246ea92232aSPoul-Henning Kamp plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0); 1247ea92232aSPoul-Henning Kamp } 1248ea92232aSPoul-Henning Kamp 1249ea92232aSPoul-Henning Kamp /* c_path is the same as c_name */ 1250ea92232aSPoul-Henning Kamp 1251ea92232aSPoul-Henning Kamp /* 1252ea92232aSPoul-Henning Kamp * -perm functions -- 1253ea92232aSPoul-Henning Kamp * 1254ea92232aSPoul-Henning Kamp * The mode argument is used to represent file mode bits. If it starts 1255ea92232aSPoul-Henning Kamp * with a leading digit, it's treated as an octal mode, otherwise as a 1256ea92232aSPoul-Henning Kamp * symbolic mode. 1257ea92232aSPoul-Henning Kamp */ 1258ea92232aSPoul-Henning Kamp int 1259ef646f18SMark Murray f_perm(PLAN *plan, FTSENT *entry) 1260ea92232aSPoul-Henning Kamp { 1261ea92232aSPoul-Henning Kamp mode_t mode; 1262ea92232aSPoul-Henning Kamp 1263ea92232aSPoul-Henning Kamp mode = entry->fts_statp->st_mode & 1264ea92232aSPoul-Henning Kamp (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO); 1265ea92232aSPoul-Henning Kamp if (plan->flags & F_ATLEAST) 1266ea92232aSPoul-Henning Kamp return (plan->m_data | mode) == mode; 1267c0ff9709SRuslan Ermilov else if (plan->flags & F_ANY) 1268c0ff9709SRuslan Ermilov return (mode & plan->m_data); 1269ea92232aSPoul-Henning Kamp else 1270ea92232aSPoul-Henning Kamp return mode == plan->m_data; 1271ea92232aSPoul-Henning Kamp /* NOTREACHED */ 1272ea92232aSPoul-Henning Kamp } 1273ea92232aSPoul-Henning Kamp 1274ea92232aSPoul-Henning Kamp PLAN * 1275ef646f18SMark Murray c_perm(OPTION *option, char ***argvp) 1276ea92232aSPoul-Henning Kamp { 1277ea92232aSPoul-Henning Kamp char *perm; 1278ea92232aSPoul-Henning Kamp PLAN *new; 1279ea92232aSPoul-Henning Kamp mode_t *set; 1280ea92232aSPoul-Henning Kamp 1281ea92232aSPoul-Henning Kamp perm = nextarg(option, argvp); 1282ea92232aSPoul-Henning Kamp ftsoptions &= ~FTS_NOSTAT; 1283ea92232aSPoul-Henning Kamp 1284ea92232aSPoul-Henning Kamp new = palloc(option); 1285ea92232aSPoul-Henning Kamp 1286ea92232aSPoul-Henning Kamp if (*perm == '-') { 1287ea92232aSPoul-Henning Kamp new->flags |= F_ATLEAST; 1288ea92232aSPoul-Henning Kamp ++perm; 1289ea92232aSPoul-Henning Kamp } else if (*perm == '+') { 1290ea92232aSPoul-Henning Kamp new->flags |= F_ANY; 1291ea92232aSPoul-Henning Kamp ++perm; 1292ea92232aSPoul-Henning Kamp } 1293ea92232aSPoul-Henning Kamp 1294ea92232aSPoul-Henning Kamp if ((set = setmode(perm)) == NULL) 1295ea92232aSPoul-Henning Kamp errx(1, "%s: %s: illegal mode string", option->name, perm); 1296ea92232aSPoul-Henning Kamp 1297ea92232aSPoul-Henning Kamp new->m_data = getmode(set, 0); 1298ea92232aSPoul-Henning Kamp free(set); 1299ea92232aSPoul-Henning Kamp return new; 1300ea92232aSPoul-Henning Kamp } 1301ea92232aSPoul-Henning Kamp 1302ea92232aSPoul-Henning Kamp /* 1303ea92232aSPoul-Henning Kamp * -print functions -- 1304ea92232aSPoul-Henning Kamp * 13059725a7b9SPhilippe Charnier * Always true, causes the current pathname to be written to 1306ea92232aSPoul-Henning Kamp * standard output. 1307ea92232aSPoul-Henning Kamp */ 1308ea92232aSPoul-Henning Kamp int 1309ef646f18SMark Murray f_print(PLAN *plan __unused, FTSENT *entry) 1310ea92232aSPoul-Henning Kamp { 1311ea92232aSPoul-Henning Kamp (void)puts(entry->fts_path); 1312ea92232aSPoul-Henning Kamp return 1; 1313ea92232aSPoul-Henning Kamp } 1314ea92232aSPoul-Henning Kamp 1315ea92232aSPoul-Henning Kamp PLAN * 1316ef646f18SMark Murray c_print(OPTION *option, char ***argvp __unused) 1317ea92232aSPoul-Henning Kamp { 1318ea92232aSPoul-Henning Kamp isoutput = 1; 1319ea92232aSPoul-Henning Kamp 1320ea92232aSPoul-Henning Kamp return palloc(option); 1321ea92232aSPoul-Henning Kamp } 1322ea92232aSPoul-Henning Kamp 1323ea92232aSPoul-Henning Kamp /* 1324ea92232aSPoul-Henning Kamp * -print0 functions -- 1325ea92232aSPoul-Henning Kamp * 13269725a7b9SPhilippe Charnier * Always true, causes the current pathname to be written to 1327ea92232aSPoul-Henning Kamp * standard output followed by a NUL character 1328ea92232aSPoul-Henning Kamp */ 1329ea92232aSPoul-Henning Kamp int 1330ef646f18SMark Murray f_print0(PLAN *plan __unused, FTSENT *entry) 1331ea92232aSPoul-Henning Kamp { 1332ea92232aSPoul-Henning Kamp fputs(entry->fts_path, stdout); 1333ea92232aSPoul-Henning Kamp fputc('\0', stdout); 1334ea92232aSPoul-Henning Kamp return 1; 1335ea92232aSPoul-Henning Kamp } 1336ea92232aSPoul-Henning Kamp 1337ea92232aSPoul-Henning Kamp /* c_print0 is the same as c_print */ 1338ea92232aSPoul-Henning Kamp 1339ea92232aSPoul-Henning Kamp /* 1340ea92232aSPoul-Henning Kamp * -prune functions -- 1341ea92232aSPoul-Henning Kamp * 1342ea92232aSPoul-Henning Kamp * Prune a portion of the hierarchy. 1343ea92232aSPoul-Henning Kamp */ 1344ea92232aSPoul-Henning Kamp int 1345ef646f18SMark Murray f_prune(PLAN *plan __unused, FTSENT *entry) 1346ea92232aSPoul-Henning Kamp { 1347ea92232aSPoul-Henning Kamp if (fts_set(tree, entry, FTS_SKIP)) 1348ea92232aSPoul-Henning Kamp err(1, "%s", entry->fts_path); 1349ea92232aSPoul-Henning Kamp return 1; 1350ea92232aSPoul-Henning Kamp } 1351ea92232aSPoul-Henning Kamp 1352ea92232aSPoul-Henning Kamp /* c_prune == c_simple */ 13537c1d4b3aSAkinori MUSHA 13547c1d4b3aSAkinori MUSHA /* 13557c1d4b3aSAkinori MUSHA * -regex functions -- 13567c1d4b3aSAkinori MUSHA * 13577c1d4b3aSAkinori MUSHA * True if the whole path of the file matches pattern using 13587c1d4b3aSAkinori MUSHA * regular expression. 13597c1d4b3aSAkinori MUSHA */ 13607c1d4b3aSAkinori MUSHA int 1361ef646f18SMark Murray f_regex(PLAN *plan, FTSENT *entry) 13627c1d4b3aSAkinori MUSHA { 13637c1d4b3aSAkinori MUSHA char *str; 136447bca8b0SJuli Mallett int len; 13657c1d4b3aSAkinori MUSHA regex_t *pre; 13667c1d4b3aSAkinori MUSHA regmatch_t pmatch; 13677c1d4b3aSAkinori MUSHA int errcode; 13687c1d4b3aSAkinori MUSHA char errbuf[LINE_MAX]; 13697c1d4b3aSAkinori MUSHA int matched; 13707c1d4b3aSAkinori MUSHA 13717c1d4b3aSAkinori MUSHA pre = plan->re_data; 13727c1d4b3aSAkinori MUSHA str = entry->fts_path; 13737c1d4b3aSAkinori MUSHA len = strlen(str); 13747c1d4b3aSAkinori MUSHA matched = 0; 13757c1d4b3aSAkinori MUSHA 13767c1d4b3aSAkinori MUSHA pmatch.rm_so = 0; 13777c1d4b3aSAkinori MUSHA pmatch.rm_eo = len; 13787c1d4b3aSAkinori MUSHA 13797c1d4b3aSAkinori MUSHA errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND); 13807c1d4b3aSAkinori MUSHA 13817c1d4b3aSAkinori MUSHA if (errcode != 0 && errcode != REG_NOMATCH) { 13827c1d4b3aSAkinori MUSHA regerror(errcode, pre, errbuf, sizeof errbuf); 13837c1d4b3aSAkinori MUSHA errx(1, "%s: %s", 1384ea92232aSPoul-Henning Kamp plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf); 13857c1d4b3aSAkinori MUSHA } 13867c1d4b3aSAkinori MUSHA 13877c1d4b3aSAkinori MUSHA if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len) 13887c1d4b3aSAkinori MUSHA matched = 1; 13897c1d4b3aSAkinori MUSHA 1390ea92232aSPoul-Henning Kamp return matched; 13917c1d4b3aSAkinori MUSHA } 13927c1d4b3aSAkinori MUSHA 13937c1d4b3aSAkinori MUSHA PLAN * 1394ef646f18SMark Murray c_regex(OPTION *option, char ***argvp) 13957c1d4b3aSAkinori MUSHA { 13967c1d4b3aSAkinori MUSHA PLAN *new; 1397ea92232aSPoul-Henning Kamp char *pattern; 13987c1d4b3aSAkinori MUSHA regex_t *pre; 13997c1d4b3aSAkinori MUSHA int errcode; 14007c1d4b3aSAkinori MUSHA char errbuf[LINE_MAX]; 14017c1d4b3aSAkinori MUSHA 14027c1d4b3aSAkinori MUSHA if ((pre = malloc(sizeof(regex_t))) == NULL) 14037c1d4b3aSAkinori MUSHA err(1, NULL); 14047c1d4b3aSAkinori MUSHA 1405ea92232aSPoul-Henning Kamp pattern = nextarg(option, argvp); 1406ea92232aSPoul-Henning Kamp 1407ea92232aSPoul-Henning Kamp if ((errcode = regcomp(pre, pattern, 1408ea92232aSPoul-Henning Kamp regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) { 14097c1d4b3aSAkinori MUSHA regerror(errcode, pre, errbuf, sizeof errbuf); 14107c1d4b3aSAkinori MUSHA errx(1, "%s: %s: %s", 1411ea92232aSPoul-Henning Kamp option->flags & F_IGNCASE ? "-iregex" : "-regex", 1412ea92232aSPoul-Henning Kamp pattern, errbuf); 14137c1d4b3aSAkinori MUSHA } 14147c1d4b3aSAkinori MUSHA 1415ea92232aSPoul-Henning Kamp new = palloc(option); 14167c1d4b3aSAkinori MUSHA new->re_data = pre; 14177c1d4b3aSAkinori MUSHA 1418567664c4SOllivier Robert return new; 1419567664c4SOllivier Robert } 1420567664c4SOllivier Robert 142146b993ffSWarner Losh /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or, c_true, c_false */ 14229b50d902SRodney W. Grimes 14239b50d902SRodney W. Grimes PLAN * 1424ef646f18SMark Murray c_simple(OPTION *option, char ***argvp __unused) 14259b50d902SRodney W. Grimes { 1426ea92232aSPoul-Henning Kamp return palloc(option); 14279b50d902SRodney W. Grimes } 14289b50d902SRodney W. Grimes 14299b50d902SRodney W. Grimes /* 14309b50d902SRodney W. Grimes * -size n[c] functions -- 14319b50d902SRodney W. Grimes * 14329b50d902SRodney W. Grimes * True if the file size in bytes, divided by an implementation defined 14339b50d902SRodney W. Grimes * value and rounded up to the next integer, is n. If n is followed by 14345a890aacSKirill Ponomarev * one of c k M G T P, the size is in bytes, kilobytes, 14355a890aacSKirill Ponomarev * megabytes, gigabytes, terabytes or petabytes respectively. 14369b50d902SRodney W. Grimes */ 14379b50d902SRodney W. Grimes #define FIND_SIZE 512 14389b50d902SRodney W. Grimes static int divsize = 1; 14399b50d902SRodney W. Grimes 14409b50d902SRodney W. Grimes int 1441ef646f18SMark Murray f_size(PLAN *plan, FTSENT *entry) 14429b50d902SRodney W. Grimes { 14439b50d902SRodney W. Grimes off_t size; 14449b50d902SRodney W. Grimes 14459b50d902SRodney W. Grimes size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) / 14469b50d902SRodney W. Grimes FIND_SIZE : entry->fts_statp->st_size; 14479b50d902SRodney W. Grimes COMPARE(size, plan->o_data); 14489b50d902SRodney W. Grimes } 14499b50d902SRodney W. Grimes 14509b50d902SRodney W. Grimes PLAN * 1451ef646f18SMark Murray c_size(OPTION *option, char ***argvp) 14529b50d902SRodney W. Grimes { 1453ea92232aSPoul-Henning Kamp char *size_str; 14549b50d902SRodney W. Grimes PLAN *new; 14559b50d902SRodney W. Grimes char endch; 14565a890aacSKirill Ponomarev off_t scale; 14579b50d902SRodney W. Grimes 1458ea92232aSPoul-Henning Kamp size_str = nextarg(option, argvp); 14599b50d902SRodney W. Grimes ftsoptions &= ~FTS_NOSTAT; 14609b50d902SRodney W. Grimes 1461ea92232aSPoul-Henning Kamp new = palloc(option); 14629b50d902SRodney W. Grimes endch = 'c'; 1463ea92232aSPoul-Henning Kamp new->o_data = find_parsenum(new, option->name, size_str, &endch); 14645a890aacSKirill Ponomarev if (endch != '\0') { 14659b50d902SRodney W. Grimes divsize = 0; 14665a890aacSKirill Ponomarev 14675a890aacSKirill Ponomarev switch (endch) { 14685a890aacSKirill Ponomarev case 'c': /* characters */ 14695a890aacSKirill Ponomarev scale = 0x1LL; 14705a890aacSKirill Ponomarev break; 14715a890aacSKirill Ponomarev case 'k': /* kilobytes 1<<10 */ 14725a890aacSKirill Ponomarev scale = 0x400LL; 14735a890aacSKirill Ponomarev break; 14745a890aacSKirill Ponomarev case 'M': /* megabytes 1<<20 */ 14755a890aacSKirill Ponomarev scale = 0x100000LL; 14765a890aacSKirill Ponomarev break; 14775a890aacSKirill Ponomarev case 'G': /* gigabytes 1<<30 */ 14785a890aacSKirill Ponomarev scale = 0x40000000LL; 14795a890aacSKirill Ponomarev break; 14805a890aacSKirill Ponomarev case 'T': /* terabytes 1<<40 */ 14815a890aacSKirill Ponomarev scale = 0x1000000000LL; 14825a890aacSKirill Ponomarev break; 14835a890aacSKirill Ponomarev case 'P': /* petabytes 1<<50 */ 14845a890aacSKirill Ponomarev scale = 0x4000000000000LL; 14855a890aacSKirill Ponomarev break; 14865a890aacSKirill Ponomarev default: 14875a890aacSKirill Ponomarev errx(1, "%s: %s: illegal trailing character", 14885a890aacSKirill Ponomarev option->name, size_str); 14895a890aacSKirill Ponomarev break; 14905a890aacSKirill Ponomarev } 14915a890aacSKirill Ponomarev if (new->o_data > QUAD_MAX / scale) 14925a890aacSKirill Ponomarev errx(1, "%s: %s: value too large", 14935a890aacSKirill Ponomarev option->name, size_str); 14945a890aacSKirill Ponomarev new->o_data *= scale; 14955a890aacSKirill Ponomarev } 1496ea92232aSPoul-Henning Kamp return new; 14979b50d902SRodney W. Grimes } 14989b50d902SRodney W. Grimes 14999b50d902SRodney W. Grimes /* 15009b50d902SRodney W. Grimes * -type c functions -- 15019b50d902SRodney W. Grimes * 1502841484cdSPeter Wemm * True if the type of the file is c, where c is b, c, d, p, f or w 1503841484cdSPeter Wemm * for block special file, character special file, directory, FIFO, 1504841484cdSPeter Wemm * regular file or whiteout respectively. 15059b50d902SRodney W. Grimes */ 15069b50d902SRodney W. Grimes int 1507ef646f18SMark Murray f_type(PLAN *plan, FTSENT *entry) 15089b50d902SRodney W. Grimes { 1509ea92232aSPoul-Henning Kamp return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data; 15109b50d902SRodney W. Grimes } 15119b50d902SRodney W. Grimes 15129b50d902SRodney W. Grimes PLAN * 1513ef646f18SMark Murray c_type(OPTION *option, char ***argvp) 15149b50d902SRodney W. Grimes { 1515ea92232aSPoul-Henning Kamp char *typestring; 15169b50d902SRodney W. Grimes PLAN *new; 15179b50d902SRodney W. Grimes mode_t mask; 15189b50d902SRodney W. Grimes 1519ea92232aSPoul-Henning Kamp typestring = nextarg(option, argvp); 15209b50d902SRodney W. Grimes ftsoptions &= ~FTS_NOSTAT; 15219b50d902SRodney W. Grimes 15229b50d902SRodney W. Grimes switch (typestring[0]) { 15239b50d902SRodney W. Grimes case 'b': 15249b50d902SRodney W. Grimes mask = S_IFBLK; 15259b50d902SRodney W. Grimes break; 15269b50d902SRodney W. Grimes case 'c': 15279b50d902SRodney W. Grimes mask = S_IFCHR; 15289b50d902SRodney W. Grimes break; 15299b50d902SRodney W. Grimes case 'd': 15309b50d902SRodney W. Grimes mask = S_IFDIR; 15319b50d902SRodney W. Grimes break; 15329b50d902SRodney W. Grimes case 'f': 15339b50d902SRodney W. Grimes mask = S_IFREG; 15349b50d902SRodney W. Grimes break; 15359b50d902SRodney W. Grimes case 'l': 15369b50d902SRodney W. Grimes mask = S_IFLNK; 15379b50d902SRodney W. Grimes break; 15389b50d902SRodney W. Grimes case 'p': 15399b50d902SRodney W. Grimes mask = S_IFIFO; 15409b50d902SRodney W. Grimes break; 15419b50d902SRodney W. Grimes case 's': 15429b50d902SRodney W. Grimes mask = S_IFSOCK; 15439b50d902SRodney W. Grimes break; 1544841484cdSPeter Wemm #ifdef FTS_WHITEOUT 1545841484cdSPeter Wemm case 'w': 1546841484cdSPeter Wemm mask = S_IFWHT; 1547841484cdSPeter Wemm ftsoptions |= FTS_WHITEOUT; 1548841484cdSPeter Wemm break; 1549841484cdSPeter Wemm #endif /* FTS_WHITEOUT */ 15509b50d902SRodney W. Grimes default: 1551ea92232aSPoul-Henning Kamp errx(1, "%s: %s: unknown type", option->name, typestring); 15529b50d902SRodney W. Grimes } 15539b50d902SRodney W. Grimes 1554ea92232aSPoul-Henning Kamp new = palloc(option); 15559b50d902SRodney W. Grimes new->m_data = mask; 1556ea92232aSPoul-Henning Kamp return new; 1557abacbbbfSPeter Wemm } 1558abacbbbfSPeter Wemm 1559abacbbbfSPeter Wemm /* 15609b50d902SRodney W. Grimes * -user uname functions -- 15619b50d902SRodney W. Grimes * 15629b50d902SRodney W. Grimes * True if the file belongs to the user uname. If uname is numeric and 15639b50d902SRodney W. Grimes * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not 15649b50d902SRodney W. Grimes * return a valid user name, uname is taken as a user ID. 15659b50d902SRodney W. Grimes */ 15669b50d902SRodney W. Grimes int 1567ef646f18SMark Murray f_user(PLAN *plan, FTSENT *entry) 15689b50d902SRodney W. Grimes { 15694ba3b38bSKirill Ponomarev COMPARE(entry->fts_statp->st_uid, plan->u_data); 15709b50d902SRodney W. Grimes } 15719b50d902SRodney W. Grimes 15729b50d902SRodney W. Grimes PLAN * 1573ef646f18SMark Murray c_user(OPTION *option, char ***argvp) 15749b50d902SRodney W. Grimes { 1575ea92232aSPoul-Henning Kamp char *username; 15769b50d902SRodney W. Grimes PLAN *new; 15779b50d902SRodney W. Grimes struct passwd *p; 15789b50d902SRodney W. Grimes uid_t uid; 15799b50d902SRodney W. Grimes 1580ea92232aSPoul-Henning Kamp username = nextarg(option, argvp); 15819b50d902SRodney W. Grimes ftsoptions &= ~FTS_NOSTAT; 15829b50d902SRodney W. Grimes 15834ba3b38bSKirill Ponomarev new = palloc(option); 15849b50d902SRodney W. Grimes p = getpwnam(username); 15859b50d902SRodney W. Grimes if (p == NULL) { 15864ba3b38bSKirill Ponomarev char* cp = username; 15874ba3b38bSKirill Ponomarev if( username[0] == '-' || username[0] == '+' ) 15884ba3b38bSKirill Ponomarev username++; 15899b50d902SRodney W. Grimes uid = atoi(username); 15909b50d902SRodney W. Grimes if (uid == 0 && username[0] != '0') 1591ea92232aSPoul-Henning Kamp errx(1, "%s: %s: no such user", option->name, username); 15924ba3b38bSKirill Ponomarev uid = find_parsenum(new, option->name, cp, NULL); 15939b50d902SRodney W. Grimes } else 15949b50d902SRodney W. Grimes uid = p->pw_uid; 15959b50d902SRodney W. Grimes 15969b50d902SRodney W. Grimes new->u_data = uid; 1597ea92232aSPoul-Henning Kamp return new; 15989b50d902SRodney W. Grimes } 15999b50d902SRodney W. Grimes 16009b50d902SRodney W. Grimes /* 16019b50d902SRodney W. Grimes * -xdev functions -- 16029b50d902SRodney W. Grimes * 16039725a7b9SPhilippe Charnier * Always true, causes find not to descend past directories that have a 16049b50d902SRodney W. Grimes * different device ID (st_dev, see stat() S5.6.2 [POSIX.1]) 16059b50d902SRodney W. Grimes */ 16069b50d902SRodney W. Grimes PLAN * 1607ef646f18SMark Murray c_xdev(OPTION *option, char ***argvp __unused) 16089b50d902SRodney W. Grimes { 16099b50d902SRodney W. Grimes ftsoptions |= FTS_XDEV; 16109b50d902SRodney W. Grimes 1611ea92232aSPoul-Henning Kamp return palloc(option); 16129b50d902SRodney W. Grimes } 16139b50d902SRodney W. Grimes 16149b50d902SRodney W. Grimes /* 16159b50d902SRodney W. Grimes * ( expression ) functions -- 16169b50d902SRodney W. Grimes * 16179b50d902SRodney W. Grimes * True if expression is true. 16189b50d902SRodney W. Grimes */ 16199b50d902SRodney W. Grimes int 1620ef646f18SMark Murray f_expr(PLAN *plan, FTSENT *entry) 16219b50d902SRodney W. Grimes { 1622e98080b1SDavid Malone PLAN *p; 1623e98080b1SDavid Malone int state = 0; 16249b50d902SRodney W. Grimes 16259b50d902SRodney W. Grimes for (p = plan->p_data[0]; 1626ea92232aSPoul-Henning Kamp p && (state = (p->execute)(p, entry)); p = p->next); 1627ea92232aSPoul-Henning Kamp return state; 16289b50d902SRodney W. Grimes } 16299b50d902SRodney W. Grimes 16309b50d902SRodney W. Grimes /* 1631ea92232aSPoul-Henning Kamp * f_openparen and f_closeparen nodes are temporary place markers. They are 16329b50d902SRodney W. Grimes * eliminated during phase 2 of find_formplan() --- the '(' node is converted 1633ea92232aSPoul-Henning Kamp * to a f_expr node containing the expression and the ')' node is discarded. 1634ea92232aSPoul-Henning Kamp * The functions themselves are only used as constants. 16359b50d902SRodney W. Grimes */ 1636ea92232aSPoul-Henning Kamp 1637ea92232aSPoul-Henning Kamp int 1638ef646f18SMark Murray f_openparen(PLAN *plan __unused, FTSENT *entry __unused) 16399b50d902SRodney W. Grimes { 1640ea92232aSPoul-Henning Kamp abort(); 16419b50d902SRodney W. Grimes } 16429b50d902SRodney W. Grimes 1643ea92232aSPoul-Henning Kamp int 1644ef646f18SMark Murray f_closeparen(PLAN *plan __unused, FTSENT *entry __unused) 16459b50d902SRodney W. Grimes { 1646ea92232aSPoul-Henning Kamp abort(); 1647ea92232aSPoul-Henning Kamp } 1648ea92232aSPoul-Henning Kamp 1649ea92232aSPoul-Henning Kamp /* c_openparen == c_simple */ 1650ea92232aSPoul-Henning Kamp /* c_closeparen == c_simple */ 1651ea92232aSPoul-Henning Kamp 1652ea92232aSPoul-Henning Kamp /* 1653ea92232aSPoul-Henning Kamp * AND operator. Since AND is implicit, no node is allocated. 1654ea92232aSPoul-Henning Kamp */ 1655ea92232aSPoul-Henning Kamp PLAN * 1656ef646f18SMark Murray c_and(OPTION *option __unused, char ***argvp __unused) 1657ea92232aSPoul-Henning Kamp { 1658ea92232aSPoul-Henning Kamp return NULL; 16599b50d902SRodney W. Grimes } 16609b50d902SRodney W. Grimes 16619b50d902SRodney W. Grimes /* 16629b50d902SRodney W. Grimes * ! expression functions -- 16639b50d902SRodney W. Grimes * 16649b50d902SRodney W. Grimes * Negation of a primary; the unary NOT operator. 16659b50d902SRodney W. Grimes */ 16669b50d902SRodney W. Grimes int 1667ef646f18SMark Murray f_not(PLAN *plan, FTSENT *entry) 16689b50d902SRodney W. Grimes { 1669e98080b1SDavid Malone PLAN *p; 1670e98080b1SDavid Malone int state = 0; 16719b50d902SRodney W. Grimes 16729b50d902SRodney W. Grimes for (p = plan->p_data[0]; 1673ea92232aSPoul-Henning Kamp p && (state = (p->execute)(p, entry)); p = p->next); 1674ea92232aSPoul-Henning Kamp return !state; 16759b50d902SRodney W. Grimes } 16769b50d902SRodney W. Grimes 1677ea92232aSPoul-Henning Kamp /* c_not == c_simple */ 16789b50d902SRodney W. Grimes 16799b50d902SRodney W. Grimes /* 16809b50d902SRodney W. Grimes * expression -o expression functions -- 16819b50d902SRodney W. Grimes * 16829b50d902SRodney W. Grimes * Alternation of primaries; the OR operator. The second expression is 16839b50d902SRodney W. Grimes * not evaluated if the first expression is true. 16849b50d902SRodney W. Grimes */ 16859b50d902SRodney W. Grimes int 1686ef646f18SMark Murray f_or(PLAN *plan, FTSENT *entry) 16879b50d902SRodney W. Grimes { 1688e98080b1SDavid Malone PLAN *p; 1689e98080b1SDavid Malone int state = 0; 16909b50d902SRodney W. Grimes 16919b50d902SRodney W. Grimes for (p = plan->p_data[0]; 1692ea92232aSPoul-Henning Kamp p && (state = (p->execute)(p, entry)); p = p->next); 16939b50d902SRodney W. Grimes 16949b50d902SRodney W. Grimes if (state) 1695ea92232aSPoul-Henning Kamp return 1; 16969b50d902SRodney W. Grimes 16979b50d902SRodney W. Grimes for (p = plan->p_data[1]; 1698ea92232aSPoul-Henning Kamp p && (state = (p->execute)(p, entry)); p = p->next); 1699ea92232aSPoul-Henning Kamp return state; 17009b50d902SRodney W. Grimes } 17019b50d902SRodney W. Grimes 1702ea92232aSPoul-Henning Kamp /* c_or == c_simple */ 170346b993ffSWarner Losh 170446b993ffSWarner Losh /* 170546b993ffSWarner Losh * -false 170646b993ffSWarner Losh * 170746b993ffSWarner Losh * Always false. 170846b993ffSWarner Losh */ 170946b993ffSWarner Losh int 171046b993ffSWarner Losh f_false(PLAN *plan __unused, FTSENT *entry __unused) 171146b993ffSWarner Losh { 171246b993ffSWarner Losh return 0; 171346b993ffSWarner Losh } 171446b993ffSWarner Losh 171546b993ffSWarner Losh /* c_false == c_simple */ 171646b993ffSWarner Losh 171746b993ffSWarner Losh /* 171846b993ffSWarner Losh * -quit 171946b993ffSWarner Losh * 172046b993ffSWarner Losh * Exits the program 172146b993ffSWarner Losh */ 172246b993ffSWarner Losh int 172346b993ffSWarner Losh f_quit(PLAN *plan __unused, FTSENT *entry __unused) 172446b993ffSWarner Losh { 172546b993ffSWarner Losh exit(0); 172646b993ffSWarner Losh } 172746b993ffSWarner Losh 172846b993ffSWarner Losh /* c_quit == c_simple */ 1729