19b50d902SRodney W. Grimes /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro 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 359b50d902SRodney W. Grimes #include <sys/param.h> 369b50d902SRodney W. Grimes #include <sys/ucred.h> 379b50d902SRodney W. Grimes #include <sys/stat.h> 389c5d31dfSBosko Milekic #include <sys/types.h> 399c5d31dfSBosko Milekic #include <sys/acl.h> 409b50d902SRodney W. Grimes #include <sys/wait.h> 419b50d902SRodney W. Grimes #include <sys/mount.h> 429b50d902SRodney W. Grimes 43ed1a4621SPeter Wemm #include <dirent.h> 449b50d902SRodney W. Grimes #include <err.h> 459b50d902SRodney W. Grimes #include <errno.h> 469b50d902SRodney W. Grimes #include <fnmatch.h> 479b50d902SRodney W. Grimes #include <fts.h> 489b50d902SRodney W. Grimes #include <grp.h> 495e25d888STim J. Robbins #include <limits.h> 509b50d902SRodney W. Grimes #include <pwd.h> 517c1d4b3aSAkinori MUSHA #include <regex.h> 529b50d902SRodney W. Grimes #include <stdio.h> 539b50d902SRodney W. Grimes #include <stdlib.h> 549b50d902SRodney W. Grimes #include <string.h> 559b50d902SRodney W. Grimes #include <unistd.h> 561c832963SOliver Eikemeier #include <ctype.h> 579b50d902SRodney W. Grimes 589b50d902SRodney W. Grimes #include "find.h" 599b50d902SRodney W. Grimes 60ecca1f1cSMark Murray static PLAN *palloc(OPTION *); 61ecca1f1cSMark Murray static long long find_parsenum(PLAN *, const char *, char *, char *); 62ecca1f1cSMark Murray static long long find_parsetime(PLAN *, const char *, char *); 63ecca1f1cSMark Murray static char *nextarg(OPTION *, char ***); 64ea92232aSPoul-Henning Kamp 65a07af811STim J. Robbins extern char **environ; 66a07af811STim J. Robbins 6722170420SKirill Ponomarev static PLAN *lastexecplus = NULL; 6822170420SKirill Ponomarev 69adff4fcaSRuslan Ermilov #define COMPARE(a, b) do { \ 70ea92232aSPoul-Henning Kamp switch (plan->flags & F_ELG_MASK) { \ 719b50d902SRodney W. Grimes case F_EQUAL: \ 729b50d902SRodney W. Grimes return (a == b); \ 739b50d902SRodney W. Grimes case F_LESSTHAN: \ 749b50d902SRodney W. Grimes return (a < b); \ 759b50d902SRodney W. Grimes case F_GREATER: \ 769b50d902SRodney W. Grimes return (a > b); \ 779b50d902SRodney W. Grimes default: \ 789b50d902SRodney W. Grimes abort(); \ 799b50d902SRodney W. Grimes } \ 80adff4fcaSRuslan Ermilov } while(0) 819b50d902SRodney W. Grimes 82ea92232aSPoul-Henning Kamp static PLAN * 83ef646f18SMark Murray palloc(OPTION *option) 84ea92232aSPoul-Henning Kamp { 85ea92232aSPoul-Henning Kamp PLAN *new; 86ea92232aSPoul-Henning Kamp 87ea92232aSPoul-Henning Kamp if ((new = malloc(sizeof(PLAN))) == NULL) 88ea92232aSPoul-Henning Kamp err(1, NULL); 89ea92232aSPoul-Henning Kamp new->execute = option->execute; 90ea92232aSPoul-Henning Kamp new->flags = option->flags; 91ea92232aSPoul-Henning Kamp new->next = NULL; 92ea92232aSPoul-Henning Kamp return new; 93ea92232aSPoul-Henning Kamp } 949b50d902SRodney W. Grimes 959b50d902SRodney W. Grimes /* 969b50d902SRodney W. Grimes * find_parsenum -- 979b50d902SRodney W. Grimes * Parse a string of the form [+-]# and return the value. 989b50d902SRodney W. Grimes */ 999192bbf4SBruce Evans static long long 100ef646f18SMark Murray find_parsenum(PLAN *plan, const char *option, char *vp, char *endch) 1019b50d902SRodney W. Grimes { 1029192bbf4SBruce Evans long long value; 1039b50d902SRodney W. Grimes char *endchar, *str; /* Pointer to character ending conversion. */ 1049b50d902SRodney W. Grimes 1059b50d902SRodney W. Grimes /* Determine comparison from leading + or -. */ 1069b50d902SRodney W. Grimes str = vp; 1079b50d902SRodney W. Grimes switch (*str) { 1089b50d902SRodney W. Grimes case '+': 1099b50d902SRodney W. Grimes ++str; 110ea92232aSPoul-Henning Kamp plan->flags |= F_GREATER; 1119b50d902SRodney W. Grimes break; 1129b50d902SRodney W. Grimes case '-': 1139b50d902SRodney W. Grimes ++str; 114ea92232aSPoul-Henning Kamp plan->flags |= F_LESSTHAN; 1159b50d902SRodney W. Grimes break; 1169b50d902SRodney W. Grimes default: 117ea92232aSPoul-Henning Kamp plan->flags |= F_EQUAL; 1189b50d902SRodney W. Grimes break; 1199b50d902SRodney W. Grimes } 1209b50d902SRodney W. Grimes 1219b50d902SRodney W. Grimes /* 1229192bbf4SBruce Evans * Convert the string with strtoq(). Note, if strtoq() returns zero 1239b50d902SRodney W. Grimes * and endchar points to the beginning of the string we know we have 1249b50d902SRodney W. Grimes * a syntax error. 1259b50d902SRodney W. Grimes */ 1269192bbf4SBruce Evans value = strtoq(str, &endchar, 10); 1279b50d902SRodney W. Grimes if (value == 0 && endchar == str) 1289b50d902SRodney W. Grimes errx(1, "%s: %s: illegal numeric value", option, vp); 1295a890aacSKirill Ponomarev if (endchar[0] && endch == NULL) 1309b50d902SRodney W. Grimes errx(1, "%s: %s: illegal trailing character", option, vp); 1319b50d902SRodney W. Grimes if (endch) 1329b50d902SRodney W. Grimes *endch = endchar[0]; 133ea92232aSPoul-Henning Kamp return value; 1349b50d902SRodney W. Grimes } 1359b50d902SRodney W. Grimes 1369b50d902SRodney W. Grimes /* 137adff4fcaSRuslan Ermilov * find_parsetime -- 138adff4fcaSRuslan Ermilov * Parse a string of the form [+-]([0-9]+[smhdw]?)+ and return the value. 139adff4fcaSRuslan Ermilov */ 140adff4fcaSRuslan Ermilov static long long 141ef646f18SMark Murray find_parsetime(PLAN *plan, const char *option, char *vp) 142adff4fcaSRuslan Ermilov { 143adff4fcaSRuslan Ermilov long long secs, value; 144adff4fcaSRuslan Ermilov char *str, *unit; /* Pointer to character ending conversion. */ 145adff4fcaSRuslan Ermilov 146adff4fcaSRuslan Ermilov /* Determine comparison from leading + or -. */ 147adff4fcaSRuslan Ermilov str = vp; 148adff4fcaSRuslan Ermilov switch (*str) { 149adff4fcaSRuslan Ermilov case '+': 150adff4fcaSRuslan Ermilov ++str; 151adff4fcaSRuslan Ermilov plan->flags |= F_GREATER; 152adff4fcaSRuslan Ermilov break; 153adff4fcaSRuslan Ermilov case '-': 154adff4fcaSRuslan Ermilov ++str; 155adff4fcaSRuslan Ermilov plan->flags |= F_LESSTHAN; 156adff4fcaSRuslan Ermilov break; 157adff4fcaSRuslan Ermilov default: 158adff4fcaSRuslan Ermilov plan->flags |= F_EQUAL; 159adff4fcaSRuslan Ermilov break; 160adff4fcaSRuslan Ermilov } 161adff4fcaSRuslan Ermilov 162adff4fcaSRuslan Ermilov value = strtoq(str, &unit, 10); 163adff4fcaSRuslan Ermilov if (value == 0 && unit == str) { 164adff4fcaSRuslan Ermilov errx(1, "%s: %s: illegal time value", option, vp); 165adff4fcaSRuslan Ermilov /* NOTREACHED */ 166adff4fcaSRuslan Ermilov } 167adff4fcaSRuslan Ermilov if (*unit == '\0') 168adff4fcaSRuslan Ermilov return value; 169adff4fcaSRuslan Ermilov 170adff4fcaSRuslan Ermilov /* Units syntax. */ 171adff4fcaSRuslan Ermilov secs = 0; 172adff4fcaSRuslan Ermilov for (;;) { 173adff4fcaSRuslan Ermilov switch(*unit) { 174adff4fcaSRuslan Ermilov case 's': /* seconds */ 175adff4fcaSRuslan Ermilov secs += value; 176adff4fcaSRuslan Ermilov break; 177adff4fcaSRuslan Ermilov case 'm': /* minutes */ 178adff4fcaSRuslan Ermilov secs += value * 60; 179adff4fcaSRuslan Ermilov break; 180adff4fcaSRuslan Ermilov case 'h': /* hours */ 181adff4fcaSRuslan Ermilov secs += value * 3600; 182adff4fcaSRuslan Ermilov break; 183adff4fcaSRuslan Ermilov case 'd': /* days */ 184adff4fcaSRuslan Ermilov secs += value * 86400; 185adff4fcaSRuslan Ermilov break; 186adff4fcaSRuslan Ermilov case 'w': /* weeks */ 187adff4fcaSRuslan Ermilov secs += value * 604800; 188adff4fcaSRuslan Ermilov break; 189adff4fcaSRuslan Ermilov default: 190adff4fcaSRuslan Ermilov errx(1, "%s: %s: bad unit '%c'", option, vp, *unit); 191adff4fcaSRuslan Ermilov /* NOTREACHED */ 192adff4fcaSRuslan Ermilov } 193adff4fcaSRuslan Ermilov str = unit + 1; 194adff4fcaSRuslan Ermilov if (*str == '\0') /* EOS */ 195adff4fcaSRuslan Ermilov break; 196adff4fcaSRuslan Ermilov value = strtoq(str, &unit, 10); 197adff4fcaSRuslan Ermilov if (value == 0 && unit == str) { 198adff4fcaSRuslan Ermilov errx(1, "%s: %s: illegal time value", option, vp); 199adff4fcaSRuslan Ermilov /* NOTREACHED */ 200adff4fcaSRuslan Ermilov } 201adff4fcaSRuslan Ermilov if (*unit == '\0') { 202adff4fcaSRuslan Ermilov errx(1, "%s: %s: missing trailing unit", option, vp); 203adff4fcaSRuslan Ermilov /* NOTREACHED */ 204adff4fcaSRuslan Ermilov } 205adff4fcaSRuslan Ermilov } 206adff4fcaSRuslan Ermilov plan->flags |= F_EXACTTIME; 207adff4fcaSRuslan Ermilov return secs; 208adff4fcaSRuslan Ermilov } 209adff4fcaSRuslan Ermilov 210adff4fcaSRuslan Ermilov /* 211ea92232aSPoul-Henning Kamp * nextarg -- 212ea92232aSPoul-Henning Kamp * Check that another argument still exists, return a pointer to it, 213ea92232aSPoul-Henning Kamp * and increment the argument vector pointer. 214ea92232aSPoul-Henning Kamp */ 215ea92232aSPoul-Henning Kamp static char * 216ef646f18SMark Murray nextarg(OPTION *option, char ***argvp) 217ea92232aSPoul-Henning Kamp { 218ea92232aSPoul-Henning Kamp char *arg; 219ea92232aSPoul-Henning Kamp 22026ac9660SMarcelo Araujo if ((arg = **argvp) == NULL) 221ea92232aSPoul-Henning Kamp errx(1, "%s: requires additional arguments", option->name); 222ea92232aSPoul-Henning Kamp (*argvp)++; 223ea92232aSPoul-Henning Kamp return arg; 224ea92232aSPoul-Henning Kamp } /* nextarg() */ 225ea92232aSPoul-Henning Kamp 226ea92232aSPoul-Henning Kamp /* 22731d53425SCeri Davies * The value of n for the inode times (atime, birthtime, ctime, mtime) is a 22831d53425SCeri Davies * range, i.e. n matches from (n - 1) to n 24 hour periods. This interacts 22931d53425SCeri Davies * with -n, such that "-mtime -1" would be less than 0 days, which isn't what 23031d53425SCeri Davies * the user wanted. Correct so that -1 is "less than 1". 2319b50d902SRodney W. Grimes */ 232ea92232aSPoul-Henning Kamp #define TIME_CORRECT(p) \ 233ea92232aSPoul-Henning Kamp if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \ 234ddd956b0SJilles Tjoelker ++((p)->t_data.tv_sec); 2359b50d902SRodney W. Grimes 2369b50d902SRodney W. Grimes /* 237ea92232aSPoul-Henning Kamp * -[acm]min n functions -- 2383f5223f8SWolfram Schneider * 239ea92232aSPoul-Henning Kamp * True if the difference between the 240ea92232aSPoul-Henning Kamp * file access time (-amin) 24131d53425SCeri Davies * file birth time (-Bmin) 242ea92232aSPoul-Henning Kamp * last change of file status information (-cmin) 243ea92232aSPoul-Henning Kamp * file modification time (-mmin) 244ea92232aSPoul-Henning Kamp * and the current time is n min periods. 2453f5223f8SWolfram Schneider */ 2463f5223f8SWolfram Schneider int 247ef646f18SMark Murray f_Xmin(PLAN *plan, FTSENT *entry) 2483f5223f8SWolfram Schneider { 249ea92232aSPoul-Henning Kamp if (plan->flags & F_TIME_C) { 2503f5223f8SWolfram Schneider COMPARE((now - entry->fts_statp->st_ctime + 251ddd956b0SJilles Tjoelker 60 - 1) / 60, plan->t_data.tv_sec); 252ea92232aSPoul-Henning Kamp } else if (plan->flags & F_TIME_A) { 253ea92232aSPoul-Henning Kamp COMPARE((now - entry->fts_statp->st_atime + 254ddd956b0SJilles Tjoelker 60 - 1) / 60, plan->t_data.tv_sec); 255c3a6ea5bSAlex Richardson #if HAVE_STRUCT_STAT_ST_BIRTHTIME 25631d53425SCeri Davies } else if (plan->flags & F_TIME_B) { 25731d53425SCeri Davies COMPARE((now - entry->fts_statp->st_birthtime + 258ddd956b0SJilles Tjoelker 60 - 1) / 60, plan->t_data.tv_sec); 259c3a6ea5bSAlex Richardson #endif 260ea92232aSPoul-Henning Kamp } else { 261ea92232aSPoul-Henning Kamp COMPARE((now - entry->fts_statp->st_mtime + 262ddd956b0SJilles Tjoelker 60 - 1) / 60, plan->t_data.tv_sec); 263ea92232aSPoul-Henning Kamp } 2643f5223f8SWolfram Schneider } 2653f5223f8SWolfram Schneider 2663f5223f8SWolfram Schneider PLAN * 267ef646f18SMark Murray c_Xmin(OPTION *option, char ***argvp) 2683f5223f8SWolfram Schneider { 269ea92232aSPoul-Henning Kamp char *nmins; 2703f5223f8SWolfram Schneider PLAN *new; 2713f5223f8SWolfram Schneider 272ea92232aSPoul-Henning Kamp nmins = nextarg(option, argvp); 2733f5223f8SWolfram Schneider ftsoptions &= ~FTS_NOSTAT; 2743f5223f8SWolfram Schneider 275ea92232aSPoul-Henning Kamp new = palloc(option); 276ddd956b0SJilles Tjoelker new->t_data.tv_sec = find_parsenum(new, option->name, nmins, NULL); 277ddd956b0SJilles Tjoelker new->t_data.tv_nsec = 0; 278ea92232aSPoul-Henning Kamp TIME_CORRECT(new); 279ea92232aSPoul-Henning Kamp return new; 2803f5223f8SWolfram Schneider } 2813f5223f8SWolfram Schneider 2829b50d902SRodney W. Grimes /* 283ea92232aSPoul-Henning Kamp * -[acm]time n functions -- 2849b50d902SRodney W. Grimes * 285ea92232aSPoul-Henning Kamp * True if the difference between the 286ea92232aSPoul-Henning Kamp * file access time (-atime) 28731d53425SCeri Davies * file birth time (-Btime) 288ea92232aSPoul-Henning Kamp * last change of file status information (-ctime) 289ea92232aSPoul-Henning Kamp * file modification time (-mtime) 290ea92232aSPoul-Henning Kamp * and the current time is n 24 hour periods. 2919b50d902SRodney W. Grimes */ 292ea92232aSPoul-Henning Kamp 2939b50d902SRodney W. Grimes int 294ef646f18SMark Murray f_Xtime(PLAN *plan, FTSENT *entry) 2959b50d902SRodney W. Grimes { 296631a8765SRuslan Ermilov time_t xtime; 297adff4fcaSRuslan Ermilov 298631a8765SRuslan Ermilov if (plan->flags & F_TIME_A) 299631a8765SRuslan Ermilov xtime = entry->fts_statp->st_atime; 300c3a6ea5bSAlex Richardson #if HAVE_STRUCT_STAT_ST_BIRTHTIME 30131d53425SCeri Davies else if (plan->flags & F_TIME_B) 30231d53425SCeri Davies xtime = entry->fts_statp->st_birthtime; 303c3a6ea5bSAlex Richardson #endif 304631a8765SRuslan Ermilov else if (plan->flags & F_TIME_C) 305631a8765SRuslan Ermilov xtime = entry->fts_statp->st_ctime; 306631a8765SRuslan Ermilov else 307631a8765SRuslan Ermilov xtime = entry->fts_statp->st_mtime; 3089b50d902SRodney W. Grimes 309631a8765SRuslan Ermilov if (plan->flags & F_EXACTTIME) 310ddd956b0SJilles Tjoelker COMPARE(now - xtime, plan->t_data.tv_sec); 311adff4fcaSRuslan Ermilov else 312ddd956b0SJilles Tjoelker COMPARE((now - xtime + 86400 - 1) / 86400, plan->t_data.tv_sec); 3139b50d902SRodney W. Grimes } 3149b50d902SRodney W. Grimes 3159b50d902SRodney W. Grimes PLAN * 316ef646f18SMark Murray c_Xtime(OPTION *option, char ***argvp) 3179b50d902SRodney W. Grimes { 318adff4fcaSRuslan Ermilov char *value; 3199b50d902SRodney W. Grimes PLAN *new; 3209b50d902SRodney W. Grimes 321adff4fcaSRuslan Ermilov value = nextarg(option, argvp); 3229b50d902SRodney W. Grimes ftsoptions &= ~FTS_NOSTAT; 3239b50d902SRodney W. Grimes 324ea92232aSPoul-Henning Kamp new = palloc(option); 325ddd956b0SJilles Tjoelker new->t_data.tv_sec = find_parsetime(new, option->name, value); 326ddd956b0SJilles Tjoelker new->t_data.tv_nsec = 0; 327adff4fcaSRuslan Ermilov if (!(new->flags & F_EXACTTIME)) 328ea92232aSPoul-Henning Kamp TIME_CORRECT(new); 329ea92232aSPoul-Henning Kamp return new; 330ea92232aSPoul-Henning Kamp } 331ea92232aSPoul-Henning Kamp 332ea92232aSPoul-Henning Kamp /* 333ea92232aSPoul-Henning Kamp * -maxdepth/-mindepth n functions -- 334ea92232aSPoul-Henning Kamp * 335ea92232aSPoul-Henning Kamp * Does the same as -prune if the level of the current file is 336ea92232aSPoul-Henning Kamp * greater/less than the specified maximum/minimum depth. 337ea92232aSPoul-Henning Kamp * 338ea92232aSPoul-Henning Kamp * Note that -maxdepth and -mindepth are handled specially in 339ea92232aSPoul-Henning Kamp * find_execute() so their f_* functions are set to f_always_true(). 340ea92232aSPoul-Henning Kamp */ 341ea92232aSPoul-Henning Kamp PLAN * 342ef646f18SMark Murray c_mXXdepth(OPTION *option, char ***argvp) 343ea92232aSPoul-Henning Kamp { 344ea92232aSPoul-Henning Kamp char *dstr; 345ea92232aSPoul-Henning Kamp PLAN *new; 346ea92232aSPoul-Henning Kamp 347ea92232aSPoul-Henning Kamp dstr = nextarg(option, argvp); 348ea92232aSPoul-Henning Kamp if (dstr[0] == '-') 349ea92232aSPoul-Henning Kamp /* all other errors handled by find_parsenum() */ 350ea92232aSPoul-Henning Kamp errx(1, "%s: %s: value must be positive", option->name, dstr); 351ea92232aSPoul-Henning Kamp 352ea92232aSPoul-Henning Kamp new = palloc(option); 353ea92232aSPoul-Henning Kamp if (option->flags & F_MAXDEPTH) 354ea92232aSPoul-Henning Kamp maxdepth = find_parsenum(new, option->name, dstr, NULL); 355ea92232aSPoul-Henning Kamp else 356ea92232aSPoul-Henning Kamp mindepth = find_parsenum(new, option->name, dstr, NULL); 357ea92232aSPoul-Henning Kamp return new; 358ea92232aSPoul-Henning Kamp } 359ea92232aSPoul-Henning Kamp 360c3a6ea5bSAlex Richardson #ifdef ACL_TYPE_NFS4 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 return (0); 406fa2db914SEdward Tomasz Napierala } 407fa2db914SEdward Tomasz Napierala if (trivial) 408fa2db914SEdward Tomasz Napierala return (0); 409fa2db914SEdward Tomasz Napierala return (1); 4109c5d31dfSBosko Milekic } 411c3a6ea5bSAlex Richardson #endif 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 */ 4439d6d5a71SJilles Tjoelker if (entry->fts_level > FTS_ROOTLEVEL && 4449d6d5a71SJilles Tjoelker strchr(entry->fts_accpath, '/') != NULL) 445ea92232aSPoul-Henning Kamp errx(1, "-delete: %s: relative path potentially not safe", 446ea92232aSPoul-Henning Kamp entry->fts_accpath); 447ea92232aSPoul-Henning Kamp 448c3a6ea5bSAlex Richardson #if HAVE_STRUCT_STAT_ST_FLAGS 449ea92232aSPoul-Henning Kamp /* Turn off user immutable bits if running as root */ 450ea92232aSPoul-Henning Kamp if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) && 451ea92232aSPoul-Henning Kamp !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) && 452ea92232aSPoul-Henning Kamp geteuid() == 0) 4536911f596SJilles Tjoelker lchflags(entry->fts_accpath, 454ea92232aSPoul-Henning Kamp entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)); 455c3a6ea5bSAlex Richardson #endif 456ea92232aSPoul-Henning Kamp 457ea92232aSPoul-Henning Kamp /* rmdir directories, unlink everything else */ 458ea92232aSPoul-Henning Kamp if (S_ISDIR(entry->fts_statp->st_mode)) { 459ea92232aSPoul-Henning Kamp if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY) 460ea92232aSPoul-Henning Kamp warn("-delete: rmdir(%s)", entry->fts_path); 461ea92232aSPoul-Henning Kamp } else { 462ea92232aSPoul-Henning Kamp if (unlink(entry->fts_accpath) < 0) 463ea92232aSPoul-Henning Kamp warn("-delete: unlink(%s)", entry->fts_path); 464ea92232aSPoul-Henning Kamp } 465ea92232aSPoul-Henning Kamp 466ea92232aSPoul-Henning Kamp /* "succeed" */ 467ea92232aSPoul-Henning Kamp return 1; 468ea92232aSPoul-Henning Kamp } 469ea92232aSPoul-Henning Kamp 470ea92232aSPoul-Henning Kamp PLAN * 471ef646f18SMark Murray c_delete(OPTION *option, char ***argvp __unused) 472ea92232aSPoul-Henning Kamp { 473ea92232aSPoul-Henning Kamp 474ea92232aSPoul-Henning Kamp ftsoptions &= ~FTS_NOSTAT; /* no optimise */ 475ea92232aSPoul-Henning Kamp isoutput = 1; /* possible output */ 476ea92232aSPoul-Henning Kamp isdepth = 1; /* -depth implied */ 477ea92232aSPoul-Henning Kamp 47817ef6d3aSJilles Tjoelker /* 47917ef6d3aSJilles Tjoelker * Try to avoid the confusing error message about relative paths 48017ef6d3aSJilles Tjoelker * being potentially not safe. 48117ef6d3aSJilles Tjoelker */ 48217ef6d3aSJilles Tjoelker if (ftsoptions & FTS_NOCHDIR) 48317ef6d3aSJilles Tjoelker errx(1, "%s: forbidden when the current directory cannot be opened", 48417ef6d3aSJilles Tjoelker "-delete"); 48517ef6d3aSJilles Tjoelker 486ea92232aSPoul-Henning Kamp return palloc(option); 4879b50d902SRodney W. Grimes } 4889b50d902SRodney W. Grimes 4893f5223f8SWolfram Schneider 4909b50d902SRodney W. Grimes /* 4911c832963SOliver Eikemeier * always_true -- 4929b50d902SRodney W. Grimes * 49346b993ffSWarner Losh * Always true, used for -maxdepth, -mindepth, -xdev, -follow, and -true 4949b50d902SRodney W. Grimes */ 4959b50d902SRodney W. Grimes int 496ef646f18SMark Murray f_always_true(PLAN *plan __unused, FTSENT *entry __unused) 4979b50d902SRodney W. Grimes { 498ea92232aSPoul-Henning Kamp return 1; 4999b50d902SRodney W. Grimes } 5009b50d902SRodney W. Grimes 5011c832963SOliver Eikemeier /* 5021c832963SOliver Eikemeier * -depth functions -- 5031c832963SOliver Eikemeier * 5041c832963SOliver Eikemeier * With argument: True if the file is at level n. 5051c832963SOliver Eikemeier * Without argument: Always true, causes descent of the directory hierarchy 5061c832963SOliver Eikemeier * to be done so that all entries in a directory are acted on before the 5071c832963SOliver Eikemeier * directory itself. 5081c832963SOliver Eikemeier */ 5091c832963SOliver Eikemeier int 5101c832963SOliver Eikemeier f_depth(PLAN *plan, FTSENT *entry) 5119b50d902SRodney W. Grimes { 5121c832963SOliver Eikemeier if (plan->flags & F_DEPTH) 5131c832963SOliver Eikemeier COMPARE(entry->fts_level, plan->d_data); 5141c832963SOliver Eikemeier else 5151c832963SOliver Eikemeier return 1; 5161c832963SOliver Eikemeier } 5179b50d902SRodney W. Grimes 5181c832963SOliver Eikemeier PLAN * 5191c832963SOliver Eikemeier c_depth(OPTION *option, char ***argvp) 5201c832963SOliver Eikemeier { 5211c832963SOliver Eikemeier PLAN *new; 5221c832963SOliver Eikemeier char *str; 5231c832963SOliver Eikemeier 5241c832963SOliver Eikemeier new = palloc(option); 5251c832963SOliver Eikemeier 5261c832963SOliver Eikemeier str = **argvp; 5271c832963SOliver Eikemeier if (str && !(new->flags & F_DEPTH)) { 5281c832963SOliver Eikemeier /* skip leading + or - */ 5291c832963SOliver Eikemeier if (*str == '+' || *str == '-') 5301c832963SOliver Eikemeier str++; 5311c832963SOliver Eikemeier /* skip sign */ 5321c832963SOliver Eikemeier if (*str == '+' || *str == '-') 5331c832963SOliver Eikemeier str++; 5341c832963SOliver Eikemeier if (isdigit(*str)) 5351c832963SOliver Eikemeier new->flags |= F_DEPTH; 5361c832963SOliver Eikemeier } 5371c832963SOliver Eikemeier 5381c832963SOliver Eikemeier if (new->flags & F_DEPTH) { /* -depth n */ 5391c832963SOliver Eikemeier char *ndepth; 5401c832963SOliver Eikemeier 5411c832963SOliver Eikemeier ndepth = nextarg(option, argvp); 5421c832963SOliver Eikemeier new->d_data = find_parsenum(new, option->name, ndepth, NULL); 5431c832963SOliver Eikemeier } else { /* -d */ 5441c832963SOliver Eikemeier isdepth = 1; 5451c832963SOliver Eikemeier } 5461c832963SOliver Eikemeier 5471c832963SOliver Eikemeier return new; 5489b50d902SRodney W. Grimes } 549127d7563SWarner Losh 550127d7563SWarner Losh /* 551ed1a4621SPeter Wemm * -empty functions -- 552ed1a4621SPeter Wemm * 553ed1a4621SPeter Wemm * True if the file or directory is empty 554ed1a4621SPeter Wemm */ 555ed1a4621SPeter Wemm int 556ef646f18SMark Murray f_empty(PLAN *plan __unused, FTSENT *entry) 557ed1a4621SPeter Wemm { 558ea92232aSPoul-Henning Kamp if (S_ISREG(entry->fts_statp->st_mode) && 559ea92232aSPoul-Henning Kamp entry->fts_statp->st_size == 0) 560ea92232aSPoul-Henning Kamp return 1; 561ed1a4621SPeter Wemm if (S_ISDIR(entry->fts_statp->st_mode)) { 562ed1a4621SPeter Wemm struct dirent *dp; 563ed1a4621SPeter Wemm int empty; 564ed1a4621SPeter Wemm DIR *dir; 565ed1a4621SPeter Wemm 566ed1a4621SPeter Wemm empty = 1; 567ed1a4621SPeter Wemm dir = opendir(entry->fts_accpath); 568ed1a4621SPeter Wemm if (dir == NULL) 569e66a677bSKevin Lo return 0; 570ed1a4621SPeter Wemm for (dp = readdir(dir); dp; dp = readdir(dir)) 571ed1a4621SPeter Wemm if (dp->d_name[0] != '.' || 572ed1a4621SPeter Wemm (dp->d_name[1] != '\0' && 573ed1a4621SPeter Wemm (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) { 574ed1a4621SPeter Wemm empty = 0; 575ed1a4621SPeter Wemm break; 576ed1a4621SPeter Wemm } 577ed1a4621SPeter Wemm closedir(dir); 578ea92232aSPoul-Henning Kamp return empty; 579ed1a4621SPeter Wemm } 580ea92232aSPoul-Henning Kamp return 0; 581ed1a4621SPeter Wemm } 582ed1a4621SPeter Wemm 583ed1a4621SPeter Wemm PLAN * 584ef646f18SMark Murray c_empty(OPTION *option, char ***argvp __unused) 585ed1a4621SPeter Wemm { 586ed1a4621SPeter Wemm ftsoptions &= ~FTS_NOSTAT; 587ed1a4621SPeter Wemm 588ea92232aSPoul-Henning Kamp return palloc(option); 589ed1a4621SPeter Wemm } 590ed1a4621SPeter Wemm 591ed1a4621SPeter Wemm /* 592ea92232aSPoul-Henning Kamp * [-exec | -execdir | -ok] utility [arg ... ] ; functions -- 593127d7563SWarner Losh * 594127d7563SWarner Losh * True if the executed utility returns a zero value as exit status. 595127d7563SWarner Losh * The end of the primary expression is delimited by a semicolon. If 596ea92232aSPoul-Henning Kamp * "{}" occurs anywhere, it gets replaced by the current pathname, 597ea92232aSPoul-Henning Kamp * or, in the case of -execdir, the current basename (filename 598ea92232aSPoul-Henning Kamp * without leading directory prefix). For -exec and -ok, 599ea92232aSPoul-Henning Kamp * the current directory for the execution of utility is the same as 600ea92232aSPoul-Henning Kamp * the current directory when the find utility was started, whereas 601ea92232aSPoul-Henning Kamp * for -execdir, it is the directory the file resides in. 602ea92232aSPoul-Henning Kamp * 603ea92232aSPoul-Henning Kamp * The primary -ok differs from -exec in that it requests affirmation 604ea92232aSPoul-Henning Kamp * of the user before executing the utility. 605127d7563SWarner Losh */ 606127d7563SWarner Losh int 607ef646f18SMark Murray f_exec(PLAN *plan, FTSENT *entry) 608127d7563SWarner Losh { 609e98080b1SDavid Malone int cnt; 610127d7563SWarner Losh pid_t pid; 611127d7563SWarner Losh int status; 612127d7563SWarner Losh char *file; 613127d7563SWarner Losh 6145e25d888STim J. Robbins if (entry == NULL && plan->flags & F_EXECPLUS) { 6155e25d888STim J. Robbins if (plan->e_ppos == plan->e_pbnum) 6165e25d888STim J. Robbins return (1); 6175e25d888STim J. Robbins plan->e_argv[plan->e_ppos] = NULL; 6185e25d888STim J. Robbins goto doexec; 6195e25d888STim J. Robbins } 6205e25d888STim J. Robbins 621127d7563SWarner Losh /* XXX - if file/dir ends in '/' this will not work -- can it? */ 622ea92232aSPoul-Henning Kamp if ((plan->flags & F_EXECDIR) && \ 623ea92232aSPoul-Henning Kamp (file = strrchr(entry->fts_path, '/'))) 624127d7563SWarner Losh file++; 625127d7563SWarner Losh else 626127d7563SWarner Losh file = entry->fts_path; 627127d7563SWarner Losh 6285e25d888STim J. Robbins if (plan->flags & F_EXECPLUS) { 6295e25d888STim J. Robbins if ((plan->e_argv[plan->e_ppos] = strdup(file)) == NULL) 6305e25d888STim J. Robbins err(1, NULL); 6315e25d888STim J. Robbins plan->e_len[plan->e_ppos] = strlen(file); 6325e25d888STim J. Robbins plan->e_psize += plan->e_len[plan->e_ppos]; 6335e25d888STim J. Robbins if (++plan->e_ppos < plan->e_pnummax && 6345e25d888STim J. Robbins plan->e_psize < plan->e_psizemax) 6355e25d888STim J. Robbins return (1); 6365e25d888STim J. Robbins plan->e_argv[plan->e_ppos] = NULL; 6375e25d888STim J. Robbins } else { 638127d7563SWarner Losh for (cnt = 0; plan->e_argv[cnt]; ++cnt) 639127d7563SWarner Losh if (plan->e_len[cnt]) 6405e25d888STim J. Robbins brace_subst(plan->e_orig[cnt], 6415e25d888STim J. Robbins &plan->e_argv[cnt], file, 6425e25d888STim J. Robbins plan->e_len[cnt]); 6435e25d888STim J. Robbins } 644127d7563SWarner Losh 6455e25d888STim J. Robbins doexec: if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv)) 646ea92232aSPoul-Henning Kamp return 0; 647ea92232aSPoul-Henning Kamp 648ea92232aSPoul-Henning Kamp /* make sure find output is interspersed correctly with subprocesses */ 649127d7563SWarner Losh fflush(stdout); 650127d7563SWarner Losh fflush(stderr); 651127d7563SWarner Losh 6521fd98d7dSDag-Erling Smørgrav switch (pid = fork()) { 653127d7563SWarner Losh case -1: 654127d7563SWarner Losh err(1, "fork"); 655127d7563SWarner Losh /* NOTREACHED */ 656127d7563SWarner Losh case 0: 657ea92232aSPoul-Henning Kamp /* change dir back from where we started */ 65817ef6d3aSJilles Tjoelker if (!(plan->flags & F_EXECDIR) && 65917ef6d3aSJilles Tjoelker !(ftsoptions & FTS_NOCHDIR) && fchdir(dotfd)) { 660ea92232aSPoul-Henning Kamp warn("chdir"); 661ea92232aSPoul-Henning Kamp _exit(1); 662ea92232aSPoul-Henning Kamp } 663127d7563SWarner Losh execvp(plan->e_argv[0], plan->e_argv); 664127d7563SWarner Losh warn("%s", plan->e_argv[0]); 665127d7563SWarner Losh _exit(1); 666127d7563SWarner Losh } 6675e25d888STim J. Robbins if (plan->flags & F_EXECPLUS) { 6685e25d888STim J. Robbins while (--plan->e_ppos >= plan->e_pbnum) 6695e25d888STim J. Robbins free(plan->e_argv[plan->e_ppos]); 6705e25d888STim J. Robbins plan->e_ppos = plan->e_pbnum; 6715e25d888STim J. Robbins plan->e_psize = plan->e_pbsize; 6725e25d888STim J. Robbins } 673127d7563SWarner Losh pid = waitpid(pid, &status, 0); 6747a79617cSJilles Tjoelker if (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status)) 6757a79617cSJilles Tjoelker return (1); 6767a79617cSJilles Tjoelker if (plan->flags & F_EXECPLUS) { 6777a79617cSJilles Tjoelker exitstatus = 1; 6787a79617cSJilles Tjoelker return (1); 6797a79617cSJilles Tjoelker } 6807a79617cSJilles Tjoelker return (0); 681127d7563SWarner Losh } 682127d7563SWarner Losh 683127d7563SWarner Losh /* 684ea92232aSPoul-Henning Kamp * c_exec, c_execdir, c_ok -- 685127d7563SWarner Losh * build three parallel arrays, one with pointers to the strings passed 686127d7563SWarner Losh * on the command line, one with (possibly duplicated) pointers to the 687127d7563SWarner Losh * argv array, and one with integer values that are lengths of the 688127d7563SWarner Losh * strings, but also flags meaning that the string has to be massaged. 689127d7563SWarner Losh */ 690127d7563SWarner Losh PLAN * 691ef646f18SMark Murray c_exec(OPTION *option, char ***argvp) 692127d7563SWarner Losh { 693127d7563SWarner Losh PLAN *new; /* node returned */ 6945e25d888STim J. Robbins long argmax; 6955e25d888STim J. Robbins int cnt, i; 696a07af811STim J. Robbins char **argv, **ap, **ep, *p; 697127d7563SWarner Losh 69817ef6d3aSJilles Tjoelker /* This would defeat -execdir's intended security. */ 69917ef6d3aSJilles Tjoelker if (option->flags & F_EXECDIR && ftsoptions & FTS_NOCHDIR) 70017ef6d3aSJilles Tjoelker errx(1, "%s: forbidden when the current directory cannot be opened", 70117ef6d3aSJilles Tjoelker "-execdir"); 70217ef6d3aSJilles Tjoelker 703ea92232aSPoul-Henning Kamp /* XXX - was in c_execdir, but seems unnecessary!? 704127d7563SWarner Losh ftsoptions &= ~FTS_NOSTAT; 705ea92232aSPoul-Henning Kamp */ 706127d7563SWarner Losh isoutput = 1; 707127d7563SWarner Losh 708ea92232aSPoul-Henning Kamp /* XXX - this is a change from the previous coding */ 709ea92232aSPoul-Henning Kamp new = palloc(option); 710127d7563SWarner Losh 711127d7563SWarner Losh for (ap = argv = *argvp;; ++ap) { 712127d7563SWarner Losh if (!*ap) 713127d7563SWarner Losh errx(1, 714e22bb9dbSTim J. Robbins "%s: no terminating \";\" or \"+\"", option->name); 715127d7563SWarner Losh if (**ap == ';') 716127d7563SWarner Losh break; 7175e25d888STim J. Robbins if (**ap == '+' && ap != argv && strcmp(*(ap - 1), "{}") == 0) { 7185e25d888STim J. Robbins new->flags |= F_EXECPLUS; 7195e25d888STim J. Robbins break; 7205e25d888STim J. Robbins } 721127d7563SWarner Losh } 722127d7563SWarner Losh 72351b0534fSJuli Mallett if (ap == argv) 72451b0534fSJuli Mallett errx(1, "%s: no command specified", option->name); 72551b0534fSJuli Mallett 726127d7563SWarner Losh cnt = ap - *argvp + 1; 7275e25d888STim J. Robbins if (new->flags & F_EXECPLUS) { 7285e25d888STim J. Robbins new->e_ppos = new->e_pbnum = cnt - 2; 7295e25d888STim J. Robbins if ((argmax = sysconf(_SC_ARG_MAX)) == -1) { 7305e25d888STim J. Robbins warn("sysconf(_SC_ARG_MAX)"); 7315e25d888STim J. Robbins argmax = _POSIX_ARG_MAX; 7325e25d888STim J. Robbins } 733a07af811STim J. Robbins argmax -= 1024; 734a07af811STim J. Robbins for (ep = environ; *ep != NULL; ep++) 735a07af811STim J. Robbins argmax -= strlen(*ep) + 1 + sizeof(*ep); 736a07af811STim J. Robbins argmax -= 1 + sizeof(*ep); 737bc626176SJilles Tjoelker /* 738bc626176SJilles Tjoelker * Ensure that -execdir ... {} + does not mix files 739bc626176SJilles Tjoelker * from different directories in one invocation. 740bc626176SJilles Tjoelker * Files from the same directory should be handled 741bc626176SJilles Tjoelker * in one invocation but there is no code for it. 742bc626176SJilles Tjoelker */ 743bc626176SJilles Tjoelker new->e_pnummax = new->flags & F_EXECDIR ? 1 : argmax / 16; 744a07af811STim J. Robbins argmax -= sizeof(char *) * new->e_pnummax; 745a07af811STim J. Robbins if (argmax <= 0) 746a07af811STim J. Robbins errx(1, "no space for arguments"); 747a07af811STim J. Robbins new->e_psizemax = argmax; 7485e25d888STim J. Robbins new->e_pbsize = 0; 7495e25d888STim J. Robbins cnt += new->e_pnummax + 1; 75022170420SKirill Ponomarev new->e_next = lastexecplus; 75122170420SKirill Ponomarev lastexecplus = new; 7525e25d888STim J. Robbins } 75347bca8b0SJuli Mallett if ((new->e_argv = malloc(cnt * sizeof(char *))) == NULL) 75447bca8b0SJuli Mallett err(1, NULL); 75547bca8b0SJuli Mallett if ((new->e_orig = malloc(cnt * sizeof(char *))) == NULL) 75647bca8b0SJuli Mallett err(1, NULL); 75747bca8b0SJuli Mallett if ((new->e_len = malloc(cnt * sizeof(int))) == NULL) 75847bca8b0SJuli Mallett err(1, NULL); 759127d7563SWarner Losh 760127d7563SWarner Losh for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) { 761127d7563SWarner Losh new->e_orig[cnt] = *argv; 7625e25d888STim J. Robbins if (new->flags & F_EXECPLUS) 7635e25d888STim J. Robbins new->e_pbsize += strlen(*argv) + 1; 764127d7563SWarner Losh for (p = *argv; *p; ++p) 7655e25d888STim J. Robbins if (!(new->flags & F_EXECPLUS) && p[0] == '{' && 7665e25d888STim J. Robbins p[1] == '}') { 767f0cb9537SDavid E. O'Brien if ((new->e_argv[cnt] = 76847bca8b0SJuli Mallett malloc(MAXPATHLEN)) == NULL) 76947bca8b0SJuli Mallett err(1, NULL); 770127d7563SWarner Losh new->e_len[cnt] = MAXPATHLEN; 771127d7563SWarner Losh break; 772127d7563SWarner Losh } 773127d7563SWarner Losh if (!*p) { 774127d7563SWarner Losh new->e_argv[cnt] = *argv; 775127d7563SWarner Losh new->e_len[cnt] = 0; 776127d7563SWarner Losh } 777127d7563SWarner Losh } 7785e25d888STim J. Robbins if (new->flags & F_EXECPLUS) { 7795e25d888STim J. Robbins new->e_psize = new->e_pbsize; 7805e25d888STim J. Robbins cnt--; 7815e25d888STim J. Robbins for (i = 0; i < new->e_pnummax; i++) { 7825e25d888STim J. Robbins new->e_argv[cnt] = NULL; 7835e25d888STim J. Robbins new->e_len[cnt] = 0; 7845e25d888STim J. Robbins cnt++; 7855e25d888STim J. Robbins } 7865e25d888STim J. Robbins argv = ap; 7875e25d888STim J. Robbins goto done; 7885e25d888STim J. Robbins } 789127d7563SWarner Losh new->e_argv[cnt] = new->e_orig[cnt] = NULL; 790127d7563SWarner Losh 7915e25d888STim J. Robbins done: *argvp = argv + 1; 792ea92232aSPoul-Henning Kamp return new; 793ea92232aSPoul-Henning Kamp } 794ea92232aSPoul-Henning Kamp 79522170420SKirill Ponomarev /* Finish any pending -exec ... {} + functions. */ 79622170420SKirill Ponomarev void 79710bc3a7fSEd Schouten finish_execplus(void) 79822170420SKirill Ponomarev { 79922170420SKirill Ponomarev PLAN *p; 80022170420SKirill Ponomarev 80122170420SKirill Ponomarev p = lastexecplus; 80222170420SKirill Ponomarev while (p != NULL) { 80322170420SKirill Ponomarev (p->execute)(p, NULL); 80422170420SKirill Ponomarev p = p->e_next; 80522170420SKirill Ponomarev } 80622170420SKirill Ponomarev } 80722170420SKirill Ponomarev 808c3a6ea5bSAlex Richardson #if HAVE_STRUCT_STAT_ST_FLAGS 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 } 852c3a6ea5bSAlex Richardson #endif 8539b50d902SRodney W. Grimes 8549b50d902SRodney W. Grimes /* 8559b50d902SRodney W. Grimes * -follow functions -- 8569b50d902SRodney W. Grimes * 8579b50d902SRodney W. Grimes * Always true, causes symbolic links to be followed on a global 8589b50d902SRodney W. Grimes * basis. 8599b50d902SRodney W. Grimes */ 8609b50d902SRodney W. Grimes PLAN * 861ef646f18SMark Murray c_follow(OPTION *option, char ***argvp __unused) 8629b50d902SRodney W. Grimes { 8639b50d902SRodney W. Grimes ftsoptions &= ~FTS_PHYSICAL; 8649b50d902SRodney W. Grimes ftsoptions |= FTS_LOGICAL; 8659b50d902SRodney W. Grimes 866ea92232aSPoul-Henning Kamp return palloc(option); 8679b50d902SRodney W. Grimes } 8689b50d902SRodney W. Grimes 869c3a6ea5bSAlex Richardson #if HAVE_STRUCT_STATFS_F_FSTYPENAME 8709b50d902SRodney W. Grimes /* 8719b50d902SRodney W. Grimes * -fstype functions -- 8729b50d902SRodney W. Grimes * 8739b50d902SRodney W. Grimes * True if the file is of a certain type. 8749b50d902SRodney W. Grimes */ 8759b50d902SRodney W. Grimes int 876ef646f18SMark Murray f_fstype(PLAN *plan, FTSENT *entry) 8779b50d902SRodney W. Grimes { 8789b50d902SRodney W. Grimes static dev_t curdev; /* need a guaranteed illegal dev value */ 8799b50d902SRodney W. Grimes static int first = 1; 8809b50d902SRodney W. Grimes struct statfs sb; 8816ab780e5STai-hwa Liang static int val_flags; 8826ab780e5STai-hwa Liang static char fstype[sizeof(sb.f_fstypename)]; 8838a0a76b8SOllivier Robert char *p, save[2] = {0,0}; 8849b50d902SRodney W. Grimes 885ea92232aSPoul-Henning Kamp if ((plan->flags & F_MTMASK) == F_MTUNKNOWN) 886ea92232aSPoul-Henning Kamp return 0; 887ea92232aSPoul-Henning Kamp 8889b50d902SRodney W. Grimes /* Only check when we cross mount point. */ 8899b50d902SRodney W. Grimes if (first || curdev != entry->fts_statp->st_dev) { 8909b50d902SRodney W. Grimes curdev = entry->fts_statp->st_dev; 8919b50d902SRodney W. Grimes 8929b50d902SRodney W. Grimes /* 8939b50d902SRodney W. Grimes * Statfs follows symlinks; find wants the link's filesystem, 8949b50d902SRodney W. Grimes * not where it points. 8959b50d902SRodney W. Grimes */ 8969b50d902SRodney W. Grimes if (entry->fts_info == FTS_SL || 8979b50d902SRodney W. Grimes entry->fts_info == FTS_SLNONE) { 8989b50d902SRodney W. Grimes if ((p = strrchr(entry->fts_accpath, '/')) != NULL) 8999b50d902SRodney W. Grimes ++p; 9009b50d902SRodney W. Grimes else 9019b50d902SRodney W. Grimes p = entry->fts_accpath; 9029b50d902SRodney W. Grimes save[0] = p[0]; 9039b50d902SRodney W. Grimes p[0] = '.'; 9049b50d902SRodney W. Grimes save[1] = p[1]; 9059b50d902SRodney W. Grimes p[1] = '\0'; 9069b50d902SRodney W. Grimes } else 9079b50d902SRodney W. Grimes p = NULL; 9089b50d902SRodney W. Grimes 9090ab69d71SXin LI if (statfs(entry->fts_accpath, &sb)) { 9100ab69d71SXin LI if (!ignore_readdir_race || errno != ENOENT) { 9110ab69d71SXin LI warn("statfs: %s", entry->fts_accpath); 9120ab69d71SXin LI exitstatus = 1; 9130ab69d71SXin LI } 9140ab69d71SXin LI return 0; 9150ab69d71SXin LI } 9169b50d902SRodney W. Grimes 9179b50d902SRodney W. Grimes if (p) { 9189b50d902SRodney W. Grimes p[0] = save[0]; 9199b50d902SRodney W. Grimes p[1] = save[1]; 9209b50d902SRodney W. Grimes } 9219b50d902SRodney W. Grimes 9229b50d902SRodney W. Grimes first = 0; 923841484cdSPeter Wemm 924841484cdSPeter Wemm /* 925841484cdSPeter Wemm * Further tests may need both of these values, so 926841484cdSPeter Wemm * always copy both of them. 927841484cdSPeter Wemm */ 9289d08e419SPeter Wemm val_flags = sb.f_flags; 9296ab780e5STai-hwa Liang strlcpy(fstype, sb.f_fstypename, sizeof(fstype)); 9309b50d902SRodney W. Grimes } 931ea92232aSPoul-Henning Kamp switch (plan->flags & F_MTMASK) { 9329b50d902SRodney W. Grimes case F_MTFLAG: 933ea92232aSPoul-Henning Kamp return val_flags & plan->mt_data; 9349b50d902SRodney W. Grimes case F_MTTYPE: 9356ab780e5STai-hwa Liang return (strncmp(fstype, plan->c_data, sizeof(fstype)) == 0); 9369b50d902SRodney W. Grimes default: 9379b50d902SRodney W. Grimes abort(); 9389b50d902SRodney W. Grimes } 9399b50d902SRodney W. Grimes } 9409b50d902SRodney W. Grimes 9419b50d902SRodney W. Grimes PLAN * 942ef646f18SMark Murray c_fstype(OPTION *option, char ***argvp) 9439b50d902SRodney W. Grimes { 944ea92232aSPoul-Henning Kamp char *fsname; 945e98080b1SDavid Malone PLAN *new; 9469b50d902SRodney W. Grimes 947ea92232aSPoul-Henning Kamp fsname = nextarg(option, argvp); 9489b50d902SRodney W. Grimes ftsoptions &= ~FTS_NOSTAT; 9499b50d902SRodney W. Grimes 950ea92232aSPoul-Henning Kamp new = palloc(option); 951ea92232aSPoul-Henning Kamp switch (*fsname) { 9529b50d902SRodney W. Grimes case 'l': 953ea92232aSPoul-Henning Kamp if (!strcmp(fsname, "local")) { 954ea92232aSPoul-Henning Kamp new->flags |= F_MTFLAG; 9559b50d902SRodney W. Grimes new->mt_data = MNT_LOCAL; 956ea92232aSPoul-Henning Kamp return new; 9579b50d902SRodney W. Grimes } 9589b50d902SRodney W. Grimes break; 9599b50d902SRodney W. Grimes case 'r': 960ea92232aSPoul-Henning Kamp if (!strcmp(fsname, "rdonly")) { 961ea92232aSPoul-Henning Kamp new->flags |= F_MTFLAG; 9629b50d902SRodney W. Grimes new->mt_data = MNT_RDONLY; 963ea92232aSPoul-Henning Kamp return new; 9649b50d902SRodney W. Grimes } 9659b50d902SRodney W. Grimes break; 9669b50d902SRodney W. Grimes } 967ea92232aSPoul-Henning Kamp 9686ab780e5STai-hwa Liang new->flags |= F_MTTYPE; 9696ab780e5STai-hwa Liang new->c_data = fsname; 970ea92232aSPoul-Henning Kamp return new; 9719b50d902SRodney W. Grimes } 972c3a6ea5bSAlex Richardson #endif 9739b50d902SRodney W. Grimes 9749b50d902SRodney W. Grimes /* 9759b50d902SRodney W. Grimes * -group gname functions -- 9769b50d902SRodney W. Grimes * 9779b50d902SRodney W. Grimes * True if the file belongs to the group gname. If gname is numeric and 9789b50d902SRodney W. Grimes * an equivalent of the getgrnam() function does not return a valid group 9799b50d902SRodney W. Grimes * name, gname is taken as a group ID. 9809b50d902SRodney W. Grimes */ 9819b50d902SRodney W. Grimes int 982ef646f18SMark Murray f_group(PLAN *plan, FTSENT *entry) 9839b50d902SRodney W. Grimes { 9844ba3b38bSKirill Ponomarev COMPARE(entry->fts_statp->st_gid, plan->g_data); 9859b50d902SRodney W. Grimes } 9869b50d902SRodney W. Grimes 9879b50d902SRodney W. Grimes PLAN * 988ef646f18SMark Murray c_group(OPTION *option, char ***argvp) 9899b50d902SRodney W. Grimes { 990ea92232aSPoul-Henning Kamp char *gname; 9919b50d902SRodney W. Grimes PLAN *new; 9929b50d902SRodney W. Grimes struct group *g; 9939b50d902SRodney W. Grimes gid_t gid; 9949b50d902SRodney W. Grimes 995ea92232aSPoul-Henning Kamp gname = nextarg(option, argvp); 9969b50d902SRodney W. Grimes ftsoptions &= ~FTS_NOSTAT; 9979b50d902SRodney W. Grimes 9984ba3b38bSKirill Ponomarev new = palloc(option); 9999b50d902SRodney W. Grimes g = getgrnam(gname); 10009b50d902SRodney W. Grimes if (g == NULL) { 10014ba3b38bSKirill Ponomarev char* cp = gname; 10024ba3b38bSKirill Ponomarev if (gname[0] == '-' || gname[0] == '+') 10034ba3b38bSKirill Ponomarev gname++; 10049b50d902SRodney W. Grimes gid = atoi(gname); 10059b50d902SRodney W. Grimes if (gid == 0 && gname[0] != '0') 1006ea92232aSPoul-Henning Kamp errx(1, "%s: %s: no such group", option->name, gname); 10074ba3b38bSKirill Ponomarev gid = find_parsenum(new, option->name, cp, NULL); 10089b50d902SRodney W. Grimes } else 10099b50d902SRodney W. Grimes gid = g->gr_gid; 10109b50d902SRodney W. Grimes 10119b50d902SRodney W. Grimes new->g_data = gid; 1012ea92232aSPoul-Henning Kamp return new; 10139b50d902SRodney W. Grimes } 10149b50d902SRodney W. Grimes 10159b50d902SRodney W. Grimes /* 101640072dc2SJilles Tjoelker * -ignore_readdir_race functions -- 101740072dc2SJilles Tjoelker * 101840072dc2SJilles Tjoelker * Always true. Ignore errors which occur if a file or a directory 101940072dc2SJilles Tjoelker * in a starting point gets deleted between reading the name and calling 102040072dc2SJilles Tjoelker * stat on it while find is traversing the starting point. 102140072dc2SJilles Tjoelker */ 102240072dc2SJilles Tjoelker 102340072dc2SJilles Tjoelker PLAN * 102440072dc2SJilles Tjoelker c_ignore_readdir_race(OPTION *option, char ***argvp __unused) 102540072dc2SJilles Tjoelker { 102640072dc2SJilles Tjoelker if (strcmp(option->name, "-ignore_readdir_race") == 0) 102740072dc2SJilles Tjoelker ignore_readdir_race = 1; 102840072dc2SJilles Tjoelker else 102940072dc2SJilles Tjoelker ignore_readdir_race = 0; 103040072dc2SJilles Tjoelker 103140072dc2SJilles Tjoelker return palloc(option); 103240072dc2SJilles Tjoelker } 103340072dc2SJilles Tjoelker 103440072dc2SJilles Tjoelker /* 10359b50d902SRodney W. Grimes * -inum n functions -- 10369b50d902SRodney W. Grimes * 10379b50d902SRodney W. Grimes * True if the file has inode # n. 10389b50d902SRodney W. Grimes */ 10399b50d902SRodney W. Grimes int 1040ef646f18SMark Murray f_inum(PLAN *plan, FTSENT *entry) 10419b50d902SRodney W. Grimes { 10429b50d902SRodney W. Grimes COMPARE(entry->fts_statp->st_ino, plan->i_data); 10439b50d902SRodney W. Grimes } 10449b50d902SRodney W. Grimes 10459b50d902SRodney W. Grimes PLAN * 1046ef646f18SMark Murray c_inum(OPTION *option, char ***argvp) 10479b50d902SRodney W. Grimes { 1048ea92232aSPoul-Henning Kamp char *inum_str; 10499b50d902SRodney W. Grimes PLAN *new; 10509b50d902SRodney W. Grimes 1051ea92232aSPoul-Henning Kamp inum_str = nextarg(option, argvp); 10529b50d902SRodney W. Grimes ftsoptions &= ~FTS_NOSTAT; 10539b50d902SRodney W. Grimes 1054ea92232aSPoul-Henning Kamp new = palloc(option); 1055ea92232aSPoul-Henning Kamp new->i_data = find_parsenum(new, option->name, inum_str, NULL); 1056ea92232aSPoul-Henning Kamp return new; 10579b50d902SRodney W. Grimes } 10589b50d902SRodney W. Grimes 10599b50d902SRodney W. Grimes /* 106046b993ffSWarner Losh * -samefile FN 106146b993ffSWarner Losh * 106246b993ffSWarner Losh * True if the file has the same inode (eg hard link) FN 106346b993ffSWarner Losh */ 106446b993ffSWarner Losh 106546b993ffSWarner Losh /* f_samefile is just f_inum */ 106646b993ffSWarner Losh PLAN * 106746b993ffSWarner Losh c_samefile(OPTION *option, char ***argvp) 106846b993ffSWarner Losh { 106946b993ffSWarner Losh char *fn; 107046b993ffSWarner Losh PLAN *new; 107146b993ffSWarner Losh struct stat sb; 1072d8a0fe10SConrad Meyer int error; 107346b993ffSWarner Losh 107446b993ffSWarner Losh fn = nextarg(option, argvp); 107546b993ffSWarner Losh ftsoptions &= ~FTS_NOSTAT; 107646b993ffSWarner Losh 107746b993ffSWarner Losh new = palloc(option); 1078d8a0fe10SConrad Meyer if (ftsoptions & FTS_PHYSICAL) 1079d8a0fe10SConrad Meyer error = lstat(fn, &sb); 1080d8a0fe10SConrad Meyer else 1081d8a0fe10SConrad Meyer error = stat(fn, &sb); 1082d8a0fe10SConrad Meyer if (error != 0) 108346b993ffSWarner Losh err(1, "%s", fn); 108446b993ffSWarner Losh new->i_data = sb.st_ino; 108546b993ffSWarner Losh return new; 108646b993ffSWarner Losh } 108746b993ffSWarner Losh 108846b993ffSWarner Losh /* 10899b50d902SRodney W. Grimes * -links n functions -- 10909b50d902SRodney W. Grimes * 10919b50d902SRodney W. Grimes * True if the file has n links. 10929b50d902SRodney W. Grimes */ 10939b50d902SRodney W. Grimes int 1094ef646f18SMark Murray f_links(PLAN *plan, FTSENT *entry) 10959b50d902SRodney W. Grimes { 10969b50d902SRodney W. Grimes COMPARE(entry->fts_statp->st_nlink, plan->l_data); 10979b50d902SRodney W. Grimes } 10989b50d902SRodney W. Grimes 10999b50d902SRodney W. Grimes PLAN * 1100ef646f18SMark Murray c_links(OPTION *option, char ***argvp) 11019b50d902SRodney W. Grimes { 1102ea92232aSPoul-Henning Kamp char *nlinks; 11039b50d902SRodney W. Grimes PLAN *new; 11049b50d902SRodney W. Grimes 1105ea92232aSPoul-Henning Kamp nlinks = nextarg(option, argvp); 11069b50d902SRodney W. Grimes ftsoptions &= ~FTS_NOSTAT; 11079b50d902SRodney W. Grimes 1108ea92232aSPoul-Henning Kamp new = palloc(option); 1109ea92232aSPoul-Henning Kamp new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL); 1110ea92232aSPoul-Henning Kamp return new; 11119b50d902SRodney W. Grimes } 11129b50d902SRodney W. Grimes 11139b50d902SRodney W. Grimes /* 11149b50d902SRodney W. Grimes * -ls functions -- 11159b50d902SRodney W. Grimes * 11169b50d902SRodney W. Grimes * Always true - prints the current entry to stdout in "ls" format. 11179b50d902SRodney W. Grimes */ 11189b50d902SRodney W. Grimes int 1119ef646f18SMark Murray f_ls(PLAN *plan __unused, FTSENT *entry) 11209b50d902SRodney W. Grimes { 11219b50d902SRodney W. Grimes printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp); 1122ea92232aSPoul-Henning Kamp return 1; 11239b50d902SRodney W. Grimes } 11249b50d902SRodney W. Grimes 11259b50d902SRodney W. Grimes PLAN * 1126ef646f18SMark Murray c_ls(OPTION *option, char ***argvp __unused) 11279b50d902SRodney W. Grimes { 11289b50d902SRodney W. Grimes ftsoptions &= ~FTS_NOSTAT; 11299b50d902SRodney W. Grimes isoutput = 1; 11309b50d902SRodney W. Grimes 1131ea92232aSPoul-Henning Kamp return palloc(option); 11329b50d902SRodney W. Grimes } 11339b50d902SRodney W. Grimes 11349b50d902SRodney W. Grimes /* 11359b50d902SRodney W. Grimes * -name functions -- 11369b50d902SRodney W. Grimes * 11379b50d902SRodney W. Grimes * True if the basename of the filename being examined 11389b50d902SRodney W. Grimes * matches pattern using Pattern Matching Notation S3.14 11399b50d902SRodney W. Grimes */ 11409b50d902SRodney W. Grimes int 1141ef646f18SMark Murray f_name(PLAN *plan, FTSENT *entry) 11429b50d902SRodney W. Grimes { 1143acebb585SWarner Losh char fn[PATH_MAX]; 1144acebb585SWarner Losh const char *name; 11453e02329aSJilles Tjoelker ssize_t len; 1146acebb585SWarner Losh 1147acebb585SWarner Losh if (plan->flags & F_LINK) { 1148e810bef7SJilles Tjoelker /* 1149e810bef7SJilles Tjoelker * The below test both avoids obviously useless readlink() 1150e810bef7SJilles Tjoelker * calls and ensures that symlinks with existent target do 1151e810bef7SJilles Tjoelker * not match if symlinks are being followed. 1152e810bef7SJilles Tjoelker * Assumption: fts will stat all symlinks that are to be 1153e810bef7SJilles Tjoelker * followed and will return the stat information. 1154e810bef7SJilles Tjoelker */ 1155e810bef7SJilles Tjoelker if (entry->fts_info != FTS_NSOK && entry->fts_info != FTS_SL && 1156e810bef7SJilles Tjoelker entry->fts_info != FTS_SLNONE) 1157e810bef7SJilles Tjoelker return 0; 1158e810bef7SJilles Tjoelker len = readlink(entry->fts_accpath, fn, sizeof(fn) - 1); 11593e02329aSJilles Tjoelker if (len == -1) 116046b993ffSWarner Losh return 0; 11613e02329aSJilles Tjoelker fn[len] = '\0'; 11623e02329aSJilles Tjoelker name = fn; 1163acebb585SWarner Losh } else 1164acebb585SWarner Losh name = entry->fts_name; 1165acebb585SWarner Losh return !fnmatch(plan->c_data, name, 1166ea92232aSPoul-Henning Kamp plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0); 11679b50d902SRodney W. Grimes } 11689b50d902SRodney W. Grimes 11699b50d902SRodney W. Grimes PLAN * 1170ef646f18SMark Murray c_name(OPTION *option, char ***argvp) 11719b50d902SRodney W. Grimes { 1172ea92232aSPoul-Henning Kamp char *pattern; 11739b50d902SRodney W. Grimes PLAN *new; 11749b50d902SRodney W. Grimes 1175ea92232aSPoul-Henning Kamp pattern = nextarg(option, argvp); 1176ea92232aSPoul-Henning Kamp new = palloc(option); 11779b50d902SRodney W. Grimes new->c_data = pattern; 1178ea92232aSPoul-Henning Kamp return new; 11799b50d902SRodney W. Grimes } 11809b50d902SRodney W. Grimes 11817c1d4b3aSAkinori MUSHA /* 1182ea92232aSPoul-Henning Kamp * -newer file functions -- 11837c1d4b3aSAkinori MUSHA * 1184ea92232aSPoul-Henning Kamp * True if the current file has been modified more recently 1185ea92232aSPoul-Henning Kamp * then the modification time of the file named by the pathname 1186ea92232aSPoul-Henning Kamp * file. 11877c1d4b3aSAkinori MUSHA */ 11887c1d4b3aSAkinori MUSHA int 1189ef646f18SMark Murray f_newer(PLAN *plan, FTSENT *entry) 11907c1d4b3aSAkinori MUSHA { 1191ddd956b0SJilles Tjoelker struct timespec ft; 1192ddd956b0SJilles Tjoelker 1193ea92232aSPoul-Henning Kamp if (plan->flags & F_TIME_C) 1194ddd956b0SJilles Tjoelker ft = entry->fts_statp->st_ctim; 1195c3a6ea5bSAlex Richardson #if HAVE_STRUCT_STAT_ST_BIRTHTIME 1196ea92232aSPoul-Henning Kamp else if (plan->flags & F_TIME_A) 1197ddd956b0SJilles Tjoelker ft = entry->fts_statp->st_atim; 119831d53425SCeri Davies else if (plan->flags & F_TIME_B) 1199ddd956b0SJilles Tjoelker ft = entry->fts_statp->st_birthtim; 1200c3a6ea5bSAlex Richardson #endif 1201ea92232aSPoul-Henning Kamp else 1202ddd956b0SJilles Tjoelker ft = entry->fts_statp->st_mtim; 1203ddd956b0SJilles Tjoelker return (ft.tv_sec > plan->t_data.tv_sec || 1204ddd956b0SJilles Tjoelker (ft.tv_sec == plan->t_data.tv_sec && 1205ddd956b0SJilles Tjoelker ft.tv_nsec > plan->t_data.tv_nsec)); 12067c1d4b3aSAkinori MUSHA } 12077c1d4b3aSAkinori MUSHA 12087c1d4b3aSAkinori MUSHA PLAN * 1209ef646f18SMark Murray c_newer(OPTION *option, char ***argvp) 12107c1d4b3aSAkinori MUSHA { 1211ea92232aSPoul-Henning Kamp char *fn_or_tspec; 12127c1d4b3aSAkinori MUSHA PLAN *new; 1213ea92232aSPoul-Henning Kamp struct stat sb; 1214d8a0fe10SConrad Meyer int error; 12157c1d4b3aSAkinori MUSHA 1216ea92232aSPoul-Henning Kamp fn_or_tspec = nextarg(option, argvp); 1217ea92232aSPoul-Henning Kamp ftsoptions &= ~FTS_NOSTAT; 1218ea92232aSPoul-Henning Kamp 1219ea92232aSPoul-Henning Kamp new = palloc(option); 1220ea92232aSPoul-Henning Kamp /* compare against what */ 1221ea92232aSPoul-Henning Kamp if (option->flags & F_TIME2_T) { 1222ddd956b0SJilles Tjoelker new->t_data.tv_sec = get_date(fn_or_tspec); 1223ddd956b0SJilles Tjoelker if (new->t_data.tv_sec == (time_t) -1) 1224ea92232aSPoul-Henning Kamp errx(1, "Can't parse date/time: %s", fn_or_tspec); 1225ddd956b0SJilles Tjoelker /* Use the seconds only in the comparison. */ 1226ddd956b0SJilles Tjoelker new->t_data.tv_nsec = 999999999; 1227ea92232aSPoul-Henning Kamp } else { 1228d8a0fe10SConrad Meyer if (ftsoptions & FTS_PHYSICAL) 1229d8a0fe10SConrad Meyer error = lstat(fn_or_tspec, &sb); 1230d8a0fe10SConrad Meyer else 1231d8a0fe10SConrad Meyer error = stat(fn_or_tspec, &sb); 1232d8a0fe10SConrad Meyer if (error != 0) 1233ea92232aSPoul-Henning Kamp err(1, "%s", fn_or_tspec); 1234ea92232aSPoul-Henning Kamp if (option->flags & F_TIME2_C) 1235ddd956b0SJilles Tjoelker new->t_data = sb.st_ctim; 1236ea92232aSPoul-Henning Kamp else if (option->flags & F_TIME2_A) 1237ddd956b0SJilles Tjoelker new->t_data = sb.st_atim; 1238c3a6ea5bSAlex Richardson #if HAVE_STRUCT_STAT_ST_BIRTHTIME 12398310a1a2SGavin Atkinson else if (option->flags & F_TIME2_B) 1240ddd956b0SJilles Tjoelker new->t_data = sb.st_birthtim; 1241c3a6ea5bSAlex Richardson #endif 1242ea92232aSPoul-Henning Kamp else 1243ddd956b0SJilles Tjoelker new->t_data = sb.st_mtim; 1244ea92232aSPoul-Henning Kamp } 1245ea92232aSPoul-Henning Kamp return new; 12467c1d4b3aSAkinori MUSHA } 12477c1d4b3aSAkinori MUSHA 1248ea92232aSPoul-Henning Kamp /* 1249ea92232aSPoul-Henning Kamp * -nogroup functions -- 1250ea92232aSPoul-Henning Kamp * 1251ea92232aSPoul-Henning Kamp * True if file belongs to a user ID for which the equivalent 1252ea92232aSPoul-Henning Kamp * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL. 1253ea92232aSPoul-Henning Kamp */ 1254ea92232aSPoul-Henning Kamp int 1255ef646f18SMark Murray f_nogroup(PLAN *plan __unused, FTSENT *entry) 1256ea92232aSPoul-Henning Kamp { 1257ea92232aSPoul-Henning Kamp return group_from_gid(entry->fts_statp->st_gid, 1) == NULL; 1258ea92232aSPoul-Henning Kamp } 1259ea92232aSPoul-Henning Kamp 1260ea92232aSPoul-Henning Kamp PLAN * 1261ef646f18SMark Murray c_nogroup(OPTION *option, char ***argvp __unused) 1262ea92232aSPoul-Henning Kamp { 1263ea92232aSPoul-Henning Kamp ftsoptions &= ~FTS_NOSTAT; 1264ea92232aSPoul-Henning Kamp 1265ea92232aSPoul-Henning Kamp return palloc(option); 1266ea92232aSPoul-Henning Kamp } 1267ea92232aSPoul-Henning Kamp 1268ea92232aSPoul-Henning Kamp /* 1269ea92232aSPoul-Henning Kamp * -nouser functions -- 1270ea92232aSPoul-Henning Kamp * 1271ea92232aSPoul-Henning Kamp * True if file belongs to a user ID for which the equivalent 1272ea92232aSPoul-Henning Kamp * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL. 1273ea92232aSPoul-Henning Kamp */ 1274ea92232aSPoul-Henning Kamp int 1275ef646f18SMark Murray f_nouser(PLAN *plan __unused, FTSENT *entry) 1276ea92232aSPoul-Henning Kamp { 1277ea92232aSPoul-Henning Kamp return user_from_uid(entry->fts_statp->st_uid, 1) == NULL; 1278ea92232aSPoul-Henning Kamp } 1279ea92232aSPoul-Henning Kamp 1280ea92232aSPoul-Henning Kamp PLAN * 1281ef646f18SMark Murray c_nouser(OPTION *option, char ***argvp __unused) 1282ea92232aSPoul-Henning Kamp { 1283ea92232aSPoul-Henning Kamp ftsoptions &= ~FTS_NOSTAT; 1284ea92232aSPoul-Henning Kamp 1285ea92232aSPoul-Henning Kamp return palloc(option); 1286ea92232aSPoul-Henning Kamp } 1287ea92232aSPoul-Henning Kamp 1288ea92232aSPoul-Henning Kamp /* 1289ea92232aSPoul-Henning Kamp * -path functions -- 1290ea92232aSPoul-Henning Kamp * 1291ea92232aSPoul-Henning Kamp * True if the path of the filename being examined 1292ea92232aSPoul-Henning Kamp * matches pattern using Pattern Matching Notation S3.14 1293ea92232aSPoul-Henning Kamp */ 1294ea92232aSPoul-Henning Kamp int 1295ef646f18SMark Murray f_path(PLAN *plan, FTSENT *entry) 1296ea92232aSPoul-Henning Kamp { 1297ea92232aSPoul-Henning Kamp return !fnmatch(plan->c_data, entry->fts_path, 1298ea92232aSPoul-Henning Kamp plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0); 1299ea92232aSPoul-Henning Kamp } 1300ea92232aSPoul-Henning Kamp 1301ea92232aSPoul-Henning Kamp /* c_path is the same as c_name */ 1302ea92232aSPoul-Henning Kamp 1303ea92232aSPoul-Henning Kamp /* 1304ea92232aSPoul-Henning Kamp * -perm functions -- 1305ea92232aSPoul-Henning Kamp * 1306ea92232aSPoul-Henning Kamp * The mode argument is used to represent file mode bits. If it starts 1307ea92232aSPoul-Henning Kamp * with a leading digit, it's treated as an octal mode, otherwise as a 1308ea92232aSPoul-Henning Kamp * symbolic mode. 1309ea92232aSPoul-Henning Kamp */ 1310ea92232aSPoul-Henning Kamp int 1311ef646f18SMark Murray f_perm(PLAN *plan, FTSENT *entry) 1312ea92232aSPoul-Henning Kamp { 1313ea92232aSPoul-Henning Kamp mode_t mode; 1314ea92232aSPoul-Henning Kamp 1315ea92232aSPoul-Henning Kamp mode = entry->fts_statp->st_mode & 1316ea92232aSPoul-Henning Kamp (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO); 1317ea92232aSPoul-Henning Kamp if (plan->flags & F_ATLEAST) 1318ea92232aSPoul-Henning Kamp return (plan->m_data | mode) == mode; 1319c0ff9709SRuslan Ermilov else if (plan->flags & F_ANY) 1320c0ff9709SRuslan Ermilov return (mode & plan->m_data); 1321ea92232aSPoul-Henning Kamp else 1322ea92232aSPoul-Henning Kamp return mode == plan->m_data; 1323ea92232aSPoul-Henning Kamp /* NOTREACHED */ 1324ea92232aSPoul-Henning Kamp } 1325ea92232aSPoul-Henning Kamp 1326ea92232aSPoul-Henning Kamp PLAN * 1327ef646f18SMark Murray c_perm(OPTION *option, char ***argvp) 1328ea92232aSPoul-Henning Kamp { 1329ea92232aSPoul-Henning Kamp char *perm; 1330ea92232aSPoul-Henning Kamp PLAN *new; 1331ea92232aSPoul-Henning Kamp mode_t *set; 1332ea92232aSPoul-Henning Kamp 1333ea92232aSPoul-Henning Kamp perm = nextarg(option, argvp); 1334ea92232aSPoul-Henning Kamp ftsoptions &= ~FTS_NOSTAT; 1335ea92232aSPoul-Henning Kamp 1336ea92232aSPoul-Henning Kamp new = palloc(option); 1337ea92232aSPoul-Henning Kamp 1338ea92232aSPoul-Henning Kamp if (*perm == '-') { 1339ea92232aSPoul-Henning Kamp new->flags |= F_ATLEAST; 1340ea92232aSPoul-Henning Kamp ++perm; 1341*2a121b97SRicardo Branco } else if (*perm == '+' || *perm == '/') { 1342ea92232aSPoul-Henning Kamp new->flags |= F_ANY; 1343ea92232aSPoul-Henning Kamp ++perm; 1344ea92232aSPoul-Henning Kamp } 1345ea92232aSPoul-Henning Kamp 1346ea92232aSPoul-Henning Kamp if ((set = setmode(perm)) == NULL) 1347ea92232aSPoul-Henning Kamp errx(1, "%s: %s: illegal mode string", option->name, perm); 1348ea92232aSPoul-Henning Kamp 1349ea92232aSPoul-Henning Kamp new->m_data = getmode(set, 0); 1350ea92232aSPoul-Henning Kamp free(set); 1351ea92232aSPoul-Henning Kamp return new; 1352ea92232aSPoul-Henning Kamp } 1353ea92232aSPoul-Henning Kamp 1354ea92232aSPoul-Henning Kamp /* 1355ea92232aSPoul-Henning Kamp * -print functions -- 1356ea92232aSPoul-Henning Kamp * 13579725a7b9SPhilippe Charnier * Always true, causes the current pathname to be written to 1358ea92232aSPoul-Henning Kamp * standard output. 1359ea92232aSPoul-Henning Kamp */ 1360ea92232aSPoul-Henning Kamp int 1361ef646f18SMark Murray f_print(PLAN *plan __unused, FTSENT *entry) 1362ea92232aSPoul-Henning Kamp { 1363ea92232aSPoul-Henning Kamp (void)puts(entry->fts_path); 1364ea92232aSPoul-Henning Kamp return 1; 1365ea92232aSPoul-Henning Kamp } 1366ea92232aSPoul-Henning Kamp 1367ea92232aSPoul-Henning Kamp PLAN * 1368ef646f18SMark Murray c_print(OPTION *option, char ***argvp __unused) 1369ea92232aSPoul-Henning Kamp { 1370ea92232aSPoul-Henning Kamp isoutput = 1; 1371ea92232aSPoul-Henning Kamp 1372ea92232aSPoul-Henning Kamp return palloc(option); 1373ea92232aSPoul-Henning Kamp } 1374ea92232aSPoul-Henning Kamp 1375ea92232aSPoul-Henning Kamp /* 1376ea92232aSPoul-Henning Kamp * -print0 functions -- 1377ea92232aSPoul-Henning Kamp * 13789725a7b9SPhilippe Charnier * Always true, causes the current pathname to be written to 1379ea92232aSPoul-Henning Kamp * standard output followed by a NUL character 1380ea92232aSPoul-Henning Kamp */ 1381ea92232aSPoul-Henning Kamp int 1382ef646f18SMark Murray f_print0(PLAN *plan __unused, FTSENT *entry) 1383ea92232aSPoul-Henning Kamp { 1384ea92232aSPoul-Henning Kamp fputs(entry->fts_path, stdout); 1385ea92232aSPoul-Henning Kamp fputc('\0', stdout); 1386ea92232aSPoul-Henning Kamp return 1; 1387ea92232aSPoul-Henning Kamp } 1388ea92232aSPoul-Henning Kamp 1389ea92232aSPoul-Henning Kamp /* c_print0 is the same as c_print */ 1390ea92232aSPoul-Henning Kamp 1391ea92232aSPoul-Henning Kamp /* 1392ea92232aSPoul-Henning Kamp * -prune functions -- 1393ea92232aSPoul-Henning Kamp * 1394ea92232aSPoul-Henning Kamp * Prune a portion of the hierarchy. 1395ea92232aSPoul-Henning Kamp */ 1396ea92232aSPoul-Henning Kamp int 1397ef646f18SMark Murray f_prune(PLAN *plan __unused, FTSENT *entry) 1398ea92232aSPoul-Henning Kamp { 1399ea92232aSPoul-Henning Kamp if (fts_set(tree, entry, FTS_SKIP)) 1400ea92232aSPoul-Henning Kamp err(1, "%s", entry->fts_path); 1401ea92232aSPoul-Henning Kamp return 1; 1402ea92232aSPoul-Henning Kamp } 1403ea92232aSPoul-Henning Kamp 1404ea92232aSPoul-Henning Kamp /* c_prune == c_simple */ 14057c1d4b3aSAkinori MUSHA 14067c1d4b3aSAkinori MUSHA /* 14077c1d4b3aSAkinori MUSHA * -regex functions -- 14087c1d4b3aSAkinori MUSHA * 14097c1d4b3aSAkinori MUSHA * True if the whole path of the file matches pattern using 14107c1d4b3aSAkinori MUSHA * regular expression. 14117c1d4b3aSAkinori MUSHA */ 14127c1d4b3aSAkinori MUSHA int 1413ef646f18SMark Murray f_regex(PLAN *plan, FTSENT *entry) 14147c1d4b3aSAkinori MUSHA { 14157c1d4b3aSAkinori MUSHA char *str; 141647bca8b0SJuli Mallett int len; 14177c1d4b3aSAkinori MUSHA regex_t *pre; 14187c1d4b3aSAkinori MUSHA regmatch_t pmatch; 14197c1d4b3aSAkinori MUSHA int errcode; 14207c1d4b3aSAkinori MUSHA char errbuf[LINE_MAX]; 14217c1d4b3aSAkinori MUSHA int matched; 14227c1d4b3aSAkinori MUSHA 14237c1d4b3aSAkinori MUSHA pre = plan->re_data; 14247c1d4b3aSAkinori MUSHA str = entry->fts_path; 14257c1d4b3aSAkinori MUSHA len = strlen(str); 14267c1d4b3aSAkinori MUSHA matched = 0; 14277c1d4b3aSAkinori MUSHA 14287c1d4b3aSAkinori MUSHA pmatch.rm_so = 0; 14297c1d4b3aSAkinori MUSHA pmatch.rm_eo = len; 14307c1d4b3aSAkinori MUSHA 14317c1d4b3aSAkinori MUSHA errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND); 14327c1d4b3aSAkinori MUSHA 14337c1d4b3aSAkinori MUSHA if (errcode != 0 && errcode != REG_NOMATCH) { 14347c1d4b3aSAkinori MUSHA regerror(errcode, pre, errbuf, sizeof errbuf); 14357c1d4b3aSAkinori MUSHA errx(1, "%s: %s", 1436ea92232aSPoul-Henning Kamp plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf); 14377c1d4b3aSAkinori MUSHA } 14387c1d4b3aSAkinori MUSHA 14397c1d4b3aSAkinori MUSHA if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len) 14407c1d4b3aSAkinori MUSHA matched = 1; 14417c1d4b3aSAkinori MUSHA 1442ea92232aSPoul-Henning Kamp return matched; 14437c1d4b3aSAkinori MUSHA } 14447c1d4b3aSAkinori MUSHA 14457c1d4b3aSAkinori MUSHA PLAN * 1446ef646f18SMark Murray c_regex(OPTION *option, char ***argvp) 14477c1d4b3aSAkinori MUSHA { 14487c1d4b3aSAkinori MUSHA PLAN *new; 1449ea92232aSPoul-Henning Kamp char *pattern; 14507c1d4b3aSAkinori MUSHA regex_t *pre; 14517c1d4b3aSAkinori MUSHA int errcode; 14527c1d4b3aSAkinori MUSHA char errbuf[LINE_MAX]; 14537c1d4b3aSAkinori MUSHA 14547c1d4b3aSAkinori MUSHA if ((pre = malloc(sizeof(regex_t))) == NULL) 14557c1d4b3aSAkinori MUSHA err(1, NULL); 14567c1d4b3aSAkinori MUSHA 1457ea92232aSPoul-Henning Kamp pattern = nextarg(option, argvp); 1458ea92232aSPoul-Henning Kamp 1459ea92232aSPoul-Henning Kamp if ((errcode = regcomp(pre, pattern, 1460ea92232aSPoul-Henning Kamp regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) { 14617c1d4b3aSAkinori MUSHA regerror(errcode, pre, errbuf, sizeof errbuf); 14627c1d4b3aSAkinori MUSHA errx(1, "%s: %s: %s", 1463ea92232aSPoul-Henning Kamp option->flags & F_IGNCASE ? "-iregex" : "-regex", 1464ea92232aSPoul-Henning Kamp pattern, errbuf); 14657c1d4b3aSAkinori MUSHA } 14667c1d4b3aSAkinori MUSHA 1467ea92232aSPoul-Henning Kamp new = palloc(option); 14687c1d4b3aSAkinori MUSHA new->re_data = pre; 14697c1d4b3aSAkinori MUSHA 1470567664c4SOllivier Robert return new; 1471567664c4SOllivier Robert } 1472567664c4SOllivier Robert 147346b993ffSWarner Losh /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or, c_true, c_false */ 14749b50d902SRodney W. Grimes 14759b50d902SRodney W. Grimes PLAN * 1476ef646f18SMark Murray c_simple(OPTION *option, char ***argvp __unused) 14779b50d902SRodney W. Grimes { 1478ea92232aSPoul-Henning Kamp return palloc(option); 14799b50d902SRodney W. Grimes } 14809b50d902SRodney W. Grimes 14819b50d902SRodney W. Grimes /* 14829b50d902SRodney W. Grimes * -size n[c] functions -- 14839b50d902SRodney W. Grimes * 14849b50d902SRodney W. Grimes * True if the file size in bytes, divided by an implementation defined 14859b50d902SRodney W. Grimes * value and rounded up to the next integer, is n. If n is followed by 14865a890aacSKirill Ponomarev * one of c k M G T P, the size is in bytes, kilobytes, 14875a890aacSKirill Ponomarev * megabytes, gigabytes, terabytes or petabytes respectively. 14889b50d902SRodney W. Grimes */ 14899b50d902SRodney W. Grimes #define FIND_SIZE 512 14909b50d902SRodney W. Grimes static int divsize = 1; 14919b50d902SRodney W. Grimes 14929b50d902SRodney W. Grimes int 1493ef646f18SMark Murray f_size(PLAN *plan, FTSENT *entry) 14949b50d902SRodney W. Grimes { 14959b50d902SRodney W. Grimes off_t size; 14969b50d902SRodney W. Grimes 14979b50d902SRodney W. Grimes size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) / 14989b50d902SRodney W. Grimes FIND_SIZE : entry->fts_statp->st_size; 14999b50d902SRodney W. Grimes COMPARE(size, plan->o_data); 15009b50d902SRodney W. Grimes } 15019b50d902SRodney W. Grimes 15029b50d902SRodney W. Grimes PLAN * 1503ef646f18SMark Murray c_size(OPTION *option, char ***argvp) 15049b50d902SRodney W. Grimes { 1505ea92232aSPoul-Henning Kamp char *size_str; 15069b50d902SRodney W. Grimes PLAN *new; 15079b50d902SRodney W. Grimes char endch; 15085a890aacSKirill Ponomarev off_t scale; 15099b50d902SRodney W. Grimes 1510ea92232aSPoul-Henning Kamp size_str = nextarg(option, argvp); 15119b50d902SRodney W. Grimes ftsoptions &= ~FTS_NOSTAT; 15129b50d902SRodney W. Grimes 1513ea92232aSPoul-Henning Kamp new = palloc(option); 15149b50d902SRodney W. Grimes endch = 'c'; 1515ea92232aSPoul-Henning Kamp new->o_data = find_parsenum(new, option->name, size_str, &endch); 15165a890aacSKirill Ponomarev if (endch != '\0') { 15179b50d902SRodney W. Grimes divsize = 0; 15185a890aacSKirill Ponomarev 15195a890aacSKirill Ponomarev switch (endch) { 15205a890aacSKirill Ponomarev case 'c': /* characters */ 15215a890aacSKirill Ponomarev scale = 0x1LL; 15225a890aacSKirill Ponomarev break; 15235a890aacSKirill Ponomarev case 'k': /* kilobytes 1<<10 */ 15245a890aacSKirill Ponomarev scale = 0x400LL; 15255a890aacSKirill Ponomarev break; 15265a890aacSKirill Ponomarev case 'M': /* megabytes 1<<20 */ 15275a890aacSKirill Ponomarev scale = 0x100000LL; 15285a890aacSKirill Ponomarev break; 15295a890aacSKirill Ponomarev case 'G': /* gigabytes 1<<30 */ 15305a890aacSKirill Ponomarev scale = 0x40000000LL; 15315a890aacSKirill Ponomarev break; 15325a890aacSKirill Ponomarev case 'T': /* terabytes 1<<40 */ 1533599df3efSEd Maste scale = 0x10000000000LL; 15345a890aacSKirill Ponomarev break; 15355a890aacSKirill Ponomarev case 'P': /* petabytes 1<<50 */ 15365a890aacSKirill Ponomarev scale = 0x4000000000000LL; 15375a890aacSKirill Ponomarev break; 15385a890aacSKirill Ponomarev default: 15395a890aacSKirill Ponomarev errx(1, "%s: %s: illegal trailing character", 15405a890aacSKirill Ponomarev option->name, size_str); 15415a890aacSKirill Ponomarev break; 15425a890aacSKirill Ponomarev } 15435a890aacSKirill Ponomarev if (new->o_data > QUAD_MAX / scale) 15445a890aacSKirill Ponomarev errx(1, "%s: %s: value too large", 15455a890aacSKirill Ponomarev option->name, size_str); 15465a890aacSKirill Ponomarev new->o_data *= scale; 15475a890aacSKirill Ponomarev } 1548ea92232aSPoul-Henning Kamp return new; 15499b50d902SRodney W. Grimes } 15509b50d902SRodney W. Grimes 15519b50d902SRodney W. Grimes /* 15529ed0c92cSDavid Malone * -sparse functions -- 15539ed0c92cSDavid Malone * 15549ed0c92cSDavid Malone * Check if a file is sparse by finding if it occupies fewer blocks 15559ed0c92cSDavid Malone * than we expect based on its size. 15569ed0c92cSDavid Malone */ 15579ed0c92cSDavid Malone int 15589ed0c92cSDavid Malone f_sparse(PLAN *plan __unused, FTSENT *entry) 15599ed0c92cSDavid Malone { 15609ed0c92cSDavid Malone off_t expected_blocks; 15619ed0c92cSDavid Malone 15629ed0c92cSDavid Malone expected_blocks = (entry->fts_statp->st_size + 511) / 512; 15639ed0c92cSDavid Malone return entry->fts_statp->st_blocks < expected_blocks; 15649ed0c92cSDavid Malone } 15659ed0c92cSDavid Malone 15669ed0c92cSDavid Malone PLAN * 15679ed0c92cSDavid Malone c_sparse(OPTION *option, char ***argvp __unused) 15689ed0c92cSDavid Malone { 15699ed0c92cSDavid Malone ftsoptions &= ~FTS_NOSTAT; 15709ed0c92cSDavid Malone 15719ed0c92cSDavid Malone return palloc(option); 15729ed0c92cSDavid Malone } 15739ed0c92cSDavid Malone 15749ed0c92cSDavid Malone /* 15759b50d902SRodney W. Grimes * -type c functions -- 15769b50d902SRodney W. Grimes * 1577841484cdSPeter Wemm * True if the type of the file is c, where c is b, c, d, p, f or w 1578841484cdSPeter Wemm * for block special file, character special file, directory, FIFO, 1579841484cdSPeter Wemm * regular file or whiteout respectively. 15809b50d902SRodney W. Grimes */ 15819b50d902SRodney W. Grimes int 1582ef646f18SMark Murray f_type(PLAN *plan, FTSENT *entry) 15839b50d902SRodney W. Grimes { 1584b95de98aSJilles Tjoelker if (plan->m_data == S_IFDIR) 1585b95de98aSJilles Tjoelker return (entry->fts_info == FTS_D || entry->fts_info == FTS_DC || 1586b95de98aSJilles Tjoelker entry->fts_info == FTS_DNR || entry->fts_info == FTS_DOT || 1587b95de98aSJilles Tjoelker entry->fts_info == FTS_DP); 1588b95de98aSJilles Tjoelker else 1589ea92232aSPoul-Henning Kamp return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data; 15909b50d902SRodney W. Grimes } 15919b50d902SRodney W. Grimes 15929b50d902SRodney W. Grimes PLAN * 1593ef646f18SMark Murray c_type(OPTION *option, char ***argvp) 15949b50d902SRodney W. Grimes { 1595ea92232aSPoul-Henning Kamp char *typestring; 15969b50d902SRodney W. Grimes PLAN *new; 15979b50d902SRodney W. Grimes mode_t mask; 15989b50d902SRodney W. Grimes 1599ea92232aSPoul-Henning Kamp typestring = nextarg(option, argvp); 1600b95de98aSJilles Tjoelker if (typestring[0] != 'd') 16019b50d902SRodney W. Grimes ftsoptions &= ~FTS_NOSTAT; 16029b50d902SRodney W. Grimes 16039b50d902SRodney W. Grimes switch (typestring[0]) { 16049b50d902SRodney W. Grimes case 'b': 16059b50d902SRodney W. Grimes mask = S_IFBLK; 16069b50d902SRodney W. Grimes break; 16079b50d902SRodney W. Grimes case 'c': 16089b50d902SRodney W. Grimes mask = S_IFCHR; 16099b50d902SRodney W. Grimes break; 16109b50d902SRodney W. Grimes case 'd': 16119b50d902SRodney W. Grimes mask = S_IFDIR; 16129b50d902SRodney W. Grimes break; 16139b50d902SRodney W. Grimes case 'f': 16149b50d902SRodney W. Grimes mask = S_IFREG; 16159b50d902SRodney W. Grimes break; 16169b50d902SRodney W. Grimes case 'l': 16179b50d902SRodney W. Grimes mask = S_IFLNK; 16189b50d902SRodney W. Grimes break; 16199b50d902SRodney W. Grimes case 'p': 16209b50d902SRodney W. Grimes mask = S_IFIFO; 16219b50d902SRodney W. Grimes break; 16229b50d902SRodney W. Grimes case 's': 16239b50d902SRodney W. Grimes mask = S_IFSOCK; 16249b50d902SRodney W. Grimes break; 1625c3a6ea5bSAlex Richardson #if defined(FTS_WHITEOUT) && defined(S_IFWHT) 1626841484cdSPeter Wemm case 'w': 1627841484cdSPeter Wemm mask = S_IFWHT; 1628841484cdSPeter Wemm ftsoptions |= FTS_WHITEOUT; 1629841484cdSPeter Wemm break; 1630841484cdSPeter Wemm #endif /* FTS_WHITEOUT */ 16319b50d902SRodney W. Grimes default: 1632ea92232aSPoul-Henning Kamp errx(1, "%s: %s: unknown type", option->name, typestring); 16339b50d902SRodney W. Grimes } 16349b50d902SRodney W. Grimes 1635ea92232aSPoul-Henning Kamp new = palloc(option); 16369b50d902SRodney W. Grimes new->m_data = mask; 1637ea92232aSPoul-Henning Kamp return new; 1638abacbbbfSPeter Wemm } 1639abacbbbfSPeter Wemm 1640abacbbbfSPeter Wemm /* 16419b50d902SRodney W. Grimes * -user uname functions -- 16429b50d902SRodney W. Grimes * 16439b50d902SRodney W. Grimes * True if the file belongs to the user uname. If uname is numeric and 16449b50d902SRodney W. Grimes * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not 16459b50d902SRodney W. Grimes * return a valid user name, uname is taken as a user ID. 16469b50d902SRodney W. Grimes */ 16479b50d902SRodney W. Grimes int 1648ef646f18SMark Murray f_user(PLAN *plan, FTSENT *entry) 16499b50d902SRodney W. Grimes { 16504ba3b38bSKirill Ponomarev COMPARE(entry->fts_statp->st_uid, plan->u_data); 16519b50d902SRodney W. Grimes } 16529b50d902SRodney W. Grimes 16539b50d902SRodney W. Grimes PLAN * 1654ef646f18SMark Murray c_user(OPTION *option, char ***argvp) 16559b50d902SRodney W. Grimes { 1656ea92232aSPoul-Henning Kamp char *username; 16579b50d902SRodney W. Grimes PLAN *new; 16589b50d902SRodney W. Grimes struct passwd *p; 16599b50d902SRodney W. Grimes uid_t uid; 16609b50d902SRodney W. Grimes 1661ea92232aSPoul-Henning Kamp username = nextarg(option, argvp); 16629b50d902SRodney W. Grimes ftsoptions &= ~FTS_NOSTAT; 16639b50d902SRodney W. Grimes 16644ba3b38bSKirill Ponomarev new = palloc(option); 16659b50d902SRodney W. Grimes p = getpwnam(username); 16669b50d902SRodney W. Grimes if (p == NULL) { 16674ba3b38bSKirill Ponomarev char* cp = username; 16684ba3b38bSKirill Ponomarev if( username[0] == '-' || username[0] == '+' ) 16694ba3b38bSKirill Ponomarev username++; 16709b50d902SRodney W. Grimes uid = atoi(username); 16719b50d902SRodney W. Grimes if (uid == 0 && username[0] != '0') 1672ea92232aSPoul-Henning Kamp errx(1, "%s: %s: no such user", option->name, username); 16734ba3b38bSKirill Ponomarev uid = find_parsenum(new, option->name, cp, NULL); 16749b50d902SRodney W. Grimes } else 16759b50d902SRodney W. Grimes uid = p->pw_uid; 16769b50d902SRodney W. Grimes 16779b50d902SRodney W. Grimes new->u_data = uid; 1678ea92232aSPoul-Henning Kamp return new; 16799b50d902SRodney W. Grimes } 16809b50d902SRodney W. Grimes 16819b50d902SRodney W. Grimes /* 16829b50d902SRodney W. Grimes * -xdev functions -- 16839b50d902SRodney W. Grimes * 16849725a7b9SPhilippe Charnier * Always true, causes find not to descend past directories that have a 16859b50d902SRodney W. Grimes * different device ID (st_dev, see stat() S5.6.2 [POSIX.1]) 16869b50d902SRodney W. Grimes */ 16879b50d902SRodney W. Grimes PLAN * 1688ef646f18SMark Murray c_xdev(OPTION *option, char ***argvp __unused) 16899b50d902SRodney W. Grimes { 16909b50d902SRodney W. Grimes ftsoptions |= FTS_XDEV; 16919b50d902SRodney W. Grimes 1692ea92232aSPoul-Henning Kamp return palloc(option); 16939b50d902SRodney W. Grimes } 16949b50d902SRodney W. Grimes 16959b50d902SRodney W. Grimes /* 16969b50d902SRodney W. Grimes * ( expression ) functions -- 16979b50d902SRodney W. Grimes * 16989b50d902SRodney W. Grimes * True if expression is true. 16999b50d902SRodney W. Grimes */ 17009b50d902SRodney W. Grimes int 1701ef646f18SMark Murray f_expr(PLAN *plan, FTSENT *entry) 17029b50d902SRodney W. Grimes { 1703e98080b1SDavid Malone PLAN *p; 1704e98080b1SDavid Malone int state = 0; 17059b50d902SRodney W. Grimes 17069b50d902SRodney W. Grimes for (p = plan->p_data[0]; 1707ea92232aSPoul-Henning Kamp p && (state = (p->execute)(p, entry)); p = p->next); 1708ea92232aSPoul-Henning Kamp return state; 17099b50d902SRodney W. Grimes } 17109b50d902SRodney W. Grimes 17119b50d902SRodney W. Grimes /* 1712ea92232aSPoul-Henning Kamp * f_openparen and f_closeparen nodes are temporary place markers. They are 17139b50d902SRodney W. Grimes * eliminated during phase 2 of find_formplan() --- the '(' node is converted 1714ea92232aSPoul-Henning Kamp * to a f_expr node containing the expression and the ')' node is discarded. 1715ea92232aSPoul-Henning Kamp * The functions themselves are only used as constants. 17169b50d902SRodney W. Grimes */ 1717ea92232aSPoul-Henning Kamp 1718ea92232aSPoul-Henning Kamp int 1719ef646f18SMark Murray f_openparen(PLAN *plan __unused, FTSENT *entry __unused) 17209b50d902SRodney W. Grimes { 1721ea92232aSPoul-Henning Kamp abort(); 17229b50d902SRodney W. Grimes } 17239b50d902SRodney W. Grimes 1724ea92232aSPoul-Henning Kamp int 1725ef646f18SMark Murray f_closeparen(PLAN *plan __unused, FTSENT *entry __unused) 17269b50d902SRodney W. Grimes { 1727ea92232aSPoul-Henning Kamp abort(); 1728ea92232aSPoul-Henning Kamp } 1729ea92232aSPoul-Henning Kamp 1730ea92232aSPoul-Henning Kamp /* c_openparen == c_simple */ 1731ea92232aSPoul-Henning Kamp /* c_closeparen == c_simple */ 1732ea92232aSPoul-Henning Kamp 1733ea92232aSPoul-Henning Kamp /* 1734ea92232aSPoul-Henning Kamp * AND operator. Since AND is implicit, no node is allocated. 1735ea92232aSPoul-Henning Kamp */ 1736ea92232aSPoul-Henning Kamp PLAN * 1737ef646f18SMark Murray c_and(OPTION *option __unused, char ***argvp __unused) 1738ea92232aSPoul-Henning Kamp { 1739ea92232aSPoul-Henning Kamp return NULL; 17409b50d902SRodney W. Grimes } 17419b50d902SRodney W. Grimes 17429b50d902SRodney W. Grimes /* 17439b50d902SRodney W. Grimes * ! expression functions -- 17449b50d902SRodney W. Grimes * 17459b50d902SRodney W. Grimes * Negation of a primary; the unary NOT operator. 17469b50d902SRodney W. Grimes */ 17479b50d902SRodney W. Grimes int 1748ef646f18SMark Murray f_not(PLAN *plan, FTSENT *entry) 17499b50d902SRodney W. Grimes { 1750e98080b1SDavid Malone PLAN *p; 1751e98080b1SDavid Malone int state = 0; 17529b50d902SRodney W. Grimes 17539b50d902SRodney W. Grimes for (p = plan->p_data[0]; 1754ea92232aSPoul-Henning Kamp p && (state = (p->execute)(p, entry)); p = p->next); 1755ea92232aSPoul-Henning Kamp return !state; 17569b50d902SRodney W. Grimes } 17579b50d902SRodney W. Grimes 1758ea92232aSPoul-Henning Kamp /* c_not == c_simple */ 17599b50d902SRodney W. Grimes 17609b50d902SRodney W. Grimes /* 17619b50d902SRodney W. Grimes * expression -o expression functions -- 17629b50d902SRodney W. Grimes * 17639b50d902SRodney W. Grimes * Alternation of primaries; the OR operator. The second expression is 17649b50d902SRodney W. Grimes * not evaluated if the first expression is true. 17659b50d902SRodney W. Grimes */ 17669b50d902SRodney W. Grimes int 1767ef646f18SMark Murray f_or(PLAN *plan, FTSENT *entry) 17689b50d902SRodney W. Grimes { 1769e98080b1SDavid Malone PLAN *p; 1770e98080b1SDavid Malone int state = 0; 17719b50d902SRodney W. Grimes 17729b50d902SRodney W. Grimes for (p = plan->p_data[0]; 1773ea92232aSPoul-Henning Kamp p && (state = (p->execute)(p, entry)); p = p->next); 17749b50d902SRodney W. Grimes 17759b50d902SRodney W. Grimes if (state) 1776ea92232aSPoul-Henning Kamp return 1; 17779b50d902SRodney W. Grimes 17789b50d902SRodney W. Grimes for (p = plan->p_data[1]; 1779ea92232aSPoul-Henning Kamp p && (state = (p->execute)(p, entry)); p = p->next); 1780ea92232aSPoul-Henning Kamp return state; 17819b50d902SRodney W. Grimes } 17829b50d902SRodney W. Grimes 1783ea92232aSPoul-Henning Kamp /* c_or == c_simple */ 178446b993ffSWarner Losh 178546b993ffSWarner Losh /* 178646b993ffSWarner Losh * -false 178746b993ffSWarner Losh * 178846b993ffSWarner Losh * Always false. 178946b993ffSWarner Losh */ 179046b993ffSWarner Losh int 179146b993ffSWarner Losh f_false(PLAN *plan __unused, FTSENT *entry __unused) 179246b993ffSWarner Losh { 179346b993ffSWarner Losh return 0; 179446b993ffSWarner Losh } 179546b993ffSWarner Losh 179646b993ffSWarner Losh /* c_false == c_simple */ 179746b993ffSWarner Losh 179846b993ffSWarner Losh /* 179946b993ffSWarner Losh * -quit 180046b993ffSWarner Losh * 180146b993ffSWarner Losh * Exits the program 180246b993ffSWarner Losh */ 180346b993ffSWarner Losh int 180446b993ffSWarner Losh f_quit(PLAN *plan __unused, FTSENT *entry __unused) 180546b993ffSWarner Losh { 1806cf599562SJilles Tjoelker finish_execplus(); 1807b547523eSJilles Tjoelker exit(exitstatus); 180846b993ffSWarner Losh } 180946b993ffSWarner Losh 181046b993ffSWarner Losh /* c_quit == c_simple */ 1811