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

Annotation of src/usr.bin/find/function.c, Revision 1.46

1.46    ! millert     1: /*     $OpenBSD: function.c,v 1.45 2017/01/03 21:31:16 tedu Exp $      */
1.4       deraadt     2:
1.1       deraadt     3: /*-
                      4:  * Copyright (c) 1990, 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.26      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: #include <sys/stat.h>
                     36: #include <sys/wait.h>
                     37: #include <sys/mount.h>
                     38:
1.7       tholo      39: #include <dirent.h>
1.1       deraadt    40: #include <err.h>
                     41: #include <errno.h>
1.15      millert    42: #include <fcntl.h>
1.1       deraadt    43: #include <fnmatch.h>
                     44: #include <fts.h>
                     45: #include <grp.h>
1.10      millert    46: #include <libgen.h>
1.37      pascal     47: #include <limits.h>
1.1       deraadt    48: #include <pwd.h>
                     49: #include <stdio.h>
                     50: #include <stdlib.h>
                     51: #include <string.h>
                     52: #include <unistd.h>
                     53:
                     54: #include "find.h"
1.28      deraadt    55: #include "extern.h"
1.1       deraadt    56:
                     57: #define        COMPARE(a, b) {                                                 \
                     58:        switch (plan->flags) {                                          \
                     59:        case F_EQUAL:                                                   \
                     60:                return (a == b);                                        \
                     61:        case F_LESSTHAN:                                                \
                     62:                return (a < b);                                         \
                     63:        case F_GREATER:                                                 \
                     64:                return (a > b);                                         \
                     65:        default:                                                        \
                     66:                abort();                                                \
                     67:        }                                                               \
                     68: }
                     69:
1.23      millert    70: static PLAN *palloc(enum ntype, int (*)(PLAN *, FTSENT *));
1.39      deraadt    71: static long long find_parsenum(PLAN *plan, char *option, char *vp, char *endch);
1.37      pascal     72: static void run_f_exec(PLAN *plan);
1.28      deraadt    73: static PLAN *palloc(enum ntype t, int (*f)(PLAN *, FTSENT *));
                     74:
                     75: int    f_amin(PLAN *, FTSENT *);
                     76: int    f_atime(PLAN *, FTSENT *);
                     77: int    f_cmin(PLAN *, FTSENT *);
                     78: int    f_ctime(PLAN *, FTSENT *);
                     79: int    f_always_true(PLAN *, FTSENT *);
                     80: int    f_empty(PLAN *, FTSENT *);
                     81: int    f_exec(PLAN *, FTSENT *);
                     82: int    f_execdir(PLAN *, FTSENT *);
                     83: int    f_flags(PLAN *, FTSENT *);
                     84: int    f_fstype(PLAN *, FTSENT *);
                     85: int    f_group(PLAN *, FTSENT *);
                     86: int    f_inum(PLAN *, FTSENT *);
                     87: int    f_empty(PLAN *, FTSENT *);
                     88: int    f_links(PLAN *, FTSENT *);
                     89: int    f_ls(PLAN *, FTSENT *);
                     90: int    f_maxdepth(PLAN *, FTSENT *);
                     91: int    f_mindepth(PLAN *, FTSENT *);
                     92: int    f_mtime(PLAN *, FTSENT *);
                     93: int    f_mmin(PLAN *, FTSENT *);
                     94: int    f_name(PLAN *, FTSENT *);
                     95: int    f_iname(PLAN *, FTSENT *);
                     96: int    f_newer(PLAN *, FTSENT *);
                     97: int    f_anewer(PLAN *, FTSENT *);
                     98: int    f_cnewer(PLAN *, FTSENT *);
                     99: int    f_nogroup(PLAN *, FTSENT *);
                    100: int    f_nouser(PLAN *, FTSENT *);
                    101: int    f_path(PLAN *, FTSENT *);
                    102: int    f_perm(PLAN *, FTSENT *);
                    103: int    f_print(PLAN *, FTSENT *);
                    104: int    f_print0(PLAN *, FTSENT *);
                    105: int    f_prune(PLAN *, FTSENT *);
                    106: int    f_size(PLAN *, FTSENT *);
                    107: int    f_type(PLAN *, FTSENT *);
                    108: int    f_user(PLAN *, FTSENT *);
                    109: int    f_expr(PLAN *, FTSENT *);
                    110: int    f_not(PLAN *, FTSENT *);
                    111: int    f_or(PLAN *, FTSENT *);
1.1       deraadt   112:
1.9       millert   113: extern int dotfd;
                    114: extern time_t now;
                    115: extern FTS *tree;
                    116:
1.1       deraadt   117: /*
                    118:  * find_parsenum --
                    119:  *     Parse a string of the form [+-]# and return the value.
                    120:  */
1.39      deraadt   121: static long long
1.30      deraadt   122: find_parsenum(PLAN *plan, char *option, char *vp, char *endch)
1.1       deraadt   123: {
1.39      deraadt   124:        long long value;
1.1       deraadt   125:        char *endchar, *str;    /* Pointer to character ending conversion. */
                    126:
                    127:        /* Determine comparison from leading + or -. */
                    128:        str = vp;
                    129:        switch (*str) {
                    130:        case '+':
                    131:                ++str;
                    132:                plan->flags = F_GREATER;
                    133:                break;
                    134:        case '-':
                    135:                ++str;
                    136:                plan->flags = F_LESSTHAN;
                    137:                break;
                    138:        default:
                    139:                plan->flags = F_EQUAL;
                    140:                break;
                    141:        }
                    142:
                    143:        /*
1.40      deraadt   144:         * Convert the string with strtoll().  Note, if strtoll() returns
                    145:         * zero and endchar points to the beginning of the string we know
                    146:         * we have a syntax error.
1.1       deraadt   147:         */
1.39      deraadt   148:        value = strtoll(str, &endchar, 10);
1.1       deraadt   149:        if (value == 0 && endchar == str)
                    150:                errx(1, "%s: %s: illegal numeric value", option, vp);
                    151:        if (endchar[0] && (endch == NULL || endchar[0] != *endch))
                    152:                errx(1, "%s: %s: illegal trailing character", option, vp);
                    153:        if (endch)
                    154:                *endch = endchar[0];
                    155:        return (value);
                    156: }
                    157:
                    158: /*
                    159:  * The value of n for the inode times (atime, ctime, and mtime) is a range,
                    160:  * i.e. n matches from (n - 1) to n 24 hour periods.  This interacts with
                    161:  * -n, such that "-mtime -1" would be less than 0 days, which isn't what the
                    162:  * user wanted.  Correct so that -1 is "less than 1".
                    163:  */
                    164: #define        TIME_CORRECT(p, ttype)                                          \
                    165:        if ((p)->type == ttype && (p)->flags == F_LESSTHAN)             \
1.17      millert   166:                ++((p)->sec_data);
1.1       deraadt   167:
                    168: /*
1.11      deraadt   169:  * -amin n functions --
                    170:  *
                    171:  *     True if the difference between the file access time and the
                    172:  *     current time is n min periods.
                    173:  */
                    174: int
1.30      deraadt   175: f_amin(PLAN *plan, FTSENT *entry)
1.11      deraadt   176: {
                    177:        extern time_t now;
                    178:
                    179:        COMPARE((now - entry->fts_statp->st_atime +
1.17      millert   180:            60 - 1) / 60, plan->sec_data);
1.11      deraadt   181: }
                    182:
                    183: PLAN *
1.28      deraadt   184: c_amin(char *arg, char ***ignored, int unused)
1.11      deraadt   185: {
                    186:        PLAN *new;
                    187:
                    188:        ftsoptions &= ~FTS_NOSTAT;
                    189:
                    190:        new = palloc(N_AMIN, f_amin);
1.17      millert   191:        new->sec_data = find_parsenum(new, "-amin", arg, NULL);
1.11      deraadt   192:        TIME_CORRECT(new, N_AMIN);
                    193:        return (new);
                    194: }
                    195:
                    196: /*
1.1       deraadt   197:  * -atime n functions --
                    198:  *
                    199:  *     True if the difference between the file access time and the
                    200:  *     current time is n 24 hour periods.
                    201:  */
                    202: int
1.30      deraadt   203: f_atime(PLAN *plan, FTSENT *entry)
1.1       deraadt   204: {
                    205:
                    206:        COMPARE((now - entry->fts_statp->st_atime +
1.17      millert   207:            SECSPERDAY - 1) / SECSPERDAY, plan->sec_data);
1.1       deraadt   208: }
                    209:
                    210: PLAN *
