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 * @(#)find.c 8.5 (Berkeley) 8/5/94 37 * $FreeBSD: src/usr.bin/find/find.c,v 1.7.6.3 2001/05/06 09:53:22 phk Exp $ 38 * $DragonFly: src/usr.bin/find/find.c,v 1.3 2003/10/04 20:36:44 hmp Exp $ 39 */ 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 <regex.h> 48 #include <stdio.h> 49 #include <string.h> 50 #include <stdlib.h> 51 52 #include "find.h" 53 54 static int find_compare(const FTSENT **s1, const FTSENT **s2); 55 56 /* 57 * find_compare -- 58 * tell fts_open() how to order the traversal of the hierarchy. 59 * This variant gives lexicographical order, i.e., alphabetical 60 * order within each directory. 61 */ 62 static int 63 find_compare(const FTSENT **s1, const FTSENT **s2) 64 { 65 66 return (strcoll((*s1)->fts_name, (*s2)->fts_name)); 67 } 68 69 /* 70 * find_formplan -- 71 * process the command line and create a "plan" corresponding to the 72 * command arguments. 73 */ 74 PLAN * 75 find_formplan(char **argv) 76 { 77 PLAN *plan, *tail, *new; 78 79 /* 80 * for each argument in the command line, determine what kind of node 81 * it is, create the appropriate node type and add the new plan node 82 * to the end of the existing plan. The resulting plan is a linked 83 * list of plan nodes. For example, the string: 84 * 85 * % find . -name foo -newer bar -print 86 * 87 * results in the plan: 88 * 89 * [-name foo]--> [-newer bar]--> [-print] 90 * 91 * in this diagram, `[-name foo]' represents the plan node generated 92 * by c_name() with an argument of foo and `-->' represents the 93 * plan->next pointer. 94 */ 95 for (plan = tail = NULL; *argv;) { 96 if (!(new = find_create(&argv))) 97 continue; 98 if (plan == NULL) 99 tail = plan = new; 100 else { 101 tail->next = new; 102 tail = new; 103 } 104 } 105 106 /* 107 * if the user didn't specify one of -print, -ok or -exec, then -print 108 * is assumed so we bracket the current expression with parens, if 109 * necessary, and add a -print node on the end. 110 */ 111 if (!isoutput) { 112 OPTION *p; 113 char **argv = 0; 114 115 if (plan == NULL) { 116 p = option("-print"); 117 new = (p->create)(p, &argv); 118 tail = plan = new; 119 } else { 120 p = option("("); 121 new = (p->create)(p, &argv); 122 new->next = plan; 123 plan = new; 124 p = option(")"); 125 new = (p->create)(p, &argv); 126 tail->next = new; 127 tail = new; 128 p = option("-print"); 129 new = (p->create)(p, &argv); 130 tail->next = new; 131 tail = new; 132 } 133 } 134 135 /* 136 * the command line has been completely processed into a search plan 137 * except for the (, ), !, and -o operators. Rearrange the plan so 138 * that the portions of the plan which are affected by the operators 139 * are moved into operator nodes themselves. For example: 140 * 141 * [!]--> [-name foo]--> [-print] 142 * 143 * becomes 144 * 145 * [! [-name foo] ]--> [-print] 146 * 147 * and 148 * 149 * [(]--> [-depth]--> [-name foo]--> [)]--> [-print] 150 * 151 * becomes 152 * 153 * [expr [-depth]-->[-name foo] ]--> [-print] 154 * 155 * operators are handled in order of precedence. 156 */ 157 158 plan = paren_squish(plan); /* ()'s */ 159 plan = not_squish(plan); /* !'s */ 160 plan = or_squish(plan); /* -o's */ 161 return (plan); 162 } 163 164 FTS *tree; /* pointer to top of FTS hierarchy */ 165 166 /* 167 * find_execute -- 168 * take a search plan and an array of search paths and executes the plan 169 * over all FTSENT's returned for the given search paths. 170 * 171 * plan: search plan 172 * paths: array of pathnames to traverse 173 */ 174 int 175 find_execute(PLAN *plan, char **paths) 176 { 177 register FTSENT *entry; 178 PLAN *p; 179 int rval; 180 181 tree = fts_open(paths, ftsoptions, (issort ? find_compare : NULL)); 182 if (tree == NULL) 183 err(1, "ftsopen"); 184 185 for (rval = 0; (entry = fts_read(tree)) != NULL;) { 186 switch (entry->fts_info) { 187 case FTS_D: 188 if (isdepth) 189 continue; 190 break; 191 case FTS_DP: 192 if (!isdepth) 193 continue; 194 break; 195 case FTS_DNR: 196 case FTS_ERR: 197 case FTS_NS: 198 (void)fflush(stdout); 199 warnx("%s: %s", 200 entry->fts_path, strerror(entry->fts_errno)); 201 rval = 1; 202 continue; 203 #ifdef FTS_W 204 case FTS_W: 205 continue; 206 #endif /* FTS_W */ 207 } 208 #define BADCH " \t\n\\'\"" 209 if (isxargs && strpbrk(entry->fts_path, BADCH)) { 210 (void)fflush(stdout); 211 warnx("%s: illegal path", entry->fts_path); 212 rval = 1; 213 continue; 214 } 215 216 if (mindepth != -1 && entry->fts_level < mindepth) 217 continue; 218 219 /* 220 * Call all the functions in the execution plan until one is 221 * false or all have been executed. This is where we do all 222 * the work specified by the user on the command line. 223 */ 224 for (p = plan; p && (p->execute)(p, entry); p = p->next); 225 226 if (maxdepth != -1 && entry->fts_level >= maxdepth) { 227 if (fts_set(tree, entry, FTS_SKIP)) 228 err(1, "%s", entry->fts_path); 229 continue; 230 } 231 } 232 if (errno) 233 err(1, "fts_read"); 234 return (rval); 235 } 236