19b50d902SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
49b50d902SRodney W. Grimes * Copyright (c) 1991, 1993, 1994
59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved.
69b50d902SRodney W. Grimes *
79b50d902SRodney W. Grimes * This code is derived from software contributed to Berkeley by
89b50d902SRodney W. Grimes * Cimarron D. Taylor of the University of California, Berkeley.
99b50d902SRodney W. Grimes *
109b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
119b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions
129b50d902SRodney W. Grimes * are met:
139b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
149b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
159b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
169b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
179b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
199b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software
209b50d902SRodney W. Grimes * without specific prior written permission.
219b50d902SRodney W. Grimes *
229b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
239b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
249b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
259b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
269b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
279b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
289b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
299b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
309b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
319b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
329b50d902SRodney W. Grimes * SUCH DAMAGE.
339b50d902SRodney W. Grimes */
349b50d902SRodney W. Grimes
359b50d902SRodney W. Grimes #include <sys/types.h>
369b50d902SRodney W. Grimes #include <sys/stat.h>
379b50d902SRodney W. Grimes
389b50d902SRodney W. Grimes #include <err.h>
399b50d902SRodney W. Grimes #include <errno.h>
409b50d902SRodney W. Grimes #include <fts.h>
417c1d4b3aSAkinori MUSHA #include <regex.h>
429b50d902SRodney W. Grimes #include <stdio.h>
43821df508SXin LI #include <stdlib.h>
44ef646f18SMark Murray #include <string.h>
459b50d902SRodney W. Grimes
469b50d902SRodney W. Grimes #include "find.h"
479b50d902SRodney W. Grimes
480d3bcc2eSGarrett Wollman static int find_compare(const FTSENT * const *s1, const FTSENT * const *s2);
49ad04e613SBruce Evans
50ad04e613SBruce Evans /*
51ad04e613SBruce Evans * find_compare --
52ad04e613SBruce Evans * tell fts_open() how to order the traversal of the hierarchy.
531b4db9d1SBruce Evans * This variant gives lexicographical order, i.e., alphabetical
541b4db9d1SBruce Evans * order within each directory.
55ad04e613SBruce Evans */
56ad04e613SBruce Evans static int
find_compare(const FTSENT * const * s1,const FTSENT * const * s2)57ef646f18SMark Murray find_compare(const FTSENT * const *s1, const FTSENT * const *s2)
58ad04e613SBruce Evans {
59ad04e613SBruce Evans
60ad04e613SBruce Evans return (strcoll((*s1)->fts_name, (*s2)->fts_name));
61ad04e613SBruce Evans }
626d0c7e13SWolfram Schneider
639b50d902SRodney W. Grimes /*
649b50d902SRodney W. Grimes * find_formplan --
659b50d902SRodney W. Grimes * process the command line and create a "plan" corresponding to the
669b50d902SRodney W. Grimes * command arguments.
679b50d902SRodney W. Grimes */
689b50d902SRodney W. Grimes PLAN *
find_formplan(char * argv[])69ef646f18SMark Murray find_formplan(char *argv[])
709b50d902SRodney W. Grimes {
719b50d902SRodney W. Grimes PLAN *plan, *tail, *new;
729b50d902SRodney W. Grimes
739b50d902SRodney W. Grimes /*
749b50d902SRodney W. Grimes * for each argument in the command line, determine what kind of node
759b50d902SRodney W. Grimes * it is, create the appropriate node type and add the new plan node
769b50d902SRodney W. Grimes * to the end of the existing plan. The resulting plan is a linked
779b50d902SRodney W. Grimes * list of plan nodes. For example, the string:
789b50d902SRodney W. Grimes *
799b50d902SRodney W. Grimes * % find . -name foo -newer bar -print
809b50d902SRodney W. Grimes *
819b50d902SRodney W. Grimes * results in the plan:
829b50d902SRodney W. Grimes *
839b50d902SRodney W. Grimes * [-name foo]--> [-newer bar]--> [-print]
849b50d902SRodney W. Grimes *
859b50d902SRodney W. Grimes * in this diagram, `[-name foo]' represents the plan node generated
869b50d902SRodney W. Grimes * by c_name() with an argument of foo and `-->' represents the
879b50d902SRodney W. Grimes * plan->next pointer.
889b50d902SRodney W. Grimes */
899b50d902SRodney W. Grimes for (plan = tail = NULL; *argv;) {
909b50d902SRodney W. Grimes if (!(new = find_create(&argv)))
919b50d902SRodney W. Grimes continue;
929b50d902SRodney W. Grimes if (plan == NULL)
939b50d902SRodney W. Grimes tail = plan = new;
949b50d902SRodney W. Grimes else {
959b50d902SRodney W. Grimes tail->next = new;
969b50d902SRodney W. Grimes tail = new;
979b50d902SRodney W. Grimes }
989b50d902SRodney W. Grimes }
999b50d902SRodney W. Grimes
1009b50d902SRodney W. Grimes /*
1019b50d902SRodney W. Grimes * if the user didn't specify one of -print, -ok or -exec, then -print
102841484cdSPeter Wemm * is assumed so we bracket the current expression with parens, if
103841484cdSPeter Wemm * necessary, and add a -print node on the end.
1049b50d902SRodney W. Grimes */
1059b50d902SRodney W. Grimes if (!isoutput) {
106ea92232aSPoul-Henning Kamp OPTION *p;
107e98080b1SDavid Malone char **argv1 = 0;
108ea92232aSPoul-Henning Kamp
109841484cdSPeter Wemm if (plan == NULL) {
110e98080b1SDavid Malone p = lookup_option("-print");
111e98080b1SDavid Malone new = (p->create)(p, &argv1);
1129b50d902SRodney W. Grimes tail = plan = new;
113841484cdSPeter Wemm } else {
114e98080b1SDavid Malone p = lookup_option("(");
115e98080b1SDavid Malone new = (p->create)(p, &argv1);
116dcb8def0SGuido van Rooij new->next = plan;
117dcb8def0SGuido van Rooij plan = new;
118e98080b1SDavid Malone p = lookup_option(")");
119e98080b1SDavid Malone new = (p->create)(p, &argv1);
120dcb8def0SGuido van Rooij tail->next = new;
121dcb8def0SGuido van Rooij tail = new;
122e98080b1SDavid Malone p = lookup_option("-print");
123e98080b1SDavid Malone new = (p->create)(p, &argv1);
1249b50d902SRodney W. Grimes tail->next = new;
1259b50d902SRodney W. Grimes tail = new;
1269b50d902SRodney W. Grimes }
1279b50d902SRodney W. Grimes }
1289b50d902SRodney W. Grimes
1299b50d902SRodney W. Grimes /*
1309b50d902SRodney W. Grimes * the command line has been completely processed into a search plan
1319b50d902SRodney W. Grimes * except for the (, ), !, and -o operators. Rearrange the plan so
1329b50d902SRodney W. Grimes * that the portions of the plan which are affected by the operators
1339b50d902SRodney W. Grimes * are moved into operator nodes themselves. For example:
1349b50d902SRodney W. Grimes *
1359b50d902SRodney W. Grimes * [!]--> [-name foo]--> [-print]
1369b50d902SRodney W. Grimes *
1379b50d902SRodney W. Grimes * becomes
1389b50d902SRodney W. Grimes *
1399b50d902SRodney W. Grimes * [! [-name foo] ]--> [-print]
1409b50d902SRodney W. Grimes *
1419b50d902SRodney W. Grimes * and
1429b50d902SRodney W. Grimes *
1439b50d902SRodney W. Grimes * [(]--> [-depth]--> [-name foo]--> [)]--> [-print]
1449b50d902SRodney W. Grimes *
1459b50d902SRodney W. Grimes * becomes
1469b50d902SRodney W. Grimes *
1479b50d902SRodney W. Grimes * [expr [-depth]-->[-name foo] ]--> [-print]
1489b50d902SRodney W. Grimes *
1499b50d902SRodney W. Grimes * operators are handled in order of precedence.
1509b50d902SRodney W. Grimes */
1519b50d902SRodney W. Grimes
1529b50d902SRodney W. Grimes plan = paren_squish(plan); /* ()'s */
1539b50d902SRodney W. Grimes plan = not_squish(plan); /* !'s */
1549b50d902SRodney W. Grimes plan = or_squish(plan); /* -o's */
1559b50d902SRodney W. Grimes return (plan);
1569b50d902SRodney W. Grimes }
1579b50d902SRodney W. Grimes
1589b50d902SRodney W. Grimes FTS *tree; /* pointer to top of FTS hierarchy */
1599b50d902SRodney W. Grimes
1609b50d902SRodney W. Grimes /*
1619b50d902SRodney W. Grimes * find_execute --
1629b50d902SRodney W. Grimes * take a search plan and an array of search paths and executes the plan
1639b50d902SRodney W. Grimes * over all FTSENT's returned for the given search paths.
1649b50d902SRodney W. Grimes */
1659b50d902SRodney W. Grimes int
find_execute(PLAN * plan,char * paths[])166ef646f18SMark Murray find_execute(PLAN *plan, char *paths[])
1679b50d902SRodney W. Grimes {
168e98080b1SDavid Malone FTSENT *entry;
1699b50d902SRodney W. Grimes PLAN *p;
170d06a0096SGoran Mekić size_t counter = 0;
1717a79617cSJilles Tjoelker int e;
1729b50d902SRodney W. Grimes
173ad04e613SBruce Evans tree = fts_open(paths, ftsoptions, (issort ? find_compare : NULL));
174ad04e613SBruce Evans if (tree == NULL)
1759b50d902SRodney W. Grimes err(1, "ftsopen");
1769b50d902SRodney W. Grimes
1777a79617cSJilles Tjoelker exitstatus = 0;
1787a79617cSJilles Tjoelker while (errno = 0, (entry = fts_read(tree)) != NULL) {
1791c832963SOliver Eikemeier if (maxdepth != -1 && entry->fts_level >= maxdepth) {
1801c832963SOliver Eikemeier if (fts_set(tree, entry, FTS_SKIP))
1811c832963SOliver Eikemeier err(1, "%s", entry->fts_path);
1821c832963SOliver Eikemeier }
1831c832963SOliver Eikemeier
1849b50d902SRodney W. Grimes switch (entry->fts_info) {
1859b50d902SRodney W. Grimes case FTS_D:
1869b50d902SRodney W. Grimes if (isdepth)
1879b50d902SRodney W. Grimes continue;
1889b50d902SRodney W. Grimes break;
1899b50d902SRodney W. Grimes case FTS_DP:
1909b50d902SRodney W. Grimes if (!isdepth)
1919b50d902SRodney W. Grimes continue;
1929b50d902SRodney W. Grimes break;
1939b50d902SRodney W. Grimes case FTS_DNR:
1949b50d902SRodney W. Grimes case FTS_NS:
19540072dc2SJilles Tjoelker if (ignore_readdir_race &&
19640072dc2SJilles Tjoelker entry->fts_errno == ENOENT && entry->fts_level > 0)
19740072dc2SJilles Tjoelker continue;
19840072dc2SJilles Tjoelker /* FALLTHROUGH */
19940072dc2SJilles Tjoelker case FTS_ERR:
2009b50d902SRodney W. Grimes (void)fflush(stdout);
2019b50d902SRodney W. Grimes warnx("%s: %s",
2029b50d902SRodney W. Grimes entry->fts_path, strerror(entry->fts_errno));
2037a79617cSJilles Tjoelker exitstatus = 1;
2049b50d902SRodney W. Grimes continue;
205f2ef15feSOleksandr Tymoshenko #if defined(FTS_W) && defined(FTS_WHITEOUT)
206841484cdSPeter Wemm case FTS_W:
207f2ef15feSOleksandr Tymoshenko if (ftsoptions & FTS_WHITEOUT)
208f2ef15feSOleksandr Tymoshenko break;
209841484cdSPeter Wemm continue;
210841484cdSPeter Wemm #endif /* FTS_W */
2119b50d902SRodney W. Grimes }
212d06a0096SGoran Mekić
213d06a0096SGoran Mekić if (showinfo) {
214d06a0096SGoran Mekić fprintf(stderr, "Scanning: %s/%s\n", entry->fts_path, entry->fts_name);
215*91dc2374SBaptiste Daroussin fprintf(stderr, "Scanned: %zu\n\n", counter);
216d06a0096SGoran Mekić showinfo = 0;
217d06a0096SGoran Mekić }
218d06a0096SGoran Mekić ++counter;
219d06a0096SGoran Mekić
2209b50d902SRodney W. Grimes #define BADCH " \t\n\\'\""
2219b50d902SRodney W. Grimes if (isxargs && strpbrk(entry->fts_path, BADCH)) {
2229b50d902SRodney W. Grimes (void)fflush(stdout);
2239b50d902SRodney W. Grimes warnx("%s: illegal path", entry->fts_path);
2247a79617cSJilles Tjoelker exitstatus = 1;
2259b50d902SRodney W. Grimes continue;
2269b50d902SRodney W. Grimes }
2279b50d902SRodney W. Grimes
228c76bc8f3SOllivier Robert if (mindepth != -1 && entry->fts_level < mindepth)
229c76bc8f3SOllivier Robert continue;
230c76bc8f3SOllivier Robert
2319b50d902SRodney W. Grimes /*
2329b50d902SRodney W. Grimes * Call all the functions in the execution plan until one is
2339b50d902SRodney W. Grimes * false or all have been executed. This is where we do all
2349b50d902SRodney W. Grimes * the work specified by the user on the command line.
2359b50d902SRodney W. Grimes */
236ea92232aSPoul-Henning Kamp for (p = plan; p && (p->execute)(p, entry); p = p->next);
2379b50d902SRodney W. Grimes }
23873a0af46SJilles Tjoelker e = errno;
23922170420SKirill Ponomarev finish_execplus();
24073a0af46SJilles Tjoelker if (e && (!ignore_readdir_race || e != ENOENT))
24173a0af46SJilles Tjoelker errc(1, e, "fts_read");
2427a79617cSJilles Tjoelker return (exitstatus);
2439b50d902SRodney W. Grimes }
244