1.28      deraadt   211: c_atime(char *arg, char ***ignored, int unused)
1.1       deraadt   212: {
                    213:        PLAN *new;
                    214:
                    215:        ftsoptions &= ~FTS_NOSTAT;
                    216:
                    217:        new = palloc(N_ATIME, f_atime);
1.17      millert   218:        new->sec_data = find_parsenum(new, "-atime", arg, NULL);
1.1       deraadt   219:        TIME_CORRECT(new, N_ATIME);
                    220:        return (new);
                    221: }
1.11      deraadt   222:
                    223: /*
                    224:  * -cmin n functions --
                    225:  *
                    226:  *     True if the difference between the last change of file
                    227:  *     status information and the current time is n min periods.
                    228:  */
                    229: int
1.30      deraadt   230: f_cmin(PLAN *plan, FTSENT *entry)
1.11      deraadt   231: {
                    232:        extern time_t now;
                    233:
                    234:        COMPARE((now - entry->fts_statp->st_ctime +
1.17      millert   235:            60 - 1) / 60, plan->sec_data);
1.11      deraadt   236: }
                    237:
                    238: PLAN *
1.28      deraadt   239: c_cmin(char *arg, char ***ignored, int unused)
1.11      deraadt   240: {
                    241:        PLAN *new;
                    242:
                    243:        ftsoptions &= ~FTS_NOSTAT;
                    244:
                    245:        new = palloc(N_CMIN, f_cmin);
1.17      millert   246:        new->sec_data = find_parsenum(new, "-cmin", arg, NULL);
1.11      deraadt   247:        TIME_CORRECT(new, N_CMIN);
                    248:        return (new);
                    249: }
                    250:
1.1       deraadt   251: /*
                    252:  * -ctime n functions --
                    253:  *
                    254:  *     True if the difference between the last change of file
                    255:  *     status information and the current time is n 24 hour periods.
                    256:  */
                    257: int
1.30      deraadt   258: f_ctime(PLAN *plan, FTSENT *entry)
1.1       deraadt   259: {
                    260:
                    261:        COMPARE((now - entry->fts_statp->st_ctime +
1.17      millert   262:            SECSPERDAY - 1) / SECSPERDAY, plan->sec_data);
1.1       deraadt   263: }
                    264:
                    265: PLAN *
1.28      deraadt   266: c_ctime(char *arg, char ***ignored, int unused)
1.1       deraadt   267: {
                    268:        PLAN *new;
                    269:
                    270:        ftsoptions &= ~FTS_NOSTAT;
                    271:
                    272:        new = palloc(N_CTIME, f_ctime);
1.17      millert   273:        new->sec_data = find_parsenum(new, "-ctime", arg, NULL);
1.1       deraadt   274:        TIME_CORRECT(new, N_CTIME);
                    275:        return (new);
                    276: }
                    277:
                    278: /*
                    279:  * -depth functions --
                    280:  *
                    281:  *     Always true, causes descent of the directory hierarchy to be done
                    282:  *     so that all entries in a directory are acted on before the directory
                    283:  *     itself.
                    284:  */
                    285: int
1.30      deraadt   286: f_always_true(PLAN *plan, FTSENT *entry)
1.1       deraadt   287: {
                    288:        return (1);
                    289: }
                    290:
                    291: PLAN *
1.28      deraadt   292: c_depth(char *ignore, char ***ignored, int unused)
1.1       deraadt   293: {
                    294:        isdepth = 1;
                    295:
                    296:        return (palloc(N_DEPTH, f_always_true));
1.45      tedu      297: }
                    298:
                    299: /*
                    300:  * -delete functions
                    301:  */
                    302: int
                    303: f_delete(PLAN *plan, FTSENT *entry)
                    304: {
                    305:
                    306:        /* can't delete these */
                    307:        if (strcmp(entry->fts_accpath, ".") == 0 ||
                    308:            strcmp(entry->fts_accpath, "..") == 0)
                    309:                return 1;
                    310:
                    311:        /* sanity check */
                    312:        if (isdepth == 0 ||                     /* depth off */
                    313:            (ftsoptions & FTS_NOSTAT))          /* not stat()ing */
                    314:                errx(1, "-delete: insecure options got turned on");
                    315:        if (!(ftsoptions & FTS_PHYSICAL) ||     /* physical off */
                    316:            (ftsoptions & FTS_LOGICAL))         /* or finally, logical on */
                    317:                errx(1, "-delete: forbidden when symlinks are followed");
                    318:
                    319:        /* Potentially unsafe - do not accept relative paths whatsoever */
                    320:        if (entry->fts_level > FTS_ROOTLEVEL &&
                    321:            strchr(entry->fts_accpath, '/') != NULL)
                    322:                errx(1, "-delete: %s: relative path potentially not safe",
                    323:                    entry->fts_accpath);
                    324: #if 0
                    325:        /* Turn off user immutable bits if running as root */
                    326:        if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
                    327:            !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
                    328:            geteuid() == 0)
                    329:                lchflags(entry->fts_accpath,
                    330:                    entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
                    331: #endif
                    332:        /* rmdir directories, unlink everything else */
                    333:        if (S_ISDIR(entry->fts_statp->st_mode)) {
                    334:                if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
                    335:                        warn("-delete: rmdir(%s)", entry->fts_path);
                    336:        } else {
                    337:                if (unlink(entry->fts_accpath) < 0)
                    338:                        warn("-delete: unlink(%s)", entry->fts_path);
                    339:
                    340:        }
                    341:
                    342:        return 1;
                    343: }
                    344:
                    345: PLAN *
                    346: c_delete(char *ignore, char ***ignored, int unused)
                    347: {
                    348:        ftsoptions &= ~FTS_NOSTAT;
                    349:        isoutput = 1;
                    350:        isdelete = 1;
                    351:        isdepth = 1;
                    352:
                    353:        return (palloc(N_DELETE, f_delete));
1.1       deraadt   354: }
                    355:
1.7       tholo     356: /*
                    357:  * -empty functions --
                    358:  *
                    359:  *     True if the file or directory is empty
                    360:  */
                    361: int
