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

Annotation of src/usr.bin/find/option.c, Revision 1.3

1.3     ! tholo       1: /*     $OpenBSD: option.c,v 1.2 1996/06/26 05:33:13 deraadt Exp $      */
1.2       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.
                     18:  * 3. All advertising materials mentioning features or use of this software
                     19:  *    must display the following acknowledgement:
                     20:  *     This product includes software developed by the University of
                     21:  *     California, Berkeley and its contributors.
                     22:  * 4. Neither the name of the University nor the names of its contributors
                     23:  *    may be used to endorse or promote products derived from this software
                     24:  *    without specific prior written permission.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     27:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     28:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     29:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     30:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     31:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     32:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     33:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     34:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     35:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     36:  * SUCH DAMAGE.
                     37:  */
                     38:
                     39: #ifndef lint
                     40: /*static char sccsid[] = "from: @(#)option.c   8.1 (Berkeley) 6/6/93";*/
1.3     ! tholo      41: static char rcsid[] = "$OpenBSD: option.c,v 1.2 1996/06/26 05:33:13 deraadt Exp $";
1.1       deraadt    42: #endif /* not lint */
                     43:
                     44: #include <sys/types.h>
                     45: #include <sys/stat.h>
                     46:
                     47: #include <err.h>
                     48: #include <fts.h>
                     49: #include <stdio.h>
                     50: #include <stdlib.h>
                     51: #include <string.h>
                     52:
                     53: #include "find.h"
                     54:
                     55: /* NB: the following table must be sorted lexically. */
                     56: static OPTION options[] = {
                     57:        { "!",          N_NOT,          c_not,          O_ZERO },
                     58:        { "(",          N_OPENPAREN,    c_openparen,    O_ZERO },
                     59:        { ")",          N_CLOSEPAREN,   c_closeparen,   O_ZERO },
                     60:        { "-a",         N_AND,          NULL,           O_NONE },
                     61:        { "-and",       N_AND,          NULL,           O_NONE },
                     62:        { "-atime",     N_ATIME,        c_atime,        O_ARGV },
                     63:        { "-ctime",     N_CTIME,        c_ctime,        O_ARGV },
                     64:        { "-depth",     N_DEPTH,        c_depth,        O_ZERO },
                     65:        { "-exec",      N_EXEC,         c_exec,         O_ARGVP },
                     66:        { "-follow",    N_FOLLOW,       c_follow,       O_ZERO },
                     67:        { "-fstype",    N_FSTYPE,       c_fstype,       O_ARGV },
                     68:        { "-group",     N_GROUP,        c_group,        O_ARGV },
                     69:        { "-inum",      N_INUM,         c_inum,         O_ARGV },
                     70:        { "-links",     N_LINKS,        c_links,        O_ARGV },
                     71:        { "-ls",        N_LS,           c_ls,           O_ZERO },
1.3     ! tholo      72:        { "-maxdepth",  N_MDEPTH,       c_mdepth,       O_ARGV },
1.1       deraadt    73:        { "-mtime",     N_MTIME,        c_mtime,        O_ARGV },
                     74:        { "-name",      N_NAME,         c_name,         O_ARGV },
                     75:        { "-newer",     N_NEWER,        c_newer,        O_ARGV },
                     76:        { "-nogroup",   N_NOGROUP,      c_nogroup,      O_ZERO },
                     77:        { "-nouser",    N_NOUSER,       c_nouser,       O_ZERO },
                     78:        { "-o",         N_OR,           c_or,           O_ZERO },
                     79:        { "-ok",        N_OK,           c_exec,         O_ARGVP },
                     80:        { "-or",        N_OR,           c_or,           O_ZERO },
                     81:        { "-path",      N_PATH,         c_path,         O_ARGV },
                     82:        { "-perm",      N_PERM,         c_perm,         O_ARGV },
                     83:        { "-print",     N_PRINT,        c_print,        O_ZERO },
                     84:        { "-print0",    N_PRINT0,       c_print0,       O_ZERO },
                     85:        { "-prune",     N_PRUNE,        c_prune,        O_ZERO },
                     86:        { "-size",      N_SIZE,         c_size,         O_ARGV },
                     87:        { "-type",      N_TYPE,         c_type,         O_ARGV },
                     88:        { "-user",      N_USER,         c_user,         O_ARGV },
                     89:        { "-xdev",      N_XDEV,         c_xdev,         O_ZERO },
                     90: };
                     91:
                     92: /*
                     93:  * find_create --
                     94:  *     create a node corresponding to a command line argument.
                     95:  *
                     96:  * TODO:
                     97:  *     add create/process function pointers to node, so we can skip
                     98:  *     this switch stuff.
                     99:  */
                    100: PLAN *
                    101: find_create(argvp)
                    102:        char ***argvp;
                    103: {
                    104:        register OPTION *p;
                    105:        PLAN *new;
                    106:        char **argv;
                    107:
                    108:        argv = *argvp;
                    109:
                    110:        if ((p = option(*argv)) == NULL)
                    111:                errx(1, "%s: unknown option", *argv);
                    112:        ++argv;
                    113:        if (p->flags & (O_ARGV|O_ARGVP) && !*argv)
                    114:                errx(1, "%s: requires additional arguments", *--argv);
                    115:
                    116:        switch(p->flags) {
                    117:        case O_NONE:
                    118:                new = NULL;
                    119:                break;
                    120:        case O_ZERO:
                    121:                new = (p->create)();
                    122:                break;
                    123:        case O_ARGV:
                    124:                new = (p->create)(*argv++);
                    125:                break;
                    126:        case O_ARGVP:
                    127:                new = (p->create)(&argv, p->token == N_OK);
                    128:                break;
                    129:        default:
                    130:                abort();
                    131:        }
                    132:        *argvp = argv;
                    133:        return (new);
                    134: }
                    135:
                    136: OPTION *
                    137: option(name)
                    138:        char *name;
                    139: {
                    140:        OPTION tmp;
                    141:        int typecompare __P((const void *, const void *));
                    142:
                    143:        tmp.name = name;
                    144:        return ((OPTION *)bsearch(&tmp, options,
                    145:            sizeof(options)/sizeof(OPTION), sizeof(OPTION), typecompare));
                    146: }
                    147:
                    148: int
                    149: typecompare(a, b)
                    150:        const void *a, *b;
                    151: {
                    152:        return (strcmp(((OPTION *)a)->name, ((OPTION *)b)->name));
                    153: }