Home | History | Annotate | Line # | Download | only in find
find.c revision 1.26
      1 /*	$NetBSD: find.c,v 1.26 2010/12/27 16:09:46 christos 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.26 2010/12/27 16:09:46 christos 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 <signal.h>
     51 #include <stdio.h>
     52 #include <string.h>
     53 #include <stdlib.h>
     54 #include <stdbool.h>
     55 #include <unistd.h>
     56 
     57 #include "find.h"
     58 
     59 static int ftscompare(const FTSENT **, const FTSENT **);
     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 sigset_t ss;
    157 static bool notty;
    158 
    159 static __inline void
    160 sig_init(void)
    161 {
    162 	notty = !(isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
    163 	    isatty(STDERR_FILENO));
    164 	if (notty)
    165 		return;
    166 	sigemptyset(&ss);
    167 	sigaddset(&ss, SIGINFO); /* block SIGINFO */
    168 }
    169 
    170 static __inline void
    171 sig_lock(sigset_t *s)
    172 {
    173 	if (notty)
    174 		return;
    175 	sigprocmask(SIG_BLOCK, &ss, s);
    176 }
    177 
    178 static __inline void
    179 sig_unlock(const sigset_t *s)
    180 {
    181 	if (notty)
    182 		return;
    183 	sigprocmask(SIG_SETMASK, s, NULL);
    184 }
    185 
    186 FTS *tree;			/* pointer to top of FTS hierarchy */
    187 FTSENT *g_entry;		/* shared with SIGINFO handler */
    188 
    189 /*
    190  * find_execute --
    191  *	take a search plan and an array of search paths and executes the plan
    192  *	over all FTSENT's returned for the given search paths.
    193  */
    194 int
    195 find_execute(PLAN *plan, char **paths)
    196 {
    197 	PLAN *p;
    198 	int r, rval, cval;
    199 	sigset_t s;
    200 
    201 	cval = 1;
    202 
    203 	if (!(tree = fts_open(paths, ftsoptions, issort ? ftscompare : NULL)))
    204 		err(1, "ftsopen");
    205 
    206 	sig_init();
    207 	sig_lock(&s);
    208 	for (rval = 0; cval && (g_entry = fts_read(tree)) != NULL;) {
    209 		switch (g_entry->fts_info) {
    210 		case FTS_D:
    211 			if (isdepth)
    212 				continue;
    213 			break;
    214 		case FTS_DP:
    215 			if (!isdepth)
    216 				continue;
    217 			break;
    218 		case FTS_DNR:
    219 		case FTS_ERR:
    220 		case FTS_NS:
    221 			sig_unlock(&s);
    222 			(void)fflush(stdout);
    223 			warnx("%s: %s",
    224 			    g_entry->fts_path, strerror(g_entry->fts_errno));
    225 			rval = 1;
    226 			sig_lock(&s);
    227 			continue;
    228 		}
    229 #define	BADCH	" \t\n\\'\""
    230 		if (isxargs && strpbrk(g_entry->fts_path, BADCH)) {
    231 			sig_unlock(&s);
    232 			(void)fflush(stdout);
    233 			warnx("%s: illegal path", g_entry->fts_path);
    234 			rval = 1;
    235 			sig_lock(&s);
    236 			continue;
    237 		}
    238 
    239 		/*
    240 		 * Call all the functions in the execution plan until one is
    241 		 * false or all have been executed.  This is where we do all
    242 		 * the work specified by the user on the command line.
    243 		 */
    244 		sig_unlock(&s);
    245 		for (p = plan; p && (p->eval)(p, g_entry); p = p->next)
    246 			if (p->type == N_EXIT) {
    247 				rval = p->exit_val;
    248 				cval = 0;
    249 			}
    250 		sig_lock(&s);
    251 	}
    252 
    253 	sig_unlock(&s);
    254 	if (errno)
    255 		err(1, "fts_read");
    256 	(void)fts_close(tree);
    257 
    258 	/*
    259 	 * Cleanup any plans with leftover state.
    260 	 * Keep the last non-zero return value.
    261 	 */
    262 	if ((r = find_traverse(plan, plan_cleanup, NULL)) != 0)
    263 		rval = r;
    264 
    265 	return (rval);
    266 }
    267 
    268 /*
    269  * find_traverse --
    270  *	traverse the plan tree and execute func() on all plans.  This
    271  *	does not evaluate each plan's eval() function; it is intended
    272  *	for operations that must run on all plans, such as state
    273  *	cleanup.
    274  *
    275  *	If any func() returns non-zero, then so will find_traverse().
    276  */
    277 int
    278 find_traverse(plan, func, arg)
    279 	PLAN *plan;
    280 	int (*func)(PLAN *, void *);
    281 	void *arg;
    282 {
    283 	PLAN *p;
    284 	int r, rval;
    285 
    286 	rval = 0;
    287 	for (p = plan; p; p = p->next) {
    288 		if ((r = func(p, arg)) != 0)
    289 			rval = r;
    290 		if (p->type == N_EXPR || p->type == N_OR) {
    291 			if (p->p_data[0])
    292 				if ((r = find_traverse(p->p_data[0],
    293 					    func, arg)) != 0)
    294 					rval = r;
    295 			if (p->p_data[1])
    296 				if ((r = find_traverse(p->p_data[1],
    297 					    func, arg)) != 0)
    298 					rval = r;
    299 		}
    300 	}
    301 	return rval;
    302 }
    303