1994-05-27 12:33:43 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1991, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Cimarron D. Taylor of the University of California, Berkeley.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2010-05-06 17:06:36 +00:00
|
|
|
#ifndef lint
|
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)find.c 8.5 (Berkeley) 8/5/94";
|
|
|
|
#else
|
|
|
|
#endif
|
|
|
|
#endif /* not lint */
|
|
|
|
|
2002-04-01 22:56:56 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fts.h>
|
2001-02-23 16:20:55 +00:00
|
|
|
#include <regex.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <stdio.h>
|
2009-12-13 03:14:06 +00:00
|
|
|
#include <stdlib.h>
|
2003-06-14 13:00:21 +00:00
|
|
|
#include <string.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#include "find.h"
|
|
|
|
|
2002-09-21 01:28:41 +00:00
|
|
|
static int find_compare(const FTSENT * const *s1, const FTSENT * const *s2);
|
1998-11-29 11:11:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* find_compare --
|
|
|
|
* tell fts_open() how to order the traversal of the hierarchy.
|
1998-11-29 11:34:30 +00:00
|
|
|
* This variant gives lexicographical order, i.e., alphabetical
|
|
|
|
* order within each directory.
|
1998-11-29 11:11:07 +00:00
|
|
|
*/
|
|
|
|
static int
|
2003-06-14 13:00:21 +00:00
|
|
|
find_compare(const FTSENT * const *s1, const FTSENT * const *s2)
|
1998-11-29 11:11:07 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
return (strcoll((*s1)->fts_name, (*s2)->fts_name));
|
|
|
|
}
|
1998-11-29 00:54:21 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* find_formplan --
|
|
|
|
* process the command line and create a "plan" corresponding to the
|
|
|
|
* command arguments.
|
|
|
|
*/
|
|
|
|
PLAN *
|
2003-06-14 13:00:21 +00:00
|
|
|
find_formplan(char *argv[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
PLAN *plan, *tail, *new;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* for each argument in the command line, determine what kind of node
|
|
|
|
* it is, create the appropriate node type and add the new plan node
|
|
|
|
* to the end of the existing plan. The resulting plan is a linked
|
|
|
|
* list of plan nodes. For example, the string:
|
|
|
|
*
|
|
|
|
* % find . -name foo -newer bar -print
|
|
|
|
*
|
|
|
|
* results in the plan:
|
|
|
|
*
|
|
|
|
* [-name foo]--> [-newer bar]--> [-print]
|
|
|
|
*
|
|
|
|
* in this diagram, `[-name foo]' represents the plan node generated
|
|
|
|
* by c_name() with an argument of foo and `-->' represents the
|
|
|
|
* plan->next pointer.
|
|
|
|
*/
|
|
|
|
for (plan = tail = NULL; *argv;) {
|
|
|
|
if (!(new = find_create(&argv)))
|
|
|
|
continue;
|
|
|
|
if (plan == NULL)
|
|
|
|
tail = plan = new;
|
|
|
|
else {
|
|
|
|
tail->next = new;
|
|
|
|
tail = new;
|
|
|
|
}
|
|
|
|
}
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* if the user didn't specify one of -print, -ok or -exec, then -print
|
1997-03-11 13:48:37 +00:00
|
|
|
* is assumed so we bracket the current expression with parens, if
|
|
|
|
* necessary, and add a -print node on the end.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
|
|
|
if (!isoutput) {
|
2001-05-03 18:05:35 +00:00
|
|
|
OPTION *p;
|
2002-02-27 17:57:00 +00:00
|
|
|
char **argv1 = 0;
|
2001-05-03 18:05:35 +00:00
|
|
|
|
1997-03-11 13:48:37 +00:00
|
|
|
if (plan == NULL) {
|
2002-02-27 17:57:00 +00:00
|
|
|
p = lookup_option("-print");
|
|
|
|
new = (p->create)(p, &argv1);
|
1994-05-27 12:33:43 +00:00
|
|
|
tail = plan = new;
|
1997-03-11 13:48:37 +00:00
|
|
|
} else {
|
2002-02-27 17:57:00 +00:00
|
|
|
p = lookup_option("(");
|
|
|
|
new = (p->create)(p, &argv1);
|
1995-02-27 20:52:36 +00:00
|
|
|
new->next = plan;
|
|
|
|
plan = new;
|
2002-02-27 17:57:00 +00:00
|
|
|
p = lookup_option(")");
|
|
|
|
new = (p->create)(p, &argv1);
|
1995-02-27 20:52:36 +00:00
|
|
|
tail->next = new;
|
|
|
|
tail = new;
|
2002-02-27 17:57:00 +00:00
|
|
|
p = lookup_option("-print");
|
|
|
|
new = (p->create)(p, &argv1);
|
1994-05-27 12:33:43 +00:00
|
|
|
tail->next = new;
|
|
|
|
tail = new;
|
|
|
|
}
|
|
|
|
}
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* the command line has been completely processed into a search plan
|
|
|
|
* except for the (, ), !, and -o operators. Rearrange the plan so
|
|
|
|
* that the portions of the plan which are affected by the operators
|
|
|
|
* are moved into operator nodes themselves. For example:
|
|
|
|
*
|
|
|
|
* [!]--> [-name foo]--> [-print]
|
|
|
|
*
|
|
|
|
* becomes
|
|
|
|
*
|
|
|
|
* [! [-name foo] ]--> [-print]
|
|
|
|
*
|
|
|
|
* and
|
|
|
|
*
|
|
|
|
* [(]--> [-depth]--> [-name foo]--> [)]--> [-print]
|
|
|
|
*
|
|
|
|
* becomes
|
|
|
|
*
|
|
|
|
* [expr [-depth]-->[-name foo] ]--> [-print]
|
|
|
|
*
|
|
|
|
* operators are handled in order of precedence.
|
|
|
|
*/
|
|
|
|
|
|
|
|
plan = paren_squish(plan); /* ()'s */
|
|
|
|
plan = not_squish(plan); /* !'s */
|
|
|
|
plan = or_squish(plan); /* -o's */
|
|
|
|
return (plan);
|
|
|
|
}
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
FTS *tree; /* pointer to top of FTS hierarchy */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* find_execute --
|
|
|
|
* take a search plan and an array of search paths and executes the plan
|
|
|
|
* over all FTSENT's returned for the given search paths.
|
|
|
|
*/
|
|
|
|
int
|
2003-06-14 13:00:21 +00:00
|
|
|
find_execute(PLAN *plan, char *paths[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-02-27 17:57:00 +00:00
|
|
|
FTSENT *entry;
|
1994-05-27 12:33:43 +00:00
|
|
|
PLAN *p;
|
|
|
|
int rval;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1998-11-29 11:11:07 +00:00
|
|
|
tree = fts_open(paths, ftsoptions, (issort ? find_compare : NULL));
|
|
|
|
if (tree == NULL)
|
1994-05-27 12:33:43 +00:00
|
|
|
err(1, "ftsopen");
|
|
|
|
|
|
|
|
for (rval = 0; (entry = fts_read(tree)) != NULL;) {
|
2004-05-28 17:17:15 +00:00
|
|
|
if (maxdepth != -1 && entry->fts_level >= maxdepth) {
|
|
|
|
if (fts_set(tree, entry, FTS_SKIP))
|
|
|
|
err(1, "%s", entry->fts_path);
|
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
switch (entry->fts_info) {
|
|
|
|
case FTS_D:
|
|
|
|
if (isdepth)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
case FTS_DP:
|
|
|
|
if (!isdepth)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
case FTS_DNR:
|
|
|
|
case FTS_NS:
|
2012-07-25 21:59:10 +00:00
|
|
|
if (ignore_readdir_race &&
|
|
|
|
entry->fts_errno == ENOENT && entry->fts_level > 0)
|
|
|
|
continue;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case FTS_ERR:
|
1994-05-27 12:33:43 +00:00
|
|
|
(void)fflush(stdout);
|
|
|
|
warnx("%s: %s",
|
|
|
|
entry->fts_path, strerror(entry->fts_errno));
|
|
|
|
rval = 1;
|
|
|
|
continue;
|
1997-03-11 13:48:37 +00:00
|
|
|
#ifdef FTS_W
|
|
|
|
case FTS_W:
|
|
|
|
continue;
|
|
|
|
#endif /* FTS_W */
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
#define BADCH " \t\n\\'\""
|
|
|
|
if (isxargs && strpbrk(entry->fts_path, BADCH)) {
|
|
|
|
(void)fflush(stdout);
|
|
|
|
warnx("%s: illegal path", entry->fts_path);
|
|
|
|
rval = 1;
|
|
|
|
continue;
|
|
|
|
}
|
1995-05-30 06:41:30 +00:00
|
|
|
|
2000-06-12 11:12:41 +00:00
|
|
|
if (mindepth != -1 && entry->fts_level < mindepth)
|
|
|
|
continue;
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* Call all the functions in the execution plan until one is
|
|
|
|
* false or all have been executed. This is where we do all
|
|
|
|
* the work specified by the user on the command line.
|
|
|
|
*/
|
2001-05-03 18:05:35 +00:00
|
|
|
for (p = plan; p && (p->execute)(p, entry); p = p->next);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2006-05-14 20:23:01 +00:00
|
|
|
finish_execplus();
|
2012-07-25 21:59:10 +00:00
|
|
|
if (errno && (!ignore_readdir_race || errno != ENOENT))
|
1994-05-27 12:33:43 +00:00
|
|
|
err(1, "fts_read");
|
|
|
|
return (rval);
|
|
|
|
}
|