Home | History | Annotate | Line # | Download | only in find
find.c revision 1.24
      1 /*	$NetBSD: find.c,v 1.24 2007/09/25 04:09:03 lukem Exp $	*/
      2 
      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 #include <sys/cdefs.h>
     36 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "from: @(#)find.c	8.5 (Berkeley) 8/5/94";
     39 #else
     40 __RCSID("$NetBSD: find.c,v 1.24 2007/09/25 04:09:03 lukem Exp $");
     41 #endif
     42 #endif /* not lint */
     43 
     44 #include <sys/types.h>
     45 #include <sys/stat.h>
     46 
     47 #include <err.h>
     48 #include <errno.h>
     49 #include <fts.h>
     50 #include <stdio.h>
     51 #include <string.h>
     52 #include <stdlib.h>
     53 
     54 #include "find.h"
     55 
     56 static int ftscompare(const FTSENT **, const FTSENT **);
     57 
     58 static void sig_lock(sigset_t *);
     59 static void sig_unlock(const sigset_t *);
     60 
     61 /*
     62  * find_formplan --
     63  *	process the command line and create a "plan" corresponding to the
     64  *	command arguments.
     65  */
     66 PLAN *
     67 find_formplan(char **argv)
     68 {
     69 	PLAN *plan, *tail, *new;
     70 
     71 	/*
     72 	 * for each argument in the command line, determine what kind of node
     73 	 * it is, create the appropriate node type and add the new plan node
     74 	 * to the end of the existing plan.  The resulting plan is a linked
     75 	 * list of plan nodes.  For example, the string:
     76 	 *
     77 	 *	% find . -name foo -newer bar -print
     78 	 *
     79 	 * results in the plan:
     80 	 *
     81 	 *	[-name foo]--> [-newer bar]--> [-print]
     82 	 *
     83 	 * in this diagram, `[-name foo]' represents the plan node generated
     84 	 * by c_name() with an argument of foo and `-->' represents the
     85 	 * plan->next pointer.
     86 	 */
     87 	for (plan = tail = NULL; *argv;) {
     88 		if (!(new = find_create(&argv)))
     89 			continue;
     90 		if (plan == NULL)
     91 			tail = plan = new;
     92 		else {
     93 			tail->next = new;
     94 			tail = new;
     95 		}
     96 	}
     97 
     98 	/*
     99 	 * if the user didn't specify one of -print, -ok, -fprint, -exec, or
    100 	 * -exit, then -print is assumed so we bracket the current expression
    101 	 * with parens, if necessary, and add a -print node on the end.
    102 	 */
    103 	if (!isoutput) {
    104 		if (plan == NULL) {
    105 			new = c_print(NULL, 0);
    106 			tail = plan = new;
    107 		} else {
    108 			new = c_openparen(NULL, 0);
    109 			new->next = plan;
    110 			plan = new;
    111 			new = c_closeparen(NULL, 0);
    112 			tail->next = new;
    113 			tail = new;
    114 			new = c_print(NULL, 0);
    115 			tail->next = new;
    116 			tail = new;
    117 		}
    118 	}
    119 
    120 	/*
    121 	 * the command line has been completely processed into a search plan
    122 	 * except for the (, ), !, and -o operators.  Rearrange the plan so
    123 	 * that the portions of the plan which are affected by the operators
    124 	 * are moved into operator nodes themselves.  For example:
    125 	 *
    126 	 *	[!]--> [-name foo]--> [-print]
    127 	 *
    128 	 * becomes
    129 	 *
    130 	 *	[! [-name foo] ]--> [-print]
    131 	 *
    132 	 * and
    133 	 *
    134 	 *	[(]--> [-depth]--> [-name foo]--> [)]--> [-print]
    135 	 *
    136 	 * becomes
    137 	 *
    138 	 *	[expr [-depth]-->[-name foo] ]--> [-print]
    139 	 *
    140 	 * operators are handled in order of precedence.
    141 	 */
    142 
    143 	plan = paren_squish(plan);		/* ()'s */
    144 	plan = not_squish(plan);		/* !'s */
    145 	plan = or_squish(plan);			/* -o's */
    146 	return (plan);
    147 }
    148 
    149 static int
    150 ftscompare(const FTSENT **e1, const FTSENT **e2)
    151 {
    152 
    153 	return (strcoll((*e1)->fts_name, (*e2)->fts_name));
    154 }
    155 
    156 static void
    157 sig_lock(sigset_t *s)
    158 {
    159 	sigset_t new;
    160 
    161 	sigemptyset(&new);
    162 	sigaddset(&new, SIGINFO); /* block SIGINFO */
    163 	sigprocmask(SIG_BLOCK, &new, s);
    164 }
    165 
    166 static void
    167 sig_unlock(const sigset_t *s)
    168 {
    169 
    170 	sigprocmask(SIG_SETMASK, s, NULL);
    171 }
    172 
    173 FTS *tree;			/* pointer to top of FTS hierarchy */
    174 FTSENT *g_entry;		/* shared with SIGINFO handler */
    175 
    176 /*
    177  * find_execute --
    178  *	take a search plan and an array of search paths and executes the plan
    179  *	over all FTSENT's returned for the given search paths.
    180  */
    181 int
    182 find_execute(PLAN *plan, char **paths)
    183 {
    184 	PLAN *p;
    185 	int r, rval, cval;
    186 	sigset_t s;
    187 
    188 	cval = 1;
    189 
    190 	if (!(tree = fts_open(paths, ftsoptions, issort ? ftscompare : NULL)))
    191 		err(1, "ftsopen");
    192 
    193 	sig_lock(&s);
    194 	for (rval = 0; cval && (g_entry = fts_read(tree)) != NULL; sig_lock(&s)) {
    195 		sig_unlock(&s);
    196 		switch (g_entry->fts_info) {
    197 		case FTS_D:
    198 			if (isdepth)
    199 				continue;
    200 			break;
    201 		case FTS_DP:
    202 			if (!isdepth)
    203 				continue;
    204 			break;
    205 		case FTS_DNR:
    206 		case FTS_ERR:
    207 		case FTS_NS:
    208 			(void)fflush(stdout);
    209 			warnx("%s: %s",
    210 			    g_entry->fts_path, strerror(g_entry->fts_errno));
    211 			rval = 1;
    212 			continue;
    213 		}
    214 #define	BADCH	" \t\n\\'\""
    215 		if (isxargs && strpbrk(g_entry->fts_path, BADCH)) {
    216 			(void)fflush(stdout);
    217 			warnx("%s: illegal path", g_entry->fts_path);
    218 			rval = 1;
    219 			continue;
    220 		}
    221 
    222 		/*
    223 		 * Call all the functions in the execution plan until one is
    224 		 * false or all have been executed.  This is where we do all
    225 		 * the work specified by the user on the command line.
    226 		 */
    227 		for (p = plan; p && (p->eval)(p, g_entry); p = p->next)
    228 			if (p->type == N_EXIT) {
    229 				rval = p->exit_val;
    230 				cval = 0;
    231 			}
    232 	}
    233 
    234 	sig_unlock(&s);
    235 	if (errno)
    236 		err(1, "fts_read");
    237 	(void)fts_close(tree);
    238 
    239 	/*
    240 	 * Cleanup any plans with leftover state.
    241 	 * Keep the last non-zero return value.
    242 	 */
    243 	if ((r = find_traverse(plan, plan_cleanup, NULL)) != 0)
    244 		rval = r;
    245 
    246 	return (rval);
    247 }
    248 
    249 /*
    250  * find_traverse --
    251  *	traverse the plan tree and execute func() on all plans.  This
    252  *	does not evaluate each plan's eval() function; it is intended
    253  *	for operations that must run on all plans, such as state
    254  *	cleanup.
    255  *
    256  *	If any func() returns non-zero, then so will find_traverse().
    257  */
    258 int
    259 find_traverse(plan, func, arg)
    260 	PLAN *plan;
    261 	int (*func)(PLAN *, void *);
    262 	void *arg;
    263 {
    264 	PLAN *p;
    265 	int r, rval;
    266 
    267 	rval = 0;
    268 	for (p = plan; p; p = p->next) {
    269 		if ((r = func(p, arg)) != 0)
    270 			rval = r;
    271 		if (p->type == N_EXPR || p->type == N_OR) {
    272 			if (p->p_data[0])
    273 				if ((r = find_traverse(p->p_data[0],
    274 					    func, arg)) != 0)
    275 					rval = r;
    276 			if (p->p_data[1])
    277 				if ((r = find_traverse(p->p_data[1],
    278 					    func, arg)) != 0)
    279 					rval = r;
    280 		}
    281 	}
    282 	return rval;
    283 }
    284