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 * 4. Neither the name of the University nor the names of its contributors
171590Srgrimes *    may be used to endorse or promote products derived from this software
181590Srgrimes *    without specific prior written permission.
191590Srgrimes *
201590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301590Srgrimes * SUCH DAMAGE.
311590Srgrimes */
321590Srgrimes
33207705Sdelphij#ifndef lint
34207705Sdelphij#if 0
35207705Sdelphijstatic char sccsid[] = "@(#)find.c	8.5 (Berkeley) 8/5/94";
36207705Sdelphij#else
37207705Sdelphij#endif
38207705Sdelphij#endif /* not lint */
39207705Sdelphij
4093604Sobrien#include <sys/cdefs.h>
4193604Sobrien__FBSDID("$FreeBSD$");
421590Srgrimes
431590Srgrimes#include <sys/types.h>
441590Srgrimes#include <sys/stat.h>
451590Srgrimes
461590Srgrimes#include <err.h>
471590Srgrimes#include <errno.h>
481590Srgrimes#include <fts.h>
4972945Sknu#include <regex.h>
501590Srgrimes#include <stdio.h>
51200462Sdelphij#include <stdlib.h>
521590Srgrimes#include <string.h>
531590Srgrimes
541590Srgrimes#include "find.h"
551590Srgrimes
56103726Swollmanstatic int find_compare(const FTSENT * const *s1, const FTSENT * const *s2);
5741391Swosch
581590Srgrimes/*
5941398Sbde * find_compare --
6041398Sbde *	tell fts_open() how to order the traversal of the hierarchy.
6141399Sbde *	This variant gives lexicographical order, i.e., alphabetical
6241399Sbde *	order within each directory.
6341398Sbde */
6441398Sbdestatic int
65116333Smarkmfind_compare(const FTSENT * const *s1, const FTSENT * const *s2)
6641398Sbde{
6741398Sbde
6841398Sbde	return (strcoll((*s1)->fts_name, (*s2)->fts_name));
6941398Sbde}
7041398Sbde
7141398Sbde/*
721590Srgrimes * find_formplan --
731590Srgrimes *	process the command line and create a "plan" corresponding to the
741590Srgrimes *	command arguments.
751590Srgrimes */
761590SrgrimesPLAN *
77116333Smarkmfind_formplan(char *argv[])
781590Srgrimes{
791590Srgrimes	PLAN *plan, *tail, *new;
801590Srgrimes
811590Srgrimes	/*
821590Srgrimes	 * for each argument in the command line, determine what kind of node
831590Srgrimes	 * it is, create the appropriate node type and add the new plan node
841590Srgrimes	 * to the end of the existing plan.  The resulting plan is a linked
851590Srgrimes	 * list of plan nodes.  For example, the string:
861590Srgrimes	 *
871590Srgrimes	 *	% find . -name foo -newer bar -print
881590Srgrimes	 *
891590Srgrimes	 * results in the plan:
901590Srgrimes	 *
911590Srgrimes	 *	[-name foo]--> [-newer bar]--> [-print]
921590Srgrimes	 *
931590Srgrimes	 * in this diagram, `[-name foo]' represents the plan node generated
941590Srgrimes	 * by c_name() with an argument of foo and `-->' represents the
951590Srgrimes	 * plan->next pointer.
961590Srgrimes	 */
971590Srgrimes	for (plan = tail = NULL; *argv;) {
981590Srgrimes		if (!(new = find_create(&argv)))
991590Srgrimes			continue;
1001590Srgrimes		if (plan == NULL)
1011590Srgrimes			tail = plan = new;
1021590Srgrimes		else {
1031590Srgrimes			tail->next = new;
1041590Srgrimes			tail = new;
1051590Srgrimes		}
1061590Srgrimes	}
1078874Srgrimes
1081590Srgrimes	/*
1091590Srgrimes	 * if the user didn't specify one of -print, -ok or -exec, then -print
11023695Speter	 * is assumed so we bracket the current expression with parens, if
11123695Speter	 * necessary, and add a -print node on the end.
1121590Srgrimes	 */
1131590Srgrimes	if (!isoutput) {
11476250Sphk		OPTION *p;
11591400Sdwmalone		char **argv1 = 0;
11676250Sphk
11723695Speter		if (plan == NULL) {
11891400Sdwmalone			p = lookup_option("-print");
11991400Sdwmalone			new = (p->create)(p, &argv1);
1201590Srgrimes			tail = plan = new;
12123695Speter		} else {
12291400Sdwmalone			p = lookup_option("(");
12391400Sdwmalone			new = (p->create)(p, &argv1);
1246776Sguido			new->next = plan;
1256776Sguido			plan = new;
12691400Sdwmalone			p = lookup_option(")");
12791400Sdwmalone			new = (p->create)(p, &argv1);
1281590Srgrimes			tail->next = new;
1291590Srgrimes			tail = new;
13091400Sdwmalone			p = lookup_option("-print");
13191400Sdwmalone			new = (p->create)(p, &argv1);
1326776Sguido			tail->next = new;
1336776Sguido			tail = new;
1341590Srgrimes		}
1351590Srgrimes	}
1368874Srgrimes
1371590Srgrimes	/*
1381590Srgrimes	 * the command line has been completely processed into a search plan
1391590Srgrimes	 * except for the (, ), !, and -o operators.  Rearrange the plan so
1401590Srgrimes	 * that the portions of the plan which are affected by the operators
1411590Srgrimes	 * are moved into operator nodes themselves.  For example:
1421590Srgrimes	 *
1431590Srgrimes	 *	[!]--> [-name foo]--> [-print]
1441590Srgrimes	 *
1451590Srgrimes	 * becomes
1461590Srgrimes	 *
1471590Srgrimes	 *	[! [-name foo] ]--> [-print]
1481590Srgrimes	 *
1491590Srgrimes	 * and
1501590Srgrimes	 *
1511590Srgrimes	 *	[(]--> [-depth]--> [-name foo]--> [)]--> [-print]
1521590Srgrimes	 *
1531590Srgrimes	 * becomes
1541590Srgrimes	 *
1551590Srgrimes	 *	[expr [-depth]-->[-name foo] ]--> [-print]
1561590Srgrimes	 *
1571590Srgrimes	 * operators are handled in order of precedence.
1581590Srgrimes	 */
1591590Srgrimes
1601590Srgrimes	plan = paren_squish(plan);		/* ()'s */
1611590Srgrimes	plan = not_squish(plan);		/* !'s */
1621590Srgrimes	plan = or_squish(plan);			/* -o's */
1631590Srgrimes	return (plan);
1641590Srgrimes}
1658874Srgrimes
1661590SrgrimesFTS *tree;			/* pointer to top of FTS hierarchy */
1671590Srgrimes
1681590Srgrimes/*
1691590Srgrimes * find_execute --
1701590Srgrimes *	take a search plan and an array of search paths and executes the plan
1711590Srgrimes *	over all FTSENT's returned for the given search paths.
1721590Srgrimes */
1731590Srgrimesint
174116333Smarkmfind_execute(PLAN *plan, char *paths[])
1751590Srgrimes{
17691400Sdwmalone	FTSENT *entry;
1771590Srgrimes	PLAN *p;
1781590Srgrimes	int rval;
1798874Srgrimes
18041398Sbde	tree = fts_open(paths, ftsoptions, (issort ? find_compare : NULL));
18141398Sbde	if (tree == NULL)
1821590Srgrimes		err(1, "ftsopen");
1831590Srgrimes
1841590Srgrimes	for (rval = 0; (entry = fts_read(tree)) != NULL;) {
185129812Seik		if (maxdepth != -1 && entry->fts_level >= maxdepth) {
186129812Seik			if (fts_set(tree, entry, FTS_SKIP))
187129812Seik				err(1, "%s", entry->fts_path);
188129812Seik		}
189129812Seik
1901590Srgrimes		switch (entry->fts_info) {
1911590Srgrimes		case FTS_D:
1921590Srgrimes			if (isdepth)
1931590Srgrimes				continue;
1941590Srgrimes			break;
1951590Srgrimes		case FTS_DP:
1961590Srgrimes			if (!isdepth)
1971590Srgrimes				continue;
1981590Srgrimes			break;
1991590Srgrimes		case FTS_DNR:
200264699Sjilles		case FTS_NS:
201264699Sjilles			if (ignore_readdir_race &&
202264699Sjilles			    entry->fts_errno == ENOENT && entry->fts_level > 0)
203264699Sjilles				continue;
204264699Sjilles			/* FALLTHROUGH */
2051590Srgrimes		case FTS_ERR:
2061590Srgrimes			(void)fflush(stdout);
2071590Srgrimes			warnx("%s: %s",
2081590Srgrimes			    entry->fts_path, strerror(entry->fts_errno));
2091590Srgrimes			rval = 1;
2101590Srgrimes			continue;
21123695Speter#ifdef FTS_W
21223695Speter		case FTS_W:
21323695Speter			continue;
21423695Speter#endif /* FTS_W */
2151590Srgrimes		}
2161590Srgrimes#define	BADCH	" \t\n\\'\""
2171590Srgrimes		if (isxargs && strpbrk(entry->fts_path, BADCH)) {
2181590Srgrimes			(void)fflush(stdout);
2191590Srgrimes			warnx("%s: illegal path", entry->fts_path);
2201590Srgrimes			rval = 1;
2211590Srgrimes			continue;
2221590Srgrimes		}
2238874Srgrimes
22461575Sroberto		if (mindepth != -1 && entry->fts_level < mindepth)
22561575Sroberto			continue;
22661575Sroberto
2271590Srgrimes		/*
2281590Srgrimes		 * Call all the functions in the execution plan until one is
2291590Srgrimes		 * false or all have been executed.  This is where we do all
2301590Srgrimes		 * the work specified by the user on the command line.
2311590Srgrimes		 */
23276250Sphk		for (p = plan; p && (p->execute)(p, entry); p = p->next);
2331590Srgrimes	}
234158572Skrion	finish_execplus();
235264699Sjilles	if (errno && (!ignore_readdir_race || errno != ENOENT))
2361590Srgrimes		err(1, "fts_read");
2371590Srgrimes	return (rval);
2381590Srgrimes}
239