function.c revision 1.28 1 1.28 lukem /* $NetBSD: function.c,v 1.28 1999/01/12 00:18:50 lukem Exp $ */
2 1.18 tls
3 1.1 cgd /*-
4 1.10 jtc * Copyright (c) 1990, 1993
5 1.10 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Cimarron D. Taylor of the University of California, Berkeley.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.1 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1 cgd * must display the following acknowledgement:
20 1.1 cgd * This product includes software developed by the University of
21 1.1 cgd * California, Berkeley and its contributors.
22 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1 cgd * may be used to endorse or promote products derived from this software
24 1.1 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 cgd * SUCH DAMAGE.
37 1.1 cgd */
38 1.1 cgd
39 1.21 lukem #include <sys/cdefs.h>
40 1.1 cgd #ifndef lint
41 1.21 lukem #if 0
42 1.22 mrg static char sccsid[] = "from: @(#)function.c 8.10 (Berkeley) 5/4/95";
43 1.21 lukem #else
44 1.28 lukem __RCSID("$NetBSD: function.c,v 1.28 1999/01/12 00:18:50 lukem Exp $");
45 1.21 lukem #endif
46 1.1 cgd #endif /* not lint */
47 1.1 cgd
48 1.1 cgd #include <sys/param.h>
49 1.1 cgd #include <sys/stat.h>
50 1.1 cgd #include <sys/wait.h>
51 1.1 cgd #include <sys/mount.h>
52 1.10 jtc
53 1.10 jtc #include <err.h>
54 1.1 cgd #include <errno.h>
55 1.10 jtc #include <fnmatch.h>
56 1.10 jtc #include <fts.h>
57 1.1 cgd #include <grp.h>
58 1.1 cgd #include <pwd.h>
59 1.1 cgd #include <stdio.h>
60 1.1 cgd #include <stdlib.h>
61 1.1 cgd #include <string.h>
62 1.10 jtc #include <tzfile.h>
63 1.10 jtc #include <unistd.h>
64 1.10 jtc
65 1.1 cgd #include "find.h"
66 1.27 lukem #include "stat_flags.h"
67 1.1 cgd
68 1.10 jtc #define COMPARE(a, b) { \
69 1.10 jtc switch (plan->flags) { \
70 1.10 jtc case F_EQUAL: \
71 1.10 jtc return (a == b); \
72 1.10 jtc case F_LESSTHAN: \
73 1.10 jtc return (a < b); \
74 1.10 jtc case F_GREATER: \
75 1.10 jtc return (a > b); \
76 1.10 jtc default: \
77 1.10 jtc abort(); \
78 1.10 jtc } \
79 1.1 cgd }
80 1.1 cgd
81 1.21 lukem static long find_parsenum __P((PLAN *, char *, char *, char *));
82 1.21 lukem int f_always_true __P((PLAN *, FTSENT *));
83 1.21 lukem int f_atime __P((PLAN *, FTSENT *));
84 1.21 lukem int f_ctime __P((PLAN *, FTSENT *));
85 1.21 lukem int f_exec __P((PLAN *, FTSENT *));
86 1.27 lukem int f_flags __P((PLAN *, FTSENT *));
87 1.21 lukem int f_fstype __P((PLAN *, FTSENT *));
88 1.21 lukem int f_group __P((PLAN *, FTSENT *));
89 1.21 lukem int f_inum __P((PLAN *, FTSENT *));
90 1.21 lukem int f_links __P((PLAN *, FTSENT *));
91 1.21 lukem int f_ls __P((PLAN *, FTSENT *));
92 1.21 lukem int f_mtime __P((PLAN *, FTSENT *));
93 1.21 lukem int f_name __P((PLAN *, FTSENT *));
94 1.21 lukem int f_newer __P((PLAN *, FTSENT *));
95 1.21 lukem int f_nogroup __P((PLAN *, FTSENT *));
96 1.21 lukem int f_nouser __P((PLAN *, FTSENT *));
97 1.21 lukem int f_path __P((PLAN *, FTSENT *));
98 1.21 lukem int f_perm __P((PLAN *, FTSENT *));
99 1.21 lukem int f_print __P((PLAN *, FTSENT *));
100 1.21 lukem int f_print0 __P((PLAN *, FTSENT *));
101 1.28 lukem int f_printx __P((PLAN *, FTSENT *));
102 1.21 lukem int f_prune __P((PLAN *, FTSENT *));
103 1.21 lukem int f_size __P((PLAN *, FTSENT *));
104 1.21 lukem int f_type __P((PLAN *, FTSENT *));
105 1.21 lukem int f_user __P((PLAN *, FTSENT *));
106 1.21 lukem int f_not __P((PLAN *, FTSENT *));
107 1.21 lukem int f_or __P((PLAN *, FTSENT *));
108 1.21 lukem static PLAN *palloc __P((enum ntype, int (*) __P((PLAN *, FTSENT *))));
109 1.1 cgd
110 1.1 cgd /*
111 1.1 cgd * find_parsenum --
112 1.1 cgd * Parse a string of the form [+-]# and return the value.
113 1.1 cgd */
114 1.10 jtc static long
115 1.10 jtc find_parsenum(plan, option, vp, endch)
116 1.1 cgd PLAN *plan;
117 1.10 jtc char *option, *vp, *endch;
118 1.1 cgd {
119 1.1 cgd long value;
120 1.10 jtc char *endchar, *str; /* Pointer to character ending conversion. */
121 1.1 cgd
122 1.10 jtc /* Determine comparison from leading + or -. */
123 1.10 jtc str = vp;
124 1.10 jtc switch (*str) {
125 1.1 cgd case '+':
126 1.1 cgd ++str;
127 1.10 jtc plan->flags = F_GREATER;
128 1.1 cgd break;
129 1.1 cgd case '-':
130 1.1 cgd ++str;
131 1.10 jtc plan->flags = F_LESSTHAN;
132 1.1 cgd break;
133 1.1 cgd default:
134 1.10 jtc plan->flags = F_EQUAL;
135 1.1 cgd break;
136 1.1 cgd }
137 1.1 cgd
138 1.1 cgd /*
139 1.10 jtc * Convert the string with strtol(). Note, if strtol() returns zero
140 1.1 cgd * and endchar points to the beginning of the string we know we have
141 1.1 cgd * a syntax error.
142 1.1 cgd */
143 1.1 cgd value = strtol(str, &endchar, 10);
144 1.10 jtc if (value == 0 && endchar == str)
145 1.10 jtc errx(1, "%s: %s: illegal numeric value", option, vp);
146 1.10 jtc if (endchar[0] && (endch == NULL || endchar[0] != *endch))
147 1.10 jtc errx(1, "%s: %s: illegal trailing character", option, vp);
148 1.1 cgd if (endch)
149 1.1 cgd *endch = endchar[0];
150 1.10 jtc return (value);
151 1.1 cgd }
152 1.1 cgd
153 1.1 cgd /*
154 1.10 jtc * The value of n for the inode times (atime, ctime, and mtime) is a range,
155 1.10 jtc * i.e. n matches from (n - 1) to n 24 hour periods. This interacts with
156 1.10 jtc * -n, such that "-mtime -1" would be less than 0 days, which isn't what the
157 1.10 jtc * user wanted. Correct so that -1 is "less than 1".
158 1.10 jtc */
159 1.10 jtc #define TIME_CORRECT(p, ttype) \
160 1.10 jtc if ((p)->type == ttype && (p)->flags == F_LESSTHAN) \
161 1.10 jtc ++((p)->t_data);
162 1.10 jtc
163 1.10 jtc /*
164 1.1 cgd * -atime n functions --
165 1.1 cgd *
166 1.1 cgd * True if the difference between the file access time and the
167 1.1 cgd * current time is n 24 hour periods.
168 1.1 cgd */
169 1.10 jtc int
170 1.1 cgd f_atime(plan, entry)
171 1.1 cgd PLAN *plan;
172 1.1 cgd FTSENT *entry;
173 1.1 cgd {
174 1.1 cgd extern time_t now;
175 1.1 cgd
176 1.7 deraadt COMPARE((now - entry->fts_statp->st_atime +
177 1.1 cgd SECSPERDAY - 1) / SECSPERDAY, plan->t_data);
178 1.1 cgd }
179 1.1 cgd
180 1.1 cgd PLAN *
181 1.24 christos c_atime(argvp, isok)
182 1.24 christos char ***argvp;
183 1.24 christos int isok;
184 1.1 cgd {
185 1.24 christos char *arg = **argvp;
186 1.1 cgd PLAN *new;
187 1.1 cgd
188 1.24 christos (*argvp)++;
189 1.1 cgd ftsoptions &= ~FTS_NOSTAT;
190 1.1 cgd
191 1.1 cgd new = palloc(N_ATIME, f_atime);
192 1.1 cgd new->t_data = find_parsenum(new, "-atime", arg, NULL);
193 1.10 jtc TIME_CORRECT(new, N_ATIME);
194 1.10 jtc return (new);
195 1.1 cgd }
196 1.1 cgd /*
197 1.1 cgd * -ctime n functions --
198 1.1 cgd *
199 1.1 cgd * True if the difference between the last change of file
200 1.1 cgd * status information and the current time is n 24 hour periods.
201 1.1 cgd */
202 1.10 jtc int
203 1.1 cgd f_ctime(plan, entry)
204 1.1 cgd PLAN *plan;
205 1.1 cgd FTSENT *entry;
206 1.1 cgd {
207 1.1 cgd extern time_t now;
208 1.1 cgd
209 1.7 deraadt COMPARE((now - entry->fts_statp->st_ctime +
210 1.1 cgd SECSPERDAY - 1) / SECSPERDAY, plan->t_data);
211 1.1 cgd }
212 1.1 cgd
213 1.1 cgd PLAN *
214 1.24 christos c_ctime(argvp, isok)
215 1.24 christos char ***argvp;
216 1.24 christos int isok;
217 1.1 cgd {
218 1.24 christos char *arg = **argvp;
219 1.1 cgd PLAN *new;
220 1.1 cgd
221 1.24 christos (*argvp)++;
222 1.1 cgd ftsoptions &= ~FTS_NOSTAT;
223 1.1 cgd
224 1.1 cgd new = palloc(N_CTIME, f_ctime);
225 1.10 jtc new->t_data = find_parsenum(new, "-ctime", arg, NULL);
226 1.10 jtc TIME_CORRECT(new, N_CTIME);
227 1.10 jtc return (new);
228 1.1 cgd }
229 1.1 cgd
230 1.1 cgd /*
231 1.1 cgd * -depth functions --
232 1.1 cgd *
233 1.1 cgd * Always true, causes descent of the directory hierarchy to be done
234 1.1 cgd * so that all entries in a directory are acted on before the directory
235 1.1 cgd * itself.
236 1.1 cgd */
237 1.10 jtc int
238 1.1 cgd f_always_true(plan, entry)
239 1.1 cgd PLAN *plan;
240 1.1 cgd FTSENT *entry;
241 1.1 cgd {
242 1.10 jtc return (1);
243 1.1 cgd }
244 1.1 cgd
245 1.1 cgd PLAN *
246 1.24 christos c_depth(argvp, isok)
247 1.24 christos char ***argvp;
248 1.24 christos int isok;
249 1.1 cgd {
250 1.1 cgd isdepth = 1;
251 1.1 cgd
252 1.10 jtc return (palloc(N_DEPTH, f_always_true));
253 1.1 cgd }
254 1.1 cgd
255 1.1 cgd /*
256 1.1 cgd * [-exec | -ok] utility [arg ... ] ; functions --
257 1.1 cgd *
258 1.1 cgd * True if the executed utility returns a zero value as exit status.
259 1.1 cgd * The end of the primary expression is delimited by a semicolon. If
260 1.1 cgd * "{}" occurs anywhere, it gets replaced by the current pathname.
261 1.1 cgd * The current directory for the execution of utility is the same as
262 1.1 cgd * the current directory when the find utility was started.
263 1.1 cgd *
264 1.1 cgd * The primary -ok is different in that it requests affirmation of the
265 1.1 cgd * user before executing the utility.
266 1.1 cgd */
267 1.10 jtc int
268 1.1 cgd f_exec(plan, entry)
269 1.21 lukem PLAN *plan;
270 1.1 cgd FTSENT *entry;
271 1.1 cgd {
272 1.1 cgd extern int dotfd;
273 1.21 lukem int cnt;
274 1.1 cgd pid_t pid;
275 1.1 cgd int status;
276 1.1 cgd
277 1.1 cgd for (cnt = 0; plan->e_argv[cnt]; ++cnt)
278 1.1 cgd if (plan->e_len[cnt])
279 1.1 cgd brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
280 1.1 cgd entry->fts_path, plan->e_len[cnt]);
281 1.1 cgd
282 1.10 jtc if (plan->flags == F_NEEDOK && !queryuser(plan->e_argv))
283 1.10 jtc return (0);
284 1.11 jtc
285 1.11 jtc /* don't mix output of command with find output */
286 1.11 jtc fflush(stdout);
287 1.11 jtc fflush(stderr);
288 1.1 cgd
289 1.10 jtc switch (pid = vfork()) {
290 1.1 cgd case -1:
291 1.10 jtc err(1, "fork");
292 1.1 cgd /* NOTREACHED */
293 1.1 cgd case 0:
294 1.1 cgd if (fchdir(dotfd)) {
295 1.10 jtc warn("chdir");
296 1.1 cgd _exit(1);
297 1.1 cgd }
298 1.1 cgd execvp(plan->e_argv[0], plan->e_argv);
299 1.10 jtc warn("%s", plan->e_argv[0]);
300 1.1 cgd _exit(1);
301 1.1 cgd }
302 1.1 cgd pid = waitpid(pid, &status, 0);
303 1.10 jtc return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
304 1.1 cgd }
305 1.1 cgd
306 1.1 cgd /*
307 1.1 cgd * c_exec --
308 1.1 cgd * build three parallel arrays, one with pointers to the strings passed
309 1.1 cgd * on the command line, one with (possibly duplicated) pointers to the
310 1.1 cgd * argv array, and one with integer values that are lengths of the
311 1.1 cgd * strings, but also flags meaning that the string has to be massaged.
312 1.1 cgd */
313 1.1 cgd PLAN *
314 1.1 cgd c_exec(argvp, isok)
315 1.1 cgd char ***argvp;
316 1.1 cgd int isok;
317 1.1 cgd {
318 1.1 cgd PLAN *new; /* node returned */
319 1.21 lukem int cnt;
320 1.21 lukem char **argv, **ap, *p;
321 1.1 cgd
322 1.1 cgd isoutput = 1;
323 1.1 cgd
324 1.1 cgd new = palloc(N_EXEC, f_exec);
325 1.10 jtc if (isok)
326 1.10 jtc new->flags = F_NEEDOK;
327 1.1 cgd
328 1.1 cgd for (ap = argv = *argvp;; ++ap) {
329 1.1 cgd if (!*ap)
330 1.10 jtc errx(1,
331 1.10 jtc "%s: no terminating \";\"", isok ? "-ok" : "-exec");
332 1.1 cgd if (**ap == ';')
333 1.1 cgd break;
334 1.1 cgd }
335 1.1 cgd
336 1.1 cgd cnt = ap - *argvp + 1;
337 1.1 cgd new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
338 1.1 cgd new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
339 1.1 cgd new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
340 1.1 cgd
341 1.1 cgd for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
342 1.1 cgd new->e_orig[cnt] = *argv;
343 1.1 cgd for (p = *argv; *p; ++p)
344 1.1 cgd if (p[0] == '{' && p[1] == '}') {
345 1.1 cgd new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
346 1.1 cgd new->e_len[cnt] = MAXPATHLEN;
347 1.1 cgd break;
348 1.1 cgd }
349 1.1 cgd if (!*p) {
350 1.1 cgd new->e_argv[cnt] = *argv;
351 1.1 cgd new->e_len[cnt] = 0;
352 1.1 cgd }
353 1.1 cgd }
354 1.1 cgd new->e_argv[cnt] = new->e_orig[cnt] = NULL;
355 1.1 cgd
356 1.1 cgd *argvp = argv + 1;
357 1.10 jtc return (new);
358 1.1 cgd }
359 1.27 lukem
360 1.27 lukem /*
361 1.27 lukem * -flags [-]flags functions --
362 1.27 lukem */
363 1.27 lukem int
364 1.27 lukem f_flags(plan, entry)
365 1.27 lukem PLAN *plan;
366 1.27 lukem FTSENT *entry;
367 1.27 lukem {
368 1.27 lukem u_int32_t flags;
369 1.27 lukem
370 1.27 lukem flags = entry->fts_statp->st_flags;
371 1.27 lukem if (plan->flags == F_ATLEAST)
372 1.27 lukem return ((plan->f_data | flags) == flags);
373 1.27 lukem else
374 1.27 lukem return (flags == plan->f_data);
375 1.27 lukem /* NOTREACHED */
376 1.27 lukem }
377 1.27 lukem
378 1.27 lukem PLAN *
379 1.27 lukem c_flags(argvp, isok)
380 1.27 lukem char ***argvp;
381 1.27 lukem int isok;
382 1.27 lukem {
383 1.27 lukem char *flags = **argvp;
384 1.27 lukem PLAN *new;
385 1.27 lukem u_long flagset;
386 1.27 lukem
387 1.27 lukem (*argvp)++;
388 1.27 lukem ftsoptions &= ~FTS_NOSTAT;
389 1.27 lukem
390 1.27 lukem new = palloc(N_FLAGS, f_flags);
391 1.27 lukem
392 1.27 lukem if (*flags == '-') {
393 1.27 lukem new->flags = F_ATLEAST;
394 1.27 lukem ++flags;
395 1.27 lukem }
396 1.27 lukem
397 1.27 lukem flagset = 0;
398 1.27 lukem if ((strcmp(flags, "none") != 0) &&
399 1.27 lukem (string_to_flags(&flags, &flagset, NULL) != 0))
400 1.27 lukem errx(1, "-flags: %s: illegal flags string", flags);
401 1.27 lukem new->f_data = flagset;
402 1.27 lukem return (new);
403 1.27 lukem }
404 1.27 lukem
405 1.1 cgd
406 1.1 cgd /*
407 1.1 cgd * -follow functions --
408 1.1 cgd *
409 1.1 cgd * Always true, causes symbolic links to be followed on a global
410 1.1 cgd * basis.
411 1.1 cgd */
412 1.1 cgd PLAN *
413 1.24 christos c_follow(argvp, isok)
414 1.24 christos char ***argvp;
415 1.24 christos int isok;
416 1.1 cgd {
417 1.1 cgd ftsoptions &= ~FTS_PHYSICAL;
418 1.1 cgd ftsoptions |= FTS_LOGICAL;
419 1.1 cgd
420 1.10 jtc return (palloc(N_FOLLOW, f_always_true));
421 1.1 cgd }
422 1.1 cgd
423 1.1 cgd /*
424 1.1 cgd * -fstype functions --
425 1.1 cgd *
426 1.1 cgd * True if the file is of a certain type.
427 1.1 cgd */
428 1.10 jtc int
429 1.1 cgd f_fstype(plan, entry)
430 1.1 cgd PLAN *plan;
431 1.1 cgd FTSENT *entry;
432 1.1 cgd {
433 1.1 cgd static dev_t curdev; /* need a guaranteed illegal dev value */
434 1.1 cgd static int first = 1;
435 1.10 jtc struct statfs sb;
436 1.10 jtc static short val;
437 1.16 cgd static char fstype[MFSNAMELEN];
438 1.1 cgd char *p, save[2];
439 1.1 cgd
440 1.10 jtc /* Only check when we cross mount point. */
441 1.7 deraadt if (first || curdev != entry->fts_statp->st_dev) {
442 1.7 deraadt curdev = entry->fts_statp->st_dev;
443 1.1 cgd
444 1.1 cgd /*
445 1.1 cgd * Statfs follows symlinks; find wants the link's file system,
446 1.1 cgd * not where it points.
447 1.1 cgd */
448 1.1 cgd if (entry->fts_info == FTS_SL ||
449 1.1 cgd entry->fts_info == FTS_SLNONE) {
450 1.21 lukem if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
451 1.1 cgd ++p;
452 1.1 cgd else
453 1.1 cgd p = entry->fts_accpath;
454 1.1 cgd save[0] = p[0];
455 1.1 cgd p[0] = '.';
456 1.1 cgd save[1] = p[1];
457 1.1 cgd p[1] = '\0';
458 1.1 cgd
459 1.1 cgd } else
460 1.1 cgd p = NULL;
461 1.1 cgd
462 1.1 cgd if (statfs(entry->fts_accpath, &sb))
463 1.10 jtc err(1, "%s", entry->fts_accpath);
464 1.1 cgd
465 1.1 cgd if (p) {
466 1.1 cgd p[0] = save[0];
467 1.1 cgd p[1] = save[1];
468 1.1 cgd }
469 1.1 cgd
470 1.1 cgd first = 0;
471 1.15 mycroft
472 1.15 mycroft /*
473 1.15 mycroft * Further tests may need both of these values, so
474 1.15 mycroft * always copy both of them.
475 1.15 mycroft */
476 1.15 mycroft val = sb.f_flags;
477 1.15 mycroft strncpy(fstype, sb.f_fstypename, MFSNAMELEN);
478 1.10 jtc }
479 1.14 mycroft switch (plan->flags) {
480 1.10 jtc case F_MTFLAG:
481 1.10 jtc return (val & plan->mt_data);
482 1.10 jtc case F_MTTYPE:
483 1.13 cgd return (strncmp(fstype, plan->c_data, MFSNAMELEN) == 0);
484 1.10 jtc default:
485 1.10 jtc abort();
486 1.1 cgd }
487 1.1 cgd }
488 1.1 cgd
489 1.1 cgd PLAN *
490 1.24 christos c_fstype(argvp, isok)
491 1.24 christos char ***argvp;
492 1.24 christos int isok;
493 1.1 cgd {
494 1.24 christos char *arg = **argvp;
495 1.21 lukem PLAN *new;
496 1.1 cgd
497 1.24 christos (*argvp)++;
498 1.1 cgd ftsoptions &= ~FTS_NOSTAT;
499 1.1 cgd
500 1.1 cgd new = palloc(N_FSTYPE, f_fstype);
501 1.22 mrg
502 1.10 jtc switch (*arg) {
503 1.1 cgd case 'l':
504 1.1 cgd if (!strcmp(arg, "local")) {
505 1.10 jtc new->flags = F_MTFLAG;
506 1.10 jtc new->mt_data = MNT_LOCAL;
507 1.10 jtc return (new);
508 1.1 cgd }
509 1.1 cgd break;
510 1.2 cgd case 'r':
511 1.2 cgd if (!strcmp(arg, "rdonly")) {
512 1.10 jtc new->flags = F_MTFLAG;
513 1.10 jtc new->mt_data = MNT_RDONLY;
514 1.10 jtc return (new);
515 1.1 cgd }
516 1.1 cgd break;
517 1.1 cgd }
518 1.13 cgd
519 1.13 cgd new->flags = F_MTTYPE;
520 1.13 cgd new->c_data = arg;
521 1.13 cgd return (new);
522 1.1 cgd }
523 1.1 cgd
524 1.1 cgd /*
525 1.1 cgd * -group gname functions --
526 1.1 cgd *
527 1.1 cgd * True if the file belongs to the group gname. If gname is numeric and
528 1.1 cgd * an equivalent of the getgrnam() function does not return a valid group
529 1.1 cgd * name, gname is taken as a group ID.
530 1.1 cgd */
531 1.10 jtc int
532 1.1 cgd f_group(plan, entry)
533 1.1 cgd PLAN *plan;
534 1.1 cgd FTSENT *entry;
535 1.1 cgd {
536 1.10 jtc return (entry->fts_statp->st_gid == plan->g_data);
537 1.1 cgd }
538 1.1 cgd
539 1.1 cgd PLAN *
540 1.24 christos c_group(argvp, isok)
541 1.24 christos char ***argvp;
542 1.24 christos int isok;
543 1.1 cgd {
544 1.24 christos char *gname = **argvp;
545 1.1 cgd PLAN *new;
546 1.1 cgd struct group *g;
547 1.1 cgd gid_t gid;
548 1.1 cgd
549 1.24 christos (*argvp)++;
550 1.1 cgd ftsoptions &= ~FTS_NOSTAT;
551 1.1 cgd
552 1.1 cgd g = getgrnam(gname);
553 1.1 cgd if (g == NULL) {
554 1.1 cgd gid = atoi(gname);
555 1.1 cgd if (gid == 0 && gname[0] != '0')
556 1.10 jtc errx(1, "-group: %s: no such group", gname);
557 1.1 cgd } else
558 1.1 cgd gid = g->gr_gid;
559 1.1 cgd
560 1.1 cgd new = palloc(N_GROUP, f_group);
561 1.1 cgd new->g_data = gid;
562 1.10 jtc return (new);
563 1.1 cgd }
564 1.1 cgd
565 1.1 cgd /*
566 1.1 cgd * -inum n functions --
567 1.1 cgd *
568 1.1 cgd * True if the file has inode # n.
569 1.1 cgd */
570 1.10 jtc int
571 1.1 cgd f_inum(plan, entry)
572 1.1 cgd PLAN *plan;
573 1.1 cgd FTSENT *entry;
574 1.1 cgd {
575 1.7 deraadt COMPARE(entry->fts_statp->st_ino, plan->i_data);
576 1.1 cgd }
577 1.1 cgd
578 1.1 cgd PLAN *
579 1.24 christos c_inum(argvp, isok)
580 1.24 christos char ***argvp;
581 1.24 christos int isok;
582 1.1 cgd {
583 1.24 christos char *arg = **argvp;
584 1.1 cgd PLAN *new;
585 1.1 cgd
586 1.24 christos (*argvp)++;
587 1.1 cgd ftsoptions &= ~FTS_NOSTAT;
588 1.1 cgd
589 1.1 cgd new = palloc(N_INUM, f_inum);
590 1.10 jtc new->i_data = find_parsenum(new, "-inum", arg, NULL);
591 1.10 jtc return (new);
592 1.1 cgd }
593 1.1 cgd
594 1.1 cgd /*
595 1.1 cgd * -links n functions --
596 1.1 cgd *
597 1.1 cgd * True if the file has n links.
598 1.1 cgd */
599 1.10 jtc int
600 1.1 cgd f_links(plan, entry)
601 1.1 cgd PLAN *plan;
602 1.1 cgd FTSENT *entry;
603 1.1 cgd {
604 1.7 deraadt COMPARE(entry->fts_statp->st_nlink, plan->l_data);
605 1.1 cgd }
606 1.1 cgd
607 1.1 cgd PLAN *
608 1.24 christos c_links(argvp, isok)
609 1.24 christos char ***argvp;
610 1.24 christos int isok;
611 1.1 cgd {
612 1.24 christos char *arg = **argvp;
613 1.1 cgd PLAN *new;
614 1.1 cgd
615 1.24 christos (*argvp)++;
616 1.1 cgd ftsoptions &= ~FTS_NOSTAT;
617 1.1 cgd
618 1.1 cgd new = palloc(N_LINKS, f_links);
619 1.10 jtc new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL);
620 1.10 jtc return (new);
621 1.1 cgd }
622 1.1 cgd
623 1.1 cgd /*
624 1.1 cgd * -ls functions --
625 1.1 cgd *
626 1.1 cgd * Always true - prints the current entry to stdout in "ls" format.
627 1.1 cgd */
628 1.10 jtc int
629 1.1 cgd f_ls(plan, entry)
630 1.1 cgd PLAN *plan;
631 1.1 cgd FTSENT *entry;
632 1.1 cgd {
633 1.7 deraadt printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
634 1.10 jtc return (1);
635 1.1 cgd }
636 1.1 cgd
637 1.1 cgd PLAN *
638 1.24 christos c_ls(argvp, isok)
639 1.24 christos char ***argvp;
640 1.24 christos int isok;
641 1.1 cgd {
642 1.1 cgd ftsoptions &= ~FTS_NOSTAT;
643 1.1 cgd isoutput = 1;
644 1.1 cgd
645 1.10 jtc return (palloc(N_LS, f_ls));
646 1.10 jtc }
647 1.10 jtc
648 1.10 jtc /*
649 1.10 jtc * -mtime n functions --
650 1.10 jtc *
651 1.10 jtc * True if the difference between the file modification time and the
652 1.10 jtc * current time is n 24 hour periods.
653 1.10 jtc */
654 1.10 jtc int
655 1.10 jtc f_mtime(plan, entry)
656 1.10 jtc PLAN *plan;
657 1.10 jtc FTSENT *entry;
658 1.10 jtc {
659 1.10 jtc extern time_t now;
660 1.10 jtc
661 1.10 jtc COMPARE((now - entry->fts_statp->st_mtime + SECSPERDAY - 1) /
662 1.10 jtc SECSPERDAY, plan->t_data);
663 1.10 jtc }
664 1.10 jtc
665 1.10 jtc PLAN *
666 1.24 christos c_mtime(argvp, isok)
667 1.24 christos char ***argvp;
668 1.24 christos int isok;
669 1.10 jtc {
670 1.24 christos char *arg = **argvp;
671 1.10 jtc PLAN *new;
672 1.10 jtc
673 1.24 christos (*argvp)++;
674 1.10 jtc ftsoptions &= ~FTS_NOSTAT;
675 1.10 jtc
676 1.10 jtc new = palloc(N_MTIME, f_mtime);
677 1.10 jtc new->t_data = find_parsenum(new, "-mtime", arg, NULL);
678 1.10 jtc TIME_CORRECT(new, N_MTIME);
679 1.10 jtc return (new);
680 1.1 cgd }
681 1.1 cgd
682 1.1 cgd /*
683 1.1 cgd * -name functions --
684 1.1 cgd *
685 1.1 cgd * True if the basename of the filename being examined
686 1.1 cgd * matches pattern using Pattern Matching Notation S3.14
687 1.1 cgd */
688 1.10 jtc int
689 1.1 cgd f_name(plan, entry)
690 1.1 cgd PLAN *plan;
691 1.1 cgd FTSENT *entry;
692 1.1 cgd {
693 1.10 jtc return (!fnmatch(plan->c_data, entry->fts_name, 0));
694 1.1 cgd }
695 1.1 cgd
696 1.1 cgd PLAN *
697 1.24 christos c_name(argvp, isok)
698 1.24 christos char ***argvp;
699 1.24 christos int isok;
700 1.1 cgd {
701 1.24 christos char *pattern = **argvp;
702 1.1 cgd PLAN *new;
703 1.1 cgd
704 1.24 christos (*argvp)++;
705 1.1 cgd new = palloc(N_NAME, f_name);
706 1.1 cgd new->c_data = pattern;
707 1.10 jtc return (new);
708 1.1 cgd }
709 1.1 cgd
710 1.1 cgd /*
711 1.1 cgd * -newer file functions --
712 1.1 cgd *
713 1.1 cgd * True if the current file has been modified more recently
714 1.1 cgd * then the modification time of the file named by the pathname
715 1.1 cgd * file.
716 1.1 cgd */
717 1.10 jtc int
718 1.1 cgd f_newer(plan, entry)
719 1.1 cgd PLAN *plan;
720 1.1 cgd FTSENT *entry;
721 1.1 cgd {
722 1.10 jtc return (entry->fts_statp->st_mtime > plan->t_data);
723 1.1 cgd }
724 1.1 cgd
725 1.1 cgd PLAN *
726 1.24 christos c_newer(argvp, isok)
727 1.24 christos char ***argvp;
728 1.24 christos int isok;
729 1.1 cgd {
730 1.24 christos char *filename = **argvp;
731 1.1 cgd PLAN *new;
732 1.1 cgd struct stat sb;
733 1.1 cgd
734 1.24 christos (*argvp)++;
735 1.1 cgd ftsoptions &= ~FTS_NOSTAT;
736 1.1 cgd
737 1.1 cgd if (stat(filename, &sb))
738 1.10 jtc err(1, "%s", filename);
739 1.1 cgd new = palloc(N_NEWER, f_newer);
740 1.1 cgd new->t_data = sb.st_mtime;
741 1.10 jtc return (new);
742 1.1 cgd }
743 1.1 cgd
744 1.1 cgd /*
745 1.1 cgd * -nogroup functions --
746 1.1 cgd *
747 1.1 cgd * True if file belongs to a user ID for which the equivalent
748 1.1 cgd * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
749 1.1 cgd */
750 1.10 jtc int
751 1.1 cgd f_nogroup(plan, entry)
752 1.1 cgd PLAN *plan;
753 1.1 cgd FTSENT *entry;
754 1.1 cgd {
755 1.22 mrg
756 1.12 andrew return (group_from_gid(entry->fts_statp->st_gid, 1) ? 0 : 1);
757 1.1 cgd }
758 1.1 cgd
759 1.1 cgd PLAN *
760 1.24 christos c_nogroup(argvp, isok)
761 1.24 christos char ***argvp;
762 1.24 christos int isok;
763 1.1 cgd {
764 1.1 cgd ftsoptions &= ~FTS_NOSTAT;
765 1.1 cgd
766 1.10 jtc return (palloc(N_NOGROUP, f_nogroup));
767 1.1 cgd }
768 1.1 cgd
769 1.1 cgd /*
770 1.1 cgd * -nouser functions --
771 1.1 cgd *
772 1.1 cgd * True if file belongs to a user ID for which the equivalent
773 1.1 cgd * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
774 1.1 cgd */
775 1.10 jtc int
776 1.1 cgd f_nouser(plan, entry)
777 1.1 cgd PLAN *plan;
778 1.1 cgd FTSENT *entry;
779 1.1 cgd {
780 1.22 mrg
781 1.12 andrew return (user_from_uid(entry->fts_statp->st_uid, 1) ? 0 : 1);
782 1.1 cgd }
783 1.1 cgd
784 1.1 cgd PLAN *
785 1.24 christos c_nouser(argvp, isok)
786 1.24 christos char ***argvp;
787 1.24 christos int isok;
788 1.1 cgd {
789 1.1 cgd ftsoptions &= ~FTS_NOSTAT;
790 1.1 cgd
791 1.10 jtc return (palloc(N_NOUSER, f_nouser));
792 1.10 jtc }
793 1.10 jtc
794 1.10 jtc /*
795 1.10 jtc * -path functions --
796 1.10 jtc *
797 1.10 jtc * True if the path of the filename being examined
798 1.10 jtc * matches pattern using Pattern Matching Notation S3.14
799 1.10 jtc */
800 1.10 jtc int
801 1.10 jtc f_path(plan, entry)
802 1.10 jtc PLAN *plan;
803 1.10 jtc FTSENT *entry;
804 1.10 jtc {
805 1.10 jtc return (!fnmatch(plan->c_data, entry->fts_path, 0));
806 1.10 jtc }
807 1.10 jtc
808 1.10 jtc PLAN *
809 1.24 christos c_path(argvp, isok)
810 1.24 christos char ***argvp;
811 1.24 christos int isok;
812 1.10 jtc {
813 1.24 christos char *pattern = **argvp;
814 1.10 jtc PLAN *new;
815 1.10 jtc
816 1.24 christos (*argvp)++;
817 1.10 jtc new = palloc(N_NAME, f_path);
818 1.10 jtc new->c_data = pattern;
819 1.10 jtc return (new);
820 1.1 cgd }
821 1.1 cgd
822 1.1 cgd /*
823 1.1 cgd * -perm functions --
824 1.1 cgd *
825 1.1 cgd * The mode argument is used to represent file mode bits. If it starts
826 1.1 cgd * with a leading digit, it's treated as an octal mode, otherwise as a
827 1.1 cgd * symbolic mode.
828 1.1 cgd */
829 1.10 jtc int
830 1.1 cgd f_perm(plan, entry)
831 1.1 cgd PLAN *plan;
832 1.1 cgd FTSENT *entry;
833 1.1 cgd {
834 1.1 cgd mode_t mode;
835 1.1 cgd
836 1.7 deraadt mode = entry->fts_statp->st_mode &
837 1.1 cgd (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
838 1.10 jtc if (plan->flags == F_ATLEAST)
839 1.10 jtc return ((plan->m_data | mode) == mode);
840 1.1 cgd else
841 1.10 jtc return (mode == plan->m_data);
842 1.1 cgd /* NOTREACHED */
843 1.1 cgd }
844 1.1 cgd
845 1.1 cgd PLAN *
846 1.24 christos c_perm(argvp, isok)
847 1.24 christos char ***argvp;
848 1.24 christos int isok;
849 1.1 cgd {
850 1.24 christos char *perm = **argvp;
851 1.1 cgd PLAN *new;
852 1.1 cgd mode_t *set;
853 1.1 cgd
854 1.24 christos (*argvp)++;
855 1.1 cgd ftsoptions &= ~FTS_NOSTAT;
856 1.1 cgd
857 1.1 cgd new = palloc(N_PERM, f_perm);
858 1.1 cgd
859 1.1 cgd if (*perm == '-') {
860 1.10 jtc new->flags = F_ATLEAST;
861 1.1 cgd ++perm;
862 1.1 cgd }
863 1.1 cgd
864 1.1 cgd if ((set = setmode(perm)) == NULL)
865 1.10 jtc err(1, "-perm: %s: illegal mode string", perm);
866 1.1 cgd
867 1.1 cgd new->m_data = getmode(set, 0);
868 1.10 jtc return (new);
869 1.1 cgd }
870 1.1 cgd
871 1.1 cgd /*
872 1.1 cgd * -print functions --
873 1.1 cgd *
874 1.1 cgd * Always true, causes the current pathame to be written to
875 1.1 cgd * standard output.
876 1.1 cgd */
877 1.10 jtc int
878 1.1 cgd f_print(plan, entry)
879 1.1 cgd PLAN *plan;
880 1.1 cgd FTSENT *entry;
881 1.1 cgd {
882 1.1 cgd (void)printf("%s\n", entry->fts_path);
883 1.22 mrg return (1);
884 1.1 cgd }
885 1.9 jtc
886 1.21 lukem int
887 1.9 jtc f_print0(plan, entry)
888 1.9 jtc PLAN *plan;
889 1.9 jtc FTSENT *entry;
890 1.9 jtc {
891 1.9 jtc (void)fputs(entry->fts_path, stdout);
892 1.9 jtc (void)fputc('\0', stdout);
893 1.22 mrg return (1);
894 1.9 jtc }
895 1.28 lukem
896 1.28 lukem int
897 1.28 lukem f_printx(plan, entry)
898 1.28 lukem PLAN *plan;
899 1.28 lukem FTSENT *entry;
900 1.28 lukem {
901 1.28 lukem char *cp;
902 1.28 lukem
903 1.28 lukem for (cp = entry->fts_path; *cp; cp++) {
904 1.28 lukem if (*cp == '\'' || *cp == '\"' || *cp == ' ' ||
905 1.28 lukem *cp == '\t' || *cp == '\n' || *cp == '\\')
906 1.28 lukem fputc('\\', stdout);
907 1.28 lukem
908 1.28 lukem fputc(*cp, stdout);
909 1.28 lukem }
910 1.28 lukem
911 1.28 lukem fputc('\n', stdout);
912 1.28 lukem return 1;
913 1.28 lukem }
914 1.1 cgd
915 1.1 cgd PLAN *
916 1.24 christos c_print(argvp, isok)
917 1.24 christos char ***argvp;
918 1.24 christos int isok;
919 1.1 cgd {
920 1.1 cgd isoutput = 1;
921 1.1 cgd
922 1.22 mrg return (palloc(N_PRINT, f_print));
923 1.9 jtc }
924 1.9 jtc
925 1.9 jtc PLAN *
926 1.24 christos c_print0(argvp, isok)
927 1.24 christos char ***argvp;
928 1.24 christos int isok;
929 1.9 jtc {
930 1.9 jtc isoutput = 1;
931 1.9 jtc
932 1.22 mrg return (palloc(N_PRINT0, f_print0));
933 1.28 lukem }
934 1.28 lukem
935 1.28 lukem PLAN *
936 1.28 lukem c_printx(argvp, isok)
937 1.28 lukem char ***argvp;
938 1.28 lukem int isok;
939 1.28 lukem {
940 1.28 lukem isoutput = 1;
941 1.28 lukem
942 1.28 lukem return palloc(N_PRINTX, f_printx);
943 1.1 cgd }
944 1.1 cgd
945 1.1 cgd /*
946 1.1 cgd * -prune functions --
947 1.1 cgd *
948 1.1 cgd * Prune a portion of the hierarchy.
949 1.1 cgd */
950 1.10 jtc int
951 1.1 cgd f_prune(plan, entry)
952 1.1 cgd PLAN *plan;
953 1.1 cgd FTSENT *entry;
954 1.1 cgd {
955 1.1 cgd extern FTS *tree;
956 1.1 cgd
957 1.1 cgd if (fts_set(tree, entry, FTS_SKIP))
958 1.10 jtc err(1, "%s", entry->fts_path);
959 1.10 jtc return (1);
960 1.1 cgd }
961 1.1 cgd
962 1.1 cgd PLAN *
963 1.24 christos c_prune(argvp, isok)
964 1.24 christos char ***argvp;
965 1.24 christos int isok;
966 1.1 cgd {
967 1.10 jtc return (palloc(N_PRUNE, f_prune));
968 1.1 cgd }
969 1.1 cgd
970 1.1 cgd /*
971 1.1 cgd * -size n[c] functions --
972 1.1 cgd *
973 1.1 cgd * True if the file size in bytes, divided by an implementation defined
974 1.1 cgd * value and rounded up to the next integer, is n. If n is followed by
975 1.1 cgd * a c, the size is in bytes.
976 1.1 cgd */
977 1.1 cgd #define FIND_SIZE 512
978 1.1 cgd static int divsize = 1;
979 1.1 cgd
980 1.10 jtc int
981 1.1 cgd f_size(plan, entry)
982 1.1 cgd PLAN *plan;
983 1.1 cgd FTSENT *entry;
984 1.1 cgd {
985 1.1 cgd off_t size;
986 1.1 cgd
987 1.7 deraadt size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
988 1.7 deraadt FIND_SIZE : entry->fts_statp->st_size;
989 1.1 cgd COMPARE(size, plan->o_data);
990 1.1 cgd }
991 1.1 cgd
992 1.1 cgd PLAN *
993 1.24 christos c_size(argvp, isok)
994 1.24 christos char ***argvp;
995 1.24 christos int isok;
996 1.1 cgd {
997 1.24 christos char *arg = **argvp;
998 1.1 cgd PLAN *new;
999 1.1 cgd char endch;
1000 1.1 cgd
1001 1.24 christos (*argvp)++;
1002 1.1 cgd ftsoptions &= ~FTS_NOSTAT;
1003 1.1 cgd
1004 1.1 cgd new = palloc(N_SIZE, f_size);
1005 1.8 cgd endch = 'c';
1006 1.1 cgd new->o_data = find_parsenum(new, "-size", arg, &endch);
1007 1.1 cgd if (endch == 'c')
1008 1.1 cgd divsize = 0;
1009 1.10 jtc return (new);
1010 1.1 cgd }
1011 1.1 cgd
1012 1.1 cgd /*
1013 1.1 cgd * -type c functions --
1014 1.1 cgd *
1015 1.22 mrg * True if the type of the file is c, where c is b, c, d, p, f or w
1016 1.22 mrg * for block special file, character special file, directory, FIFO,
1017 1.22 mrg * regular file or whiteout respectively.
1018 1.1 cgd */
1019 1.10 jtc int
1020 1.1 cgd f_type(plan, entry)
1021 1.1 cgd PLAN *plan;
1022 1.1 cgd FTSENT *entry;
1023 1.1 cgd {
1024 1.10 jtc return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data);
1025 1.1 cgd }
1026 1.1 cgd
1027 1.1 cgd PLAN *
1028 1.24 christos c_type(argvp, isok)
1029 1.24 christos char ***argvp;
1030 1.24 christos int isok;
1031 1.1 cgd {
1032 1.24 christos char *typestring = **argvp;
1033 1.1 cgd PLAN *new;
1034 1.25 wsanchez mode_t mask = (mode_t)0;
1035 1.1 cgd
1036 1.24 christos (*argvp)++;
1037 1.1 cgd ftsoptions &= ~FTS_NOSTAT;
1038 1.1 cgd
1039 1.1 cgd switch (typestring[0]) {
1040 1.17 mrg #ifdef S_IFWHT
1041 1.17 mrg case 'W':
1042 1.19 matthias #ifdef FTS_WHITEOUT
1043 1.19 matthias ftsoptions |= FTS_WHITEOUT;
1044 1.19 matthias #endif
1045 1.17 mrg mask = S_IFWHT;
1046 1.17 mrg break;
1047 1.17 mrg #endif
1048 1.1 cgd case 'b':
1049 1.1 cgd mask = S_IFBLK;
1050 1.1 cgd break;
1051 1.1 cgd case 'c':
1052 1.1 cgd mask = S_IFCHR;
1053 1.1 cgd break;
1054 1.1 cgd case 'd':
1055 1.1 cgd mask = S_IFDIR;
1056 1.1 cgd break;
1057 1.1 cgd case 'f':
1058 1.1 cgd mask = S_IFREG;
1059 1.1 cgd break;
1060 1.1 cgd case 'l':
1061 1.1 cgd mask = S_IFLNK;
1062 1.1 cgd break;
1063 1.1 cgd case 'p':
1064 1.1 cgd mask = S_IFIFO;
1065 1.1 cgd break;
1066 1.1 cgd case 's':
1067 1.1 cgd mask = S_IFSOCK;
1068 1.1 cgd break;
1069 1.22 mrg #ifdef FTS_WHITEOUT
1070 1.22 mrg case 'w':
1071 1.22 mrg mask = S_IFWHT;
1072 1.22 mrg ftsoptions |= FTS_WHITEOUT;
1073 1.22 mrg break;
1074 1.22 mrg #endif /* FTS_WHITEOUT */
1075 1.1 cgd default:
1076 1.10 jtc errx(1, "-type: %s: unknown type", typestring);
1077 1.1 cgd }
1078 1.1 cgd
1079 1.1 cgd new = palloc(N_TYPE, f_type);
1080 1.1 cgd new->m_data = mask;
1081 1.10 jtc return (new);
1082 1.1 cgd }
1083 1.1 cgd
1084 1.1 cgd /*
1085 1.1 cgd * -user uname functions --
1086 1.1 cgd *
1087 1.1 cgd * True if the file belongs to the user uname. If uname is numeric and
1088 1.1 cgd * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1089 1.1 cgd * return a valid user name, uname is taken as a user ID.
1090 1.1 cgd */
1091 1.10 jtc int
1092 1.1 cgd f_user(plan, entry)
1093 1.1 cgd PLAN *plan;
1094 1.1 cgd FTSENT *entry;
1095 1.1 cgd {
1096 1.10 jtc return (entry->fts_statp->st_uid == plan->u_data);
1097 1.1 cgd }
1098 1.1 cgd
1099 1.1 cgd PLAN *
1100 1.24 christos c_user(argvp, isok)
1101 1.24 christos char ***argvp;
1102 1.24 christos int isok;
1103 1.1 cgd {
1104 1.24 christos char *username = **argvp;
1105 1.1 cgd PLAN *new;
1106 1.1 cgd struct passwd *p;
1107 1.1 cgd uid_t uid;
1108 1.1 cgd
1109 1.24 christos (*argvp)++;
1110 1.1 cgd ftsoptions &= ~FTS_NOSTAT;
1111 1.1 cgd
1112 1.1 cgd p = getpwnam(username);
1113 1.1 cgd if (p == NULL) {
1114 1.1 cgd uid = atoi(username);
1115 1.1 cgd if (uid == 0 && username[0] != '0')
1116 1.10 jtc errx(1, "-user: %s: no such user", username);
1117 1.1 cgd } else
1118 1.1 cgd uid = p->pw_uid;
1119 1.1 cgd
1120 1.1 cgd new = palloc(N_USER, f_user);
1121 1.1 cgd new->u_data = uid;
1122 1.10 jtc return (new);
1123 1.1 cgd }
1124 1.1 cgd
1125 1.1 cgd /*
1126 1.1 cgd * -xdev functions --
1127 1.1 cgd *
1128 1.1 cgd * Always true, causes find not to decend past directories that have a
1129 1.1 cgd * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1130 1.1 cgd */
1131 1.1 cgd PLAN *
1132 1.24 christos c_xdev(argvp, isok)
1133 1.24 christos char ***argvp;
1134 1.24 christos int isok;
1135 1.1 cgd {
1136 1.1 cgd ftsoptions |= FTS_XDEV;
1137 1.1 cgd
1138 1.10 jtc return (palloc(N_XDEV, f_always_true));
1139 1.1 cgd }
1140 1.1 cgd
1141 1.1 cgd /*
1142 1.1 cgd * ( expression ) functions --
1143 1.1 cgd *
1144 1.1 cgd * True if expression is true.
1145 1.1 cgd */
1146 1.10 jtc int
1147 1.1 cgd f_expr(plan, entry)
1148 1.1 cgd PLAN *plan;
1149 1.1 cgd FTSENT *entry;
1150 1.1 cgd {
1151 1.21 lukem PLAN *p;
1152 1.21 lukem int state;
1153 1.1 cgd
1154 1.21 lukem state = 0;
1155 1.1 cgd for (p = plan->p_data[0];
1156 1.1 cgd p && (state = (p->eval)(p, entry)); p = p->next);
1157 1.10 jtc return (state);
1158 1.1 cgd }
1159 1.1 cgd
1160 1.1 cgd /*
1161 1.1 cgd * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers. They are
1162 1.1 cgd * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1163 1.1 cgd * to a N_EXPR node containing the expression and the ')' node is discarded.
1164 1.1 cgd */
1165 1.1 cgd PLAN *
1166 1.24 christos c_openparen(argvp, isok)
1167 1.24 christos char ***argvp;
1168 1.24 christos int isok;
1169 1.1 cgd {
1170 1.21 lukem return (palloc(N_OPENPAREN, (int (*) __P((PLAN *, FTSENT *)))-1));
1171 1.1 cgd }
1172 1.1 cgd
1173 1.1 cgd PLAN *
1174 1.24 christos c_closeparen(argvp, isok)
1175 1.24 christos char ***argvp;
1176 1.24 christos int isok;
1177 1.1 cgd {
1178 1.21 lukem return (palloc(N_CLOSEPAREN, (int (*) __P((PLAN *, FTSENT *)))-1));
1179 1.1 cgd }
1180 1.1 cgd
1181 1.1 cgd /*
1182 1.1 cgd * ! expression functions --
1183 1.1 cgd *
1184 1.1 cgd * Negation of a primary; the unary NOT operator.
1185 1.1 cgd */
1186 1.10 jtc int
1187 1.1 cgd f_not(plan, entry)
1188 1.1 cgd PLAN *plan;
1189 1.1 cgd FTSENT *entry;
1190 1.1 cgd {
1191 1.21 lukem PLAN *p;
1192 1.21 lukem int state;
1193 1.1 cgd
1194 1.21 lukem state = 0;
1195 1.1 cgd for (p = plan->p_data[0];
1196 1.1 cgd p && (state = (p->eval)(p, entry)); p = p->next);
1197 1.10 jtc return (!state);
1198 1.1 cgd }
1199 1.1 cgd
1200 1.1 cgd PLAN *
1201 1.24 christos c_not(argvp, isok)
1202 1.24 christos char ***argvp;
1203 1.24 christos int isok;
1204 1.1 cgd {
1205 1.10 jtc return (palloc(N_NOT, f_not));
1206 1.1 cgd }
1207 1.1 cgd
1208 1.1 cgd /*
1209 1.1 cgd * expression -o expression functions --
1210 1.1 cgd *
1211 1.1 cgd * Alternation of primaries; the OR operator. The second expression is
1212 1.1 cgd * not evaluated if the first expression is true.
1213 1.1 cgd */
1214 1.10 jtc int
1215 1.1 cgd f_or(plan, entry)
1216 1.1 cgd PLAN *plan;
1217 1.1 cgd FTSENT *entry;
1218 1.1 cgd {
1219 1.21 lukem PLAN *p;
1220 1.21 lukem int state;
1221 1.1 cgd
1222 1.21 lukem state = 0;
1223 1.1 cgd for (p = plan->p_data[0];
1224 1.1 cgd p && (state = (p->eval)(p, entry)); p = p->next);
1225 1.1 cgd
1226 1.1 cgd if (state)
1227 1.10 jtc return (1);
1228 1.1 cgd
1229 1.1 cgd for (p = plan->p_data[1];
1230 1.1 cgd p && (state = (p->eval)(p, entry)); p = p->next);
1231 1.10 jtc return (state);
1232 1.1 cgd }
1233 1.1 cgd
1234 1.1 cgd PLAN *
1235 1.24 christos c_or(argvp, isok)
1236 1.24 christos char ***argvp;
1237 1.24 christos int isok;
1238 1.1 cgd {
1239 1.10 jtc return (palloc(N_OR, f_or));
1240 1.1 cgd }
1241 1.1 cgd
1242 1.24 christos PLAN *
1243 1.24 christos c_null(argvp, isok)
1244 1.24 christos char ***argvp;
1245 1.24 christos int isok;
1246 1.24 christos {
1247 1.24 christos return NULL;
1248 1.24 christos }
1249 1.24 christos
1250 1.1 cgd static PLAN *
1251 1.1 cgd palloc(t, f)
1252 1.1 cgd enum ntype t;
1253 1.10 jtc int (*f) __P((PLAN *, FTSENT *));
1254 1.1 cgd {
1255 1.1 cgd PLAN *new;
1256 1.1 cgd
1257 1.22 mrg if ((new = malloc(sizeof(PLAN))) == NULL)
1258 1.24 christos err(1, "%s", "");
1259 1.22 mrg new->type = t;
1260 1.22 mrg new->eval = f;
1261 1.22 mrg new->flags = 0;
1262 1.22 mrg new->next = NULL;
1263 1.22 mrg return (new);
1264 1.1 cgd }
1265