find.c revision 41391
11590Srgrimes/*-
21590Srgrimes * Copyright (c) 1991, 1993, 1994
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * This code is derived from software contributed to Berkeley by
61590Srgrimes * Cimarron D. Taylor of the University of California, Berkeley.
71590Srgrimes *
81590Srgrimes * Redistribution and use in source and binary forms, with or without
91590Srgrimes * modification, are permitted provided that the following conditions
101590Srgrimes * are met:
111590Srgrimes * 1. Redistributions of source code must retain the above copyright
121590Srgrimes *    notice, this list of conditions and the following disclaimer.
131590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141590Srgrimes *    notice, this list of conditions and the following disclaimer in the
151590Srgrimes *    documentation and/or other materials provided with the distribution.
161590Srgrimes * 3. All advertising materials mentioning features or use of this software
171590Srgrimes *    must display the following acknowledgement:
181590Srgrimes *	This product includes software developed by the University of
191590Srgrimes *	California, Berkeley and its contributors.
201590Srgrimes * 4. Neither the name of the University nor the names of its contributors
211590Srgrimes *    may be used to endorse or promote products derived from this software
221590Srgrimes *    without specific prior written permission.
231590Srgrimes *
241590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341590Srgrimes * SUCH DAMAGE.
351590Srgrimes */
361590Srgrimes
371590Srgrimes#ifndef lint
3823695Speterstatic char sccsid[] = "@(#)find.c	8.5 (Berkeley) 8/5/94";
391590Srgrimes#endif /* not lint */
401590Srgrimes
411590Srgrimes#include <sys/types.h>
421590Srgrimes#include <sys/stat.h>
431590Srgrimes
441590Srgrimes#include <err.h>
451590Srgrimes#include <errno.h>
461590Srgrimes#include <fts.h>
471590Srgrimes#include <stdio.h>
481590Srgrimes#include <string.h>
491590Srgrimes#include <stdlib.h>
501590Srgrimes
511590Srgrimes#include "find.h"
521590Srgrimes
5341391Swoschstatic int find_compare(const FTSENT **s1, const FTSENT **s2);
5441391Swosch
551590Srgrimes/*
561590Srgrimes * find_formplan --
571590Srgrimes *	process the command line and create a "plan" corresponding to the
581590Srgrimes *	command arguments.
591590Srgrimes */
601590SrgrimesPLAN *
611590Srgrimesfind_formplan(argv)
621590Srgrimes	char **argv;
631590Srgrimes{
641590Srgrimes	PLAN *plan, *tail, *new;
651590Srgrimes
661590Srgrimes	/*
671590Srgrimes	 * for each argument in the command line, determine what kind of node
681590Srgrimes	 * it is, create the appropriate node type and add the new plan node
691590Srgrimes	 * to the end of the existing plan.  The resulting plan is a linked
701590Srgrimes	 * list of plan nodes.  For example, the string:
711590Srgrimes	 *
721590Srgrimes	 *	% find . -name foo -newer bar -print
731590Srgrimes	 *
741590Srgrimes	 * results in the plan:
751590Srgrimes	 *
761590Srgrimes	 *	[-name foo]--> [-newer bar]--> [-print]
771590Srgrimes	 *
781590Srgrimes	 * in this diagram, `[-name foo]' represents the plan node generated
791590Srgrimes	 * by c_name() with an argument of foo and `-->' represents the
801590Srgrimes	 * plan->next pointer.
811590Srgrimes	 */
821590Srgrimes	for (plan = tail = NULL; *argv;) {
831590Srgrimes		if (!(new = find_create(&argv)))
841590Srgrimes			continue;
851590Srgrimes		if (plan == NULL)
861590Srgrimes			tail = plan = new;
871590Srgrimes		else {
881590Srgrimes			tail->next = new;
891590Srgrimes			tail = new;
901590Srgrimes		}
911590Srgrimes	}
928874Srgrimes
931590Srgrimes	/*
941590Srgrimes	 * if the user didn't specify one of -print, -ok or -exec, then -print
9523695Speter	 * is assumed so we bracket the current expression with parens, if
9623695Speter	 * necessary, and add a -print node on the end.
971590Srgrimes	 */
981590Srgrimes	if (!isoutput) {
9923695Speter		if (plan == NULL) {
10023695Speter			new = c_print();
1011590Srgrimes			tail = plan = new;
10223695Speter		} else {
1036776Sguido			new = c_openparen();
1046776Sguido			new->next = plan;
1056776Sguido			plan = new;
1066776Sguido			new = c_closeparen();
1071590Srgrimes			tail->next = new;
1081590Srgrimes			tail = new;
1096776Sguido			new = c_print();
1106776Sguido			tail->next = new;
1116776Sguido			tail = new;
1121590Srgrimes		}
1131590Srgrimes	}
1148874Srgrimes
1151590Srgrimes	/*
1161590Srgrimes	 * the command line has been completely processed into a search plan
1171590Srgrimes	 * except for the (, ), !, and -o operators.  Rearrange the plan so
1181590Srgrimes	 * that the portions of the plan which are affected by the operators
1191590Srgrimes	 * are moved into operator nodes themselves.  For example:
1201590Srgrimes	 *
1211590Srgrimes	 *	[!]--> [-name foo]--> [-print]
1221590Srgrimes	 *
1231590Srgrimes	 * becomes
1241590Srgrimes	 *
1251590Srgrimes	 *	[! [-name foo] ]--> [-print]
1261590Srgrimes	 *
1271590Srgrimes	 * and
1281590Srgrimes	 *
1291590Srgrimes	 *	[(]--> [-depth]--> [-name foo]--> [)]--> [-print]
1301590Srgrimes	 *
1311590Srgrimes	 * becomes
1321590Srgrimes	 *
1331590Srgrimes	 *	[expr [-depth]-->[-name foo] ]--> [-print]
1341590Srgrimes	 *
1351590Srgrimes	 * operators are handled in order of precedence.
1361590Srgrimes	 */
1371590Srgrimes
1381590Srgrimes	plan = paren_squish(plan);		/* ()'s */
1391590Srgrimes	plan = not_squish(plan);		/* !'s */
1401590Srgrimes	plan = or_squish(plan);			/* -o's */
1411590Srgrimes	return (plan);
1421590Srgrimes}
1438874Srgrimes
1441590SrgrimesFTS *tree;			/* pointer to top of FTS hierarchy */
1451590Srgrimes
1461590Srgrimes/*
14741391Swosch * find_compare --
14841391Swosch *    A function which be used in fts_open() to order the
14941391Swosch *    traversal of the hierarchy.
15041391Swosch *    This function give you a lexicographical sorted output.
15141391Swosch */
15241391Swoschstatic int find_compare(s1, s2)
15341391Swosch	const FTSENT **s1, **s2;
15441391Swosch{
15541391Swosch	return strcoll( (*s1)->fts_name, (*s2)->fts_name );
15641391Swosch}
15741391Swosch
15841391Swosch/*
1591590Srgrimes * find_execute --
1601590Srgrimes *	take a search plan and an array of search paths and executes the plan
1611590Srgrimes *	over all FTSENT's returned for the given search paths.
1621590Srgrimes */
1631590Srgrimesint
1641590Srgrimesfind_execute(plan, paths)
1651590Srgrimes	PLAN *plan;		/* search plan */
1661590Srgrimes	char **paths;		/* array of pathnames to traverse */
1671590Srgrimes{
1681590Srgrimes	register FTSENT *entry;
1691590Srgrimes	PLAN *p;
1701590Srgrimes	int rval;
1718874Srgrimes
17241391Swosch	if ((tree = fts_open(paths, ftsoptions,
17341391Swosch		(issort ? find_compare : NULL) )) == NULL)
1741590Srgrimes		err(1, "ftsopen");
1751590Srgrimes
1761590Srgrimes	for (rval = 0; (entry = fts_read(tree)) != NULL;) {
1771590Srgrimes		switch (entry->fts_info) {
1781590Srgrimes		case FTS_D:
1791590Srgrimes			if (isdepth)
1801590Srgrimes				continue;
1811590Srgrimes			break;
1821590Srgrimes		case FTS_DP:
1831590Srgrimes			if (!isdepth)
1841590Srgrimes				continue;
1851590Srgrimes			break;
1861590Srgrimes		case FTS_DNR:
1871590Srgrimes		case FTS_ERR:
1881590Srgrimes		case FTS_NS:
1891590Srgrimes			(void)fflush(stdout);
1901590Srgrimes			warnx("%s: %s",
1911590Srgrimes			    entry->fts_path, strerror(entry->fts_errno));
1921590Srgrimes			rval = 1;
1931590Srgrimes			continue;
19423695Speter#ifdef FTS_W
19523695Speter		case FTS_W:
19623695Speter			continue;
19723695Speter#endif /* FTS_W */
1981590Srgrimes		}
1991590Srgrimes#define	BADCH	" \t\n\\'\""
2001590Srgrimes		if (isxargs && strpbrk(entry->fts_path, BADCH)) {
2011590Srgrimes			(void)fflush(stdout);
2021590Srgrimes			warnx("%s: illegal path", entry->fts_path);
2031590Srgrimes			rval = 1;
2041590Srgrimes			continue;
2051590Srgrimes		}
2068874Srgrimes
2071590Srgrimes		/*
2081590Srgrimes		 * Call all the functions in the execution plan until one is
2091590Srgrimes		 * false or all have been executed.  This is where we do all
2101590Srgrimes		 * the work specified by the user on the command line.
2111590Srgrimes		 */
2121590Srgrimes		for (p = plan; p && (p->eval)(p, entry); p = p->next);
2131590Srgrimes	}
2141590Srgrimes	if (errno)
2151590Srgrimes		err(1, "fts_read");
2161590Srgrimes	return (rval);
2171590Srgrimes}
218