1.30      deraadt   362: f_empty(PLAN *plan, FTSENT *entry)
1.7       tholo     363: {
                    364:        if (S_ISREG(entry->fts_statp->st_mode) && entry->fts_statp->st_size == 0)
                    365:                return (1);
                    366:        if (S_ISDIR(entry->fts_statp->st_mode)) {
                    367:                struct dirent *dp;
                    368:                int empty;
                    369:                DIR *dir;
                    370:
                    371:                empty = 1;
                    372:                dir = opendir(entry->fts_accpath);
                    373:                if (dir == NULL)
1.36      millert   374:                        return (0);
1.7       tholo     375:                for (dp = readdir(dir); dp; dp = readdir(dir))
                    376:                        if (dp->d_name[0] != '.' ||
                    377:                            (dp->d_name[1] != '\0' &&
                    378:                             (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
                    379:                                empty = 0;
                    380:                                break;
                    381:                        }
                    382:                closedir(dir);
                    383:                return (empty);
                    384:        }
                    385:        return (0);
                    386: }
                    387:
                    388: PLAN *
1.28      deraadt   389: c_empty(char *ignore, char ***ignored, int unused)
1.7       tholo     390: {
                    391:        ftsoptions &= ~FTS_NOSTAT;
                    392:
                    393:        return (palloc(N_EMPTY, f_empty));
                    394: }
                    395:
1.1       deraadt   396: /*
                    397:  * [-exec | -ok] utility [arg ... ] ; functions --
1.37      pascal    398:  * [-exec | -ok] utility [arg ... ] {} + functions --
1.1       deraadt   399:  *
1.37      pascal    400:  *     If the end of the primary expression is delimited by a
                    401:  *     semicolon: true if the executed utility returns a zero value
                    402:  *     as exit status.  If "{}" occurs anywhere, it gets replaced by
                    403:  *     the current pathname.
                    404:  *
                    405:  *     If the end of the primary expression is delimited by a plus
                    406:  *     sign: always true. Pathnames for which the primary is
                    407:  *     evaluated shall be aggregated into sets. The utility will be
                    408:  *     executed once per set, with "{}" replaced by the entire set of
                    409:  *     pathnames (as if xargs). "{}" must appear last.
                    410:  *
                    411:  *     The current directory for the execution of utility is the same
                    412:  *     as the current directory when the find utility was started.
1.1       deraadt   413:  *
1.37      pascal    414:  *     The primary -ok is different in that it requests affirmation
                    415:  *     of the user before executing the utility.
1.1       deraadt   416:  */
                    417: int
1.30      deraadt   418: f_exec(PLAN *plan, FTSENT *entry)
1.1       deraadt   419: {
1.37      pascal    420:        int cnt, l;
1.1       deraadt   421:        pid_t pid;
                    422:        int status;
                    423:
1.37      pascal    424:        if (plan->flags & F_PLUSSET) {
                    425:                /*
                    426:                 * Confirm sufficient buffer space, then copy the path
                    427:                 * to the buffer.
                    428:                 */
                    429:                l = strlen(entry->fts_path);
                    430:                if (plan->ep_p + l < plan->ep_ebp) {
                    431:                        plan->ep_bxp[plan->ep_narg++] = plan->ep_p;
                    432:                        strlcpy(plan->ep_p, entry->fts_path, l + 1);
                    433:                        plan->ep_p += l + 1;
                    434:
                    435:                        if (plan->ep_narg == plan->ep_maxargs)
                    436:                                run_f_exec(plan);
                    437:                } else {
                    438:                        /*
                    439:                         * Without sufficient space to copy in the next
                    440:                         * argument, run the command to empty out the
                    441:                         * buffer before re-attepting the copy.
                    442:                         */
                    443:                        run_f_exec(plan);
                    444:                        if (plan->ep_p + l < plan->ep_ebp) {
                    445:                                plan->ep_bxp[plan->ep_narg++] = plan->ep_p;
                    446:                                strlcpy(plan->ep_p, entry->fts_path, l + 1);
                    447:                                plan->ep_p += l + 1;
                    448:                        } else
                    449:                                errx(1, "insufficient space for argument");
                    450:                }
                    451:                return (1);
                    452:        } else {
                    453:                for (cnt = 0; plan->e_argv[cnt]; ++cnt)
                    454:                        if (plan->e_len[cnt])
                    455:                                brace_subst(plan->e_orig[cnt],
                    456:                                    &plan->e_argv[cnt],
                    457:                                    entry->fts_path,
                    458:                                    plan->e_len[cnt]);
                    459:                if (plan->flags & F_NEEDOK && !queryuser(plan->e_argv))
                    460:                        return (0);
                    461:
                    462:                /* don't mix output of command with find output */
                    463:                fflush(stdout);
                    464:                fflush(stderr);
                    465:
                    466:                switch (pid = vfork()) {
                    467:                case -1:
                    468:                        err(1, "fork");
                    469:                        /* NOTREACHED */
                    470:                case 0:
                    471:                        if (fchdir(dotfd)) {
                    472:                                warn("chdir");
                    473:                                _exit(1);
                    474:                        }
                    475:                        execvp(plan->e_argv[0], plan->e_argv);
                    476:                        warn("%s", plan->e_argv[0]);
                    477:                        _exit(1);
                    478:                }
                    479:                pid = waitpid(pid, &status, 0);
                    480:                return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
                    481:        }
                    482: }
                    483:
                    484: static void
                    485: run_f_exec(PLAN *plan)
                    486: {
                    487:        pid_t pid;
                    488:        int rval, status;
1.1       deraadt   489:
1.37      pascal    490:        /* Ensure arg list is null terminated. */
                    491:        plan->ep_bxp[plan->ep_narg] = NULL;
1.1       deraadt   492:
1.37      pascal    493:        /* Don't mix output of command with find output. */
                    494:        fflush(stdout);
                    495:        fflush(stderr);
1.1       deraadt   496:
                    497:        switch (pid = vfork()) {
                    498:        case -1:
1.37      pascal    499:                err(1, "vfork");
1.1       deraadt   500:                /* NOTREACHED */
                    501:        case 0:
                    502:                if (fchdir(dotfd)) {
                    503:                        warn("chdir");
                    504:                        _exit(1);
                    505:                }
                    506:                execvp(plan->e_argv[0], plan->e_argv);
                    507:                warn("%s", plan->e_argv[0]);
                    508:                _exit(1);
                    509:        }
1.37      pascal    510:
                    511:        /* Clear out the argument list. */
                    512:        plan->ep_narg = 0;
                    513:        plan->ep_bxp[plan->ep_narg] = NULL;
                    514:        /* As well as the argument buffer. */
                    515:        plan->ep_p = plan->ep_bbp;
                    516:        *plan->ep_p = '\0';
                    517:
1.1       deraadt   518:        pid = waitpid(pid, &status, 0);
1.37      pascal    519:        if (WIFEXITED(status))
                    520:                rval = WEXITSTATUS(status);
                    521:        else
                    522:                rval = -1;
                    523:
                    524:        /*
                    525:         * If we have a non-zero exit status, preserve it so find(1) can
                    526:         * later exit with it.
                    527:         */
                    528:        if (rval)
                    529:                plan->ep_rval = rval;
1.1       deraadt   530: }
                    531:
                    532: /*
                    533:  * c_exec --
                    534:  *     build three parallel arrays, one with pointers to the strings passed
                    535:  *     on the command line, one with (possibly duplicated) pointers to the
                    536:  *     argv array, and one with integer values that are lengths of the
                    537:  *     strings, but also flags meaning that the string has to be massaged.
1.37      pascal    538:  *
                    539:  *     If -exec ... {} +, use only the first array, but make it large
                    540:  *     enough to hold 5000 args (cf. src/usr.bin/xargs/xargs.c for a
                    541:  *     discussion), and then allocate ARG_MAX - 4K of space for args.
1.1       deraadt   542:  */
                    543: PLAN *
1.28      deraadt   544: c_exec(char *unused, char ***argvp, int isok)
1.1       deraadt   545: {
                    546:        PLAN *new;                      /* node returned */
1.37      pascal    547:        int cnt, brace, lastbrace;
1.21      mpech     548:        char **argv, **ap, *p;
1.35      millert   549:
                    550:        /* make sure the current directory is readable */
                    551:        if (dotfd == -1)
                    552:                errx(1, "%s: cannot open \".\"", isok ? "-ok" : "-exec");
1.1       deraadt   553:
                    554:        isoutput = 1;
                    555:
                    556:        new = palloc(N_EXEC, f_exec);
                    557:        if (isok)
1.37      pascal    558:                new->flags |= F_NEEDOK;
1.1       deraadt   559:
1.37      pascal    560:        /*
1.38      sthen     561:         * Terminate if we encounter an arg exactly equal to ";", or an
                    562:         * arg exactly equal to "+" following an arg exactly equal to
1.37      pascal    563:         * "{}".
                    564:         */
                    565:        for (ap = argv = *argvp, brace = 0;; ++ap) {
1.1       deraadt   566:                if (!*ap)
1.37      pascal    567:                        errx(1, "%s: no terminating \";\" or \"+\"",
                    568:                            isok ? "-ok" : "-exec");
                    569:                lastbrace = brace;
                    570:                brace = 0;
                    571:                if (strcmp(*ap, "{}") == 0)
                    572:                        brace = 1;
                    573:                if (strcmp(*ap, ";") == 0)
                    574:                        break;
                    575:                if (strcmp(*ap, "+") == 0 && lastbrace) {
                    576:                        new->flags |= F_PLUSSET;
1.8       millert   577:                        break;
1.37      pascal    578:                }
1.8       millert   579:        }
                    580:
                    581:
1.37      pascal    582:        /*
                    583:         * POSIX says -ok ... {} + "need not be supported," and it does
                    584:         * not make much sense anyway.
                    585:         */
                    586:        if (new->flags & F_NEEDOK && new->flags & F_PLUSSET)
                    587:                errx(1, "-ok: terminating \"+\" not permitted.");
                    588:
                    589:        if (new->flags & F_PLUSSET) {
                    590:                u_int c, bufsize;
                    591:
                    592:                cnt = ap - *argvp - 1;                  /* units are words */
                    593:                new->ep_maxargs = 5000;
1.41      espie     594:                new->e_argv = ereallocarray(NULL,
                    595:                    (size_t)(cnt + new->ep_maxargs), sizeof(char **));
1.37      pascal    596:
                    597:                /* We start stuffing arguments after the user's last one. */
                    598:                new->ep_bxp = &new->e_argv[cnt];
                    599:                new->ep_narg = 0;
                    600:
                    601:                /*
                    602:                 * Count up the space of the user's arguments, and
                    603:                 * subtract that from what we allocate.
                    604:                 */
                    605:                for (argv = *argvp, c = 0, cnt = 0;
                    606:                     argv < ap;
                    607:                     ++argv, ++cnt) {
                    608:                        c += strlen(*argv) + 1;
                    609:                        new->e_argv[cnt] = *argv;
                    610:                }
                    611:                bufsize = ARG_MAX - 4 * 1024 - c;
                    612:
                    613:
                    614:                /*
                    615:                 * Allocate, and then initialize current, base, and
                    616:                 * end pointers.
                    617:                 */
                    618:                new->ep_p = new->ep_bbp = malloc(bufsize + 1);
                    619:                new->ep_ebp = new->ep_bbp + bufsize - 1;
                    620:                new->ep_rval = 0;
                    621:        } else { /* !F_PLUSSET */
                    622:                cnt = ap - *argvp + 1;
1.41      espie     623:                new->e_argv = ereallocarray(NULL, cnt, sizeof(char *));
                    624:                new->e_orig = ereallocarray(NULL, cnt, sizeof(char *));
                    625:                new->e_len = ereallocarray(NULL, cnt, sizeof(int));
1.37      pascal    626:
                    627:                for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
                    628:                        new->e_orig[cnt] = *argv;
                    629:                        for (p = *argv; *p; ++p)
                    630:                                if (p[0] == '{' && p[1] == '}') {
                    631:                                        new->e_argv[cnt] =
1.42      deraadt   632:                                                emalloc((u_int)PATH_MAX);
                    633:                                        new->e_len[cnt] = PATH_MAX;
1.37      pascal    634:                                        break;
                    635:                                }
                    636:                        if (!*p) {
                    637:                                new->e_argv[cnt] = *argv;
                    638:                                new->e_len[cnt] = 0;
1.8       millert   639:                        }
                    640:                }
1.37      pascal    641:                new->e_orig[cnt] = NULL;
                    642:        }
1.8       millert   643:
1.37      pascal    644:        new->e_argv[cnt] = NULL;
1.8       millert   645:        *argvp = argv + 1;
                    646:        return (new);
                    647: }
                    648:
                    649: /*
                    650:  * -execdir utility [arg ... ] ; functions --
                    651:  *
                    652:  *     True if the executed utility returns a zero value as exit status.
                    653:  *     The end of the primary expression is delimited by a semicolon.  If
                    654:  *     "{}" occurs anywhere, it gets replaced by the unqualified pathname.
                    655:  *     The current directory for the execution of utility is the same as
                    656:  *     the directory where the file lives.
                    657:  */
                    658: int
1.30      deraadt   659: f_execdir(PLAN *plan, FTSENT *entry)
1.8       millert   660: {
1.21      mpech     661:        int cnt;
1.8       millert   662:        pid_t pid;
1.15      millert   663:        int status, fd;
1.42      deraadt   664:        char base[PATH_MAX];
1.8       millert   665:
1.15      millert   666:        /* fts(3) does not chdir for the root level so we do it ourselves. */
                    667:        if (entry->fts_level == FTS_ROOTLEVEL) {
                    668:                if ((fd = open(".", O_RDONLY)) == -1) {
                    669:                        warn("cannot open \".\"");
                    670:                        return (0);
                    671:                }
                    672:                if (chdir(entry->fts_accpath)) {
1.16      millert   673:                        (void) close(fd);
1.15      millert   674:                        return (0);
                    675:                }
                    676:        }
                    677:
1.10      millert   678:        /* Substitute basename(path) for {} since cwd is it's parent dir */
                    679:        (void)strncpy(base, basename(entry->fts_path), sizeof(base) - 1);
                    680:        base[sizeof(base) - 1] = '\0';
1.8       millert   681:        for (cnt = 0; plan->e_argv[cnt]; ++cnt)
                    682:                if (plan->e_len[cnt])
                    683:                        brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
1.10      millert   684:                            base, plan->e_len[cnt]);
1.8       millert   685:
                    686:        /* don't mix output of command with find output */
                    687:        fflush(stdout);
                    688:        fflush(stderr);
                    689:
                    690:        switch (pid = vfork()) {
                    691:        case -1:
                    692:                err(1, "fork");
                    693:                /* NOTREACHED */
                    694:        case 0:
                    695:                execvp(plan->e_argv[0], plan->e_argv);
                    696:                warn("%s", plan->e_argv[0]);
                    697:                _exit(1);
                    698:        }
1.15      millert   699:
                    700:        /* Undo the above... */
                    701:        if (entry->fts_level == FTS_ROOTLEVEL) {
                    702:                if (fchdir(fd) == -1) {
                    703:                        warn("unable to chdir back to starting directory");
1.16      millert   704:                        (void) close(fd);
1.15      millert   705:                        return (0);
                    706:                }
1.16      millert   707:                (void) close(fd);
1.15      millert   708:        }
                    709:
1.8       millert   710:        pid = waitpid(pid, &status, 0);
                    711:        return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
                    712: }
                    713:
                    714: /*
                    715:  * c_execdir --
                    716:  *     build three parallel arrays, one with pointers to the strings passed
                    717:  *     on the command line, one with (possibly duplicated) pointers to the
                    718:  *     argv array, and one with integer values that are lengths of the
                    719:  *     strings, but also flags meaning that the string has to be massaged.
                    720:  */
                    721: PLAN *
