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