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