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