[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.13

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