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