Home | History | Annotate | Line # | Download | only in find
find.c revision 1.1
      1 /*-
      2  * Copyright (c) 1991 The Regents of the University of California.
      3  * 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[] = "@(#)find.c	5.3 (Berkeley) 5/25/91";
     39 #endif /* not lint */
     40 
     41 #include <sys/types.h>
     42 #include <sys/stat.h>
     43 #include <sys/errno.h>
     44 #include <fts.h>
     45 #include <stdio.h>
     46 #include <string.h>
     47 #include <stdlib.h>
     48 #include "find.h"
     49 
     50 /*
     51  * find_formplan --
     52  *	process the command line and create a "plan" corresponding to the
     53  *	command arguments.
     54  */
     55 PLAN *
     56 find_formplan(argv)
     57 	char **argv;
     58 {
     59 	PLAN *plan, *tail, *new;
     60 	PLAN *c_print(), *find_create(), *not_squish(), *or_squish();
     61 	PLAN *paren_squish();
     62 
     63 	/*
     64 	 * for each argument in the command line, determine what kind of node
     65 	 * it is, create the appropriate node type and add the new plan node
     66 	 * to the end of the existing plan.  The resulting plan is a linked
     67 	 * list of plan nodes.  For example, the string:
     68 	 *
     69 	 *	% find . -name foo -newer bar -print
     70 	 *
     71 	 * results in the plan:
     72 	 *
     73 	 *	[-name foo]--> [-newer bar]--> [-print]
     74 	 *
     75 	 * in this diagram, `[-name foo]' represents the plan node generated
     76 	 * by c_name() with an argument of foo and `-->' represents the
     77 	 * plan->next pointer.
     78 	 */
     79 	for (plan = NULL; *argv;) {
     80 		if (!(new = find_create(&argv)))
     81 			continue;
     82 		if (plan == NULL)
     83 			tail = plan = new;
     84 		else {
     85 			tail->next = new;
     86 			tail = new;
     87 		}
     88 	}
     89 
     90 	/*
     91 	 * if the user didn't specify one of -print, -ok or -exec, then -print
     92 	 * is assumed so we add a -print node on the end.  It is possible that
     93 	 * the user might want the -print someplace else on the command line,
     94 	 * but there's no way to know that.
     95 	 */
     96 	if (!isoutput) {
     97 		new = c_print();
     98 		if (plan == NULL)
     99 			tail = plan = new;
    100 		else {
    101 			tail->next = new;
    102 			tail = new;
    103 		}
    104 	}
    105 
    106 	/*
    107 	 * the command line has been completely processed into a search plan
    108 	 * except for the (, ), !, and -o operators.  Rearrange the plan so
    109 	 * that the portions of the plan which are affected by the operators
    110 	 * are moved into operator nodes themselves.  For example:
    111 	 *
    112 	 *	[!]--> [-name foo]--> [-print]
    113 	 *
    114 	 * becomes
    115 	 *
    116 	 *	[! [-name foo] ]--> [-print]
    117 	 *
    118 	 * and
    119 	 *
    120 	 *	[(]--> [-depth]--> [-name foo]--> [)]--> [-print]
    121 	 *
    122 	 * becomes
    123 	 *
    124 	 *	[expr [-depth]-->[-name foo] ]--> [-print]
    125 	 *
    126 	 * operators are handled in order of precedence.
    127 	 */
    128 
    129 	plan = paren_squish(plan);		/* ()'s */
    130 	plan = not_squish(plan);		/* !'s */
    131 	plan = or_squish(plan);			/* -o's */
    132 	return(plan);
    133 }
    134 
    135 FTS *tree;			/* pointer to top of FTS hierarchy */
    136 
    137 /*
    138  * find_execute --
    139  *	take a search plan and an array of search paths and executes the plan
    140  *	over all FTSENT's returned for the given search paths.
    141  */
    142 void
    143 find_execute(plan, paths)
    144 	PLAN *plan;		/* search plan */
    145 	char **paths;		/* array of pathnames to traverse */
    146 {
    147 	register FTSENT *entry;
    148 	PLAN *p;
    149 
    150 	if (!(tree = fts_open(paths, ftsoptions, (int (*)())NULL)))
    151 		err("ftsopen: %s", strerror(errno));
    152 
    153 	while (entry = fts_read(tree)) {
    154 		switch(entry->fts_info) {
    155 		case FTS_D:
    156 			if (isdepth)
    157 				continue;
    158 			break;
    159 		case FTS_DP:
    160 			if (!isdepth)
    161 				continue;
    162 			break;
    163 		case FTS_DNR:
    164 		case FTS_ERR:
    165 		case FTS_NS:
    166 			(void)fprintf(stderr, "find: %s: %s\n",
    167 			    entry->fts_path, strerror(errno));
    168 			continue;
    169 		case FTS_SL:
    170 			if (entry->fts_level == FTS_ROOTLEVEL) {
    171 				(void)fts_set(tree, entry, FTS_FOLLOW);
    172 				continue;
    173 			}
    174 			break;
    175 		}
    176 
    177 #define	BADCH	" \t\n\\'\""
    178 		if (isxargs && strpbrk(entry->fts_path, BADCH)) {
    179 			(void)fprintf(stderr,
    180 			    "find: illegal path: %s\n", entry->fts_path);
    181 			continue;
    182 		}
    183 
    184 		/*
    185 		 * call all the functions in the execution plan until one is
    186 		 * false or all have been executed.  This is where we do all
    187 		 * the work specified by the user on the command line.
    188 		 */
    189 		for (p = plan; p && (p->eval)(p, entry); p = p->next);
    190 	}
    191 	(void)fts_close(tree);
    192 }
    193