Annotation of 43BSDReno/usr.bin/find/option.c, revision 1.1.1.1

1.1       root        1: /*-
                      2:  * Copyright (c) 1990 The Regents of the University of California.
                      3:  * All rights reserved.
                      4:  *
                      5:  * This code is derived from software contributed to Berkeley by
                      6:  * Cimarron D. Taylor of the University of California, Berkeley.
                      7:  *
                      8:  * Redistribution and use in source and binary forms are permitted provided
                      9:  * that: (1) source distributions retain this entire copyright notice and
                     10:  * comment, and (2) distributions including binaries display the following
                     11:  * acknowledgement:  ``This product includes software developed by the
                     12:  * University of California, Berkeley and its contributors'' in the
                     13:  * documentation or other materials provided with the distribution and in
                     14:  * all advertising materials mentioning features or use of this software.
                     15:  * Neither the name of the University nor the names of its contributors may
                     16:  * be used to endorse or promote products derived from this software without
                     17:  * specific prior written permission.
                     18:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
                     19:  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
                     20:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     21:  */
                     22: 
                     23: #ifndef lint
                     24: static char sccsid[] = "@(#)option.c   5.3 (Berkeley) 7/1/90";
                     25: #endif /* not lint */
                     26: 
                     27: #include <sys/types.h>
                     28: #include <sys/stat.h>
                     29: #include <fts.h>
                     30: #include <stdio.h>
                     31: #include "find.h"
                     32: 
                     33: typedef struct _option {
                     34:        char *name;             /* option name */
                     35:        int token;              /* token value */
                     36:        PLAN *(*create)();      /* create function */
                     37: #define        O_NONE          0x01    /* no call required */
                     38: #define        O_ZERO          0x02    /* pass: nothing */
                     39: #define        O_ARGV          0x04    /* pass: argv, increment argv */
                     40: #define        O_ARGVP         0x08    /* pass: *argv, T_OK || T_EXEC */
                     41: #define        O_MASK          0x0f    /* mask of op bits */
                     42: #define        O_OLD           0x10    /* deprecated syntax */
                     43: #define        O_NEW           0x20    /* new syntax */
                     44:        int flags;
                     45: } OPTION;
                     46: 
                     47: PLAN   *c_atime(), *c_ctime(), *c_depth(), *c_exec(), *c_follow(),
                     48:        *c_fstype(), *c_group(), *c_inum(), *c_links(), *c_ls(),
                     49:        *c_mtime(), *c_name(), *c_newer(), *c_nogroup(), *c_nouser(),
                     50:        *c_perm(), *c_print(), *c_prune(), *c_size(), *c_type(),
                     51:        *c_user(), *c_xdev(), *c_openparen(), *c_closeparen(), *c_not(),
                     52:        *c_or();
                     53: 
                     54: OPTION options[] = {
                     55:        "!",            T_NOT,          c_not,          O_ZERO,
                     56:        "(",            T_OPENPAREN,    c_openparen,    O_ZERO,
                     57:        ")",            T_CLOSEPAREN,   c_closeparen,   O_ZERO,
                     58:        "a",            T_AND,          NULL,           O_NONE|O_OLD,
                     59:        "and",          T_AND,          NULL,           O_NONE|O_NEW,
                     60:        "atime",        T_ATIME,        c_atime,        O_ARGV,
                     61:        "ctime",        T_CTIME,        c_ctime,        O_ARGV,
                     62:        "depth",        T_DEPTH,        c_depth,        O_ZERO|O_OLD,
                     63:        "exec",         T_EXEC,         c_exec,         O_ARGVP,
                     64:        "follow",       T_FOLLOW,       c_follow,       O_ZERO|O_OLD,
                     65:        "fstype",       T_FSTYPE,       c_fstype,       O_ARGV,
                     66:        "group",        T_GROUP,        c_group,        O_ARGV,
                     67:        "inum",         T_INUM,         c_inum,         O_ARGV,
                     68:        "links",        T_LINKS,        c_links,        O_ARGV,
                     69:        "ls",           T_LS,           c_ls,           O_ZERO,
                     70:        "mtime",        T_MTIME,        c_mtime,        O_ARGV,
                     71:        "name",         T_NAME,         c_name,         O_ARGV,
                     72:        "newer",        T_NEWER,        c_newer,        O_ARGV,
                     73:        "nogroup",      T_NOGROUP,      c_nogroup,      O_ZERO,
                     74:        "nouser",       T_NOUSER,       c_nouser,       O_ZERO,
                     75:        "o",            T_OR,           c_or,           O_ZERO|O_OLD,
                     76:        "ok",           T_OK,           c_exec,         O_ARGVP,
                     77:        "or",           T_OR,           c_or,           O_ZERO|O_NEW,
                     78:        "perm",         T_PERM,         c_perm,         O_ARGV,
                     79:        "print",        T_PRINT,        c_print,        O_ZERO,
                     80:        "prune",        T_PRUNE,        c_prune,        O_ZERO,
                     81:        "size",         T_SIZE,         c_size,         O_ARGV,
                     82:        "type",         T_TYPE,         c_type,         O_ARGV,
                     83:        "user",         T_USER,         c_user,         O_ARGV,
                     84:        "xdev",         T_XDEV,         c_xdev,         O_ZERO|O_OLD,
                     85:        { NULL },
                     86: };
                     87: 
                     88: /*
                     89:  * find_create --
                     90:  *     create a node corresponding to a command line argument.
                     91:  *
                     92:  * TODO:
                     93:  *     add create/process function pointers to node, so we can skip
                     94:  *     this switch stuff.
                     95:  */
                     96: PLAN *
                     97: find_create(argvp)
                     98:        char ***argvp;
                     99: {
                    100:        extern int deprecated;
                    101:        register OPTION *p;
                    102:        OPTION tmp;
                    103:        PLAN *new;
                    104:        char **argv;
                    105:        int typecompare();
                    106: 
                    107:        argv = *argvp;
                    108:        tmp.name = *argv++;
                    109: 
                    110:        /* strip off any leading dash */
                    111:        if (*tmp.name == '-')
                    112:                ++tmp.name;
                    113: 
                    114:        p = (OPTION *)bsearch(&tmp, options, sizeof(options)/sizeof(OPTION),
                    115:            sizeof(OPTION), typecompare);
                    116:        if (!p || deprecated && p->flags&O_NEW ||
                    117:            !deprecated && p->flags&O_OLD) {
                    118:                (void)fprintf(stderr, "find: unknown option %s.\n", *--argv);
                    119:                exit(1);
                    120:        }
                    121:        if (p->flags & (O_ARGV|O_ARGVP) && !*argv) {
                    122:                (void)fprintf(stderr,
                    123:                    "find: %s requires additional arguments.\n", *--argv);
                    124:                exit(1);
                    125:        }
                    126: 
                    127:        switch(p->flags&O_MASK) {
                    128:        case O_NONE:
                    129:                new = NULL;
                    130:                break;
                    131:        case O_ZERO:
                    132:                new = (p->create)();
                    133:                break;
                    134:        case O_ARGV:
                    135:                new = (p->create)(*argv++);
                    136:                break;
                    137:        case O_ARGVP:
                    138:                new = (p->create)(&argv, p->token == T_OK);
                    139:                break;
                    140:        }
                    141:        *argvp = argv;
                    142:        return(new);
                    143: }
                    144: 
                    145: typecompare(a, b)
                    146:        OPTION *a, *b;
                    147: {
                    148:        return(strcmp(a->name, b->name));
                    149: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.