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