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