xref: /openbsd-src/usr.bin/find/option.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: option.c,v 1.12 2000/07/08 16:09:34 millert Exp $	*/
2 
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";*/
41 static char rcsid[] = "$OpenBSD: option.c,v 1.12 2000/07/08 16:09:34 millert Exp $";
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 	{ "-amin",	N_AMIN,		c_amin,		O_ARGV },
62 	{ "-and",	N_AND,		NULL,		O_NONE },
63 	{ "-anewer",	N_ANEWER,	c_anewer,	O_ARGV },
64 	{ "-atime",	N_ATIME,	c_atime,	O_ARGV },
65 	{ "-cmin",	N_CMIN,		c_cmin,		O_ARGV },
66 	{ "-cnewer",	N_CNEWER,	c_cnewer,	O_ARGV },
67 	{ "-ctime",	N_CTIME,	c_ctime,	O_ARGV },
68 	{ "-depth",	N_DEPTH,	c_depth,	O_ZERO },
69 	{ "-empty",	N_EMPTY,	c_empty,	O_ZERO },
70 	{ "-exec",	N_EXEC,		c_exec,		O_ARGVP },
71 	{ "-execdir",	N_EXECDIR,	c_execdir,	O_ARGVP },
72 	{ "-flags",	N_FLAGS,	c_flags,	O_ARGV },
73 	{ "-follow",	N_FOLLOW,	c_follow,	O_ZERO },
74 	{ "-fstype",	N_FSTYPE,	c_fstype,	O_ARGV },
75 	{ "-group",	N_GROUP,	c_group,	O_ARGV },
76 	{ "-iname",	N_INAME,	c_iname,	O_ARGV },
77 	{ "-inum",	N_INUM,		c_inum,		O_ARGV },
78 	{ "-links",	N_LINKS,	c_links,	O_ARGV },
79 	{ "-ls",	N_LS,		c_ls,		O_ZERO },
80 	{ "-maxdepth",	N_MAXDEPTH,	c_maxdepth,	O_ARGV },
81 	{ "-mindepth",	N_MINDEPTH,	c_mindepth,	O_ARGV },
82 	{ "-mmin",	N_MMIN,		c_mmin,		O_ARGV },
83 	{ "-mount",	N_XDEV,		c_xdev,		O_ZERO },
84 	{ "-mtime",	N_MTIME,	c_mtime,	O_ARGV },
85 	{ "-name",	N_NAME,		c_name,		O_ARGV },
86 	{ "-newer",	N_NEWER,	c_newer,	O_ARGV },
87 	{ "-nogroup",	N_NOGROUP,	c_nogroup,	O_ZERO },
88 	{ "-nouser",	N_NOUSER,	c_nouser,	O_ZERO },
89 	{ "-o",		N_OR,		c_or,		O_ZERO },
90 	{ "-ok",	N_OK,		c_exec,		O_ARGVP },
91 	{ "-or",	N_OR,		c_or,		O_ZERO },
92 	{ "-path", 	N_PATH,		c_path,		O_ARGV },
93 	{ "-perm",	N_PERM,		c_perm,		O_ARGV },
94 	{ "-print",	N_PRINT,	c_print,	O_ZERO },
95 	{ "-print0",	N_PRINT0,	c_print0,	O_ZERO },
96 	{ "-prune",	N_PRUNE,	c_prune,	O_ZERO },
97 	{ "-size",	N_SIZE,		c_size,		O_ARGV },
98 	{ "-type",	N_TYPE,		c_type,		O_ARGV },
99 	{ "-user",	N_USER,		c_user,		O_ARGV },
100 	{ "-xdev",	N_XDEV,		c_xdev,		O_ZERO },
101 };
102 
103 /*
104  * find_create --
105  *	create a node corresponding to a command line argument.
106  *
107  * TODO:
108  *	add create/process function pointers to node, so we can skip
109  *	this switch stuff.
110  */
111 PLAN *
112 find_create(argvp)
113 	char ***argvp;
114 {
115 	register OPTION *p;
116 	PLAN *new;
117 	char **argv;
118 
119 	argv = *argvp;
120 
121 	if ((p = option(*argv)) == NULL)
122 		errx(1, "%s: unknown option", *argv);
123 	++argv;
124 	if (p->flags & (O_ARGV|O_ARGVP) && !*argv)
125 		errx(1, "%s: requires additional arguments", *--argv);
126 
127 	switch(p->flags) {
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 == N_OK);
139 		break;
140 	default:
141 		abort();
142 	}
143 	*argvp = argv;
144 	return (new);
145 }
146 
147 OPTION *
148 option(name)
149 	char *name;
150 {
151 	OPTION tmp;
152 	int typecompare __P((const void *, const void *));
153 
154 	tmp.name = name;
155 	return ((OPTION *)bsearch(&tmp, options,
156 	    sizeof(options)/sizeof(OPTION), sizeof(OPTION), typecompare));
157 }
158 
159 int
160 typecompare(a, b)
161 	const void *a, *b;
162 {
163 	return (strcmp(((OPTION *)a)->name, ((OPTION *)b)->name));
164 }
165