1.28      deraadt   722: c_execdir(char *ignored, char ***argvp, int unused)
1.8       millert   723: {
                    724:        PLAN *new;                      /* node returned */
1.21      mpech     725:        int cnt;
                    726:        char **argv, **ap, *p;
1.8       millert   727:
                    728:        ftsoptions &= ~FTS_NOSTAT;
                    729:        isoutput = 1;
                    730:
                    731:        new = palloc(N_EXECDIR, f_execdir);
                    732:
                    733:        for (ap = argv = *argvp;; ++ap) {
                    734:                if (!*ap)
                    735:                        errx(1,
                    736:                            "-execdir: no terminating \";\"");
1.1       deraadt   737:                if (**ap == ';')
                    738:                        break;
                    739:        }
                    740:
                    741:        cnt = ap - *argvp + 1;
1.41      espie     742:        new->e_argv = ereallocarray(NULL, cnt, sizeof(char *));
                    743:        new->e_orig = ereallocarray(NULL, cnt, sizeof(char *));
                    744:        new->e_len = ereallocarray(NULL, cnt, sizeof(int));
1.1       deraadt   745:
                    746:        for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
                    747:                new->e_orig[cnt] = *argv;
                    748:                for (p = *argv; *p; ++p)
                    749:                        if (p[0] == '{' && p[1] == '}') {
1.42      deraadt   750:                                new->e_argv[cnt] = emalloc((u_int)PATH_MAX);
                    751:                                new->e_len[cnt] = PATH_MAX;
1.1       deraadt   752:                                break;
                    753:                        }
                    754:                if (!*p) {
                    755:                        new->e_argv[cnt] = *argv;
                    756:                        new->e_len[cnt] = 0;
                    757:                }
                    758:        }
                    759:        new->e_argv[cnt] = new->e_orig[cnt] = NULL;
                    760:
                    761:        *argvp = argv + 1;
                    762:        return (new);
                    763: }
1.19      millert   764:
                    765: /*
                    766:  * -flags functions --
                    767:  *
                    768:  *     The flags argument is used to represent file flags bits.
                    769:  */
                    770: int
1.30      deraadt   771: f_flags(PLAN *plan, FTSENT *entry)
1.19      millert   772: {
                    773:        u_int flags;
                    774:
                    775:        flags = entry->fts_statp->st_flags &
                    776:            (UF_NODUMP | UF_IMMUTABLE | UF_APPEND | UF_OPAQUE |
                    777:             SF_ARCHIVED | SF_IMMUTABLE | SF_APPEND);
                    778:        if (plan->flags == F_ATLEAST)
                    779:                /* note that plan->fl_flags always is a subset of
                    780:                   plan->fl_mask */
                    781:                return ((flags & plan->fl_mask) == plan->fl_flags);
                    782:        else
                    783:                return (flags == plan->fl_flags);
                    784:        /* NOTREACHED */
                    785: }
                    786:
                    787: PLAN *
1.28      deraadt   788: c_flags(char *flags_str, char ***ignored, int unused)
1.19      millert   789: {
                    790:        PLAN *new;
1.20      mickey    791:        u_int32_t flags, notflags;
1.19      millert   792:
                    793:        ftsoptions &= ~FTS_NOSTAT;
                    794:
                    795:        new = palloc(N_FLAGS, f_flags);
                    796:
                    797:        if (*flags_str == '-') {
                    798:                new->flags = F_ATLEAST;
                    799:                ++flags_str;
                    800:        }
                    801:
1.20      mickey    802:        if (strtofflags(&flags_str, &flags, &notflags) == 1)
1.19      millert   803:                errx(1, "-flags: %s: illegal flags string", flags_str);
                    804:
                    805:        new->fl_flags = flags;
                    806:        new->fl_mask = flags | notflags;
                    807:        return (new);
                    808: }
1.1       deraadt   809:
                    810: /*
                    811:  * -follow functions --
                    812:  *
                    813:  *     Always true, causes symbolic links to be followed on a global
                    814:  *     basis.
                    815:  */
                    816: PLAN *
