find.c revision 93604
11590Srgrimes/*- 21590Srgrimes * Copyright (c) 1991, 1993, 1994 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * This code is derived from software contributed to Berkeley by 61590Srgrimes * Cimarron D. Taylor of the University of California, Berkeley. 71590Srgrimes * 81590Srgrimes * Redistribution and use in source and binary forms, with or without 91590Srgrimes * modification, are permitted provided that the following conditions 101590Srgrimes * are met: 111590Srgrimes * 1. Redistributions of source code must retain the above copyright 121590Srgrimes * notice, this list of conditions and the following disclaimer. 131590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141590Srgrimes * notice, this list of conditions and the following disclaimer in the 151590Srgrimes * documentation and/or other materials provided with the distribution. 161590Srgrimes * 3. All advertising materials mentioning features or use of this software 171590Srgrimes * must display the following acknowledgement: 181590Srgrimes * This product includes software developed by the University of 191590Srgrimes * California, Berkeley and its contributors. 201590Srgrimes * 4. Neither the name of the University nor the names of its contributors 211590Srgrimes * may be used to endorse or promote products derived from this software 221590Srgrimes * without specific prior written permission. 231590Srgrimes * 241590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341590Srgrimes * SUCH DAMAGE. 351590Srgrimes */ 361590Srgrimes 371590Srgrimes#ifndef lint 3861575Sroberto#if 0 3923695Speterstatic char sccsid[] = "@(#)find.c 8.5 (Berkeley) 8/5/94"; 4061575Sroberto#else 4161575Sroberto#endif 421590Srgrimes#endif /* not lint */ 4393604Sobrien#include <sys/cdefs.h> 4493604Sobrien__FBSDID("$FreeBSD: head/usr.bin/find/find.c 93604 2002-04-01 22:56:56Z obrien $"); 451590Srgrimes 461590Srgrimes#include <sys/types.h> 471590Srgrimes#include <sys/stat.h> 481590Srgrimes 491590Srgrimes#include <err.h> 501590Srgrimes#include <errno.h> 511590Srgrimes#include <fts.h> 5272945Sknu#include <regex.h> 531590Srgrimes#include <stdio.h> 541590Srgrimes#include <string.h> 551590Srgrimes#include <stdlib.h> 561590Srgrimes 571590Srgrimes#include "find.h" 581590Srgrimes 5992786Smarkmstatic int find_compare(const FTSENT **s1, const FTSENT **s2); 6041391Swosch 611590Srgrimes/* 6241398Sbde * find_compare -- 6341398Sbde * tell fts_open() how to order the traversal of the hierarchy. 6441399Sbde * This variant gives lexicographical order, i.e., alphabetical 6541399Sbde * order within each directory. 6641398Sbde */ 6741398Sbdestatic int 6841398Sbdefind_compare(s1, s2) 6941398Sbde const FTSENT **s1, **s2; 7041398Sbde{ 7141398Sbde 7241398Sbde return (strcoll((*s1)->fts_name, (*s2)->fts_name)); 7341398Sbde} 7441398Sbde 7541398Sbde/* 761590Srgrimes * find_formplan -- 771590Srgrimes * process the command line and create a "plan" corresponding to the 781590Srgrimes * command arguments. 791590Srgrimes */ 801590SrgrimesPLAN * 811590Srgrimesfind_formplan(argv) 821590Srgrimes char **argv; 831590Srgrimes{ 841590Srgrimes PLAN *plan, *tail, *new; 851590Srgrimes 861590Srgrimes /* 871590Srgrimes * for each argument in the command line, determine what kind of node 881590Srgrimes * it is, create the appropriate node type and add the new plan node 891590Srgrimes * to the end of the existing plan. The resulting plan is a linked 901590Srgrimes * list of plan nodes. For example, the string: 911590Srgrimes * 921590Srgrimes * % find . -name foo -newer bar -print 931590Srgrimes * 941590Srgrimes * results in the plan: 951590Srgrimes * 961590Srgrimes * [-name foo]--> [-newer bar]--> [-print] 971590Srgrimes * 981590Srgrimes * in this diagram, `[-name foo]' represents the plan node generated 991590Srgrimes * by c_name() with an argument of foo and `-->' represents the 1001590Srgrimes * plan->next pointer. 1011590Srgrimes */ 1021590Srgrimes for (plan = tail = NULL; *argv;) { 1031590Srgrimes if (!(new = find_create(&argv))) 1041590Srgrimes continue; 1051590Srgrimes if (plan == NULL) 1061590Srgrimes tail = plan = new; 1071590Srgrimes else { 1081590Srgrimes tail->next = new; 1091590Srgrimes tail = new; 1101590Srgrimes } 1111590Srgrimes } 1128874Srgrimes 1131590Srgrimes /* 1141590Srgrimes * if the user didn't specify one of -print, -ok or -exec, then -print 11523695Speter * is assumed so we bracket the current expression with parens, if 11623695Speter * necessary, and add a -print node on the end. 1171590Srgrimes */ 1181590Srgrimes if (!isoutput) { 11976250Sphk OPTION *p; 12091400Sdwmalone char **argv1 = 0; 12176250Sphk 12223695Speter if (plan == NULL) { 12391400Sdwmalone p = lookup_option("-print"); 12491400Sdwmalone new = (p->create)(p, &argv1); 1251590Srgrimes tail = plan = new; 12623695Speter } else { 12791400Sdwmalone p = lookup_option("("); 12891400Sdwmalone new = (p->create)(p, &argv1); 1296776Sguido new->next = plan; 1306776Sguido plan = new; 13191400Sdwmalone p = lookup_option(")"); 13291400Sdwmalone new = (p->create)(p, &argv1); 1331590Srgrimes tail->next = new; 1341590Srgrimes tail = new; 13591400Sdwmalone p = lookup_option("-print"); 13691400Sdwmalone new = (p->create)(p, &argv1); 1376776Sguido tail->next = new; 1386776Sguido tail = new; 1391590Srgrimes } 1401590Srgrimes } 1418874Srgrimes 1421590Srgrimes /* 1431590Srgrimes * the command line has been completely processed into a search plan 1441590Srgrimes * except for the (, ), !, and -o operators. Rearrange the plan so 1451590Srgrimes * that the portions of the plan which are affected by the operators 1461590Srgrimes * are moved into operator nodes themselves. For example: 1471590Srgrimes * 1481590Srgrimes * [!]--> [-name foo]--> [-print] 1491590Srgrimes * 1501590Srgrimes * becomes 1511590Srgrimes * 1521590Srgrimes * [! [-name foo] ]--> [-print] 1531590Srgrimes * 1541590Srgrimes * and 1551590Srgrimes * 1561590Srgrimes * [(]--> [-depth]--> [-name foo]--> [)]--> [-print] 1571590Srgrimes * 1581590Srgrimes * becomes 1591590Srgrimes * 1601590Srgrimes * [expr [-depth]-->[-name foo] ]--> [-print] 1611590Srgrimes * 1621590Srgrimes * operators are handled in order of precedence. 1631590Srgrimes */ 1641590Srgrimes 1651590Srgrimes plan = paren_squish(plan); /* ()'s */ 1661590Srgrimes plan = not_squish(plan); /* !'s */ 1671590Srgrimes plan = or_squish(plan); /* -o's */ 1681590Srgrimes return (plan); 1691590Srgrimes} 1708874Srgrimes 1711590SrgrimesFTS *tree; /* pointer to top of FTS hierarchy */ 1721590Srgrimes 1731590Srgrimes/* 1741590Srgrimes * find_execute -- 1751590Srgrimes * take a search plan and an array of search paths and executes the plan 1761590Srgrimes * over all FTSENT's returned for the given search paths. 1771590Srgrimes */ 1781590Srgrimesint 1791590Srgrimesfind_execute(plan, paths) 1801590Srgrimes PLAN *plan; /* search plan */ 1811590Srgrimes char **paths; /* array of pathnames to traverse */ 1821590Srgrimes{ 18391400Sdwmalone FTSENT *entry; 1841590Srgrimes PLAN *p; 1851590Srgrimes int rval; 1868874Srgrimes 18741398Sbde tree = fts_open(paths, ftsoptions, (issort ? find_compare : NULL)); 18841398Sbde if (tree == NULL) 1891590Srgrimes err(1, "ftsopen"); 1901590Srgrimes 1911590Srgrimes for (rval = 0; (entry = fts_read(tree)) != NULL;) { 1921590Srgrimes switch (entry->fts_info) { 1931590Srgrimes case FTS_D: 1941590Srgrimes if (isdepth) 1951590Srgrimes continue; 1961590Srgrimes break; 1971590Srgrimes case FTS_DP: 1981590Srgrimes if (!isdepth) 1991590Srgrimes continue; 2001590Srgrimes break; 2011590Srgrimes case FTS_DNR: 2021590Srgrimes case FTS_ERR: 2031590Srgrimes case FTS_NS: 2041590Srgrimes (void)fflush(stdout); 2051590Srgrimes warnx("%s: %s", 2061590Srgrimes entry->fts_path, strerror(entry->fts_errno)); 2071590Srgrimes rval = 1; 2081590Srgrimes continue; 20923695Speter#ifdef FTS_W 21023695Speter case FTS_W: 21123695Speter continue; 21223695Speter#endif /* FTS_W */ 2131590Srgrimes } 2141590Srgrimes#define BADCH " \t\n\\'\"" 2151590Srgrimes if (isxargs && strpbrk(entry->fts_path, BADCH)) { 2161590Srgrimes (void)fflush(stdout); 2171590Srgrimes warnx("%s: illegal path", entry->fts_path); 2181590Srgrimes rval = 1; 2191590Srgrimes continue; 2201590Srgrimes } 2218874Srgrimes 22261575Sroberto if (mindepth != -1 && entry->fts_level < mindepth) 22361575Sroberto continue; 22461575Sroberto 2251590Srgrimes /* 2261590Srgrimes * Call all the functions in the execution plan until one is 2271590Srgrimes * false or all have been executed. This is where we do all 2281590Srgrimes * the work specified by the user on the command line. 2291590Srgrimes */ 23076250Sphk for (p = plan; p && (p->execute)(p, entry); p = p->next); 23161575Sroberto 23261575Sroberto if (maxdepth != -1 && entry->fts_level >= maxdepth) { 23361575Sroberto if (fts_set(tree, entry, FTS_SKIP)) 23461575Sroberto err(1, "%s", entry->fts_path); 23561575Sroberto continue; 23661575Sroberto } 2371590Srgrimes } 2381590Srgrimes if (errno) 2391590Srgrimes err(1, "fts_read"); 2401590Srgrimes return (rval); 2411590Srgrimes} 242