Home | History | Annotate | Line # | Download | only in find
find.c revision 1.5
      1 /*-
      2  * Copyright (c) 1991, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * This code is derived from software contributed to Berkeley by
      6  * Cimarron D. Taylor of the University of California, Berkeley.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #ifndef lint
     38 /*static char sccsid[] = "from: @(#)find.c	8.1 (Berkeley) 6/6/93";*/
     39 static char rcsid[] = "$Id: find.c,v 1.5 1993/12/30 21:15:21 jtc Exp $";
     40 #endif /* not lint */
     41 
     42 #include <sys/types.h>
     43 #include <sys/stat.h>
     44 
     45 #include <err.h>
     46 #include <errno.h>
     47 #include <fts.h>
     48 #include <stdio.h>
     49 #include <string.h>
     50 #include <stdlib.h>
     51 
     52 #include "find.h"
     53 
     54 /*
     55  * find_formplan --
     56  *	process the command line and create a "plan" corresponding to the
     57  *	command arguments.
     58  */
     59 PLAN *
     60 find_formplan(argv)
     61 	char **argv;
     62 {
     63 	PLAN *plan, *tail, *new;
     64 
     65 	/*
     66 	 * for each argument in the command line, determine what kind of node
     67 	 * it is, create the appropriate node type and add the new plan node
     68 	 * to the end of the existing plan.  The resulting plan is a linked
     69 	 * list of plan nodes.  For example, the string:
     70 	 *
     71 	 *	% find . -name foo -newer bar -print
     72 	 *
     73 	 * results in the plan:
     74 	 *
     75 	 *	[-name foo]--> [-newer bar]--> [-print]
     76 	 *
     77 	 * in this diagram, `[-name foo]' represents the plan node generated
     78 	 * by c_name() with an argument of foo and `-->' represents the
     79 	 * plan->next pointer.
     80 	 */
     81 	for (plan = tail = NULL; *argv;) {
     82 		if (!(new = find_create(&argv)))
     83 			continue;
     84 		if (plan == NULL)
     85 			tail = plan = new;
     86 		else {
     87 			tail->next = new;
     88 			tail = new;
     89 		}
     90 	}
     91 
     92 	/*
     93 	 * if the user didn't specify one of -print, -ok or -exec, then -print
     94 	 * is assumed so we add a -print node on the end.  It is possible that
     95 	 * the user might want the -print someplace else on the command line,
     96 	 * but there's no way to know that.
     97 	 */
     98 	if (!isoutput) {
     99 		new = c_print();
    100 		if (plan == NULL)
    101 			tail = plan = new;
    102 		else {
    103 			tail->next = new;
    104 			tail = new;
    105 		}
    106 	}
    107 
    108 	/*
    109 	 * the command line has been completely processed into a search plan
    110 	 * except for the (, ), !, and -o operators.  Rearrange the plan so
    111 	 * that the portions of the plan which are affected by the operators
    112 	 * are moved into operator nodes themselves.  For example:
    113 	 *
    114 	 *	[!]--> [-name foo]--> [-print]
    115 	 *
    116 	 * becomes
    117 	 *
    118 	 *	[! [-name foo] ]--> [-print]
    119 	 *
    120 	 * and
    121 	 *
    122 	 *	[(]--> [-depth]--> [-name foo]--> [)]--> [-print]
    123 	 *
    124 	 * becomes
    125 	 *
    126 	 *	[expr [-depth]-->[-name foo] ]--> [-print]
    127 	 *
    128 	 * operators are handled in order of precedence.
    129 	 */
    130 
    131 	plan = paren_squish(plan);		/* ()'s */
    132 	plan = not_squish(plan);		/* !'s */
    133 	plan = or_squish(plan);			/* -o's */
    134 	return (plan);
    135 }
    136 
    137 FTS *tree;			/* pointer to top of FTS hierarchy */
    138 
    139 /*
    140  * find_execute --
    141  *	take a search plan and an array of search paths and executes the plan
    142  *	over all FTSENT's returned for the given search paths.
    143  */
    144 void
    145 find_execute(plan, paths)
    146 	PLAN *plan;		/* search plan */
    147 	char **paths;		/* array of pathnames to traverse */
    148 {
    149 	register FTSENT *entry;
    150 	PLAN *p;
    151 
    152 	if (!(tree = fts_open(paths, ftsoptions, (int (*)())NULL)))
    153 		err(1, "ftsopen");
    154 
    155 	while (entry = fts_read(tree)) {
    156 		switch(entry->fts_info) {
    157 		case FTS_D:
    158 			if (isdepth)
    159 				continue;
    160 			break;
    161 		case FTS_DP:
    162 			if (!isdepth)
    163 				continue;
    164 			break;
    165 		case FTS_DNR:
    166 		case FTS_ERR:
    167 		case FTS_NS:
    168 			(void)fflush(stdout);
    169 			warn("%s", entry->fts_path);
    170 			continue;
    171 		}
    172 #define	BADCH	" \t\n\\'\""
    173 		if (isxargs && strpbrk(entry->fts_path, BADCH)) {
    174 			(void)fflush(stdout);
    175 			warnx("%s: illegal path", entry->fts_path);
    176 			continue;
    177 		}
    178 
    179 		/*
    180 		 * call all the functions in the execution plan until one is
    181 		 * false or all have been executed.  This is where we do all
    182 		 * the work specified by the user on the command line.
    183 		 */
    184 		for (p = plan; p && (p->eval)(p, entry); p = p->next);
    185 	}
    186 	(void)fts_close(tree);
    187 }
    188