1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1991, 1993, 1994 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. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #if 0 36 static char sccsid[] = "@(#)find.c 8.5 (Berkeley) 8/5/94"; 37 #endif 38 39 #include <sys/cdefs.h> 40 #include <sys/types.h> 41 #include <sys/stat.h> 42 43 #include <err.h> 44 #include <errno.h> 45 #include <fts.h> 46 #include <regex.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 51 #include "find.h" 52 53 static int find_compare(const FTSENT * const *s1, const FTSENT * const *s2); 54 55 /* 56 * find_compare -- 57 * tell fts_open() how to order the traversal of the hierarchy. 58 * This variant gives lexicographical order, i.e., alphabetical 59 * order within each directory. 60 */ 61 static int 62 find_compare(const FTSENT * const *s1, const FTSENT * const *s2) 63 { 64 65 return (strcoll((*s1)->fts_name, (*s2)->fts_name)); 66 } 67 68 /* 69 * find_formplan -- 70 * process the command line and create a "plan" corresponding to the 71 * command arguments. 72 */ 73 PLAN * 74 find_formplan(char *argv[]) 75 { 76 PLAN *plan, *tail, *new; 77 78 /* 79 * for each argument in the command line, determine what kind of node 80 * it is, create the appropriate node type and add the new plan node 81 * to the end of the existing plan. The resulting plan is a linked 82 * list of plan nodes. For example, the string: 83 * 84 * % find . -name foo -newer bar -print 85 * 86 * results in the plan: 87 * 88 * [-name foo]--> [-newer bar]--> [-print] 89 * 90 * in this diagram, `[-name foo]' represents the plan node generated 91 * by c_name() with an argument of foo and `-->' represents the 92 * plan->next pointer. 93 */ 94 for (plan = tail = NULL; *argv;) { 95 if (!(new = find_create(&argv))) 96 continue; 97 if (plan == NULL) 98 tail = plan = new; 99 else { 100 tail->next = new; 101 tail = new; 102 } 103 } 104 105 /* 106 * if the user didn't specify one of -print, -ok or -exec, then -print 107 * is assumed so we bracket the current expression with parens, if 108 * necessary, and add a -print node on the end. 109 */ 110 if (!isoutput) { 111 OPTION *p; 112 char **argv1 = 0; 113 114 if (plan == NULL) { 115 p = lookup_option("-print"); 116 new = (p->create)(p, &argv1); 117 tail = plan = new; 118 } else { 119 p = lookup_option("("); 120 new = (p->create)(p, &argv1); 121 new->next = plan; 122 plan = new; 123 p = lookup_option(")"); 124 new = (p->create)(p, &argv1); 125 tail->next = new; 126 tail = new; 127 p = lookup_option("-print"); 128 new = (p->create)(p, &argv1); 129 tail->next = new; 130 tail = new; 131 } 132 } 133 134 /* 135 * the command line has been completely processed into a search plan 136 * except for the (, ), !, and -o operators. Rearrange the plan so 137 * that the portions of the plan which are affected by the operators 138 * are moved into operator nodes themselves. For example: 139 * 140 * [!]--> [-name foo]--> [-print] 141 * 142 * becomes 143 * 144 * [! [-name foo] ]--> [-print] 145 * 146 * and 147 * 148 * [(]--> [-depth]--> [-name foo]--> [)]--> [-print] 149 * 150 * becomes 151 * 152 * [expr [-depth]-->[-name foo] ]--> [-print] 153 * 154 * operators are handled in order of precedence. 155 */ 156 157 plan = paren_squish(plan); /* ()'s */ 158 plan = not_squish(plan); /* !'s */ 159 plan = or_squish(plan); /* -o's */ 160 return (plan); 161 } 162 163 FTS *tree; /* pointer to top of FTS hierarchy */ 164 165 /* 166 * find_execute -- 167 * take a search plan and an array of search paths and executes the plan 168 * over all FTSENT's returned for the given search paths. 169 */ 170 int 171 find_execute(PLAN *plan, char *paths[]) 172 { 173 FTSENT *entry; 174 PLAN *p; 175 int e; 176 177 tree = fts_open(paths, ftsoptions, (issort ? find_compare : NULL)); 178 if (tree == NULL) 179 err(1, "ftsopen"); 180 181 exitstatus = 0; 182 while (errno = 0, (entry = fts_read(tree)) != NULL) { 183 if (maxdepth != -1 && entry->fts_level >= maxdepth) { 184 if (fts_set(tree, entry, FTS_SKIP)) 185 err(1, "%s", entry->fts_path); 186 } 187 188 switch (entry->fts_info) { 189 case FTS_D: 190 if (isdepth) 191 continue; 192 break; 193 case FTS_DP: 194 if (!isdepth) 195 continue; 196 break; 197 case FTS_DNR: 198 case FTS_NS: 199 if (ignore_readdir_race && 200 entry->fts_errno == ENOENT && entry->fts_level > 0) 201 continue; 202 /* FALLTHROUGH */ 203 case FTS_ERR: 204 (void)fflush(stdout); 205 warnx("%s: %s", 206 entry->fts_path, strerror(entry->fts_errno)); 207 exitstatus = 1; 208 continue; 209 #if defined(FTS_W) && defined(FTS_WHITEOUT) 210 case FTS_W: 211 if (ftsoptions & FTS_WHITEOUT) 212 break; 213 continue; 214 #endif /* FTS_W */ 215 } 216 #define BADCH " \t\n\\'\"" 217 if (isxargs && strpbrk(entry->fts_path, BADCH)) { 218 (void)fflush(stdout); 219 warnx("%s: illegal path", entry->fts_path); 220 exitstatus = 1; 221 continue; 222 } 223 224 if (mindepth != -1 && entry->fts_level < mindepth) 225 continue; 226 227 /* 228 * Call all the functions in the execution plan until one is 229 * false or all have been executed. This is where we do all 230 * the work specified by the user on the command line. 231 */ 232 for (p = plan; p && (p->execute)(p, entry); p = p->next); 233 } 234 e = errno; 235 finish_execplus(); 236 if (e && (!ignore_readdir_race || e != ENOENT)) 237 errc(1, e, "fts_read"); 238 return (exitstatus); 239 } 240