1.28      deraadt   817: c_follow(char *ignore, char ***ignored, int unused)
1.1       deraadt   818: {
                    819:        ftsoptions &= ~FTS_PHYSICAL;
                    820:        ftsoptions |= FTS_LOGICAL;
                    821:
                    822:        return (palloc(N_FOLLOW, f_always_true));
                    823: }
                    824:
                    825: /*
                    826:  * -fstype functions --
                    827:  *
                    828:  *     True if the file is of a certain type.
                    829:  */
                    830: int
1.30      deraadt   831: f_fstype(PLAN *plan, FTSENT *entry)
1.1       deraadt   832: {
                    833:        static dev_t curdev;    /* need a guaranteed illegal dev value */
                    834:        static int first = 1;
                    835:        struct statfs sb;
                    836:        static short val;
                    837:        static char fstype[MFSNAMELEN];
                    838:        char *p, save[2];
                    839:
                    840:        /* Only check when we cross mount point. */
                    841:        if (first || curdev != entry->fts_statp->st_dev) {
                    842:                curdev = entry->fts_statp->st_dev;
                    843:
                    844:                /*
                    845:                 * Statfs follows symlinks; find wants the link's file system,
                    846:                 * not where it points.
                    847:                 */
                    848:                if (entry->fts_info == FTS_SL ||
                    849:                    entry->fts_info == FTS_SLNONE) {
1.9       millert   850:                        if ((p = strrchr(entry->fts_accpath, '/')))
1.1       deraadt   851:                                ++p;
                    852:                        else
                    853:                                p = entry->fts_accpath;
                    854:                        save[0] = p[0];
                    855:                        p[0] = '.';
                    856:                        save[1] = p[1];
                    857:                        p[1] = '\0';
                    858:
                    859:                } else
                    860:                        p = NULL;
                    861:
                    862:                if (statfs(entry->fts_accpath, &sb))
                    863:                        err(1, "%s", entry->fts_accpath);
                    864:
                    865:                if (p) {
                    866:                        p[0] = save[0];
                    867:                        p[1] = save[1];
                    868:                }
                    869:
                    870:                first = 0;
                    871:
                    872:                /*
                    873:                 * Further tests may need both of these values, so
                    874:                 * always copy both of them.
                    875:                 */
                    876:                val = sb.f_flags;
                    877:                strncpy(fstype, sb.f_fstypename, MFSNAMELEN);
                    878:        }
                    879:        switch (plan->flags) {
                    880:        case F_MTFLAG:
                    881:                return (val & plan->mt_data);
                    882:        case F_MTTYPE:
                    883:                return (strncmp(fstype, plan->c_data, MFSNAMELEN) == 0);
                    884:        default:
                    885:                abort();
                    886:        }
                    887: }
                    888:
                    889: PLAN *
1.28      deraadt   890: c_fstype(char *arg, char ***ignored, int unused)
1.1       deraadt   891: {
1.21      mpech     892:        PLAN *new;
1.1       deraadt   893:
                    894:        ftsoptions &= ~FTS_NOSTAT;
                    895:
                    896:        new = palloc(N_FSTYPE, f_fstype);
                    897:        switch (*arg) {
                    898:        case 'l':
                    899:                if (!strcmp(arg, "local")) {
                    900:                        new->flags = F_MTFLAG;
                    901:                        new->mt_data = MNT_LOCAL;
                    902:                        return (new);
                    903:                }
                    904:                break;
                    905:        case 'r':
                    906:                if (!strcmp(arg, "rdonly")) {
                    907:                        new->flags = F_MTFLAG;
                    908:                        new->mt_data = MNT_RDONLY;
                    909:                        return (new);
                    910:                }
                    911:                break;
                    912:        }
                    913:
                    914:        new->flags = F_MTTYPE;
                    915:        new->c_data = arg;
                    916:        return (new);
                    917: }
                    918:
                    919: /*
                    920:  * -group gname functions --
                    921:  *
                    922:  *     True if the file belongs to the group gname.  If gname is numeric and
                    923:  *     an equivalent of the getgrnam() function does not return a valid group
                    924:  *     name, gname is taken as a group ID.
                    925:  */
                    926: int
1.30      deraadt   927: f_group(PLAN *plan, FTSENT *entry)
1.1       deraadt   928: {
                    929:        return (entry->fts_statp->st_gid == plan->g_data);
                    930: }
                    931:
                    932: PLAN *
1.28      deraadt   933: c_group(char *gname, char ***ignored, int unused)
1.1       deraadt   934: {
                    935:        PLAN *new;
                    936:        gid_t gid;
                    937:
                    938:        ftsoptions &= ~FTS_NOSTAT;
                    939:
1.46    ! millert   940:        if (gid_from_group(gname, &gid) == -1) {
1.44      deraadt   941:                const char *errstr;
                    942:
                    943:                gid = strtonum(gname, 0, GID_MAX, &errstr);
                    944:                if (errstr)
1.1       deraadt   945:                        errx(1, "-group: %s: no such group", gname);
1.46    ! millert   946:        }
1.1       deraadt   947:
                    948:        new = palloc(N_GROUP, f_group);
                    949:        new->g_data = gid;
                    950:        return (new);
                    951: }
                    952:
                    953: /*
                    954:  * -inum n functions --
                    955:  *
                    956:  *     True if the file has inode # n.
                    957:  */
                    958: int
1.30      deraadt   959: f_inum(PLAN *plan, FTSENT *entry)
1.1       deraadt   960: {
                    961:        COMPARE(entry->fts_statp->st_ino, plan->i_data);
                    962: }
                    963:
                    964: PLAN *
1.28      deraadt   965: c_inum(char *arg, char ***ignored, int unused)
1.1       deraadt   966: {
1.39      deraadt   967:        long long inum;
1.1       deraadt   968:        PLAN *new;
                    969:
                    970:        ftsoptions &= ~FTS_NOSTAT;
                    971:
                    972:        new = palloc(N_INUM, f_inum);
1.39      deraadt   973:        inum = find_parsenum(new, "-inum", arg, NULL);
                    974:        if (inum != (ino_t)inum)
                    975:                errx(1, "-inum: %s: number too great", arg);
                    976:        new->i_data = inum;
1.1       deraadt   977:        return (new);
                    978: }
                    979:
                    980: /*
                    981:  * -links n functions --
                    982:  *
                    983:  *     True if the file has n links.
                    984:  */
                    985: int
1.30      deraadt   986: f_links(PLAN *plan, FTSENT *entry)
1.1       deraadt   987: {
                    988:        COMPARE(entry->fts_statp->st_nlink, plan->l_data);
                    989: }
                    990:
                    991: PLAN *
1.28      deraadt   992: c_links(char *arg, char ***ignored, int unused)
1.1       deraadt   993: {
                    994:        PLAN *new;
1.39      deraadt   995:        long long nlink;
1.1       deraadt   996:
                    997:        ftsoptions &= ~FTS_NOSTAT;
                    998:
                    999:        new = palloc(N_LINKS, f_links);
1.39      deraadt  1000:        nlink = find_parsenum(new, "-links", arg, NULL);
                   1001:        if (nlink != (nlink_t)nlink)
                   1002:                errx(1, "-links: %s: number too great", arg);
                   1003:        new->l_data = nlink;
1.1       deraadt  1004:        return (new);
                   1005: }
                   1006:
                   1007: /*
                   1008:  * -ls functions --
                   1009:  *
                   1010:  *     Always true - prints the current entry to stdout in "ls" format.
                   1011:  */
                   1012: int
1.30      deraadt  1013: f_ls(PLAN *plan, FTSENT *entry)
1.1       deraadt  1014: {
                   1015:        printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
                   1016:        return (1);
                   1017: }
                   1018:
                   1019: PLAN *
1.28      deraadt  1020: c_ls(char *ignore, char ***ignored, int unused)
1.1       deraadt  1021: {
                   1022:        ftsoptions &= ~FTS_NOSTAT;
                   1023:        isoutput = 1;
                   1024:
                   1025:        return (palloc(N_LS, f_ls));
1.5       tholo    1026: }
                   1027:
                   1028: /*
                   1029:  * - maxdepth n functions --
                   1030:  *
                   1031:  *     True if the current search depth is less than or equal to the
                   1032:  *     maximum depth specified
                   1033:  */
                   1034: int
1.30      deraadt  1035: f_maxdepth(PLAN *plan, FTSENT *entry)
1.5       tholo    1036: {
                   1037:
1.6       tholo    1038:        if (entry->fts_level >= plan->max_data)
1.5       tholo    1039:                fts_set(tree, entry, FTS_SKIP);
1.6       tholo    1040:        return (entry->fts_level <= plan->max_data);
1.5       tholo    1041: }
                   1042:
                   1043: PLAN *
