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