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