1.28      deraadt  1044: c_maxdepth(char *arg, char ***ignored, int unused)
1.5       tholo    1045: {
                   1046:        PLAN *new;
1.32      millert  1047:        const char *errstr = NULL;
1.5       tholo    1048:
1.6       tholo    1049:        new = palloc(N_MAXDEPTH, f_maxdepth);
1.33      millert  1050:        new->max_data = strtonum(arg, 0, FTS_MAXLEVEL, &errstr);
1.32      millert  1051:        if (errstr)
                   1052:                errx(1, "%s: maxdepth value %s", arg, errstr);
1.6       tholo    1053:        return (new);
                   1054: }
                   1055:
                   1056: /*
                   1057:  * - mindepth n functions --
                   1058:  *
                   1059:  *     True if the current search depth is greater than or equal to the
                   1060:  *     minimum depth specified
                   1061:  */
                   1062: int
1.30      deraadt  1063: f_mindepth(PLAN *plan, FTSENT *entry)
1.6       tholo    1064: {
                   1065:
                   1066:        return (entry->fts_level >= plan->min_data);
                   1067: }
                   1068:
                   1069: PLAN *
1.28      deraadt  1070: c_mindepth(char *arg, char ***ignored, int unused)
1.6       tholo    1071: {
                   1072:        PLAN *new;
1.44      deraadt  1073:        const char *errstr = NULL;
1.6       tholo    1074:
                   1075:        new = palloc(N_MINDEPTH, f_mindepth);
1.44      deraadt  1076:        new->min_data = strtonum(arg, 0, INT_MAX, &errstr);
                   1077:        if (errstr)
                   1078:                errx(1, "-mindepth: %s: value %s", arg, errstr);
1.5       tholo    1079:        return (new);
1.1       deraadt  1080: }
                   1081:
                   1082: /*
                   1083:  * -mtime n functions --
                   1084:  *
                   1085:  *     True if the difference between the file modification time and the
                   1086:  *     current time is n 24 hour periods.
                   1087:  */
                   1088: int
1.30      deraadt  1089: f_mtime(PLAN *plan, FTSENT *entry)
1.1       deraadt  1090: {
                   1091:
                   1092:        COMPARE((now - entry->fts_statp->st_mtime + SECSPERDAY - 1) /
1.17      millert  1093:            SECSPERDAY, plan->sec_data);
1.1       deraadt  1094: }
                   1095:
                   1096: PLAN *
1.28      deraadt  1097: c_mtime(char *arg, char ***ignored, int unused)
1.1       deraadt  1098: {
                   1099:        PLAN *new;
                   1100:
                   1101:        ftsoptions &= ~FTS_NOSTAT;
                   1102:
                   1103:        new = palloc(N_MTIME, f_mtime);
1.17      millert  1104:        new->sec_data = find_parsenum(new, "-mtime", arg, NULL);
1.1       deraadt  1105:        TIME_CORRECT(new, N_MTIME);
1.11      deraadt  1106:        return (new);
                   1107: }
                   1108:
                   1109: /*
                   1110:  * -mmin n functions --
                   1111:  *
                   1112:  *     True if the difference between the file modification time and the
                   1113:  *     current time is n min periods.
                   1114:  */
                   1115: int
1.30      deraadt  1116: f_mmin(PLAN *plan, FTSENT *entry)
1.11      deraadt  1117: {
                   1118:        extern time_t now;
                   1119:
                   1120:        COMPARE((now - entry->fts_statp->st_mtime + 60 - 1) /
1.17      millert  1121:            60, plan->sec_data);
1.11      deraadt  1122: }
                   1123:
                   1124: PLAN *
1.28      deraadt  1125: c_mmin(char *arg, char ***ignored, int unused)
1.11      deraadt  1126: {
                   1127:        PLAN *new;
                   1128:
                   1129:        ftsoptions &= ~FTS_NOSTAT;
                   1130:
                   1131:        new = palloc(N_MMIN, f_mmin);
1.17      millert  1132:        new->sec_data = find_parsenum(new, "-mmin", arg, NULL);
1.11      deraadt  1133:        TIME_CORRECT(new, N_MMIN);
1.1       deraadt  1134:        return (new);
                   1135: }
                   1136:
                   1137: /*
                   1138:  * -name functions --
                   1139:  *
                   1140:  *     True if the basename of the filename being examined
                   1141:  *     matches pattern using Pattern Matching Notation S3.14
                   1142:  */
                   1143: int
1.30      deraadt  1144: f_name(PLAN *plan, FTSENT *entry)
1.1       deraadt  1145: {
                   1146:        return (!fnmatch(plan->c_data, entry->fts_name, 0));
                   1147: }
                   1148:
                   1149: PLAN *
1.28      deraadt  1150: c_name(char *pattern, char ***ignored, int unused)
1.1       deraadt  1151: {
                   1152:        PLAN *new;
                   1153:
                   1154:        new = palloc(N_NAME, f_name);
1.18      deraadt  1155:        new->c_data = pattern;
                   1156:        return (new);
                   1157: }
                   1158:
                   1159: /*
                   1160:  * -iname functions --
                   1161:  *
                   1162:  *     Similar to -name, but does case insensitive matching
                   1163:  *
                   1164:  */
                   1165: int
1.30      deraadt  1166: f_iname(PLAN *plan, FTSENT *entry)
1.18      deraadt  1167: {
                   1168:        return (!fnmatch(plan->c_data, entry->fts_name, FNM_CASEFOLD));
                   1169: }
                   1170:
                   1171: PLAN *
1.28      deraadt  1172: c_iname(char *pattern, char ***ignored, int unused)
1.18      deraadt  1173: {
                   1174:        PLAN *new;
                   1175:
                   1176:        new = palloc(N_INAME, f_iname);
1.1       deraadt  1177:        new->c_data = pattern;
                   1178:        return (new);
                   1179: }
                   1180:
                   1181: /*
                   1182:  * -newer file functions --
                   1183:  *
                   1184:  *     True if the current file has been modified more recently
                   1185:  *     then the modification time of the file named by the pathname
                   1186:  *     file.
                   1187:  */
                   1188: int
1.30      deraadt  1189: f_newer(PLAN *plan, FTSENT *entry)
1.1       deraadt  1190: {
1.17      millert  1191:
                   1192:        return (entry->fts_statp->st_mtimespec.tv_sec > plan->t_data.tv_sec ||
                   1193:            (entry->fts_statp->st_mtimespec.tv_sec == plan->t_data.tv_sec &&
                   1194:            entry->fts_statp->st_mtimespec.tv_nsec > plan->t_data.tv_nsec));
1.1       deraadt  1195: }
                   1196:
                   1197: PLAN *
1.28      deraadt  1198: c_newer(char *filename, char ***ignored, int unused)
1.1       deraadt  1199: {
                   1200:        PLAN *new;
                   1201:        struct stat sb;
                   1202:
                   1203:        ftsoptions &= ~FTS_NOSTAT;
                   1204:
                   1205:        if (stat(filename, &sb))
                   1206:                err(1, "%s", filename);
                   1207:        new = palloc(N_NEWER, f_newer);
1.17      millert  1208:        memcpy(&new->t_data, &sb.st_mtimespec, sizeof(struct timespec));
                   1209:        return (new);
                   1210: }
                   1211:
                   1212: /*
                   1213:  * -anewer file functions --
                   1214:  *
                   1215:  *     True if the current file has been accessed more recently
                   1216:  *     then the access time of the file named by the pathname
                   1217:  *     file.
                   1218:  */
                   1219: int
1.30      deraadt  1220: f_anewer(PLAN *plan, FTSENT *entry)
1.17      millert  1221: {
                   1222:
                   1223:        return (entry->fts_statp->st_atimespec.tv_sec > plan->t_data.tv_sec ||
                   1224:            (entry->fts_statp->st_atimespec.tv_sec == plan->t_data.tv_sec &&
                   1225:            entry->fts_statp->st_atimespec.tv_nsec > plan->t_data.tv_nsec));
                   1226: }
                   1227:
                   1228: PLAN *
1.28      deraadt  1229: c_anewer(char *filename, char ***ignored, int unused)
1.17      millert  1230: {
                   1231:        PLAN *new;
                   1232:        struct stat sb;
                   1233:
                   1234:        ftsoptions &= ~FTS_NOSTAT;
                   1235:
                   1236:        if (stat(filename, &sb))
                   1237:                err(1, "%s", filename);
1.24      millert  1238:        new = palloc(N_NEWER, f_anewer);
1.17      millert  1239:        memcpy(&new->t_data, &sb.st_atimespec, sizeof(struct timespec));
                   1240:        return (new);
                   1241: }
                   1242:
                   1243: /*
                   1244:  * -cnewer file functions --
                   1245:  *
                   1246:  *     True if the current file has been changed more recently
                   1247:  *     then the inode change time of the file named by the pathname
                   1248:  *     file.
                   1249:  */
                   1250: int
