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