Annotation of src/usr.bin/find/find.c, Revision 1.8
1.8 ! millert 1: /* $OpenBSD: find.c,v 1.7 1999/10/04 21:17:32 millert 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.
18: * 3. All advertising materials mentioning features or use of this software
19: * must display the following acknowledgement:
20: * This product includes software developed by the University of
21: * California, Berkeley and its contributors.
22: * 4. Neither the name of the University nor the names of its contributors
23: * may be used to endorse or promote products derived from this software
24: * without specific prior written permission.
25: *
26: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36: * SUCH DAMAGE.
37: */
38:
39: #ifndef lint
40: /*static char sccsid[] = "from: @(#)find.c 8.1 (Berkeley) 6/6/93";*/
1.8 ! millert 41: static char rcsid[] = "$OpenBSD: find.c,v 1.7 1999/10/04 21:17:32 millert Exp $";
1.1 deraadt 42: #endif /* not lint */
43:
44: #include <sys/types.h>
45: #include <sys/stat.h>
46:
47: #include <err.h>
48: #include <errno.h>
49: #include <fts.h>
1.8 ! millert 50: #include <signal.h>
1.1 deraadt 51: #include <stdio.h>
52: #include <string.h>
53: #include <stdlib.h>
54:
55: #include "find.h"
56:
57: /*
58: * find_formplan --
59: * process the command line and create a "plan" corresponding to the
60: * command arguments.
61: */
62: PLAN *
63: find_formplan(argv)
64: char **argv;
65: {
66: PLAN *plan, *tail, *new;
67:
68: /*
69: * for each argument in the command line, determine what kind of node
70: * it is, create the appropriate node type and add the new plan node
71: * to the end of the existing plan. The resulting plan is a linked
72: * list of plan nodes. For example, the string:
73: *
74: * % find . -name foo -newer bar -print
75: *
76: * results in the plan:
77: *
78: * [-name foo]--> [-newer bar]--> [-print]
79: *
80: * in this diagram, `[-name foo]' represents the plan node generated
81: * by c_name() with an argument of foo and `-->' represents the
82: * plan->next pointer.
83: */
84: for (plan = tail = NULL; *argv;) {
85: if (!(new = find_create(&argv)))
86: continue;
87: if (plan == NULL)
88: tail = plan = new;
89: else {
90: tail->next = new;
91: tail = new;
92: }
93: }
94:
95: /*
96: * if the user didn't specify one of -print, -ok or -exec, then -print
97: * is assumed so we bracket the current expression with parens, if
98: * necessary, and add a -print node on the end.
99: */
100: if (!isoutput) {
101: if (plan == NULL) {
102: new = c_print();
103: tail = plan = new;
104: } else {
105: new = c_openparen();
106: new->next = plan;
107: plan = new;
108: new = c_closeparen();
109: tail->next = new;
110: tail = new;
111: new = c_print();
112: tail->next = new;
113: tail = new;
114: }
115: }
116:
117: /*
118: * the command line has been completely processed into a search plan
119: * except for the (, ), !, and -o operators. Rearrange the plan so
120: * that the portions of the plan which are affected by the operators
121: * are moved into operator nodes themselves. For example:
122: *
123: * [!]--> [-name foo]--> [-print]
124: *
125: * becomes
126: *
127: * [! [-name foo] ]--> [-print]
128: *
129: * and
130: *
131: * [(]--> [-depth]--> [-name foo]--> [)]--> [-print]
132: *
133: * becomes
134: *
135: * [expr [-depth]-->[-name foo] ]--> [-print]
136: *
137: * operators are handled in order of precedence.
138: */
139:
140: plan = paren_squish(plan); /* ()'s */
141: plan = not_squish(plan); /* !'s */
142: plan = or_squish(plan); /* -o's */
143: return (plan);
144: }
145:
146: FTS *tree; /* pointer to top of FTS hierarchy */
147:
148: /*
149: * find_execute --
150: * take a search plan and an array of search paths and executes the plan
151: * over all FTSENT's returned for the given search paths.
152: */
1.3 tholo 153:
154: FTSENT *entry; /* shared with SIGINFO handler */
155:
1.1 deraadt 156: void
157: find_execute(plan, paths)
158: PLAN *plan; /* search plan */
159: char **paths; /* array of pathnames to traverse */
160: {
1.8 ! millert 161: sigset_t fullset, oset;
1.1 deraadt 162: PLAN *p;
163:
1.4 kstailey 164: if (!(tree = fts_open(paths, ftsoptions, NULL)))
1.1 deraadt 165: err(1, "ftsopen");
166:
1.8 ! millert 167: sigfillset(&fullset);
! 168: for (;;) {
! 169: (void)sigprocmask(SIG_BLOCK, &fullset, &oset);
! 170: entry = fts_read(tree);
! 171: (void)sigprocmask(SIG_SETMASK, &oset, NULL);
! 172: if (entry == NULL)
! 173: break;
! 174:
1.6 millert 175: switch (entry->fts_info) {
1.1 deraadt 176: case FTS_D:
177: if (isdepth)
178: continue;
179: break;
180: case FTS_DP:
181: if (!isdepth)
182: continue;
183: break;
184: case FTS_DNR:
185: case FTS_ERR:
186: case FTS_NS:
187: (void)fflush(stdout);
188: warn("%s", entry->fts_path);
189: continue;
190: }
191: #define BADCH " \t\n\\'\""
192: if (isxargs && strpbrk(entry->fts_path, BADCH)) {
193: (void)fflush(stdout);
194: warnx("%s: illegal path", entry->fts_path);
195: continue;
196: }
1.6 millert 197:
198: /*
199: * Call all the functions in the execution plan until one is
1.1 deraadt 200: * false or all have been executed. This is where we do all
201: * the work specified by the user on the command line.
202: */
1.6 millert 203: for (p = plan; p && (p->eval)(p, entry); p = p->next)
204: ;
1.1 deraadt 205: }
206: (void)fts_close(tree);
207: }