1.30      deraadt  1251: f_cnewer(PLAN *plan, FTSENT *entry)
1.17      millert  1252: {
                   1253:
                   1254:        return (entry->fts_statp->st_ctimespec.tv_sec > plan->t_data.tv_sec ||
                   1255:            (entry->fts_statp->st_ctimespec.tv_sec == plan->t_data.tv_sec &&
                   1256:            entry->fts_statp->st_ctimespec.tv_nsec > plan->t_data.tv_nsec));
                   1257: }
                   1258:
                   1259: PLAN *
1.28      deraadt  1260: c_cnewer(char *filename, char ***ignored, int unused)
1.17      millert  1261: {
                   1262:        PLAN *new;
                   1263:        struct stat sb;
                   1264:
                   1265:        ftsoptions &= ~FTS_NOSTAT;
                   1266:
                   1267:        if (stat(filename, &sb))
                   1268:                err(1, "%s", filename);
1.24      millert  1269:        new = palloc(N_NEWER, f_cnewer);
1.17      millert  1270:        memcpy(&new->t_data, &sb.st_ctimespec, sizeof(struct timespec));
1.1       deraadt  1271:        return (new);
                   1272: }
                   1273:
                   1274: /*
                   1275:  * -nogroup functions --
                   1276:  *
                   1277:  *     True if file belongs to a user ID for which the equivalent
                   1278:  *     of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
                   1279:  */
                   1280: int
1.30      deraadt  1281: f_nogroup(PLAN *plan, FTSENT *entry)
1.1       deraadt  1282: {
                   1283:        return (group_from_gid(entry->fts_statp->st_gid, 1) ? 0 : 1);
                   1284: }
                   1285:
                   1286: PLAN *
1.28      deraadt  1287: c_nogroup(char *ignore, char ***ignored, int unused)
1.1       deraadt  1288: {
                   1289:        ftsoptions &= ~FTS_NOSTAT;
                   1290:
                   1291:        return (palloc(N_NOGROUP, f_nogroup));
                   1292: }
                   1293:
                   1294: /*
                   1295:  * -nouser functions --
                   1296:  *
                   1297:  *     True if file belongs to a user ID for which the equivalent
                   1298:  *     of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
                   1299:  */
                   1300: int
1.30      deraadt  1301: f_nouser(PLAN *plan, FTSENT *entry)
1.1       deraadt  1302: {
                   1303:        return (user_from_uid(entry->fts_statp->st_uid, 1) ? 0 : 1);
                   1304: }
                   1305:
                   1306: PLAN *
1.28      deraadt  1307: c_nouser(char *ignore, char ***ignored, int unused)
1.1       deraadt  1308: {
                   1309:        ftsoptions &= ~FTS_NOSTAT;
                   1310:
                   1311:        return (palloc(N_NOUSER, f_nouser));
                   1312: }
                   1313:
                   1314: /*
                   1315:  * -path functions --
                   1316:  *
                   1317:  *     True if the path of the filename being examined
                   1318:  *     matches pattern using Pattern Matching Notation S3.14
                   1319:  */
                   1320: int
1.30      deraadt  1321: f_path(PLAN *plan, FTSENT *entry)
1.1       deraadt  1322: {
                   1323:        return (!fnmatch(plan->c_data, entry->fts_path, 0));
                   1324: }
                   1325:
                   1326: PLAN *
1.28      deraadt  1327: c_path(char *pattern, char ***ignored, int unused)
1.1       deraadt  1328: {
                   1329:        PLAN *new;
                   1330:
                   1331:        new = palloc(N_NAME, f_path);
                   1332:        new->c_data = pattern;
                   1333:        return (new);
                   1334: }
                   1335:
                   1336: /*
                   1337:  * -perm functions --
                   1338:  *
                   1339:  *     The mode argument is used to represent file mode bits.  If it starts
                   1340:  *     with a leading digit, it's treated as an octal mode, otherwise as a
                   1341:  *     symbolic mode.
                   1342:  */
                   1343: int
1.30      deraadt  1344: f_perm(PLAN *plan, FTSENT *entry)
1.1       deraadt  1345: {
                   1346:        mode_t mode;
                   1347:
                   1348:        mode = entry->fts_statp->st_mode &
                   1349:            (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
                   1350:        if (plan->flags == F_ATLEAST)
                   1351:                return ((plan->m_data | mode) == mode);
                   1352:        else
                   1353:                return (mode == plan->m_data);
                   1354:        /* NOTREACHED */
                   1355: }
                   1356:
                   1357: PLAN *
1.28      deraadt  1358: c_perm(char *perm, char ***ignored, int unused)
1.1       deraadt  1359: {
                   1360:        PLAN *new;
1.29      otto     1361:        void *set;
1.1       deraadt  1362:
                   1363:        ftsoptions &= ~FTS_NOSTAT;
                   1364:
                   1365:        new = palloc(N_PERM, f_perm);
                   1366:
                   1367:        if (*perm == '-') {
                   1368:                new->flags = F_ATLEAST;
                   1369:                ++perm;
                   1370:        }
                   1371:
                   1372:        if ((set = setmode(perm)) == NULL)
1.19      millert  1373:                errx(1, "-perm: %s: illegal mode string", perm);
1.1       deraadt  1374:
                   1375:        new->m_data = getmode(set, 0);
1.12      deraadt  1376:        free(set);
1.1       deraadt  1377:        return (new);
                   1378: }
                   1379:
                   1380: /*
                   1381:  * -print functions --
                   1382:  *
                   1383:  *     Always true, causes the current pathame to be written to
                   1384:  *     standard output.
                   1385:  */
                   1386: int
1.30      deraadt  1387: f_print(PLAN *plan, FTSENT *entry)
1.1       deraadt  1388: {
                   1389:        (void)printf("%s\n", entry->fts_path);
                   1390:        return(1);
                   1391: }
                   1392:
                   1393: /* ARGSUSED */
1.9       millert  1394: int
1.30      deraadt  1395: f_print0(PLAN *plan, FTSENT *entry)
1.1       deraadt  1396: {
                   1397:        (void)fputs(entry->fts_path, stdout);
                   1398:        (void)fputc('\0', stdout);
                   1399:        return(1);
                   1400: }
                   1401:
                   1402: PLAN *
1.28      deraadt  1403: c_print(char *ignore, char ***ignored, int unused)
1.1       deraadt  1404: {
                   1405:        isoutput = 1;
                   1406:
                   1407:        return(palloc(N_PRINT, f_print));
                   1408: }
                   1409:
                   1410: PLAN *
1.28      deraadt  1411: c_print0(char *ignore, char ***ignored, int unused)
1.1       deraadt  1412: {
                   1413:        isoutput = 1;
                   1414:
                   1415:        return(palloc(N_PRINT0, f_print0));
                   1416: }
                   1417:
                   1418: /*
                   1419:  * -prune functions --
                   1420:  *
                   1421:  *     Prune a portion of the hierarchy.
                   1422:  */
                   1423: int
1.30      deraadt  1424: f_prune(PLAN *plan, FTSENT *entry)
1.1       deraadt  1425: {
                   1426:
                   1427:        if (fts_set(tree, entry, FTS_SKIP))
                   1428:                err(1, "%s", entry->fts_path);
                   1429:        return (1);
                   1430: }
                   1431:
                   1432: PLAN *
1.28      deraadt  1433: c_prune(char *ignore, char ***ignored, int unused)
1.1       deraadt  1434: {
                   1435:        return (palloc(N_PRUNE, f_prune));
                   1436: }
                   1437:
                   1438: /*
                   1439:  * -size n[c] functions --
                   1440:  *
                   1441:  *     True if the file size in bytes, divided by an implementation defined
                   1442:  *     value and rounded up to the next integer, is n.  If n is followed by
                   1443:  *     a c, the size is in bytes.
                   1444:  */
                   1445: #define        FIND_SIZE       512
                   1446: static int divsize = 1;
                   1447:
                   1448: int
1.30      deraadt  1449: f_size(PLAN *plan, FTSENT *entry)
1.1       deraadt  1450: {
                   1451:        off_t size;
                   1452:
                   1453:        size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
                   1454:            FIND_SIZE : entry->fts_statp->st_size;
                   1455:        COMPARE(size, plan->o_data);
                   1456: }
                   1457:
                   1458: PLAN *
1.28      deraadt  1459: c_size(char *arg, char ***ignored, int unused)
1.1       deraadt  1460: {
                   1461:        PLAN *new;
                   1462:        char endch;
                   1463:
                   1464:        ftsoptions &= ~FTS_NOSTAT;
                   1465:
                   1466:        new = palloc(N_SIZE, f_size);
                   1467:        endch = 'c';
                   1468:        new->o_data = find_parsenum(new, "-size", arg, &endch);
                   1469:        if (endch == 'c')
                   1470:                divsize = 0;
                   1471:        return (new);
                   1472: }
                   1473:
                   1474: /*
                   1475:  * -type c functions --
                   1476:  *
                   1477:  *     True if the type of the file is c, where c is b, c, d, p, or f for
                   1478:  *     block special file, character special file, directory, FIFO, or
                   1479:  *     regular file, respectively.
                   1480:  */
                   1481: int
1.30      deraadt  1482: f_type(PLAN *plan, FTSENT *entry)
1.1       deraadt  1483: {
                   1484:        return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data);
                   1485: }
                   1486:
                   1487: PLAN *
