[BACK]Return to find.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / find

Annotation of src/usr.bin/find/find.c, Revision 1.4

1.4     ! kstailey    1: /*     $OpenBSD: find.c,v 1.3 1996/10/24 03:46:04 tholo Exp $  */
1.2       deraadt     2:
1.1       deraadt     3: /*-
                      4:  * Copyright (c) 1991, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to Berkeley by
                      8:  * Cimarron D. Taylor of the University of California, Berkeley.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  * 3. All advertising materials mentioning features or use of this software
                     19:  *    must display the following acknowledgement:
                     20:  *     This product includes software developed by the University of
                     21:  *     California, Berkeley and its contributors.
                     22:  * 4. Neither the name of the University nor the names of its contributors
                     23:  *    may be used to endorse or promote products derived from this software
                     24:  *    without specific prior written permission.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     27:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     28:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     29:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     30:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     31:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     32:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     33:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     34:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     35:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     36:  * SUCH DAMAGE.
                     37:  */
                     38:
                     39: #ifndef lint
                     40: /*static char sccsid[] = "from: @(#)find.c     8.1 (Berkeley) 6/6/93";*/
1.4     ! kstailey   41: static char rcsid[] = "$OpenBSD: find.c,v 1.3 1996/10/24 03:46:04 tholo Exp $";
1.1       deraadt    42: #endif /* not lint */
                     43:
                     44: #include <sys/types.h>
                     45: #include <sys/stat.h>
                     46:
                     47: #include <err.h>
                     48: #include <errno.h>
                     49: #include <fts.h>
                     50: #include <stdio.h>
                     51: #include <string.h>
                     52: #include <stdlib.h>
                     53:
                     54: #include "find.h"
                     55:
                     56: /*
                     57:  * find_formplan --
                     58:  *     process the command line and create a "plan" corresponding to the
                     59:  *     command arguments.
                     60:  */
                     61: PLAN *
                     62: find_formplan(argv)
                     63:        char **argv;
                     64: {
                     65:        PLAN *plan, *tail, *new;
                     66:
                     67:        /*
                     68:         * for each argument in the command line, determine what kind of node
                     69:         * it is, create the appropriate node type and add the new plan node
                     70:         * to the end of the existing plan.  The resulting plan is a linked
                     71:         * list of plan nodes.  For example, the string:
                     72:         *
                     73:         *      % find . -name foo -newer bar -print
                     74:         *
                     75:         * results in the plan:
                     76:         *
                     77:         *      [-name foo]--> [-newer bar]--> [-print]
                     78:         *
                     79:         * in this diagram, `[-name foo]' represents the plan node generated
                     80:         * by c_name() with an argument of foo and `-->' represents the
                     81:         * plan->next pointer.
                     82:         */
                     83:        for (plan = tail = NULL; *argv;) {
                     84:                if (!(new = find_create(&argv)))
                     85:                        continue;
                     86:                if (plan == NULL)
                     87:                        tail = plan = new;
                     88:                else {
                     89:                        tail->next = new;
                     90:                        tail = new;
                     91:                }
                     92:        }
                     93:
                     94:        /*
                     95:         * if the user didn't specify one of -print, -ok or -exec, then -print
                     96:         * is assumed so we bracket the current expression with parens, if
                     97:         * necessary, and add a -print node on the end.
                     98:         */
                     99:        if (!isoutput) {
                    100:                if (plan == NULL) {
                    101:                        new = c_print();
                    102:                        tail = plan = new;
                    103:                } else {
                    104:                        new = c_openparen();
                    105:                        new->next = plan;
                    106:                        plan = new;
                    107:                        new = c_closeparen();
                    108:                        tail->next = new;
                    109:                        tail = new;
                    110:                        new = c_print();
                    111:                        tail->next = new;
                    112:                        tail = new;
                    113:                }
                    114:        }
                    115:
                    116:        /*
                    117:         * the command line has been completely processed into a search plan
                    118:         * except for the (, ), !, and -o operators.  Rearrange the plan so
                    119:         * that the portions of the plan which are affected by the operators
                    120:         * are moved into operator nodes themselves.  For example:
                    121:         *
                    122:         *      [!]--> [-name foo]--> [-print]
                    123:         *
                    124:         * becomes
                    125:         *
                    126:         *      [! [-name foo] ]--> [-print]
                    127:         *
                    128:         * and
                    129:         *
                    130:         *      [(]--> [-depth]--> [-name foo]--> [)]--> [-print]
                    131:         *
                    132:         * becomes
                    133:         *
                    134:         *      [expr [-depth]-->[-name foo] ]--> [-print]
                    135:         *
                    136:         * operators are handled in order of precedence.
                    137:         */
                    138:
                    139:        plan = paren_squish(plan);              /* ()'s */
                    140:        plan = not_squish(plan);                /* !'s */
                    141:        plan = or_squish(plan);                 /* -o's */
                    142:        return (plan);
                    143: }
                    144:
                    145: FTS *tree;                     /* pointer to top of FTS hierarchy */
                    146:
                    147: /*
                    148:  * find_execute --
                    149:  *     take a search plan and an array of search paths and executes the plan
                    150:  *     over all FTSENT's returned for the given search paths.
                    151:  */
1.3       tholo     152:
                    153: FTSENT *entry;                 /* shared with SIGINFO handler */
                    154:
1.1       deraadt   155: void
                    156: find_execute(plan, paths)
                    157:        PLAN *plan;             /* search plan */
                    158:        char **paths;           /* array of pathnames to traverse */
                    159: {
                    160:        PLAN *p;
                    161:
1.4     ! kstailey  162:        if (!(tree = fts_open(paths, ftsoptions, NULL)))
1.1       deraadt   163:                err(1, "ftsopen");
                    164:
                    165:        while (entry = fts_read(tree)) {
                    166:                switch(entry->fts_info) {
                    167:                case FTS_D:
                    168:                        if (isdepth)
                    169:                                continue;
                    170:                        break;
                    171:                case FTS_DP:
                    172:                        if (!isdepth)
                    173:                                continue;
                    174:                        break;
                    175:                case FTS_DNR:
                    176:                case FTS_ERR:
                    177:                case FTS_NS:
                    178:                        (void)fflush(stdout);
                    179:                        warn("%s", entry->fts_path);
                    180:                        continue;
                    181:                }
                    182: #define        BADCH   " \t\n\\'\""
                    183:                if (isxargs && strpbrk(entry->fts_path, BADCH)) {
                    184:                        (void)fflush(stdout);
                    185:                        warnx("%s: illegal path", entry->fts_path);
                    186:                        continue;
                    187:                }
                    188:
                    189:                /*
                    190:                 * call all the functions in the execution plan until one is
                    191:                 * false or all have been executed.  This is where we do all
                    192:                 * the work specified by the user on the command line.
                    193:                 */
                    194:                for (p = plan; p && (p->eval)(p, entry); p = p->next);
                    195:        }
                    196:        (void)fts_close(tree);
                    197: }