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