Annotation of src/usr.bin/find/find.c, Revision 1.16
1.16 ! pascal 1: /* $OpenBSD: find.c,v 1.15 2011/04/21 01:14:21 jacekm Exp $ */
1.2 deraadt 2:
1.1 deraadt 3: /*-
4: * Copyright (c) 1991, 1993
5: * The Regents of the University of California. All rights reserved.
6: *
7: * This code is derived from software contributed to Berkeley by
8: * Cimarron D. Taylor of the University of California, Berkeley.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
1.9 millert 18: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 19: * may be used to endorse or promote products derived from this software
20: * without specific prior written permission.
21: *
22: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32: * SUCH DAMAGE.
33: */
34:
35: #include <sys/types.h>
36: #include <sys/stat.h>
37:
38: #include <err.h>
39: #include <errno.h>
40: #include <fts.h>
1.8 millert 41: #include <signal.h>
1.1 deraadt 42: #include <stdio.h>
43: #include <string.h>
44: #include <stdlib.h>
45:
46: #include "find.h"
47:
48: /*
49: * find_formplan --
50: * process the command line and create a "plan" corresponding to the
51: * command arguments.
52: */
53: PLAN *
1.11 deraadt 54: find_formplan(char **argv)
1.1 deraadt 55: {
56: PLAN *plan, *tail, *new;
57:
58: /*
59: * for each argument in the command line, determine what kind of node
60: * it is, create the appropriate node type and add the new plan node
61: * to the end of the existing plan. The resulting plan is a linked
62: * list of plan nodes. For example, the string:
63: *
64: * % find . -name foo -newer bar -print
65: *
66: * results in the plan:
67: *
68: * [-name foo]--> [-newer bar]--> [-print]
69: *
70: * in this diagram, `[-name foo]' represents the plan node generated
71: * by c_name() with an argument of foo and `-->' represents the
72: * plan->next pointer.
73: */
74: for (plan = tail = NULL; *argv;) {
75: if (!(new = find_create(&argv)))
76: continue;
77: if (plan == NULL)
78: tail = plan = new;
79: else {
80: tail->next = new;
81: tail = new;
82: }
83: }
84:
85: /*
86: * if the user didn't specify one of -print, -ok or -exec, then -print
87: * is assumed so we bracket the current expression with parens, if
88: * necessary, and add a -print node on the end.
89: */
90: if (!isoutput) {
91: if (plan == NULL) {
1.10 deraadt 92: new = c_print(NULL, NULL, 0);
1.1 deraadt 93: tail = plan = new;
94: } else {
1.10 deraadt 95: new = c_openparen(NULL, NULL, 0);
1.1 deraadt 96: new->next = plan;
97: plan = new;
1.10 deraadt 98: new = c_closeparen(NULL, NULL, 0);
1.1 deraadt 99: tail->next = new;
100: tail = new;
1.10 deraadt 101: new = c_print(NULL, NULL, 0);
1.1 deraadt 102: tail->next = new;
103: tail = new;
104: }
105: }
106:
107: /*
108: * the command line has been completely processed into a search plan
109: * except for the (, ), !, and -o operators. Rearrange the plan so
110: * that the portions of the plan which are affected by the operators
111: * are moved into operator nodes themselves. For example:
112: *
113: * [!]--> [-name foo]--> [-print]
114: *
115: * becomes
116: *
117: * [! [-name foo] ]--> [-print]
118: *
119: * and
120: *
121: * [(]--> [-depth]--> [-name foo]--> [)]--> [-print]
122: *
123: * becomes
124: *
125: * [expr [-depth]-->[-name foo] ]--> [-print]
126: *
127: * operators are handled in order of precedence.
128: */
129:
130: plan = paren_squish(plan); /* ()'s */
131: plan = not_squish(plan); /* !'s */
132: plan = or_squish(plan); /* -o's */
133: return (plan);
134: }
135:
136: FTS *tree; /* pointer to top of FTS hierarchy */
137:
138: /*
139: * find_execute --
140: * take a search plan and an array of search paths and executes the plan
141: * over all FTSENT's returned for the given search paths.
142: */
1.3 tholo 143:
144: FTSENT *entry; /* shared with SIGINFO handler */
145:
1.15 jacekm 146: int
1.11 deraadt 147: find_execute(PLAN *plan, /* search plan */
148: char **paths) /* array of pathnames to traverse */
1.1 deraadt 149: {
1.8 millert 150: sigset_t fullset, oset;
1.16 ! pascal 151: int r, rval;
1.1 deraadt 152: PLAN *p;
1.15 jacekm 153:
154: rval = 0;
1.1 deraadt 155:
1.4 kstailey 156: if (!(tree = fts_open(paths, ftsoptions, NULL)))
1.13 jmc 157: err(1, "fts_open");
1.1 deraadt 158:
1.8 millert 159: sigfillset(&fullset);
160: for (;;) {
161: (void)sigprocmask(SIG_BLOCK, &fullset, &oset);
162: entry = fts_read(tree);
163: (void)sigprocmask(SIG_SETMASK, &oset, NULL);
1.12 otto 164: if (entry == NULL) {
165: if (errno)
166: err(1, "fts_read");
1.8 millert 167: break;
1.12 otto 168: }
1.8 millert 169:
1.6 millert 170: switch (entry->fts_info) {
1.1 deraadt 171: case FTS_D:
172: if (isdepth)
173: continue;
174: break;
175: case FTS_DP:
176: if (!isdepth)
177: continue;
178: break;
179: case FTS_DNR:
180: case FTS_ERR:
181: case FTS_NS:
182: (void)fflush(stdout);
183: warn("%s", entry->fts_path);
1.15 jacekm 184: rval = 1;
1.1 deraadt 185: continue;
186: }
187: #define BADCH " \t\n\\'\""
188: if (isxargs && strpbrk(entry->fts_path, BADCH)) {
189: (void)fflush(stdout);
190: warnx("%s: illegal path", entry->fts_path);
1.15 jacekm 191: rval = 1;
1.1 deraadt 192: continue;
193: }
1.6 millert 194:
195: /*
196: * Call all the functions in the execution plan until one is
1.1 deraadt 197: * false or all have been executed. This is where we do all
198: * the work specified by the user on the command line.
199: */
1.6 millert 200: for (p = plan; p && (p->eval)(p, entry); p = p->next)
201: ;
1.1 deraadt 202: }
203: (void)fts_close(tree);
1.16 ! pascal 204:
! 205: /*
! 206: * Cleanup any plans with leftover state.
! 207: * Keep the last non-zero return value.
! 208: */
! 209: if ((r = find_traverse(plan, plan_cleanup, NULL)) != 0)
! 210: rval = r;
1.15 jacekm 211: return (rval);
1.16 ! pascal 212: }
! 213:
! 214: /*
! 215: * find_traverse --
! 216: * traverse the plan tree and execute func() on all plans. This
! 217: * does not evaluate each plan's eval() function; it is intended
! 218: * for operations that must run on all plans, such as state
! 219: * cleanup.
! 220: *
! 221: * If any func() returns non-zero, then so will find_traverse().
! 222: */
! 223: int
! 224: find_traverse(PLAN *plan, int (*func)(PLAN *, void *), void *arg)
! 225: {
! 226: PLAN *p;
! 227: int r, rval;
! 228:
! 229: rval = 0;
! 230: for (p = plan; p; p = p->next) {
! 231: if ((r = func(p, arg)) != 0)
! 232: rval = r;
! 233: if (p->type == N_EXPR || p->type == N_OR) {
! 234: if (p->p_data[0])
! 235: if ((r = find_traverse(p->p_data[0],
! 236: func, arg)) != 0)
! 237: rval = r;
! 238: if (p->p_data[1])
! 239: if ((r = find_traverse(p->p_data[1],
! 240: func, arg)) != 0)
! 241: rval = r;
! 242: }
! 243: }
! 244: return rval;
1.1 deraadt 245: }