1.28      deraadt  1488: c_type(char *typestring, char ***ignored, int unused)
1.1       deraadt  1489: {
                   1490:        PLAN *new;
                   1491:        mode_t  mask;
                   1492:
                   1493:        ftsoptions &= ~FTS_NOSTAT;
                   1494:
                   1495:        switch (typestring[0]) {
                   1496:        case 'b':
                   1497:                mask = S_IFBLK;
                   1498:                break;
                   1499:        case 'c':
                   1500:                mask = S_IFCHR;
                   1501:                break;
                   1502:        case 'd':
                   1503:                mask = S_IFDIR;
                   1504:                break;
                   1505:        case 'f':
                   1506:                mask = S_IFREG;
                   1507:                break;
                   1508:        case 'l':
                   1509:                mask = S_IFLNK;
                   1510:                break;
                   1511:        case 'p':
                   1512:                mask = S_IFIFO;
                   1513:                break;
                   1514:        case 's':
                   1515:                mask = S_IFSOCK;
                   1516:                break;
                   1517:        default:
                   1518:                errx(1, "-type: %s: unknown type", typestring);
                   1519:        }
                   1520:
                   1521:        new = palloc(N_TYPE, f_type);
                   1522:        new->m_data = mask;
                   1523:        return (new);
                   1524: }
                   1525:
                   1526: /*
                   1527:  * -user uname functions --
                   1528:  *
                   1529:  *     True if the file belongs to the user uname.  If uname is numeric and
                   1530:  *     an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
                   1531:  *     return a valid user name, uname is taken as a user ID.
                   1532:  */
                   1533: int
1.30      deraadt  1534: f_user(PLAN *plan, FTSENT *entry)
1.1       deraadt  1535: {
                   1536:        return (entry->fts_statp->st_uid == plan->u_data);
                   1537: }
                   1538:
                   1539: PLAN *
1.28      deraadt  1540: c_user(char *username, char ***ignored, int unused)
1.1       deraadt  1541: {
                   1542:        PLAN *new;
                   1543:        uid_t uid;
                   1544:
                   1545:        ftsoptions &= ~FTS_NOSTAT;
                   1546:
1.46    ! millert  1547:        if (uid_from_user(username, &uid) == -1) {
1.44      deraadt  1548:                const char *errstr;
                   1549:
                   1550:                uid = strtonum(username, 0, UID_MAX, &errstr);
                   1551:                if (errstr)
1.1       deraadt  1552:                        errx(1, "-user: %s: no such user", username);
1.46    ! millert  1553:        }
1.1       deraadt  1554:
                   1555:        new = palloc(N_USER, f_user);
                   1556:        new->u_data = uid;
                   1557:        return (new);
                   1558: }
                   1559:
                   1560: /*
                   1561:  * -xdev functions --
                   1562:  *
                   1563:  *     Always true, causes find not to decend past directories that have a
                   1564:  *     different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
                   1565:  */
                   1566: PLAN *
1.28      deraadt  1567: c_xdev(char *ignore, char ***ignored, int unused)
1.1       deraadt  1568: {
                   1569:        ftsoptions |= FTS_XDEV;
                   1570:
                   1571:        return (palloc(N_XDEV, f_always_true));
                   1572: }
                   1573:
                   1574: /*
                   1575:  * ( expression ) functions --
                   1576:  *
                   1577:  *     True if expression is true.
                   1578:  */
                   1579: int
1.30      deraadt  1580: f_expr(PLAN *plan, FTSENT *entry)
1.1       deraadt  1581: {
1.21      mpech    1582:        PLAN *p;
                   1583:        int state;
1.1       deraadt  1584:
                   1585:        for (p = plan->p_data[0];
                   1586:            p && (state = (p->eval)(p, entry)); p = p->next);
                   1587:        return (state);
                   1588: }
                   1589:
                   1590: /*
                   1591:  * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers.  They are
                   1592:  * eliminated during phase 2 of find_formplan() --- the '(' node is converted
                   1593:  * to a N_EXPR node containing the expression and the ')' node is discarded.
                   1594:  */
                   1595: PLAN *
1.28      deraadt  1596: c_openparen(char *ignore, char ***ignored, int unused)
1.1       deraadt  1597: {
1.28      deraadt  1598:        return (palloc(N_OPENPAREN, (int (*)(PLAN *, FTSENT *))-1));
1.1       deraadt  1599: }
                   1600:
                   1601: PLAN *
1.28      deraadt  1602: c_closeparen(char *ignore, char ***ignored, int unused)
1.1       deraadt  1603: {
1.28      deraadt  1604:        return (palloc(N_CLOSEPAREN, (int (*)(PLAN *, FTSENT *))-1));
1.1       deraadt  1605: }
                   1606:
                   1607: /*
                   1608:  * ! expression functions --
                   1609:  *
                   1610:  *     Negation of a primary; the unary NOT operator.
                   1611:  */
                   1612: int
1.30      deraadt  1613: f_not(PLAN *plan, FTSENT *entry)
1.1       deraadt  1614: {
1.21      mpech    1615:        PLAN *p;
                   1616:        int state;
1.1       deraadt  1617:
                   1618:        for (p = plan->p_data[0];
                   1619:            p && (state = (p->eval)(p, entry)); p = p->next);
                   1620:        return (!state);
                   1621: }
                   1622:
                   1623: PLAN *
1.28      deraadt  1624: c_not(char *ignore, char ***ignored, int unused)
1.1       deraadt  1625: {
                   1626:        return (palloc(N_NOT, f_not));
                   1627: }
                   1628:
                   1629: /*
                   1630:  * expression -o expression functions --
                   1631:  *
                   1632:  *     Alternation of primaries; the OR operator.  The second expression is
                   1633:  * not evaluated if the first expression is true.
                   1634:  */
                   1635: int
1.30      deraadt  1636: f_or(PLAN *plan, FTSENT *entry)
1.1       deraadt  1637: {
1.21      mpech    1638:        PLAN *p;
                   1639:        int state;
1.1       deraadt  1640:
                   1641:        for (p = plan->p_data[0];
                   1642:            p && (state = (p->eval)(p, entry)); p = p->next);
                   1643:
                   1644:        if (state)
                   1645:                return (1);
                   1646:
                   1647:        for (p = plan->p_data[1];
                   1648:            p && (state = (p->eval)(p, entry)); p = p->next);
                   1649:        return (state);
                   1650: }
                   1651:
                   1652: PLAN *
1.28      deraadt  1653: c_or(char *ignore, char ***ignored, int unused)
1.1       deraadt  1654: {
                   1655:        return (palloc(N_OR, f_or));
                   1656: }
1.37      pascal   1657:
                   1658:
                   1659: /*
                   1660:  * plan_cleanup --
                   1661:  *     Check and see if the specified plan has any residual state,
                   1662:  *     and if so, clean it up as appropriate.
                   1663:  *
                   1664:  *     At the moment, only N_EXEC has state. Two kinds: 1)
                   1665:  *     lists of files to feed to subprocesses 2) State on exit
                   1666:  *     statusses of past subprocesses.
                   1667:  */
                   1668: /* ARGSUSED1 */
                   1669: int
                   1670: plan_cleanup(PLAN *plan, void *arg)
                   1671: {
                   1672:        if (plan->type==N_EXEC && plan->ep_narg)
                   1673:                run_f_exec(plan);
                   1674:
                   1675:        return plan->ep_rval;           /* Passed save exit-status up chain */
                   1676: }
                   1677:
1.1       deraadt  1678:
                   1679: static PLAN *
1.28      deraadt  1680: palloc(enum ntype t, int (*f)(PLAN *, FTSENT *))
1.1       deraadt  1681: {
                   1682:        PLAN *new;
                   1683:
1.17      millert  1684:        if ((new = calloc(1, sizeof(PLAN)))) {
1.1       deraadt  1685:                new->type = t;
                   1686:                new->eval = f;
                   1687:                return (new);
                   1688:        }
                   1689:        err(1, NULL);
                   1690:        /* NOTREACHED */
                   1691: }