19b50d902SRodney W. Grimes /*- 2*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 3*8a16b7a1SPedro F. Giffuni * 49b50d902SRodney W. Grimes * Copyright (c) 1990, 1993 59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 69b50d902SRodney W. Grimes * 79b50d902SRodney W. Grimes * This code is derived from software contributed to Berkeley by 89b50d902SRodney W. Grimes * Cimarron D. Taylor of the University of California, Berkeley. 99b50d902SRodney W. Grimes * 109b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 119b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 129b50d902SRodney W. Grimes * are met: 139b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 149b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 159b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 169b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 179b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 199b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 209b50d902SRodney W. Grimes * without specific prior written permission. 219b50d902SRodney W. Grimes * 229b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 239b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 249b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 259b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 269b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 279b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 289b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 299b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 309b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 319b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 329b50d902SRodney W. Grimes * SUCH DAMAGE. 339b50d902SRodney W. Grimes */ 349b50d902SRodney W. Grimes 353549ef2fSXin LI #ifndef lint 363549ef2fSXin LI #if 0 373549ef2fSXin LI static const char sccsid[] = "@(#)function.c 8.10 (Berkeley) 5/4/95"; 383549ef2fSXin LI #endif 393549ef2fSXin LI #endif /* not lint */ 403549ef2fSXin LI 413077469eSDavid E. O'Brien #include <sys/cdefs.h> 423077469eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 439b50d902SRodney W. Grimes 449b50d902SRodney W. Grimes #include <sys/param.h> 459b50d902SRodney W. Grimes #include <sys/ucred.h> 469b50d902SRodney W. Grimes #include <sys/stat.h> 479c5d31dfSBosko Milekic #include <sys/types.h> 489c5d31dfSBosko Milekic #include <sys/acl.h> 499b50d902SRodney W. Grimes #include <sys/wait.h> 509b50d902SRodney W. Grimes #include <sys/mount.h> 519b50d902SRodney W. Grimes 52ed1a4621SPeter Wemm #include <dirent.h> 539b50d902SRodney W. Grimes #include <err.h> 549b50d902SRodney W. Grimes #include <errno.h> 559b50d902SRodney W. Grimes #include <fnmatch.h> 569b50d902SRodney W. Grimes #include <fts.h> 579b50d902SRodney W. Grimes #include <grp.h> 585e25d888STim J. Robbins #include <limits.h> 599b50d902SRodney W. Grimes #include <pwd.h> 607c1d4b3aSAkinori MUSHA #include <regex.h> 619b50d902SRodney W. Grimes #include <stdio.h> 629b50d902SRodney W. Grimes #include <stdlib.h> 639b50d902SRodney W. Grimes #include <string.h> 649b50d902SRodney W. Grimes #include <unistd.h> 651c832963SOliver Eikemeier #include <ctype.h> 669b50d902SRodney W. Grimes 679b50d902SRodney W. Grimes #include "find.h" 689b50d902SRodney W. Grimes 69ecca1f1cSMark Murray static PLAN *palloc(OPTION *); 70ecca1f1cSMark Murray static long long find_parsenum(PLAN *, const char *, char *, char *); 71ecca1f1cSMark Murray static long long find_parsetime(PLAN *, const char *, char *); 72ecca1f1cSMark Murray static char *nextarg(OPTION *, char ***); 73ea92232aSPoul-Henning Kamp 74a07af811STim J. Robbins extern char **environ; 75a07af811STim J. Robbins 7622170420SKirill Ponomarev static PLAN *lastexecplus = NULL; 7722170420SKirill Ponomarev 78adff4fcaSRuslan Ermilov #define COMPARE(a, b) do { \ 79ea92232aSPoul-Henning Kamp switch (plan->flags & F_ELG_MASK) { \ 809b50d902SRodney W. Grimes case F_EQUAL: \ 819b50d902SRodney W. Grimes return (a == b); \ 829b50d902SRodney W. Grimes case F_LESSTHAN: \ 839b50d902SRodney W. Grimes return (a < b); \ 849b50d902SRodney W. Grimes case F_GREATER: \ 859b50d902SRodney W. Grimes return (a > b); \ 869b50d902SRodney W. Grimes default: \ 879b50d902SRodney W. Grimes abort(); \ 889b50d902SRodney W. Grimes } \ 89adff4fcaSRuslan Ermilov } while(0) 909b50d902SRodney W. Grimes 91ea92232aSPoul-Henning Kamp static PLAN * 92ef646f18SMark Murray palloc(OPTION *option) 93ea92232aSPoul-Henning Kamp { 94ea92232aSPoul-Henning Kamp PLAN *new; 95ea92232aSPoul-Henning Kamp 96ea92232aSPoul-Henning Kamp if ((new = malloc(sizeof(PLAN))) == NULL) 97ea92232aSPoul-Henning Kamp err(1, NULL); 98ea92232aSPoul-Henning Kamp new->execute = option->execute; 99ea92232aSPoul-Henning Kamp new->flags = option->flags; 100ea92232aSPoul-Henning Kamp new->next = NULL; 101ea92232aSPoul-Henning Kamp return new; 102ea92232aSPoul-Henning Kamp } 1039b50d902SRodney W. Grimes 1049b50d902SRodney W. Grimes /* 1059b50d902SRodney W. Grimes * find_parsenum -- 1069b50d902SRodney W. Grimes * Parse a string of the form [+-]# and return the value. 1079b50d902SRodney W. Grimes */ 1089192bbf4SBruce Evans static long long 109ef646f18SMark Murray find_parsenum(PLAN *plan, const char *option, char *vp, char *endch) 1109b50d902SRodney W. Grimes { 1119192bbf4SBruce Evans long long value; 1129b50d902SRodney W. Grimes char *endchar, *str; /* Pointer to character ending conversion. */ 1139b50d902SRodney W. Grimes 1149b50d902SRodney W. Grimes /* Determine comparison from leading + or -. */ 1159b50d902SRodney W. Grimes str = vp; 1169b50d902SRodney W. Grimes switch (*str) { 1179b50d902SRodney W. Grimes case '+': 1189b50d902SRodney W. Grimes ++str; 119ea92232aSPoul-Henning Kamp plan->flags |= F_GREATER; 1209b50d902SRodney W. Grimes break; 1219b50d902SRodney W. Grimes case '-': 1229b50d902SRodney W. Grimes ++str; 123ea92232aSPoul-Henning Kamp plan->flags |= F_LESSTHAN; 1249b50d902SRodney W. Grimes break; 1259b50d902SRodney W. Grimes default: 126ea92232aSPoul-Henning Kamp plan->flags |= F_EQUAL; 1279b50d902SRodney W. Grimes break; 1289b50d902SRodney W. Grimes } 1299b50d902SRodney W. Grimes 1309b50d902SRodney W. Grimes /* 1319192bbf4SBruce Evans * Convert the string with strtoq(). Note, if strtoq() returns zero 1329b50d902SRodney W. Grimes * and endchar points to the beginning of the string we know we have 1339b50d902SRodney W. Grimes * a syntax error. 1349b50d902SRodney W. Grimes */ 1359192bbf4SBruce Evans value = strtoq(str, &endchar, 10); 1369b50d902SRodney W. Grimes if (value == 0 && endchar == str) 1379b50d902SRodney W. Grimes errx(1, "%s: %s: illegal numeric value", option, vp); 1385a890aacSKirill Ponomarev if (endchar[0] && endch == NULL) 1399b50d902SRodney W. Grimes errx(1, "%s: %s: illegal trailing character", option, vp); 1409b50d902SRodney W. Grimes if (endch) 1419b50d902SRodney W. Grimes *endch = endchar[0]; 142ea92232aSPoul-Henning Kamp return value; 1439b50d902SRodney W. Grimes } 1449b50d902SRodney W. Grimes 1459b50d902SRodney W. Grimes /* 146adff4fcaSRuslan Ermilov * find_parsetime -- 147adff4fcaSRuslan Ermilov * Parse a string of the form [+-]([0-9]+[smhdw]?)+ and return the value. 148adff4fcaSRuslan Ermilov */ 149adff4fcaSRuslan Ermilov static long long 150ef646f18SMark Murray find_parsetime(PLAN *plan, const char *option, char *vp) 151adff4fcaSRuslan Ermilov { 152adff4fcaSRuslan Ermilov long long secs, value; 153adff4fcaSRuslan Ermilov char *str, *unit; /* Pointer to character ending conversion. */ 154adff4fcaSRuslan Ermilov 155adff4fcaSRuslan Ermilov /* Determine comparison from leading + or -. */ 156adff4fcaSRuslan Ermilov str = vp; 157adff4fcaSRuslan Ermilov switch (*str) { 158adff4fcaSRuslan Ermilov case '+': 159adff4fcaSRuslan Ermilov ++str; 160adff4fcaSRuslan Ermilov plan->flags |= F_GREATER; 161adff4fcaSRuslan Ermilov break; 162adff4fcaSRuslan Ermilov case '-': 163adff4fcaSRuslan Ermilov ++str; 164adff4fcaSRuslan Ermilov plan->flags |= F_LESSTHAN; 165adff4fcaSRuslan Ermilov break; 166adff4fcaSRuslan Ermilov default: 167adff4fcaSRuslan Ermilov plan->flags |= F_EQUAL; 168adff4fcaSRuslan Ermilov break; 169adff4fcaSRuslan Ermilov } 170adff4fcaSRuslan Ermilov 171adff4fcaSRuslan Ermilov value = strtoq(str, &unit, 10); 172adff4fcaSRuslan Ermilov if (value == 0 && unit == str) { 173adff4fcaSRuslan Ermilov errx(1, "%s: %s: illegal time value", option, vp); 174adff4fcaSRuslan Ermilov /* NOTREACHED */ 175adff4fcaSRuslan Ermilov } 176adff4fcaSRuslan Ermilov if (*unit == '\0') 177adff4fcaSRuslan Ermilov return value; 178adff4fcaSRuslan Ermilov 179adff4fcaSRuslan Ermilov /* Units syntax. */ 180adff4fcaSRuslan Ermilov secs = 0; 181adff4fcaSRuslan Ermilov for (;;) { 182adff4fcaSRuslan Ermilov switch(*unit) { 183adff4fcaSRuslan Ermilov case 's': /* seconds */ 184adff4fcaSRuslan Ermilov secs += value; 185adff4fcaSRuslan Ermilov break; 186adff4fcaSRuslan Ermilov case 'm': /* minutes */ 187adff4fcaSRuslan Ermilov secs += value * 60; 188adff4fcaSRuslan Ermilov break; 189adff4fcaSRuslan Ermilov case 'h': /* hours */ 190adff4fcaSRuslan Ermilov secs += value * 3600; 191adff4fcaSRuslan Ermilov break; 192adff4fcaSRuslan Ermilov case 'd': /* days */ 193adff4fcaSRuslan Ermilov secs += value * 86400; 194adff4fcaSRuslan Ermilov break; 195adff4fcaSRuslan Ermilov case 'w': /* weeks */ 196adff4fcaSRuslan Ermilov secs += value * 604800; 197adff4fcaSRuslan Ermilov break; 198adff4fcaSRuslan Ermilov default: 199adff4fcaSRuslan Ermilov errx(1, "%s: %s: bad unit '%c'", option, vp, *unit); 200adff4fcaSRuslan Ermilov /* NOTREACHED */ 201adff4fcaSRuslan Ermilov } 202adff4fcaSRuslan Ermilov str = unit + 1; 203adff4fcaSRuslan Ermilov if (*str == '\0') /* EOS */ 204adff4fcaSRuslan Ermilov break; 205adff4fcaSRuslan Ermilov value = strtoq(str, &unit, 10); 206adff4fcaSRuslan Ermilov if (value == 0 && unit == str) { 207adff4fcaSRuslan Ermilov errx(1, "%s: %s: illegal time value", option, vp); 208adff4fcaSRuslan Ermilov /* NOTREACHED */ 209adff4fcaSRuslan Ermilov } 210adff4fcaSRuslan Ermilov if (*unit == '\0') { 211adff4fcaSRuslan Ermilov errx(1, "%s: %s: missing trailing unit", option, vp); 212adff4fcaSRuslan Ermilov /* NOTREACHED */ 213adff4fcaSRuslan Ermilov } 214adff4fcaSRuslan Ermilov } 215adff4fcaSRuslan Ermilov plan->flags |= F_EXACTTIME; 216adff4fcaSRuslan Ermilov return secs; 217adff4fcaSRuslan Ermilov } 218adff4fcaSRuslan Ermilov 219adff4fcaSRuslan Ermilov /* 220ea92232aSPoul-Henning Kamp * nextarg -- 221ea92232aSPoul-Henning Kamp * Check that another argument still exists, return a pointer to it, 222ea92232aSPoul-Henning Kamp * and increment the argument vector pointer. 223ea92232aSPoul-Henning Kamp */ 224ea92232aSPoul-Henning Kamp static char * 225ef646f18SMark Murray nextarg(OPTION *option, char ***argvp) 226ea92232aSPoul-Henning Kamp { 227ea92232aSPoul-Henning Kamp char *arg; 228ea92232aSPoul-Henning Kamp 22926ac9660SMarcelo Araujo if ((arg = **argvp) == NULL) 230ea92232aSPoul-Henning Kamp errx(1, "%s: requires additional arguments", option->name); 231ea92232aSPoul-Henning Kamp (*argvp)++; 232ea92232aSPoul-Henning Kamp return arg; 233ea92232aSPoul-Henning Kamp } /* nextarg() */ 234ea92232aSPoul-Henning Kamp 235ea92232aSPoul-Henning Kamp /* 23631d53425SCeri Davies * The value of n for the inode times (atime, birthtime, ctime, mtime) is a 23731d53425SCeri Davies * range, i.e. n matches from (n - 1) to n 24 hour periods. This interacts 23831d53425SCeri Davies * with -n, such that "-mtime -1" would be less than 0 days, which isn't what 23931d53425SCeri Davies * the user wanted. Correct so that -1 is "less than 1". 2409b50d902SRodney W. Grimes */ 241ea92232aSPoul-Henning Kamp #define TIME_CORRECT(p) \ 242ea92232aSPoul-Henning Kamp if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \ 243ddd956b0SJilles Tjoelker ++((p)->t_data.tv_sec); 2449b50d902SRodney W. Grimes 2459b50d902SRodney W. Grimes /* 246ea92232aSPoul-Henning Kamp * -[acm]min n functions -- 2473f5223f8SWolfram Schneider * 248ea92232aSPoul-Henning Kamp * True if the difference between the 249ea92232aSPoul-Henning Kamp * file access time (-amin) 25031d53425SCeri Davies * file birth time (-Bmin) 251ea92232aSPoul-Henning Kamp * last change of file status information (-cmin) 252ea92232aSPoul-Henning Kamp * file modification time (-mmin) 253ea92232aSPoul-Henning Kamp * and the current time is n min periods. 2543f5223f8SWolfram Schneider */ 2553f5223f8SWolfram Schneider int 256ef646f18SMark Murray f_Xmin(PLAN *plan, FTSENT *entry) 2573f5223f8SWolfram Schneider { 258ea92232aSPoul-Henning Kamp if (plan->flags & F_TIME_C) { 2593f5223f8SWolfram Schneider COMPARE((now - entry->fts_statp->st_ctime + 260ddd956b0SJilles Tjoelker 60 - 1) / 60, plan->t_data.tv_sec); 261ea92232aSPoul-Henning Kamp } else if (plan->flags & F_TIME_A) { 262ea92232aSPoul-Henning Kamp COMPARE((now - entry->fts_statp->st_atime + 263ddd956b0SJilles Tjoelker 60 - 1) / 60, plan->t_data.tv_sec); 26431d53425SCeri Davies } else if (plan->flags & F_TIME_B) { 26531d53425SCeri Davies COMPARE((now - entry->fts_statp->st_birthtime + 266ddd956b0SJilles Tjoelker 60 - 1) / 60, plan->t_data.tv_sec); 267ea92232aSPoul-Henning Kamp } else { 268ea92232aSPoul-Henning Kamp COMPARE((now - entry->fts_statp->st_mtime + 269ddd956b0SJilles Tjoelker 60 - 1) / 60, plan->t_data.tv_sec); 270ea92232aSPoul-Henning Kamp } 2713f5223f8SWolfram Schneider } 2723f5223f8SWolfram Schneider 2733f5223f8SWolfram Schneider PLAN * 274ef646f18SMark Murray c_Xmin(OPTION *option, char ***argvp) 2753f5223f8SWolfram Schneider { 276ea92232aSPoul-Henning Kamp char *nmins; 2773f5223f8SWolfram Schneider PLAN *new; 2783f5223f8SWolfram Schneider 279ea92232aSPoul-Henning Kamp nmins = nextarg(option, argvp); 2803f5223f8SWolfram Schneider ftsoptions &= ~FTS_NOSTAT; 2813f5223f8SWolfram Schneider 282ea92232aSPoul-Henning Kamp new = palloc(option); 283ddd956b0SJilles Tjoelker new->t_data.tv_sec = find_parsenum(new, option->name, nmins, NULL); 284ddd956b0SJilles Tjoelker new->t_data.tv_nsec = 0; 285ea92232aSPoul-Henning Kamp TIME_CORRECT(new); 286ea92232aSPoul-Henning Kamp return new; 2873f5223f8SWolfram Schneider } 2883f5223f8SWolfram Schneider 2899b50d902SRodney W. Grimes /* 290ea92232aSPoul-Henning Kamp * -[acm]time n functions -- 2919b50d902SRodney W. Grimes * 292ea92232aSPoul-Henning Kamp * True if the difference between the 293ea92232aSPoul-Henning Kamp * file access time (-atime) 29431d53425SCeri Davies * file birth time (-Btime) 295ea92232aSPoul-Henning Kamp * last change of file status information (-ctime) 296ea92232aSPoul-Henning Kamp * file modification time (-mtime) 297ea92232aSPoul-Henning Kamp * and the current time is n 24 hour periods. 2989b50d902SRodney W. Grimes */ 299ea92232aSPoul-Henning Kamp 3009b50d902SRodney W. Grimes int 301ef646f18SMark Murray f_Xtime(PLAN *plan, FTSENT *entry) 3029b50d902SRodney W. Grimes { 303631a8765SRuslan Ermilov time_t xtime; 304adff4fcaSRuslan Ermilov 305631a8765SRuslan Ermilov if (plan->flags & F_TIME_A) 306631a8765SRuslan Ermilov xtime = entry->fts_statp->st_atime; 30731d53425SCeri Davies else if (plan->flags & F_TIME_B) 30831d53425SCeri Davies xtime = entry->fts_statp->st_birthtime; 309631a8765SRuslan Ermilov else if (plan->flags & F_TIME_C) 310631a8765SRuslan Ermilov xtime = entry->fts_statp->st_ctime; 311631a8765SRuslan Ermilov else 312631a8765SRuslan Ermilov xtime = entry->fts_statp->st_mtime; 3139b50d902SRodney W. Grimes 314631a8765SRuslan Ermilov if (plan->flags & F_EXACTTIME) 315ddd956b0SJilles Tjoelker COMPARE(now - xtime, plan->t_data.tv_sec); 316adff4fcaSRuslan Ermilov else 317ddd956b0SJilles Tjoelker COMPARE((now - xtime + 86400 - 1) / 86400, plan->t_data.tv_sec); 3189b50d902SRodney W. Grimes } 3199b50d902SRodney W. Grimes 3209b50d902SRodney W. Grimes PLAN * 321ef646f18SMark Murray c_Xtime(OPTION *option, char ***argvp) 3229b50d902SRodney W. Grimes { 323adff4fcaSRuslan Ermilov char *value; 3249b50d902SRodney W. Grimes PLAN *new; 3259b50d902SRodney W. Grimes 326adff4fcaSRuslan Ermilov value = nextarg(option, argvp); 3279b50d902SRodney W. Grimes ftsoptions &= ~FTS_NOSTAT; 3289b50d902SRodney W. Grimes 329ea92232aSPoul-Henning Kamp new = palloc(option); 330ddd956b0SJilles Tjoelker new->t_data.tv_sec = find_parsetime(new, option->name, value); 331ddd956b0SJilles Tjoelker new->t_data.tv_nsec = 0; 332adff4fcaSRuslan Ermilov if (!(new->flags & F_EXACTTIME)) 333ea92232aSPoul-Henning Kamp TIME_CORRECT(new); 334ea92232aSPoul-Henning Kamp return new; 335ea92232aSPoul-Henning Kamp } 336ea92232aSPoul-Henning Kamp 337ea92232aSPoul-Henning Kamp /* 338ea92232aSPoul-Henning Kamp * -maxdepth/-mindepth n functions -- 339ea92232aSPoul-Henning Kamp * 340ea92232aSPoul-Henning Kamp * Does the same as -prune if the level of the current file is 341ea92232aSPoul-Henning Kamp * greater/less than the specified maximum/minimum depth. 342ea92232aSPoul-Henning Kamp * 343ea92232aSPoul-Henning Kamp * Note that -maxdepth and -mindepth are handled specially in 344ea92232aSPoul-Henning Kamp * find_execute() so their f_* functions are set to f_always_true(). 345ea92232aSPoul-Henning Kamp */ 346ea92232aSPoul-Henning Kamp PLAN * 347ef646f18SMark Murray c_mXXdepth(OPTION *option, char ***argvp) 348ea92232aSPoul-Henning Kamp { 349ea92232aSPoul-Henning Kamp char *dstr; 350ea92232aSPoul-Henning Kamp PLAN *new; 351ea92232aSPoul-Henning Kamp 352ea92232aSPoul-Henning Kamp dstr = nextarg(option, argvp); 353ea92232aSPoul-Henning Kamp if (dstr[0] == '-') 354ea92232aSPoul-Henning Kamp /* all other errors handled by find_parsenum() */ 355ea92232aSPoul-Henning Kamp errx(1, "%s: %s: value must be positive", option->name, dstr); 356ea92232aSPoul-Henning Kamp 357ea92232aSPoul-Henning Kamp new = palloc(option); 358ea92232aSPoul-Henning Kamp if (option->flags & F_MAXDEPTH) 359ea92232aSPoul-Henning Kamp maxdepth = find_parsenum(new, option->name, dstr, NULL); 360ea92232aSPoul-Henning Kamp else 361ea92232aSPoul-Henning Kamp mindepth = find_parsenum(new, option->name, dstr, NULL); 362ea92232aSPoul-Henning Kamp return new; 363ea92232aSPoul-Henning Kamp } 364ea92232aSPoul-Henning Kamp 365ea92232aSPoul-Henning Kamp /* 3669c5d31dfSBosko Milekic * -acl function -- 3679c5d31dfSBosko Milekic * 3689c5d31dfSBosko Milekic * Show files with EXTENDED ACL attributes. 3699c5d31dfSBosko Milekic */ 3709c5d31dfSBosko Milekic int 3719c5d31dfSBosko Milekic f_acl(PLAN *plan __unused, FTSENT *entry) 3729c5d31dfSBosko Milekic { 3739c5d31dfSBosko Milekic acl_t facl; 374fa2db914SEdward Tomasz Napierala acl_type_t acl_type; 375fa2db914SEdward Tomasz Napierala int acl_supported = 0, ret, trivial; 3769c5d31dfSBosko Milekic 3779c5d31dfSBosko Milekic if (S_ISLNK(entry->fts_statp->st_mode)) 3789c5d31dfSBosko Milekic return 0; 379fa2db914SEdward Tomasz Napierala ret = pathconf(entry->fts_accpath, _PC_ACL_NFS4); 380fa2db914SEdward Tomasz Napierala if (ret > 0) { 381fa2db914SEdward Tomasz Napierala acl_supported = 1; 382fa2db914SEdward Tomasz Napierala acl_type = ACL_TYPE_NFS4; 383fa2db914SEdward Tomasz Napierala } else if (ret < 0 && errno != EINVAL) { 3849c5d31dfSBosko Milekic warn("%s", entry->fts_accpath); 385fa2db914SEdward Tomasz Napierala return (0); 3869c5d31dfSBosko Milekic } 387fa2db914SEdward Tomasz Napierala if (acl_supported == 0) { 388fa2db914SEdward Tomasz Napierala ret = pathconf(entry->fts_accpath, _PC_ACL_EXTENDED); 389fa2db914SEdward Tomasz Napierala if (ret > 0) { 390fa2db914SEdward Tomasz Napierala acl_supported = 1; 391fa2db914SEdward Tomasz Napierala acl_type = ACL_TYPE_ACCESS; 392fa2db914SEdward Tomasz Napierala } else if (ret < 0 && errno != EINVAL) { 393fa2db914SEdward Tomasz Napierala warn("%s", entry->fts_accpath); 394fa2db914SEdward Tomasz Napierala return (0); 3959c5d31dfSBosko Milekic } 3969c5d31dfSBosko Milekic } 397fa2db914SEdward Tomasz Napierala if (acl_supported == 0) 398fa2db914SEdward Tomasz Napierala return (0); 399fa2db914SEdward Tomasz Napierala 400fa2db914SEdward Tomasz Napierala facl = acl_get_file(entry->fts_accpath, acl_type); 401fa2db914SEdward Tomasz Napierala if (facl == NULL) { 402fa2db914SEdward Tomasz Napierala warn("%s", entry->fts_accpath); 403fa2db914SEdward Tomasz Napierala return (0); 4049c5d31dfSBosko Milekic } 405fa2db914SEdward Tomasz Napierala ret = acl_is_trivial_np(facl, &trivial); 4069c5d31dfSBosko Milekic acl_free(facl); 407fa2db914SEdward Tomasz Napierala if (ret) { 4089c5d31dfSBosko Milekic warn("%s", entry->fts_accpath); 409fa2db914SEdward Tomasz Napierala return (0); 410fa2db914SEdward Tomasz Napierala } 411fa2db914SEdward Tomasz Napierala if (trivial) 412fa2db914SEdward Tomasz Napierala return (0); 413fa2db914SEdward Tomasz Napierala return (1); 4149c5d31dfSBosko Milekic } 4159c5d31dfSBosko Milekic 4169c5d31dfSBosko Milekic PLAN * 4179c5d31dfSBosko Milekic c_acl(OPTION *option, char ***argvp __unused) 4189c5d31dfSBosko Milekic { 4199c5d31dfSBosko Milekic ftsoptions &= ~FTS_NOSTAT; 4209c5d31dfSBosko Milekic return (palloc(option)); 4219c5d31dfSBosko Milekic } 4229c5d31dfSBosko Milekic 4239c5d31dfSBosko Milekic /* 424ea92232aSPoul-Henning Kamp * -delete functions -- 425ea92232aSPoul-Henning Kamp * 426ea92232aSPoul-Henning Kamp * True always. Makes its best shot and continues on regardless. 427ea92232aSPoul-Henning Kamp */ 428ea92232aSPoul-Henning Kamp int 429ef646f18SMark Murray f_delete(PLAN *plan __unused, FTSENT *entry) 430ea92232aSPoul-Henning Kamp { 431ea92232aSPoul-Henning Kamp /* ignore these from fts */ 432ea92232aSPoul-Henning Kamp if (strcmp(entry->fts_accpath, ".") == 0 || 433ea92232aSPoul-Henning Kamp strcmp(entry->fts_accpath, "..") == 0) 434ea92232aSPoul-Henning Kamp return 1; 435ea92232aSPoul-Henning Kamp 436ea92232aSPoul-Henning Kamp /* sanity check */ 437ea92232aSPoul-Henning Kamp if (isdepth == 0 || /* depth off */ 43805e605b7SAndriy Gapon (ftsoptions & FTS_NOSTAT)) /* not stat()ing */ 439ea92232aSPoul-Henning Kamp errx(1, "-delete: insecure options got turned on"); 440ea92232aSPoul-Henning Kamp 44105e605b7SAndriy Gapon if (!(ftsoptions & FTS_PHYSICAL) || /* physical off */ 44205e605b7SAndriy Gapon (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */ 44305e605b7SAndriy Gapon errx(1, "-delete: forbidden when symlinks are followed"); 44405e605b7SAndriy Gapon 445ea92232aSPoul-Henning Kamp /* Potentially unsafe - do not accept relative paths whatsoever */ 4469d6d5a71SJilles Tjoelker if (entry->fts_level > FTS_ROOTLEVEL && 4479d6d5a71SJilles Tjoelker strchr(entry->fts_accpath, '/') != NULL) 448ea92232aSPoul-Henning Kamp errx(1, "-delete: %s: relative path potentially not safe", 449ea92232aSPoul-Henning Kamp entry->fts_accpath); 450ea92232aSPoul-Henning Kamp 451ea92232aSPoul-Henning Kamp /* Turn off user immutable bits if running as root */ 452ea92232aSPoul-Henning Kamp if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) && 453ea92232aSPoul-Henning Kamp !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) && 454ea92232aSPoul-Henning Kamp geteuid() == 0) 4556911f596SJilles Tjoelker lchflags(entry->fts_accpath, 456ea92232aSPoul-Henning Kamp entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)); 457ea92232aSPoul-Henning Kamp 458ea92232aSPoul-Henning Kamp /* rmdir directories, unlink everything else */ 459ea92232aSPoul-Henning Kamp if (S_ISDIR(entry->fts_statp->st_mode)) { 460ea92232aSPoul-Henning Kamp if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY) 461ea92232aSPoul-Henning Kamp warn("-delete: rmdir(%s)", entry->fts_path); 462ea92232aSPoul-Henning Kamp } else { 463ea92232aSPoul-Henning Kamp if (unlink(entry->fts_accpath) < 0) 464ea92232aSPoul-Henning Kamp warn("-delete: unlink(%s)", entry->fts_path); 465ea92232aSPoul-Henning Kamp } 466ea92232aSPoul-Henning Kamp 467ea92232aSPoul-Henning Kamp /* "succeed" */ 468ea92232aSPoul-Henning Kamp return 1; 469ea92232aSPoul-Henning Kamp } 470ea92232aSPoul-Henning Kamp 471ea92232aSPoul-Henning Kamp PLAN * 472ef646f18SMark Murray c_delete(OPTION *option, char ***argvp __unused) 473ea92232aSPoul-Henning Kamp { 474ea92232aSPoul-Henning Kamp 475ea92232aSPoul-Henning Kamp ftsoptions &= ~FTS_NOSTAT; /* no optimise */ 476ea92232aSPoul-Henning Kamp isoutput = 1; /* possible output */ 477ea92232aSPoul-Henning Kamp isdepth = 1; /* -depth implied */ 478ea92232aSPoul-Henning Kamp 47917ef6d3aSJilles Tjoelker /* 48017ef6d3aSJilles Tjoelker * Try to avoid the confusing error message about relative paths 48117ef6d3aSJilles Tjoelker * being potentially not safe. 48217ef6d3aSJilles Tjoelker */ 48317ef6d3aSJilles Tjoelker if (ftsoptions & FTS_NOCHDIR) 48417ef6d3aSJilles Tjoelker errx(1, "%s: forbidden when the current directory cannot be opened", 48517ef6d3aSJilles Tjoelker "-delete"); 48617ef6d3aSJilles Tjoelker 487ea92232aSPoul-Henning Kamp return palloc(option); 4889b50d902SRodney W. Grimes } 4899b50d902SRodney W. Grimes 4903f5223f8SWolfram Schneider 4919b50d902SRodney W. Grimes /* 4921c832963SOliver Eikemeier * always_true -- 4939b50d902SRodney W. Grimes * 49446b993ffSWarner Losh * Always true, used for -maxdepth, -mindepth, -xdev, -follow, and -true 4959b50d902SRodney W. Grimes */ 4969b50d902SRodney W. Grimes int 497ef646f18SMark Murray f_always_true(PLAN *plan __unused, FTSENT *entry __unused) 4989b50d902SRodney W. Grimes { 499ea92232aSPoul-Henning Kamp return 1; 5009b50d902SRodney W. Grimes } 5019b50d902SRodney W. Grimes 5021c832963SOliver Eikemeier /* 5031c832963SOliver Eikemeier * -depth functions -- 5041c832963SOliver Eikemeier * 5051c832963SOliver Eikemeier * With argument: True if the file is at level n. 5061c832963SOliver Eikemeier * Without argument: Always true, causes descent of the directory hierarchy 5071c832963SOliver Eikemeier * to be done so that all entries in a directory are acted on before the 5081c832963SOliver Eikemeier * directory itself. 5091c832963SOliver Eikemeier */ 5101c832963SOliver Eikemeier int 5111c832963SOliver Eikemeier f_depth(PLAN *plan, FTSENT *entry) 5129b50d902SRodney W. Grimes { 5131c832963SOliver Eikemeier if (plan->flags & F_DEPTH) 5141c832963SOliver Eikemeier COMPARE(entry->fts_level, plan->d_data); 5151c832963SOliver Eikemeier else 5161c832963SOliver Eikemeier return 1; 5171c832963SOliver Eikemeier } 5189b50d902SRodney W. Grimes 5191c832963SOliver Eikemeier PLAN * 5201c832963SOliver Eikemeier c_depth(OPTION *option, char ***argvp) 5211c832963SOliver Eikemeier { 5221c832963SOliver Eikemeier PLAN *new; 5231c832963SOliver Eikemeier char *str; 5241c832963SOliver Eikemeier 5251c832963SOliver Eikemeier new = palloc(option); 5261c832963SOliver Eikemeier 5271c832963SOliver Eikemeier str = **argvp; 5281c832963SOliver Eikemeier if (str && !(new->flags & F_DEPTH)) { 5291c832963SOliver Eikemeier /* skip leading + or - */ 5301c832963SOliver Eikemeier if (*str == '+' || *str == '-') 5311c832963SOliver Eikemeier str++; 5321c832963SOliver Eikemeier /* skip sign */ 5331c832963SOliver Eikemeier if (*str == '+' || *str == '-') 5341c832963SOliver Eikemeier str++; 5351c832963SOliver Eikemeier if (isdigit(*str)) 5361c832963SOliver Eikemeier new->flags |= F_DEPTH; 5371c832963SOliver Eikemeier } 5381c832963SOliver Eikemeier 5391c832963SOliver Eikemeier if (new->flags & F_DEPTH) { /* -depth n */ 5401c832963SOliver Eikemeier char *ndepth; 5411c832963SOliver Eikemeier 5421c832963SOliver Eikemeier ndepth = nextarg(option, argvp); 5431c832963SOliver Eikemeier new->d_data = find_parsenum(new, option->name, ndepth, NULL); 5441c832963SOliver Eikemeier } else { /* -d */ 5451c832963SOliver Eikemeier isdepth = 1; 5461c832963SOliver Eikemeier } 5471c832963SOliver Eikemeier 5481c832963SOliver Eikemeier return new; 5499b50d902SRodney W. Grimes } 550127d7563SWarner Losh 551127d7563SWarner Losh /* 552ed1a4621SPeter Wemm * -empty functions -- 553ed1a4621SPeter Wemm * 554ed1a4621SPeter Wemm * True if the file or directory is empty 555ed1a4621SPeter Wemm */ 556ed1a4621SPeter Wemm int 557ef646f18SMark Murray f_empty(PLAN *plan __unused, FTSENT *entry) 558ed1a4621SPeter Wemm { 559ea92232aSPoul-Henning Kamp if (S_ISREG(entry->fts_statp->st_mode) && 560ea92232aSPoul-Henning Kamp entry->fts_statp->st_size == 0) 561ea92232aSPoul-Henning Kamp return 1; 562ed1a4621SPeter Wemm if (S_ISDIR(entry->fts_statp->st_mode)) { 563ed1a4621SPeter Wemm struct dirent *dp; 564ed1a4621SPeter Wemm int empty; 565ed1a4621SPeter Wemm DIR *dir; 566ed1a4621SPeter Wemm 567ed1a4621SPeter Wemm empty = 1; 568ed1a4621SPeter Wemm dir = opendir(entry->fts_accpath); 569ed1a4621SPeter Wemm if (dir == NULL) 570e66a677bSKevin Lo return 0; 571ed1a4621SPeter Wemm for (dp = readdir(dir); dp; dp = readdir(dir)) 572ed1a4621SPeter Wemm if (dp->d_name[0] != '.' || 573ed1a4621SPeter Wemm (dp->d_name[1] != '\0' && 574ed1a4621SPeter Wemm (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) { 575ed1a4621SPeter Wemm empty = 0; 576ed1a4621SPeter Wemm break; 577ed1a4621SPeter Wemm } 578ed1a4621SPeter Wemm closedir(dir); 579ea92232aSPoul-Henning Kamp return empty; 580ed1a4621SPeter Wemm } 581ea92232aSPoul-Henning Kamp return 0; 582ed1a4621SPeter Wemm } 583ed1a4621SPeter Wemm 584ed1a4621SPeter Wemm PLAN * 585ef646f18SMark Murray c_empty(OPTION *option, char ***argvp __unused) 586ed1a4621SPeter Wemm { 587ed1a4621SPeter Wemm ftsoptions &= ~FTS_NOSTAT; 588ed1a4621SPeter Wemm 589ea92232aSPoul-Henning Kamp return palloc(option); 590ed1a4621SPeter Wemm } 591ed1a4621SPeter Wemm 592ed1a4621SPeter Wemm /* 593ea92232aSPoul-Henning Kamp * [-exec | -execdir | -ok] utility [arg ... ] ; functions -- 594127d7563SWarner Losh * 595127d7563SWarner Losh * True if the executed utility returns a zero value as exit status. 596127d7563SWarner Losh * The end of the primary expression is delimited by a semicolon. If 597ea92232aSPoul-Henning Kamp * "{}" occurs anywhere, it gets replaced by the current pathname, 598ea92232aSPoul-Henning Kamp * or, in the case of -execdir, the current basename (filename 599ea92232aSPoul-Henning Kamp * without leading directory prefix). For -exec and -ok, 600ea92232aSPoul-Henning Kamp * the current directory for the execution of utility is the same as 601ea92232aSPoul-Henning Kamp * the current directory when the find utility was started, whereas 602ea92232aSPoul-Henning Kamp * for -execdir, it is the directory the file resides in. 603ea92232aSPoul-Henning Kamp * 604ea92232aSPoul-Henning Kamp * The primary -ok differs from -exec in that it requests affirmation 605ea92232aSPoul-Henning Kamp * of the user before executing the utility. 606127d7563SWarner Losh */ 607127d7563SWarner Losh int 608ef646f18SMark Murray f_exec(PLAN *plan, FTSENT *entry) 609127d7563SWarner Losh { 610e98080b1SDavid Malone int cnt; 611127d7563SWarner Losh pid_t pid; 612127d7563SWarner Losh int status; 613127d7563SWarner Losh char *file; 614127d7563SWarner Losh 6155e25d888STim J. Robbins if (entry == NULL && plan->flags & F_EXECPLUS) { 6165e25d888STim J. Robbins if (plan->e_ppos == plan->e_pbnum) 6175e25d888STim J. Robbins return (1); 6185e25d888STim J. Robbins plan->e_argv[plan->e_ppos] = NULL; 6195e25d888STim J. Robbins goto doexec; 6205e25d888STim J. Robbins } 6215e25d888STim J. Robbins 622127d7563SWarner Losh /* XXX - if file/dir ends in '/' this will not work -- can it? */ 623ea92232aSPoul-Henning Kamp if ((plan->flags & F_EXECDIR) && \ 624ea92232aSPoul-Henning Kamp (file = strrchr(entry->fts_path, '/'))) 625127d7563SWarner Losh file++; 626127d7563SWarner Losh else 627127d7563SWarner Losh file = entry->fts_path; 628127d7563SWarner Losh 6295e25d888STim J. Robbins if (plan->flags & F_EXECPLUS) { 6305e25d888STim J. Robbins if ((plan->e_argv[plan->e_ppos] = strdup(file)) == NULL) 6315e25d888STim J. Robbins err(1, NULL); 6325e25d888STim J. Robbins plan->e_len[plan->e_ppos] = strlen(file); 6335e25d888STim J. Robbins plan->e_psize += plan->e_len[plan->e_ppos]; 6345e25d888STim J. Robbins if (++plan->e_ppos < plan->e_pnummax && 6355e25d888STim J. Robbins plan->e_psize < plan->e_psizemax) 6365e25d888STim J. Robbins return (1); 6375e25d888STim J. Robbins plan->e_argv[plan->e_ppos] = NULL; 6385e25d888STim J. Robbins } else { 639127d7563SWarner Losh for (cnt = 0; plan->e_argv[cnt]; ++cnt) 640127d7563SWarner Losh if (plan->e_len[cnt]) 6415e25d888STim J. Robbins brace_subst(plan->e_orig[cnt], 6425e25d888STim J. Robbins &plan->e_argv[cnt], file, 6435e25d888STim J. Robbins plan->e_len[cnt]); 6445e25d888STim J. Robbins } 645127d7563SWarner Losh 6465e25d888STim J. Robbins doexec: if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv)) 647ea92232aSPoul-Henning Kamp return 0; 648ea92232aSPoul-Henning Kamp 649ea92232aSPoul-Henning Kamp /* make sure find output is interspersed correctly with subprocesses */ 650127d7563SWarner Losh fflush(stdout); 651127d7563SWarner Losh fflush(stderr); 652127d7563SWarner Losh 6531fd98d7dSDag-Erling Smørgrav switch (pid = fork()) { 654127d7563SWarner Losh case -1: 655127d7563SWarner Losh err(1, "fork"); 656127d7563SWarner Losh /* NOTREACHED */ 657127d7563SWarner Losh case 0: 658ea92232aSPoul-Henning Kamp /* change dir back from where we started */ 65917ef6d3aSJilles Tjoelker if (!(plan->flags & F_EXECDIR) && 66017ef6d3aSJilles Tjoelker !(ftsoptions & FTS_NOCHDIR) && fchdir(dotfd)) { 661ea92232aSPoul-Henning Kamp warn("chdir"); 662ea92232aSPoul-Henning Kamp _exit(1); 663ea92232aSPoul-Henning Kamp } 664127d7563SWarner Losh execvp(plan->e_argv[0], plan->e_argv); 665127d7563SWarner Losh warn("%s", plan->e_argv[0]); 666127d7563SWarner Losh _exit(1); 667127d7563SWarner Losh } 6685e25d888STim J. Robbins if (plan->flags & F_EXECPLUS) { 6695e25d888STim J. Robbins while (--plan->e_ppos >= plan->e_pbnum) 6705e25d888STim J. Robbins free(plan->e_argv[plan->e_ppos]); 6715e25d888STim J. Robbins plan->e_ppos = plan->e_pbnum; 6725e25d888STim J. Robbins plan->e_psize = plan->e_pbsize; 6735e25d888STim J. Robbins } 674127d7563SWarner Losh pid = waitpid(pid, &status, 0); 6757a79617cSJilles Tjoelker if (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status)) 6767a79617cSJilles Tjoelker return (1); 6777a79617cSJilles Tjoelker if (plan->flags & F_EXECPLUS) { 6787a79617cSJilles Tjoelker exitstatus = 1; 6797a79617cSJilles Tjoelker return (1); 6807a79617cSJilles Tjoelker } 6817a79617cSJilles Tjoelker return (0); 682127d7563SWarner Losh } 683127d7563SWarner Losh 684127d7563SWarner Losh /* 685ea92232aSPoul-Henning Kamp * c_exec, c_execdir, c_ok -- 686127d7563SWarner Losh * build three parallel arrays, one with pointers to the strings passed 687127d7563SWarner Losh * on the command line, one with (possibly duplicated) pointers to the 688127d7563SWarner Losh * argv array, and one with integer values that are lengths of the 689127d7563SWarner Losh * strings, but also flags meaning that the string has to be massaged. 690127d7563SWarner Losh */ 691127d7563SWarner Losh PLAN * 692ef646f18SMark Murray c_exec(OPTION *option, char ***argvp) 693127d7563SWarner Losh { 694127d7563SWarner Losh PLAN *new; /* node returned */ 6955e25d888STim J. Robbins long argmax; 6965e25d888STim J. Robbins int cnt, i; 697a07af811STim J. Robbins char **argv, **ap, **ep, *p; 698127d7563SWarner Losh 69917ef6d3aSJilles Tjoelker /* This would defeat -execdir's intended security. */ 70017ef6d3aSJilles Tjoelker if (option->flags & F_EXECDIR && ftsoptions & FTS_NOCHDIR) 70117ef6d3aSJilles Tjoelker errx(1, "%s: forbidden when the current directory cannot be opened", 70217ef6d3aSJilles Tjoelker "-execdir"); 70317ef6d3aSJilles Tjoelker 704ea92232aSPoul-Henning Kamp /* XXX - was in c_execdir, but seems unnecessary!? 705127d7563SWarner Losh ftsoptions &= ~FTS_NOSTAT; 706ea92232aSPoul-Henning Kamp */ 707127d7563SWarner Losh isoutput = 1; 708127d7563SWarner Losh 709ea92232aSPoul-Henning Kamp /* XXX - this is a change from the previous coding */ 710ea92232aSPoul-Henning Kamp new = palloc(option); 711127d7563SWarner Losh 712127d7563SWarner Losh for (ap = argv = *argvp;; ++ap) { 713127d7563SWarner Losh if (!*ap) 714127d7563SWarner Losh errx(1, 715e22bb9dbSTim J. Robbins "%s: no terminating \";\" or \"+\"", option->name); 716127d7563SWarner Losh if (**ap == ';') 717127d7563SWarner Losh break; 7185e25d888STim J. Robbins if (**ap == '+' && ap != argv && strcmp(*(ap - 1), "{}") == 0) { 7195e25d888STim J. Robbins new->flags |= F_EXECPLUS; 7205e25d888STim J. Robbins break; 7215e25d888STim J. Robbins } 722127d7563SWarner Losh } 723127d7563SWarner Losh 72451b0534fSJuli Mallett if (ap == argv) 72551b0534fSJuli Mallett errx(1, "%s: no command specified", option->name); 72651b0534fSJuli Mallett 727127d7563SWarner Losh cnt = ap - *argvp + 1; 7285e25d888STim J. Robbins if (new->flags & F_EXECPLUS) { 7295e25d888STim J. Robbins new->e_ppos = new->e_pbnum = cnt - 2; 7305e25d888STim J. Robbins if ((argmax = sysconf(_SC_ARG_MAX)) == -1) { 7315e25d888STim J. Robbins warn("sysconf(_SC_ARG_MAX)"); 7325e25d888STim J. Robbins argmax = _POSIX_ARG_MAX; 7335e25d888STim J. Robbins } 734a07af811STim J. Robbins argmax -= 1024; 735a07af811STim J. Robbins for (ep = environ; *ep != NULL; ep++) 736a07af811STim J. Robbins argmax -= strlen(*ep) + 1 + sizeof(*ep); 737a07af811STim J. Robbins argmax -= 1 + sizeof(*ep); 738bc626176SJilles Tjoelker /* 739bc626176SJilles Tjoelker * Ensure that -execdir ... {} + does not mix files 740bc626176SJilles Tjoelker * from different directories in one invocation. 741bc626176SJilles Tjoelker * Files from the same directory should be handled 742bc626176SJilles Tjoelker * in one invocation but there is no code for it. 743bc626176SJilles Tjoelker */ 744bc626176SJilles Tjoelker new->e_pnummax = new->flags & F_EXECDIR ? 1 : argmax / 16; 745a07af811STim J. Robbins argmax -= sizeof(char *) * new->e_pnummax; 746a07af811STim J. Robbins if (argmax <= 0) 747a07af811STim J. Robbins errx(1, "no space for arguments"); 748a07af811STim J. Robbins new->e_psizemax = argmax; 7495e25d888STim J. Robbins new->e_pbsize = 0; 7505e25d888STim J. Robbins cnt += new->e_pnummax + 1; 75122170420SKirill Ponomarev new->e_next = lastexecplus; 75222170420SKirill Ponomarev lastexecplus = new; 7535e25d888STim J. Robbins } 75447bca8b0SJuli Mallett if ((new->e_argv = malloc(cnt * sizeof(char *))) == NULL) 75547bca8b0SJuli Mallett err(1, NULL); 75647bca8b0SJuli Mallett if ((new->e_orig = malloc(cnt * sizeof(char *))) == NULL) 75747bca8b0SJuli Mallett err(1, NULL); 75847bca8b0SJuli Mallett if ((new->e_len = malloc(cnt * sizeof(int))) == NULL) 75947bca8b0SJuli Mallett err(1, NULL); 760127d7563SWarner Losh 761127d7563SWarner Losh for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) { 762127d7563SWarner Losh new->e_orig[cnt] = *argv; 7635e25d888STim J. Robbins if (new->flags & F_EXECPLUS) 7645e25d888STim J. Robbins new->e_pbsize += strlen(*argv) + 1; 765127d7563SWarner Losh for (p = *argv; *p; ++p) 7665e25d888STim J. Robbins if (!(new->flags & F_EXECPLUS) && p[0] == '{' && 7675e25d888STim J. Robbins p[1] == '}') { 768f0cb9537SDavid E. O'Brien if ((new->e_argv[cnt] = 76947bca8b0SJuli Mallett malloc(MAXPATHLEN)) == NULL) 77047bca8b0SJuli Mallett err(1, NULL); 771127d7563SWarner Losh new->e_len[cnt] = MAXPATHLEN; 772127d7563SWarner Losh break; 773127d7563SWarner Losh } 774127d7563SWarner Losh if (!*p) { 775127d7563SWarner Losh new->e_argv[cnt] = *argv; 776127d7563SWarner Losh new->e_len[cnt] = 0; 777127d7563SWarner Losh } 778127d7563SWarner Losh } 7795e25d888STim J. Robbins if (new->flags & F_EXECPLUS) { 7805e25d888STim J. Robbins new->e_psize = new->e_pbsize; 7815e25d888STim J. Robbins cnt--; 7825e25d888STim J. Robbins for (i = 0; i < new->e_pnummax; i++) { 7835e25d888STim J. Robbins new->e_argv[cnt] = NULL; 7845e25d888STim J. Robbins new->e_len[cnt] = 0; 7855e25d888STim J. Robbins cnt++; 7865e25d888STim J. Robbins } 7875e25d888STim J. Robbins argv = ap; 7885e25d888STim J. Robbins goto done; 7895e25d888STim J. Robbins } 790127d7563SWarner Losh new->e_argv[cnt] = new->e_orig[cnt] = NULL; 791127d7563SWarner Losh 7925e25d888STim J. Robbins done: *argvp = argv + 1; 793ea92232aSPoul-Henning Kamp return new; 794ea92232aSPoul-Henning Kamp } 795ea92232aSPoul-Henning Kamp 79622170420SKirill Ponomarev /* Finish any pending -exec ... {} + functions. */ 79722170420SKirill Ponomarev void 79810bc3a7fSEd Schouten finish_execplus(void) 79922170420SKirill Ponomarev { 80022170420SKirill Ponomarev PLAN *p; 80122170420SKirill Ponomarev 80222170420SKirill Ponomarev p = lastexecplus; 80322170420SKirill Ponomarev while (p != NULL) { 80422170420SKirill Ponomarev (p->execute)(p, NULL); 80522170420SKirill Ponomarev p = p->e_next; 80622170420SKirill Ponomarev } 80722170420SKirill Ponomarev } 80822170420SKirill Ponomarev 809ea92232aSPoul-Henning Kamp int 810ef646f18SMark Murray f_flags(PLAN *plan, FTSENT *entry) 811ea92232aSPoul-Henning Kamp { 812ea92232aSPoul-Henning Kamp u_long flags; 813ea92232aSPoul-Henning Kamp 8147fd5ee41SRuslan Ermilov flags = entry->fts_statp->st_flags; 815ea92232aSPoul-Henning Kamp if (plan->flags & F_ATLEAST) 8167fd5ee41SRuslan Ermilov return (flags | plan->fl_flags) == flags && 8177fd5ee41SRuslan Ermilov !(flags & plan->fl_notflags); 8187fd5ee41SRuslan Ermilov else if (plan->flags & F_ANY) 8197fd5ee41SRuslan Ermilov return (flags & plan->fl_flags) || 8207fd5ee41SRuslan Ermilov (flags | plan->fl_notflags) != flags; 821ea92232aSPoul-Henning Kamp else 8227fd5ee41SRuslan Ermilov return flags == plan->fl_flags && 8237fd5ee41SRuslan Ermilov !(plan->fl_flags & plan->fl_notflags); 824ea92232aSPoul-Henning Kamp } 825ea92232aSPoul-Henning Kamp 826ea92232aSPoul-Henning Kamp PLAN * 827ef646f18SMark Murray c_flags(OPTION *option, char ***argvp) 828ea92232aSPoul-Henning Kamp { 829ea92232aSPoul-Henning Kamp char *flags_str; 830ea92232aSPoul-Henning Kamp PLAN *new; 831ea92232aSPoul-Henning Kamp u_long flags, notflags; 832ea92232aSPoul-Henning Kamp 833ea92232aSPoul-Henning Kamp flags_str = nextarg(option, argvp); 834ea92232aSPoul-Henning Kamp ftsoptions &= ~FTS_NOSTAT; 835ea92232aSPoul-Henning Kamp 836ea92232aSPoul-Henning Kamp new = palloc(option); 837ea92232aSPoul-Henning Kamp 838ea92232aSPoul-Henning Kamp if (*flags_str == '-') { 839ea92232aSPoul-Henning Kamp new->flags |= F_ATLEAST; 840ea92232aSPoul-Henning Kamp flags_str++; 8417fd5ee41SRuslan Ermilov } else if (*flags_str == '+') { 8427fd5ee41SRuslan Ermilov new->flags |= F_ANY; 8437fd5ee41SRuslan Ermilov flags_str++; 844ea92232aSPoul-Henning Kamp } 845ea92232aSPoul-Henning Kamp if (strtofflags(&flags_str, &flags, ¬flags) == 1) 846ea92232aSPoul-Henning Kamp errx(1, "%s: %s: illegal flags string", option->name, flags_str); 847ea92232aSPoul-Henning Kamp 848ea92232aSPoul-Henning Kamp new->fl_flags = flags; 8497fd5ee41SRuslan Ermilov new->fl_notflags = notflags; 850ea92232aSPoul-Henning Kamp return new; 851127d7563SWarner Losh } 8529b50d902SRodney W. Grimes 8539b50d902SRodney W. Grimes /* 8549b50d902SRodney W. Grimes * -follow functions -- 8559b50d902SRodney W. Grimes * 8569b50d902SRodney W. Grimes * Always true, causes symbolic links to be followed on a global 8579b50d902SRodney W. Grimes * basis. 8589b50d902SRodney W. Grimes */ 8599b50d902SRodney W. Grimes PLAN * 860ef646f18SMark Murray c_follow(OPTION *option, char ***argvp __unused) 8619b50d902SRodney W. Grimes { 8629b50d902SRodney W. Grimes ftsoptions &= ~FTS_PHYSICAL; 8639b50d902SRodney W. Grimes ftsoptions |= FTS_LOGICAL; 8649b50d902SRodney W. Grimes 865ea92232aSPoul-Henning Kamp return palloc(option); 8669b50d902SRodney W. Grimes } 8679b50d902SRodney W. Grimes 8689b50d902SRodney W. Grimes /* 8699b50d902SRodney W. Grimes * -fstype functions -- 8709b50d902SRodney W. Grimes * 8719b50d902SRodney W. Grimes * True if the file is of a certain type. 8729b50d902SRodney W. Grimes */ 8739b50d902SRodney W. Grimes int 874ef646f18SMark Murray f_fstype(PLAN *plan, FTSENT *entry) 8759b50d902SRodney W. Grimes { 8769b50d902SRodney W. Grimes static dev_t curdev; /* need a guaranteed illegal dev value */ 8779b50d902SRodney W. Grimes static int first = 1; 8789b50d902SRodney W. Grimes struct statfs sb; 8796ab780e5STai-hwa Liang static int val_flags; 8806ab780e5STai-hwa Liang static char fstype[sizeof(sb.f_fstypename)]; 8818a0a76b8SOllivier Robert char *p, save[2] = {0,0}; 8829b50d902SRodney W. Grimes 883ea92232aSPoul-Henning Kamp if ((plan->flags & F_MTMASK) == F_MTUNKNOWN) 884ea92232aSPoul-Henning Kamp return 0; 885ea92232aSPoul-Henning Kamp 8869b50d902SRodney W. Grimes /* Only check when we cross mount point. */ 8879b50d902SRodney W. Grimes if (first || curdev != entry->fts_statp->st_dev) { 8889b50d902SRodney W. Grimes curdev = entry->fts_statp->st_dev; 8899b50d902SRodney W. Grimes 8909b50d902SRodney W. Grimes /* 8919b50d902SRodney W. Grimes * Statfs follows symlinks; find wants the link's filesystem, 8929b50d902SRodney W. Grimes * not where it points. 8939b50d902SRodney W. Grimes */ 8949b50d902SRodney W. Grimes if (entry->fts_info == FTS_SL || 8959b50d902SRodney W. Grimes entry->fts_info == FTS_SLNONE) { 8969b50d902SRodney W. Grimes if ((p = strrchr(entry->fts_accpath, '/')) != NULL) 8979b50d902SRodney W. Grimes ++p; 8989b50d902SRodney W. Grimes else 8999b50d902SRodney W. Grimes p = entry->fts_accpath; 9009b50d902SRodney W. Grimes save[0] = p[0]; 9019b50d902SRodney W. Grimes p[0] = '.'; 9029b50d902SRodney W. Grimes save[1] = p[1]; 9039b50d902SRodney W. Grimes p[1] = '\0'; 9049b50d902SRodney W. Grimes } else 9059b50d902SRodney W. Grimes p = NULL; 9069b50d902SRodney W. Grimes 9070ab69d71SXin LI if (statfs(entry->fts_accpath, &sb)) { 9080ab69d71SXin LI if (!ignore_readdir_race || errno != ENOENT) { 9090ab69d71SXin LI warn("statfs: %s", entry->fts_accpath); 9100ab69d71SXin LI exitstatus = 1; 9110ab69d71SXin LI } 9120ab69d71SXin LI return 0; 9130ab69d71SXin LI } 9149b50d902SRodney W. Grimes 9159b50d902SRodney W. Grimes if (p) { 9169b50d902SRodney W. Grimes p[0] = save[0]; 9179b50d902SRodney W. Grimes p[1] = save[1]; 9189b50d902SRodney W. Grimes } 9199b50d902SRodney W. Grimes 9209b50d902SRodney W. Grimes first = 0; 921841484cdSPeter Wemm 922841484cdSPeter Wemm /* 923841484cdSPeter Wemm * Further tests may need both of these values, so 924841484cdSPeter Wemm * always copy both of them. 925841484cdSPeter Wemm */ 9269d08e419SPeter Wemm val_flags = sb.f_flags; 9276ab780e5STai-hwa Liang strlcpy(fstype, sb.f_fstypename, sizeof(fstype)); 9289b50d902SRodney W. Grimes } 929ea92232aSPoul-Henning Kamp switch (plan->flags & F_MTMASK) { 9309b50d902SRodney W. Grimes case F_MTFLAG: 931ea92232aSPoul-Henning Kamp return val_flags & plan->mt_data; 9329b50d902SRodney W. Grimes case F_MTTYPE: 9336ab780e5STai-hwa Liang return (strncmp(fstype, plan->c_data, sizeof(fstype)) == 0); 9349b50d902SRodney W. Grimes default: 9359b50d902SRodney W. Grimes abort(); 9369b50d902SRodney W. Grimes } 9379b50d902SRodney W. Grimes } 9389b50d902SRodney W. Grimes 9399b50d902SRodney W. Grimes PLAN * 940ef646f18SMark Murray c_fstype(OPTION *option, char ***argvp) 9419b50d902SRodney W. Grimes { 942ea92232aSPoul-Henning Kamp char *fsname; 943e98080b1SDavid Malone PLAN *new; 9449b50d902SRodney W. Grimes 945ea92232aSPoul-Henning Kamp fsname = nextarg(option, argvp); 9469b50d902SRodney W. Grimes ftsoptions &= ~FTS_NOSTAT; 9479b50d902SRodney W. Grimes 948ea92232aSPoul-Henning Kamp new = palloc(option); 949ea92232aSPoul-Henning Kamp switch (*fsname) { 9509b50d902SRodney W. Grimes case 'l': 951ea92232aSPoul-Henning Kamp if (!strcmp(fsname, "local")) { 952ea92232aSPoul-Henning Kamp new->flags |= F_MTFLAG; 9539b50d902SRodney W. Grimes new->mt_data = MNT_LOCAL; 954ea92232aSPoul-Henning Kamp return new; 9559b50d902SRodney W. Grimes } 9569b50d902SRodney W. Grimes break; 9579b50d902SRodney W. Grimes case 'r': 958ea92232aSPoul-Henning Kamp if (!strcmp(fsname, "rdonly")) { 959ea92232aSPoul-Henning Kamp new->flags |= F_MTFLAG; 9609b50d902SRodney W. Grimes new->mt_data = MNT_RDONLY; 961ea92232aSPoul-Henning Kamp return new; 9629b50d902SRodney W. Grimes } 9639b50d902SRodney W. Grimes break; 9649b50d902SRodney W. Grimes } 965ea92232aSPoul-Henning Kamp 9666ab780e5STai-hwa Liang new->flags |= F_MTTYPE; 9676ab780e5STai-hwa Liang new->c_data = fsname; 968ea92232aSPoul-Henning Kamp return new; 9699b50d902SRodney W. Grimes } 9709b50d902SRodney W. Grimes 9719b50d902SRodney W. Grimes /* 9729b50d902SRodney W. Grimes * -group gname functions -- 9739b50d902SRodney W. Grimes * 9749b50d902SRodney W. Grimes * True if the file belongs to the group gname. If gname is numeric and 9759b50d902SRodney W. Grimes * an equivalent of the getgrnam() function does not return a valid group 9769b50d902SRodney W. Grimes * name, gname is taken as a group ID. 9779b50d902SRodney W. Grimes */ 9789b50d902SRodney W. Grimes int 979ef646f18SMark Murray f_group(PLAN *plan, FTSENT *entry) 9809b50d902SRodney W. Grimes { 9814ba3b38bSKirill Ponomarev COMPARE(entry->fts_statp->st_gid, plan->g_data); 9829b50d902SRodney W. Grimes } 9839b50d902SRodney W. Grimes 9849b50d902SRodney W. Grimes PLAN * 985ef646f18SMark Murray c_group(OPTION *option, char ***argvp) 9869b50d902SRodney W. Grimes { 987ea92232aSPoul-Henning Kamp char *gname; 9889b50d902SRodney W. Grimes PLAN *new; 9899b50d902SRodney W. Grimes struct group *g; 9909b50d902SRodney W. Grimes gid_t gid; 9919b50d902SRodney W. Grimes 992ea92232aSPoul-Henning Kamp gname = nextarg(option, argvp); 9939b50d902SRodney W. Grimes ftsoptions &= ~FTS_NOSTAT; 9949b50d902SRodney W. Grimes 9954ba3b38bSKirill Ponomarev new = palloc(option); 9969b50d902SRodney W. Grimes g = getgrnam(gname); 9979b50d902SRodney W. Grimes if (g == NULL) { 9984ba3b38bSKirill Ponomarev char* cp = gname; 9994ba3b38bSKirill Ponomarev if (gname[0] == '-' || gname[0] == '+') 10004ba3b38bSKirill Ponomarev gname++; 10019b50d902SRodney W. Grimes gid = atoi(gname); 10029b50d902SRodney W. Grimes if (gid == 0 && gname[0] != '0') 1003ea92232aSPoul-Henning Kamp errx(1, "%s: %s: no such group", option->name, gname); 10044ba3b38bSKirill Ponomarev gid = find_parsenum(new, option->name, cp, NULL); 10059b50d902SRodney W. Grimes } else 10069b50d902SRodney W. Grimes gid = g->gr_gid; 10079b50d902SRodney W. Grimes 10089b50d902SRodney W. Grimes new->g_data = gid; 1009ea92232aSPoul-Henning Kamp return new; 10109b50d902SRodney W. Grimes } 10119b50d902SRodney W. Grimes 10129b50d902SRodney W. Grimes /* 101340072dc2SJilles Tjoelker * -ignore_readdir_race functions -- 101440072dc2SJilles Tjoelker * 101540072dc2SJilles Tjoelker * Always true. Ignore errors which occur if a file or a directory 101640072dc2SJilles Tjoelker * in a starting point gets deleted between reading the name and calling 101740072dc2SJilles Tjoelker * stat on it while find is traversing the starting point. 101840072dc2SJilles Tjoelker */ 101940072dc2SJilles Tjoelker 102040072dc2SJilles Tjoelker PLAN * 102140072dc2SJilles Tjoelker c_ignore_readdir_race(OPTION *option, char ***argvp __unused) 102240072dc2SJilles Tjoelker { 102340072dc2SJilles Tjoelker if (strcmp(option->name, "-ignore_readdir_race") == 0) 102440072dc2SJilles Tjoelker ignore_readdir_race = 1; 102540072dc2SJilles Tjoelker else 102640072dc2SJilles Tjoelker ignore_readdir_race = 0; 102740072dc2SJilles Tjoelker 102840072dc2SJilles Tjoelker return palloc(option); 102940072dc2SJilles Tjoelker } 103040072dc2SJilles Tjoelker 103140072dc2SJilles Tjoelker /* 10329b50d902SRodney W. Grimes * -inum n functions -- 10339b50d902SRodney W. Grimes * 10349b50d902SRodney W. Grimes * True if the file has inode # n. 10359b50d902SRodney W. Grimes */ 10369b50d902SRodney W. Grimes int 1037ef646f18SMark Murray f_inum(PLAN *plan, FTSENT *entry) 10389b50d902SRodney W. Grimes { 10399b50d902SRodney W. Grimes COMPARE(entry->fts_statp->st_ino, plan->i_data); 10409b50d902SRodney W. Grimes } 10419b50d902SRodney W. Grimes 10429b50d902SRodney W. Grimes PLAN * 1043ef646f18SMark Murray c_inum(OPTION *option, char ***argvp) 10449b50d902SRodney W. Grimes { 1045ea92232aSPoul-Henning Kamp char *inum_str; 10469b50d902SRodney W. Grimes PLAN *new; 10479b50d902SRodney W. Grimes 1048ea92232aSPoul-Henning Kamp inum_str = nextarg(option, argvp); 10499b50d902SRodney W. Grimes ftsoptions &= ~FTS_NOSTAT; 10509b50d902SRodney W. Grimes 1051ea92232aSPoul-Henning Kamp new = palloc(option); 1052ea92232aSPoul-Henning Kamp new->i_data = find_parsenum(new, option->name, inum_str, NULL); 1053ea92232aSPoul-Henning Kamp return new; 10549b50d902SRodney W. Grimes } 10559b50d902SRodney W. Grimes 10569b50d902SRodney W. Grimes /* 105746b993ffSWarner Losh * -samefile FN 105846b993ffSWarner Losh * 105946b993ffSWarner Losh * True if the file has the same inode (eg hard link) FN 106046b993ffSWarner Losh */ 106146b993ffSWarner Losh 106246b993ffSWarner Losh /* f_samefile is just f_inum */ 106346b993ffSWarner Losh PLAN * 106446b993ffSWarner Losh c_samefile(OPTION *option, char ***argvp) 106546b993ffSWarner Losh { 106646b993ffSWarner Losh char *fn; 106746b993ffSWarner Losh PLAN *new; 106846b993ffSWarner Losh struct stat sb; 106946b993ffSWarner Losh 107046b993ffSWarner Losh fn = nextarg(option, argvp); 107146b993ffSWarner Losh ftsoptions &= ~FTS_NOSTAT; 107246b993ffSWarner Losh 107346b993ffSWarner Losh new = palloc(option); 107446b993ffSWarner Losh if (stat(fn, &sb)) 107546b993ffSWarner Losh err(1, "%s", fn); 107646b993ffSWarner Losh new->i_data = sb.st_ino; 107746b993ffSWarner Losh return new; 107846b993ffSWarner Losh } 107946b993ffSWarner Losh 108046b993ffSWarner Losh /* 10819b50d902SRodney W. Grimes * -links n functions -- 10829b50d902SRodney W. Grimes * 10839b50d902SRodney W. Grimes * True if the file has n links. 10849b50d902SRodney W. Grimes */ 10859b50d902SRodney W. Grimes int 1086ef646f18SMark Murray f_links(PLAN *plan, FTSENT *entry) 10879b50d902SRodney W. Grimes { 10889b50d902SRodney W. Grimes COMPARE(entry->fts_statp->st_nlink, plan->l_data); 10899b50d902SRodney W. Grimes } 10909b50d902SRodney W. Grimes 10919b50d902SRodney W. Grimes PLAN * 1092ef646f18SMark Murray c_links(OPTION *option, char ***argvp) 10939b50d902SRodney W. Grimes { 1094ea92232aSPoul-Henning Kamp char *nlinks; 10959b50d902SRodney W. Grimes PLAN *new; 10969b50d902SRodney W. Grimes 1097ea92232aSPoul-Henning Kamp nlinks = nextarg(option, argvp); 10989b50d902SRodney W. Grimes ftsoptions &= ~FTS_NOSTAT; 10999b50d902SRodney W. Grimes 1100ea92232aSPoul-Henning Kamp new = palloc(option); 1101ea92232aSPoul-Henning Kamp new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL); 1102ea92232aSPoul-Henning Kamp return new; 11039b50d902SRodney W. Grimes } 11049b50d902SRodney W. Grimes 11059b50d902SRodney W. Grimes /* 11069b50d902SRodney W. Grimes * -ls functions -- 11079b50d902SRodney W. Grimes * 11089b50d902SRodney W. Grimes * Always true - prints the current entry to stdout in "ls" format. 11099b50d902SRodney W. Grimes */ 11109b50d902SRodney W. Grimes int 1111ef646f18SMark Murray f_ls(PLAN *plan __unused, FTSENT *entry) 11129b50d902SRodney W. Grimes { 11139b50d902SRodney W. Grimes printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp); 1114ea92232aSPoul-Henning Kamp return 1; 11159b50d902SRodney W. Grimes } 11169b50d902SRodney W. Grimes 11179b50d902SRodney W. Grimes PLAN * 1118ef646f18SMark Murray c_ls(OPTION *option, char ***argvp __unused) 11199b50d902SRodney W. Grimes { 11209b50d902SRodney W. Grimes ftsoptions &= ~FTS_NOSTAT; 11219b50d902SRodney W. Grimes isoutput = 1; 11229b50d902SRodney W. Grimes 1123ea92232aSPoul-Henning Kamp return palloc(option); 11249b50d902SRodney W. Grimes } 11259b50d902SRodney W. Grimes 11269b50d902SRodney W. Grimes /* 11279b50d902SRodney W. Grimes * -name functions -- 11289b50d902SRodney W. Grimes * 11299b50d902SRodney W. Grimes * True if the basename of the filename being examined 11309b50d902SRodney W. Grimes * matches pattern using Pattern Matching Notation S3.14 11319b50d902SRodney W. Grimes */ 11329b50d902SRodney W. Grimes int 1133ef646f18SMark Murray f_name(PLAN *plan, FTSENT *entry) 11349b50d902SRodney W. Grimes { 1135acebb585SWarner Losh char fn[PATH_MAX]; 1136acebb585SWarner Losh const char *name; 11373e02329aSJilles Tjoelker ssize_t len; 1138acebb585SWarner Losh 1139acebb585SWarner Losh if (plan->flags & F_LINK) { 1140e810bef7SJilles Tjoelker /* 1141e810bef7SJilles Tjoelker * The below test both avoids obviously useless readlink() 1142e810bef7SJilles Tjoelker * calls and ensures that symlinks with existent target do 1143e810bef7SJilles Tjoelker * not match if symlinks are being followed. 1144e810bef7SJilles Tjoelker * Assumption: fts will stat all symlinks that are to be 1145e810bef7SJilles Tjoelker * followed and will return the stat information. 1146e810bef7SJilles Tjoelker */ 1147e810bef7SJilles Tjoelker if (entry->fts_info != FTS_NSOK && entry->fts_info != FTS_SL && 1148e810bef7SJilles Tjoelker entry->fts_info != FTS_SLNONE) 1149e810bef7SJilles Tjoelker return 0; 1150e810bef7SJilles Tjoelker len = readlink(entry->fts_accpath, fn, sizeof(fn) - 1); 11513e02329aSJilles Tjoelker if (len == -1) 115246b993ffSWarner Losh return 0; 11533e02329aSJilles Tjoelker fn[len] = '\0'; 11543e02329aSJilles Tjoelker name = fn; 1155acebb585SWarner Losh } else 1156acebb585SWarner Losh name = entry->fts_name; 1157acebb585SWarner Losh return !fnmatch(plan->c_data, name, 1158ea92232aSPoul-Henning Kamp plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0); 11599b50d902SRodney W. Grimes } 11609b50d902SRodney W. Grimes 11619b50d902SRodney W. Grimes PLAN * 1162ef646f18SMark Murray c_name(OPTION *option, char ***argvp) 11639b50d902SRodney W. Grimes { 1164ea92232aSPoul-Henning Kamp char *pattern; 11659b50d902SRodney W. Grimes PLAN *new; 11669b50d902SRodney W. Grimes 1167ea92232aSPoul-Henning Kamp pattern = nextarg(option, argvp); 1168ea92232aSPoul-Henning Kamp new = palloc(option); 11699b50d902SRodney W. Grimes new->c_data = pattern; 1170ea92232aSPoul-Henning Kamp return new; 11719b50d902SRodney W. Grimes } 11729b50d902SRodney W. Grimes 11737c1d4b3aSAkinori MUSHA /* 1174ea92232aSPoul-Henning Kamp * -newer file functions -- 11757c1d4b3aSAkinori MUSHA * 1176ea92232aSPoul-Henning Kamp * True if the current file has been modified more recently 1177ea92232aSPoul-Henning Kamp * then the modification time of the file named by the pathname 1178ea92232aSPoul-Henning Kamp * file. 11797c1d4b3aSAkinori MUSHA */ 11807c1d4b3aSAkinori MUSHA int 1181ef646f18SMark Murray f_newer(PLAN *plan, FTSENT *entry) 11827c1d4b3aSAkinori MUSHA { 1183ddd956b0SJilles Tjoelker struct timespec ft; 1184ddd956b0SJilles Tjoelker 1185ea92232aSPoul-Henning Kamp if (plan->flags & F_TIME_C) 1186ddd956b0SJilles Tjoelker ft = entry->fts_statp->st_ctim; 1187ea92232aSPoul-Henning Kamp else if (plan->flags & F_TIME_A) 1188ddd956b0SJilles Tjoelker ft = entry->fts_statp->st_atim; 118931d53425SCeri Davies else if (plan->flags & F_TIME_B) 1190ddd956b0SJilles Tjoelker ft = entry->fts_statp->st_birthtim; 1191ea92232aSPoul-Henning Kamp else 1192ddd956b0SJilles Tjoelker ft = entry->fts_statp->st_mtim; 1193ddd956b0SJilles Tjoelker return (ft.tv_sec > plan->t_data.tv_sec || 1194ddd956b0SJilles Tjoelker (ft.tv_sec == plan->t_data.tv_sec && 1195ddd956b0SJilles Tjoelker ft.tv_nsec > plan->t_data.tv_nsec)); 11967c1d4b3aSAkinori MUSHA } 11977c1d4b3aSAkinori MUSHA 11987c1d4b3aSAkinori MUSHA PLAN * 1199ef646f18SMark Murray c_newer(OPTION *option, char ***argvp) 12007c1d4b3aSAkinori MUSHA { 1201ea92232aSPoul-Henning Kamp char *fn_or_tspec; 12027c1d4b3aSAkinori MUSHA PLAN *new; 1203ea92232aSPoul-Henning Kamp struct stat sb; 12047c1d4b3aSAkinori MUSHA 1205ea92232aSPoul-Henning Kamp fn_or_tspec = nextarg(option, argvp); 1206ea92232aSPoul-Henning Kamp ftsoptions &= ~FTS_NOSTAT; 1207ea92232aSPoul-Henning Kamp 1208ea92232aSPoul-Henning Kamp new = palloc(option); 1209ea92232aSPoul-Henning Kamp /* compare against what */ 1210ea92232aSPoul-Henning Kamp if (option->flags & F_TIME2_T) { 1211ddd956b0SJilles Tjoelker new->t_data.tv_sec = get_date(fn_or_tspec); 1212ddd956b0SJilles Tjoelker if (new->t_data.tv_sec == (time_t) -1) 1213ea92232aSPoul-Henning Kamp errx(1, "Can't parse date/time: %s", fn_or_tspec); 1214ddd956b0SJilles Tjoelker /* Use the seconds only in the comparison. */ 1215ddd956b0SJilles Tjoelker new->t_data.tv_nsec = 999999999; 1216ea92232aSPoul-Henning Kamp } else { 1217ea92232aSPoul-Henning Kamp if (stat(fn_or_tspec, &sb)) 1218ea92232aSPoul-Henning Kamp err(1, "%s", fn_or_tspec); 1219ea92232aSPoul-Henning Kamp if (option->flags & F_TIME2_C) 1220ddd956b0SJilles Tjoelker new->t_data = sb.st_ctim; 1221ea92232aSPoul-Henning Kamp else if (option->flags & F_TIME2_A) 1222ddd956b0SJilles Tjoelker new->t_data = sb.st_atim; 12238310a1a2SGavin Atkinson else if (option->flags & F_TIME2_B) 1224ddd956b0SJilles Tjoelker new->t_data = sb.st_birthtim; 1225ea92232aSPoul-Henning Kamp else 1226ddd956b0SJilles Tjoelker new->t_data = sb.st_mtim; 1227ea92232aSPoul-Henning Kamp } 1228ea92232aSPoul-Henning Kamp return new; 12297c1d4b3aSAkinori MUSHA } 12307c1d4b3aSAkinori MUSHA 1231ea92232aSPoul-Henning Kamp /* 1232ea92232aSPoul-Henning Kamp * -nogroup functions -- 1233ea92232aSPoul-Henning Kamp * 1234ea92232aSPoul-Henning Kamp * True if file belongs to a user ID for which the equivalent 1235ea92232aSPoul-Henning Kamp * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL. 1236ea92232aSPoul-Henning Kamp */ 1237ea92232aSPoul-Henning Kamp int 1238ef646f18SMark Murray f_nogroup(PLAN *plan __unused, FTSENT *entry) 1239ea92232aSPoul-Henning Kamp { 1240ea92232aSPoul-Henning Kamp return group_from_gid(entry->fts_statp->st_gid, 1) == NULL; 1241ea92232aSPoul-Henning Kamp } 1242ea92232aSPoul-Henning Kamp 1243ea92232aSPoul-Henning Kamp PLAN * 1244ef646f18SMark Murray c_nogroup(OPTION *option, char ***argvp __unused) 1245ea92232aSPoul-Henning Kamp { 1246ea92232aSPoul-Henning Kamp ftsoptions &= ~FTS_NOSTAT; 1247ea92232aSPoul-Henning Kamp 1248ea92232aSPoul-Henning Kamp return palloc(option); 1249ea92232aSPoul-Henning Kamp } 1250ea92232aSPoul-Henning Kamp 1251ea92232aSPoul-Henning Kamp /* 1252ea92232aSPoul-Henning Kamp * -nouser functions -- 1253ea92232aSPoul-Henning Kamp * 1254ea92232aSPoul-Henning Kamp * True if file belongs to a user ID for which the equivalent 1255ea92232aSPoul-Henning Kamp * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL. 1256ea92232aSPoul-Henning Kamp */ 1257ea92232aSPoul-Henning Kamp int 1258ef646f18SMark Murray f_nouser(PLAN *plan __unused, FTSENT *entry) 1259ea92232aSPoul-Henning Kamp { 1260ea92232aSPoul-Henning Kamp return user_from_uid(entry->fts_statp->st_uid, 1) == NULL; 1261ea92232aSPoul-Henning Kamp } 1262ea92232aSPoul-Henning Kamp 1263ea92232aSPoul-Henning Kamp PLAN * 1264ef646f18SMark Murray c_nouser(OPTION *option, char ***argvp __unused) 1265ea92232aSPoul-Henning Kamp { 1266ea92232aSPoul-Henning Kamp ftsoptions &= ~FTS_NOSTAT; 1267ea92232aSPoul-Henning Kamp 1268ea92232aSPoul-Henning Kamp return palloc(option); 1269ea92232aSPoul-Henning Kamp } 1270ea92232aSPoul-Henning Kamp 1271ea92232aSPoul-Henning Kamp /* 1272ea92232aSPoul-Henning Kamp * -path functions -- 1273ea92232aSPoul-Henning Kamp * 1274ea92232aSPoul-Henning Kamp * True if the path of the filename being examined 1275ea92232aSPoul-Henning Kamp * matches pattern using Pattern Matching Notation S3.14 1276ea92232aSPoul-Henning Kamp */ 1277ea92232aSPoul-Henning Kamp int 1278ef646f18SMark Murray f_path(PLAN *plan, FTSENT *entry) 1279ea92232aSPoul-Henning Kamp { 1280ea92232aSPoul-Henning Kamp return !fnmatch(plan->c_data, entry->fts_path, 1281ea92232aSPoul-Henning Kamp plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0); 1282ea92232aSPoul-Henning Kamp } 1283ea92232aSPoul-Henning Kamp 1284ea92232aSPoul-Henning Kamp /* c_path is the same as c_name */ 1285ea92232aSPoul-Henning Kamp 1286ea92232aSPoul-Henning Kamp /* 1287ea92232aSPoul-Henning Kamp * -perm functions -- 1288ea92232aSPoul-Henning Kamp * 1289ea92232aSPoul-Henning Kamp * The mode argument is used to represent file mode bits. If it starts 1290ea92232aSPoul-Henning Kamp * with a leading digit, it's treated as an octal mode, otherwise as a 1291ea92232aSPoul-Henning Kamp * symbolic mode. 1292ea92232aSPoul-Henning Kamp */ 1293ea92232aSPoul-Henning Kamp int 1294ef646f18SMark Murray f_perm(PLAN *plan, FTSENT *entry) 1295ea92232aSPoul-Henning Kamp { 1296ea92232aSPoul-Henning Kamp mode_t mode; 1297ea92232aSPoul-Henning Kamp 1298ea92232aSPoul-Henning Kamp mode = entry->fts_statp->st_mode & 1299ea92232aSPoul-Henning Kamp (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO); 1300ea92232aSPoul-Henning Kamp if (plan->flags & F_ATLEAST) 1301ea92232aSPoul-Henning Kamp return (plan->m_data | mode) == mode; 1302c0ff9709SRuslan Ermilov else if (plan->flags & F_ANY) 1303c0ff9709SRuslan Ermilov return (mode & plan->m_data); 1304ea92232aSPoul-Henning Kamp else 1305ea92232aSPoul-Henning Kamp return mode == plan->m_data; 1306ea92232aSPoul-Henning Kamp /* NOTREACHED */ 1307ea92232aSPoul-Henning Kamp } 1308ea92232aSPoul-Henning Kamp 1309ea92232aSPoul-Henning Kamp PLAN * 1310ef646f18SMark Murray c_perm(OPTION *option, char ***argvp) 1311ea92232aSPoul-Henning Kamp { 1312ea92232aSPoul-Henning Kamp char *perm; 1313ea92232aSPoul-Henning Kamp PLAN *new; 1314ea92232aSPoul-Henning Kamp mode_t *set; 1315ea92232aSPoul-Henning Kamp 1316ea92232aSPoul-Henning Kamp perm = nextarg(option, argvp); 1317ea92232aSPoul-Henning Kamp ftsoptions &= ~FTS_NOSTAT; 1318ea92232aSPoul-Henning Kamp 1319ea92232aSPoul-Henning Kamp new = palloc(option); 1320ea92232aSPoul-Henning Kamp 1321ea92232aSPoul-Henning Kamp if (*perm == '-') { 1322ea92232aSPoul-Henning Kamp new->flags |= F_ATLEAST; 1323ea92232aSPoul-Henning Kamp ++perm; 1324ea92232aSPoul-Henning Kamp } else if (*perm == '+') { 1325ea92232aSPoul-Henning Kamp new->flags |= F_ANY; 1326ea92232aSPoul-Henning Kamp ++perm; 1327ea92232aSPoul-Henning Kamp } 1328ea92232aSPoul-Henning Kamp 1329ea92232aSPoul-Henning Kamp if ((set = setmode(perm)) == NULL) 1330ea92232aSPoul-Henning Kamp errx(1, "%s: %s: illegal mode string", option->name, perm); 1331ea92232aSPoul-Henning Kamp 1332ea92232aSPoul-Henning Kamp new->m_data = getmode(set, 0); 1333ea92232aSPoul-Henning Kamp free(set); 1334ea92232aSPoul-Henning Kamp return new; 1335ea92232aSPoul-Henning Kamp } 1336ea92232aSPoul-Henning Kamp 1337ea92232aSPoul-Henning Kamp /* 1338ea92232aSPoul-Henning Kamp * -print functions -- 1339ea92232aSPoul-Henning Kamp * 13409725a7b9SPhilippe Charnier * Always true, causes the current pathname to be written to 1341ea92232aSPoul-Henning Kamp * standard output. 1342ea92232aSPoul-Henning Kamp */ 1343ea92232aSPoul-Henning Kamp int 1344ef646f18SMark Murray f_print(PLAN *plan __unused, FTSENT *entry) 1345ea92232aSPoul-Henning Kamp { 1346ea92232aSPoul-Henning Kamp (void)puts(entry->fts_path); 1347ea92232aSPoul-Henning Kamp return 1; 1348ea92232aSPoul-Henning Kamp } 1349ea92232aSPoul-Henning Kamp 1350ea92232aSPoul-Henning Kamp PLAN * 1351ef646f18SMark Murray c_print(OPTION *option, char ***argvp __unused) 1352ea92232aSPoul-Henning Kamp { 1353ea92232aSPoul-Henning Kamp isoutput = 1; 1354ea92232aSPoul-Henning Kamp 1355ea92232aSPoul-Henning Kamp return palloc(option); 1356ea92232aSPoul-Henning Kamp } 1357ea92232aSPoul-Henning Kamp 1358ea92232aSPoul-Henning Kamp /* 1359ea92232aSPoul-Henning Kamp * -print0 functions -- 1360ea92232aSPoul-Henning Kamp * 13619725a7b9SPhilippe Charnier * Always true, causes the current pathname to be written to 1362ea92232aSPoul-Henning Kamp * standard output followed by a NUL character 1363ea92232aSPoul-Henning Kamp */ 1364ea92232aSPoul-Henning Kamp int 1365ef646f18SMark Murray f_print0(PLAN *plan __unused, FTSENT *entry) 1366ea92232aSPoul-Henning Kamp { 1367ea92232aSPoul-Henning Kamp fputs(entry->fts_path, stdout); 1368ea92232aSPoul-Henning Kamp fputc('\0', stdout); 1369ea92232aSPoul-Henning Kamp return 1; 1370ea92232aSPoul-Henning Kamp } 1371ea92232aSPoul-Henning Kamp 1372ea92232aSPoul-Henning Kamp /* c_print0 is the same as c_print */ 1373ea92232aSPoul-Henning Kamp 1374ea92232aSPoul-Henning Kamp /* 1375ea92232aSPoul-Henning Kamp * -prune functions -- 1376ea92232aSPoul-Henning Kamp * 1377ea92232aSPoul-Henning Kamp * Prune a portion of the hierarchy. 1378ea92232aSPoul-Henning Kamp */ 1379ea92232aSPoul-Henning Kamp int 1380ef646f18SMark Murray f_prune(PLAN *plan __unused, FTSENT *entry) 1381ea92232aSPoul-Henning Kamp { 1382ea92232aSPoul-Henning Kamp if (fts_set(tree, entry, FTS_SKIP)) 1383ea92232aSPoul-Henning Kamp err(1, "%s", entry->fts_path); 1384ea92232aSPoul-Henning Kamp return 1; 1385ea92232aSPoul-Henning Kamp } 1386ea92232aSPoul-Henning Kamp 1387ea92232aSPoul-Henning Kamp /* c_prune == c_simple */ 13887c1d4b3aSAkinori MUSHA 13897c1d4b3aSAkinori MUSHA /* 13907c1d4b3aSAkinori MUSHA * -regex functions -- 13917c1d4b3aSAkinori MUSHA * 13927c1d4b3aSAkinori MUSHA * True if the whole path of the file matches pattern using 13937c1d4b3aSAkinori MUSHA * regular expression. 13947c1d4b3aSAkinori MUSHA */ 13957c1d4b3aSAkinori MUSHA int 1396ef646f18SMark Murray f_regex(PLAN *plan, FTSENT *entry) 13977c1d4b3aSAkinori MUSHA { 13987c1d4b3aSAkinori MUSHA char *str; 139947bca8b0SJuli Mallett int len; 14007c1d4b3aSAkinori MUSHA regex_t *pre; 14017c1d4b3aSAkinori MUSHA regmatch_t pmatch; 14027c1d4b3aSAkinori MUSHA int errcode; 14037c1d4b3aSAkinori MUSHA char errbuf[LINE_MAX]; 14047c1d4b3aSAkinori MUSHA int matched; 14057c1d4b3aSAkinori MUSHA 14067c1d4b3aSAkinori MUSHA pre = plan->re_data; 14077c1d4b3aSAkinori MUSHA str = entry->fts_path; 14087c1d4b3aSAkinori MUSHA len = strlen(str); 14097c1d4b3aSAkinori MUSHA matched = 0; 14107c1d4b3aSAkinori MUSHA 14117c1d4b3aSAkinori MUSHA pmatch.rm_so = 0; 14127c1d4b3aSAkinori MUSHA pmatch.rm_eo = len; 14137c1d4b3aSAkinori MUSHA 14147c1d4b3aSAkinori MUSHA errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND); 14157c1d4b3aSAkinori MUSHA 14167c1d4b3aSAkinori MUSHA if (errcode != 0 && errcode != REG_NOMATCH) { 14177c1d4b3aSAkinori MUSHA regerror(errcode, pre, errbuf, sizeof errbuf); 14187c1d4b3aSAkinori MUSHA errx(1, "%s: %s", 1419ea92232aSPoul-Henning Kamp plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf); 14207c1d4b3aSAkinori MUSHA } 14217c1d4b3aSAkinori MUSHA 14227c1d4b3aSAkinori MUSHA if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len) 14237c1d4b3aSAkinori MUSHA matched = 1; 14247c1d4b3aSAkinori MUSHA 1425ea92232aSPoul-Henning Kamp return matched; 14267c1d4b3aSAkinori MUSHA } 14277c1d4b3aSAkinori MUSHA 14287c1d4b3aSAkinori MUSHA PLAN * 1429ef646f18SMark Murray c_regex(OPTION *option, char ***argvp) 14307c1d4b3aSAkinori MUSHA { 14317c1d4b3aSAkinori MUSHA PLAN *new; 1432ea92232aSPoul-Henning Kamp char *pattern; 14337c1d4b3aSAkinori MUSHA regex_t *pre; 14347c1d4b3aSAkinori MUSHA int errcode; 14357c1d4b3aSAkinori MUSHA char errbuf[LINE_MAX]; 14367c1d4b3aSAkinori MUSHA 14377c1d4b3aSAkinori MUSHA if ((pre = malloc(sizeof(regex_t))) == NULL) 14387c1d4b3aSAkinori MUSHA err(1, NULL); 14397c1d4b3aSAkinori MUSHA 1440ea92232aSPoul-Henning Kamp pattern = nextarg(option, argvp); 1441ea92232aSPoul-Henning Kamp 1442ea92232aSPoul-Henning Kamp if ((errcode = regcomp(pre, pattern, 1443ea92232aSPoul-Henning Kamp regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) { 14447c1d4b3aSAkinori MUSHA regerror(errcode, pre, errbuf, sizeof errbuf); 14457c1d4b3aSAkinori MUSHA errx(1, "%s: %s: %s", 1446ea92232aSPoul-Henning Kamp option->flags & F_IGNCASE ? "-iregex" : "-regex", 1447ea92232aSPoul-Henning Kamp pattern, errbuf); 14487c1d4b3aSAkinori MUSHA } 14497c1d4b3aSAkinori MUSHA 1450ea92232aSPoul-Henning Kamp new = palloc(option); 14517c1d4b3aSAkinori MUSHA new->re_data = pre; 14527c1d4b3aSAkinori MUSHA 1453567664c4SOllivier Robert return new; 1454567664c4SOllivier Robert } 1455567664c4SOllivier Robert 145646b993ffSWarner Losh /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or, c_true, c_false */ 14579b50d902SRodney W. Grimes 14589b50d902SRodney W. Grimes PLAN * 1459ef646f18SMark Murray c_simple(OPTION *option, char ***argvp __unused) 14609b50d902SRodney W. Grimes { 1461ea92232aSPoul-Henning Kamp return palloc(option); 14629b50d902SRodney W. Grimes } 14639b50d902SRodney W. Grimes 14649b50d902SRodney W. Grimes /* 14659b50d902SRodney W. Grimes * -size n[c] functions -- 14669b50d902SRodney W. Grimes * 14679b50d902SRodney W. Grimes * True if the file size in bytes, divided by an implementation defined 14689b50d902SRodney W. Grimes * value and rounded up to the next integer, is n. If n is followed by 14695a890aacSKirill Ponomarev * one of c k M G T P, the size is in bytes, kilobytes, 14705a890aacSKirill Ponomarev * megabytes, gigabytes, terabytes or petabytes respectively. 14719b50d902SRodney W. Grimes */ 14729b50d902SRodney W. Grimes #define FIND_SIZE 512 14739b50d902SRodney W. Grimes static int divsize = 1; 14749b50d902SRodney W. Grimes 14759b50d902SRodney W. Grimes int 1476ef646f18SMark Murray f_size(PLAN *plan, FTSENT *entry) 14779b50d902SRodney W. Grimes { 14789b50d902SRodney W. Grimes off_t size; 14799b50d902SRodney W. Grimes 14809b50d902SRodney W. Grimes size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) / 14819b50d902SRodney W. Grimes FIND_SIZE : entry->fts_statp->st_size; 14829b50d902SRodney W. Grimes COMPARE(size, plan->o_data); 14839b50d902SRodney W. Grimes } 14849b50d902SRodney W. Grimes 14859b50d902SRodney W. Grimes PLAN * 1486ef646f18SMark Murray c_size(OPTION *option, char ***argvp) 14879b50d902SRodney W. Grimes { 1488ea92232aSPoul-Henning Kamp char *size_str; 14899b50d902SRodney W. Grimes PLAN *new; 14909b50d902SRodney W. Grimes char endch; 14915a890aacSKirill Ponomarev off_t scale; 14929b50d902SRodney W. Grimes 1493ea92232aSPoul-Henning Kamp size_str = nextarg(option, argvp); 14949b50d902SRodney W. Grimes ftsoptions &= ~FTS_NOSTAT; 14959b50d902SRodney W. Grimes 1496ea92232aSPoul-Henning Kamp new = palloc(option); 14979b50d902SRodney W. Grimes endch = 'c'; 1498ea92232aSPoul-Henning Kamp new->o_data = find_parsenum(new, option->name, size_str, &endch); 14995a890aacSKirill Ponomarev if (endch != '\0') { 15009b50d902SRodney W. Grimes divsize = 0; 15015a890aacSKirill Ponomarev 15025a890aacSKirill Ponomarev switch (endch) { 15035a890aacSKirill Ponomarev case 'c': /* characters */ 15045a890aacSKirill Ponomarev scale = 0x1LL; 15055a890aacSKirill Ponomarev break; 15065a890aacSKirill Ponomarev case 'k': /* kilobytes 1<<10 */ 15075a890aacSKirill Ponomarev scale = 0x400LL; 15085a890aacSKirill Ponomarev break; 15095a890aacSKirill Ponomarev case 'M': /* megabytes 1<<20 */ 15105a890aacSKirill Ponomarev scale = 0x100000LL; 15115a890aacSKirill Ponomarev break; 15125a890aacSKirill Ponomarev case 'G': /* gigabytes 1<<30 */ 15135a890aacSKirill Ponomarev scale = 0x40000000LL; 15145a890aacSKirill Ponomarev break; 15155a890aacSKirill Ponomarev case 'T': /* terabytes 1<<40 */ 1516599df3efSEd Maste scale = 0x10000000000LL; 15175a890aacSKirill Ponomarev break; 15185a890aacSKirill Ponomarev case 'P': /* petabytes 1<<50 */ 15195a890aacSKirill Ponomarev scale = 0x4000000000000LL; 15205a890aacSKirill Ponomarev break; 15215a890aacSKirill Ponomarev default: 15225a890aacSKirill Ponomarev errx(1, "%s: %s: illegal trailing character", 15235a890aacSKirill Ponomarev option->name, size_str); 15245a890aacSKirill Ponomarev break; 15255a890aacSKirill Ponomarev } 15265a890aacSKirill Ponomarev if (new->o_data > QUAD_MAX / scale) 15275a890aacSKirill Ponomarev errx(1, "%s: %s: value too large", 15285a890aacSKirill Ponomarev option->name, size_str); 15295a890aacSKirill Ponomarev new->o_data *= scale; 15305a890aacSKirill Ponomarev } 1531ea92232aSPoul-Henning Kamp return new; 15329b50d902SRodney W. Grimes } 15339b50d902SRodney W. Grimes 15349b50d902SRodney W. Grimes /* 15359ed0c92cSDavid Malone * -sparse functions -- 15369ed0c92cSDavid Malone * 15379ed0c92cSDavid Malone * Check if a file is sparse by finding if it occupies fewer blocks 15389ed0c92cSDavid Malone * than we expect based on its size. 15399ed0c92cSDavid Malone */ 15409ed0c92cSDavid Malone int 15419ed0c92cSDavid Malone f_sparse(PLAN *plan __unused, FTSENT *entry) 15429ed0c92cSDavid Malone { 15439ed0c92cSDavid Malone off_t expected_blocks; 15449ed0c92cSDavid Malone 15459ed0c92cSDavid Malone expected_blocks = (entry->fts_statp->st_size + 511) / 512; 15469ed0c92cSDavid Malone return entry->fts_statp->st_blocks < expected_blocks; 15479ed0c92cSDavid Malone } 15489ed0c92cSDavid Malone 15499ed0c92cSDavid Malone PLAN * 15509ed0c92cSDavid Malone c_sparse(OPTION *option, char ***argvp __unused) 15519ed0c92cSDavid Malone { 15529ed0c92cSDavid Malone ftsoptions &= ~FTS_NOSTAT; 15539ed0c92cSDavid Malone 15549ed0c92cSDavid Malone return palloc(option); 15559ed0c92cSDavid Malone } 15569ed0c92cSDavid Malone 15579ed0c92cSDavid Malone /* 15589b50d902SRodney W. Grimes * -type c functions -- 15599b50d902SRodney W. Grimes * 1560841484cdSPeter Wemm * True if the type of the file is c, where c is b, c, d, p, f or w 1561841484cdSPeter Wemm * for block special file, character special file, directory, FIFO, 1562841484cdSPeter Wemm * regular file or whiteout respectively. 15639b50d902SRodney W. Grimes */ 15649b50d902SRodney W. Grimes int 1565ef646f18SMark Murray f_type(PLAN *plan, FTSENT *entry) 15669b50d902SRodney W. Grimes { 1567b95de98aSJilles Tjoelker if (plan->m_data == S_IFDIR) 1568b95de98aSJilles Tjoelker return (entry->fts_info == FTS_D || entry->fts_info == FTS_DC || 1569b95de98aSJilles Tjoelker entry->fts_info == FTS_DNR || entry->fts_info == FTS_DOT || 1570b95de98aSJilles Tjoelker entry->fts_info == FTS_DP); 1571b95de98aSJilles Tjoelker else 1572ea92232aSPoul-Henning Kamp return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data; 15739b50d902SRodney W. Grimes } 15749b50d902SRodney W. Grimes 15759b50d902SRodney W. Grimes PLAN * 1576ef646f18SMark Murray c_type(OPTION *option, char ***argvp) 15779b50d902SRodney W. Grimes { 1578ea92232aSPoul-Henning Kamp char *typestring; 15799b50d902SRodney W. Grimes PLAN *new; 15809b50d902SRodney W. Grimes mode_t mask; 15819b50d902SRodney W. Grimes 1582ea92232aSPoul-Henning Kamp typestring = nextarg(option, argvp); 1583b95de98aSJilles Tjoelker if (typestring[0] != 'd') 15849b50d902SRodney W. Grimes ftsoptions &= ~FTS_NOSTAT; 15859b50d902SRodney W. Grimes 15869b50d902SRodney W. Grimes switch (typestring[0]) { 15879b50d902SRodney W. Grimes case 'b': 15889b50d902SRodney W. Grimes mask = S_IFBLK; 15899b50d902SRodney W. Grimes break; 15909b50d902SRodney W. Grimes case 'c': 15919b50d902SRodney W. Grimes mask = S_IFCHR; 15929b50d902SRodney W. Grimes break; 15939b50d902SRodney W. Grimes case 'd': 15949b50d902SRodney W. Grimes mask = S_IFDIR; 15959b50d902SRodney W. Grimes break; 15969b50d902SRodney W. Grimes case 'f': 15979b50d902SRodney W. Grimes mask = S_IFREG; 15989b50d902SRodney W. Grimes break; 15999b50d902SRodney W. Grimes case 'l': 16009b50d902SRodney W. Grimes mask = S_IFLNK; 16019b50d902SRodney W. Grimes break; 16029b50d902SRodney W. Grimes case 'p': 16039b50d902SRodney W. Grimes mask = S_IFIFO; 16049b50d902SRodney W. Grimes break; 16059b50d902SRodney W. Grimes case 's': 16069b50d902SRodney W. Grimes mask = S_IFSOCK; 16079b50d902SRodney W. Grimes break; 1608841484cdSPeter Wemm #ifdef FTS_WHITEOUT 1609841484cdSPeter Wemm case 'w': 1610841484cdSPeter Wemm mask = S_IFWHT; 1611841484cdSPeter Wemm ftsoptions |= FTS_WHITEOUT; 1612841484cdSPeter Wemm break; 1613841484cdSPeter Wemm #endif /* FTS_WHITEOUT */ 16149b50d902SRodney W. Grimes default: 1615ea92232aSPoul-Henning Kamp errx(1, "%s: %s: unknown type", option->name, typestring); 16169b50d902SRodney W. Grimes } 16179b50d902SRodney W. Grimes 1618ea92232aSPoul-Henning Kamp new = palloc(option); 16199b50d902SRodney W. Grimes new->m_data = mask; 1620ea92232aSPoul-Henning Kamp return new; 1621abacbbbfSPeter Wemm } 1622abacbbbfSPeter Wemm 1623abacbbbfSPeter Wemm /* 16249b50d902SRodney W. Grimes * -user uname functions -- 16259b50d902SRodney W. Grimes * 16269b50d902SRodney W. Grimes * True if the file belongs to the user uname. If uname is numeric and 16279b50d902SRodney W. Grimes * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not 16289b50d902SRodney W. Grimes * return a valid user name, uname is taken as a user ID. 16299b50d902SRodney W. Grimes */ 16309b50d902SRodney W. Grimes int 1631ef646f18SMark Murray f_user(PLAN *plan, FTSENT *entry) 16329b50d902SRodney W. Grimes { 16334ba3b38bSKirill Ponomarev COMPARE(entry->fts_statp->st_uid, plan->u_data); 16349b50d902SRodney W. Grimes } 16359b50d902SRodney W. Grimes 16369b50d902SRodney W. Grimes PLAN * 1637ef646f18SMark Murray c_user(OPTION *option, char ***argvp) 16389b50d902SRodney W. Grimes { 1639ea92232aSPoul-Henning Kamp char *username; 16409b50d902SRodney W. Grimes PLAN *new; 16419b50d902SRodney W. Grimes struct passwd *p; 16429b50d902SRodney W. Grimes uid_t uid; 16439b50d902SRodney W. Grimes 1644ea92232aSPoul-Henning Kamp username = nextarg(option, argvp); 16459b50d902SRodney W. Grimes ftsoptions &= ~FTS_NOSTAT; 16469b50d902SRodney W. Grimes 16474ba3b38bSKirill Ponomarev new = palloc(option); 16489b50d902SRodney W. Grimes p = getpwnam(username); 16499b50d902SRodney W. Grimes if (p == NULL) { 16504ba3b38bSKirill Ponomarev char* cp = username; 16514ba3b38bSKirill Ponomarev if( username[0] == '-' || username[0] == '+' ) 16524ba3b38bSKirill Ponomarev username++; 16539b50d902SRodney W. Grimes uid = atoi(username); 16549b50d902SRodney W. Grimes if (uid == 0 && username[0] != '0') 1655ea92232aSPoul-Henning Kamp errx(1, "%s: %s: no such user", option->name, username); 16564ba3b38bSKirill Ponomarev uid = find_parsenum(new, option->name, cp, NULL); 16579b50d902SRodney W. Grimes } else 16589b50d902SRodney W. Grimes uid = p->pw_uid; 16599b50d902SRodney W. Grimes 16609b50d902SRodney W. Grimes new->u_data = uid; 1661ea92232aSPoul-Henning Kamp return new; 16629b50d902SRodney W. Grimes } 16639b50d902SRodney W. Grimes 16649b50d902SRodney W. Grimes /* 16659b50d902SRodney W. Grimes * -xdev functions -- 16669b50d902SRodney W. Grimes * 16679725a7b9SPhilippe Charnier * Always true, causes find not to descend past directories that have a 16689b50d902SRodney W. Grimes * different device ID (st_dev, see stat() S5.6.2 [POSIX.1]) 16699b50d902SRodney W. Grimes */ 16709b50d902SRodney W. Grimes PLAN * 1671ef646f18SMark Murray c_xdev(OPTION *option, char ***argvp __unused) 16729b50d902SRodney W. Grimes { 16739b50d902SRodney W. Grimes ftsoptions |= FTS_XDEV; 16749b50d902SRodney W. Grimes 1675ea92232aSPoul-Henning Kamp return palloc(option); 16769b50d902SRodney W. Grimes } 16779b50d902SRodney W. Grimes 16789b50d902SRodney W. Grimes /* 16799b50d902SRodney W. Grimes * ( expression ) functions -- 16809b50d902SRodney W. Grimes * 16819b50d902SRodney W. Grimes * True if expression is true. 16829b50d902SRodney W. Grimes */ 16839b50d902SRodney W. Grimes int 1684ef646f18SMark Murray f_expr(PLAN *plan, FTSENT *entry) 16859b50d902SRodney W. Grimes { 1686e98080b1SDavid Malone PLAN *p; 1687e98080b1SDavid Malone int state = 0; 16889b50d902SRodney W. Grimes 16899b50d902SRodney W. Grimes for (p = plan->p_data[0]; 1690ea92232aSPoul-Henning Kamp p && (state = (p->execute)(p, entry)); p = p->next); 1691ea92232aSPoul-Henning Kamp return state; 16929b50d902SRodney W. Grimes } 16939b50d902SRodney W. Grimes 16949b50d902SRodney W. Grimes /* 1695ea92232aSPoul-Henning Kamp * f_openparen and f_closeparen nodes are temporary place markers. They are 16969b50d902SRodney W. Grimes * eliminated during phase 2 of find_formplan() --- the '(' node is converted 1697ea92232aSPoul-Henning Kamp * to a f_expr node containing the expression and the ')' node is discarded. 1698ea92232aSPoul-Henning Kamp * The functions themselves are only used as constants. 16999b50d902SRodney W. Grimes */ 1700ea92232aSPoul-Henning Kamp 1701ea92232aSPoul-Henning Kamp int 1702ef646f18SMark Murray f_openparen(PLAN *plan __unused, FTSENT *entry __unused) 17039b50d902SRodney W. Grimes { 1704ea92232aSPoul-Henning Kamp abort(); 17059b50d902SRodney W. Grimes } 17069b50d902SRodney W. Grimes 1707ea92232aSPoul-Henning Kamp int 1708ef646f18SMark Murray f_closeparen(PLAN *plan __unused, FTSENT *entry __unused) 17099b50d902SRodney W. Grimes { 1710ea92232aSPoul-Henning Kamp abort(); 1711ea92232aSPoul-Henning Kamp } 1712ea92232aSPoul-Henning Kamp 1713ea92232aSPoul-Henning Kamp /* c_openparen == c_simple */ 1714ea92232aSPoul-Henning Kamp /* c_closeparen == c_simple */ 1715ea92232aSPoul-Henning Kamp 1716ea92232aSPoul-Henning Kamp /* 1717ea92232aSPoul-Henning Kamp * AND operator. Since AND is implicit, no node is allocated. 1718ea92232aSPoul-Henning Kamp */ 1719ea92232aSPoul-Henning Kamp PLAN * 1720ef646f18SMark Murray c_and(OPTION *option __unused, char ***argvp __unused) 1721ea92232aSPoul-Henning Kamp { 1722ea92232aSPoul-Henning Kamp return NULL; 17239b50d902SRodney W. Grimes } 17249b50d902SRodney W. Grimes 17259b50d902SRodney W. Grimes /* 17269b50d902SRodney W. Grimes * ! expression functions -- 17279b50d902SRodney W. Grimes * 17289b50d902SRodney W. Grimes * Negation of a primary; the unary NOT operator. 17299b50d902SRodney W. Grimes */ 17309b50d902SRodney W. Grimes int 1731ef646f18SMark Murray f_not(PLAN *plan, FTSENT *entry) 17329b50d902SRodney W. Grimes { 1733e98080b1SDavid Malone PLAN *p; 1734e98080b1SDavid Malone int state = 0; 17359b50d902SRodney W. Grimes 17369b50d902SRodney W. Grimes for (p = plan->p_data[0]; 1737ea92232aSPoul-Henning Kamp p && (state = (p->execute)(p, entry)); p = p->next); 1738ea92232aSPoul-Henning Kamp return !state; 17399b50d902SRodney W. Grimes } 17409b50d902SRodney W. Grimes 1741ea92232aSPoul-Henning Kamp /* c_not == c_simple */ 17429b50d902SRodney W. Grimes 17439b50d902SRodney W. Grimes /* 17449b50d902SRodney W. Grimes * expression -o expression functions -- 17459b50d902SRodney W. Grimes * 17469b50d902SRodney W. Grimes * Alternation of primaries; the OR operator. The second expression is 17479b50d902SRodney W. Grimes * not evaluated if the first expression is true. 17489b50d902SRodney W. Grimes */ 17499b50d902SRodney W. Grimes int 1750ef646f18SMark Murray f_or(PLAN *plan, FTSENT *entry) 17519b50d902SRodney W. Grimes { 1752e98080b1SDavid Malone PLAN *p; 1753e98080b1SDavid Malone int state = 0; 17549b50d902SRodney W. Grimes 17559b50d902SRodney W. Grimes for (p = plan->p_data[0]; 1756ea92232aSPoul-Henning Kamp p && (state = (p->execute)(p, entry)); p = p->next); 17579b50d902SRodney W. Grimes 17589b50d902SRodney W. Grimes if (state) 1759ea92232aSPoul-Henning Kamp return 1; 17609b50d902SRodney W. Grimes 17619b50d902SRodney W. Grimes for (p = plan->p_data[1]; 1762ea92232aSPoul-Henning Kamp p && (state = (p->execute)(p, entry)); p = p->next); 1763ea92232aSPoul-Henning Kamp return state; 17649b50d902SRodney W. Grimes } 17659b50d902SRodney W. Grimes 1766ea92232aSPoul-Henning Kamp /* c_or == c_simple */ 176746b993ffSWarner Losh 176846b993ffSWarner Losh /* 176946b993ffSWarner Losh * -false 177046b993ffSWarner Losh * 177146b993ffSWarner Losh * Always false. 177246b993ffSWarner Losh */ 177346b993ffSWarner Losh int 177446b993ffSWarner Losh f_false(PLAN *plan __unused, FTSENT *entry __unused) 177546b993ffSWarner Losh { 177646b993ffSWarner Losh return 0; 177746b993ffSWarner Losh } 177846b993ffSWarner Losh 177946b993ffSWarner Losh /* c_false == c_simple */ 178046b993ffSWarner Losh 178146b993ffSWarner Losh /* 178246b993ffSWarner Losh * -quit 178346b993ffSWarner Losh * 178446b993ffSWarner Losh * Exits the program 178546b993ffSWarner Losh */ 178646b993ffSWarner Losh int 178746b993ffSWarner Losh f_quit(PLAN *plan __unused, FTSENT *entry __unused) 178846b993ffSWarner Losh { 1789cf599562SJilles Tjoelker finish_execplus(); 1790b547523eSJilles Tjoelker exit(exitstatus); 179146b993ffSWarner Losh } 179246b993ffSWarner Losh 179346b993ffSWarner Losh /* c_quit == c_simple */ 1794