option.c revision 200420
11592Srgrimes/*-
21592Srgrimes * Copyright (c) 1990, 1993, 1994
313578Spst *	The Regents of the University of California.  All rights reserved.
413247Sgraichen *
513247Sgraichen * This code is derived from software contributed to Berkeley by
61592Srgrimes * Cimarron D. Taylor of the University of California, Berkeley.
79962Smarkm *
89962Smarkm * Redistribution and use in source and binary forms, with or without
911068Sache * modification, are permitted provided that the following conditions
1014078Sjoerg * are met:
1114078Sjoerg * 1. Redistributions of source code must retain the above copyright
1214078Sjoerg *    notice, this list of conditions and the following disclaimer.
1314078Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
1411068Sache *    notice, this list of conditions and the following disclaimer in the
159962Smarkm *    documentation and/or other materials provided with the distribution.
169962Smarkm * 3. All advertising materials mentioning features or use of this software
171847Swollman *    must display the following acknowledgement:
181847Swollman *	This product includes software developed by the University of
191592Srgrimes *	California, Berkeley and its contributors.
201592Srgrimes * 4. Neither the name of the University nor the names of its contributors
211605Srgrimes *    may be used to endorse or promote products derived from this software
221605Srgrimes *    without specific prior written permission.
231592Srgrimes *
241592Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251592Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261592Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271592Srgrimes * 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/*
39static char sccsid[] = "@(#)option.c	8.2 (Berkeley) 4/16/94";
40*/
41#endif /* not lint */
42
43#include <sys/cdefs.h>
44__FBSDID("$FreeBSD: head/usr.bin/find/option.c 200420 2009-12-11 23:35:38Z delphij $");
45
46#include <sys/types.h>
47#include <sys/stat.h>
48
49#include <err.h>
50#include <fts.h>
51#include <regex.h>
52#include <stdlib.h>
53#include <string.h>
54
55#include "find.h"
56
57int typecompare(const void *, const void *);
58
59/* NB: the following table must be sorted lexically. */
60/* Options listed with C++ comments are in gnu find, but not our find */
61static OPTION const options[] = {
62	{ "!",		c_simple,	f_not,		0 },
63	{ "(",		c_simple,	f_openparen,	0 },
64	{ ")",		c_simple,	f_closeparen,	0 },
65	{ "-Bmin",	c_Xmin,		f_Xmin,		F_TIME_B },
66	{ "-Bnewer",	c_newer,	f_newer,	F_TIME_B },
67	{ "-Btime",	c_Xtime,	f_Xtime,	F_TIME_B },
68	{ "-a",		c_and,		NULL,		0 },
69	{ "-acl",	c_acl,		f_acl,		0 },
70	{ "-amin",	c_Xmin,		f_Xmin,		F_TIME_A },
71	{ "-and",	c_and,		NULL,		0 },
72	{ "-anewer",	c_newer,	f_newer,	F_TIME_A },
73	{ "-atime",	c_Xtime,	f_Xtime,	F_TIME_A },
74	{ "-cmin",	c_Xmin,		f_Xmin,		F_TIME_C },
75	{ "-cnewer",	c_newer,	f_newer,	F_TIME_C },
76	{ "-ctime",	c_Xtime,	f_Xtime,	F_TIME_C },
77	{ "-d",		c_depth,	f_depth,	0 },
78// -daystart
79	{ "-delete",	c_delete,	f_delete,	0 },
80	{ "-depth",	c_depth,	f_depth,	0 },
81	{ "-empty",	c_empty,	f_empty,	0 },
82	{ "-exec",	c_exec,		f_exec,		0 },
83	{ "-execdir",	c_exec,		f_exec,		F_EXECDIR },
84	{ "-false",	c_simple,	f_false,	0 },
85	{ "-flags",	c_flags,	f_flags,	0 },
86// -fls
87	{ "-follow",	c_follow,	f_always_true,	0 },
88// -fprint
89// -fprint0
90// -fprintf
91	{ "-fstype",	c_fstype,	f_fstype,	0 },
92	{ "-gid",	c_group,	f_group,	0 },
93	{ "-group",	c_group,	f_group,	0 },
94	{ "-ignore_readdir_race",c_simple, f_always_true,0 },
95	{ "-ilname",	c_name,		f_name,		F_LINK | F_IGNCASE },
96	{ "-iname",	c_name,		f_name,		F_IGNCASE },
97	{ "-inum",	c_inum,		f_inum,		0 },
98	{ "-ipath",	c_name,		f_path,		F_IGNCASE },
99	{ "-iregex",	c_regex,	f_regex,	F_IGNCASE },
100	{ "-iwholename",c_name,		f_path,		F_IGNCASE },
101	{ "-links",	c_links,	f_links,	0 },
102	{ "-lname",	c_name,		f_name,		F_LINK },
103	{ "-ls",	c_ls,		f_ls,		0 },
104	{ "-maxdepth",	c_mXXdepth,	f_always_true,	F_MAXDEPTH },
105	{ "-mindepth",	c_mXXdepth,	f_always_true,	0 },
106	{ "-mmin",	c_Xmin,		f_Xmin,		0 },
107	{ "-mnewer",	c_newer,	f_newer,	0 },
108	{ "-mount",	c_xdev,		f_always_true,	0 },
109	{ "-mtime",	c_Xtime,	f_Xtime,	0 },
110	{ "-name",	c_name,		f_name,		0 },
111	{ "-newer",	c_newer,	f_newer,	0 },
112	{ "-newerBB",	c_newer,	f_newer,	F_TIME_B | F_TIME2_B },
113	{ "-newerBa",	c_newer,	f_newer,	F_TIME_B | F_TIME2_A },
114	{ "-newerBc",	c_newer,	f_newer,	F_TIME_B | F_TIME2_C },
115	{ "-newerBm",	c_newer,	f_newer,	F_TIME_B },
116	{ "-newerBt",	c_newer,	f_newer,	F_TIME_B | F_TIME2_T },
117	{ "-neweraB",	c_newer,	f_newer,	F_TIME_A | F_TIME2_B },
118	{ "-neweraa",	c_newer,	f_newer,	F_TIME_A | F_TIME2_A },
119	{ "-newerac",	c_newer,	f_newer,	F_TIME_A | F_TIME2_C },
120	{ "-neweram",	c_newer,	f_newer,	F_TIME_A },
121	{ "-newerat",	c_newer,	f_newer,	F_TIME_A | F_TIME2_T },
122	{ "-newercB",	c_newer,	f_newer,	F_TIME_C | F_TIME2_B },
123	{ "-newerca",	c_newer,	f_newer,	F_TIME_C | F_TIME2_A },
124	{ "-newercc",	c_newer,	f_newer,	F_TIME_C | F_TIME2_C },
125	{ "-newercm",	c_newer,	f_newer,	F_TIME_C },
126	{ "-newerct",	c_newer,	f_newer,	F_TIME_C | F_TIME2_T },
127	{ "-newermB",	c_newer,	f_newer,	F_TIME2_B },
128	{ "-newerma",	c_newer,	f_newer,	F_TIME2_A },
129	{ "-newermc",	c_newer,	f_newer,	F_TIME2_C },
130	{ "-newermm",	c_newer,	f_newer,	0 },
131	{ "-newermt",	c_newer,	f_newer,	F_TIME2_T },
132	{ "-nogroup",	c_nogroup,	f_nogroup,	0 },
133	{ "-noignore_readdir_race",c_simple, f_always_true,0 },
134	{ "-noleaf",	c_simple,	f_always_true,	0 },
135	{ "-not",	c_simple,	f_not,		0 },
136	{ "-nouser",	c_nouser,	f_nouser,	0 },
137	{ "-o",		c_simple,	f_or,		0 },
138	{ "-ok",	c_exec,		f_exec,		F_NEEDOK },
139	{ "-okdir",	c_exec,		f_exec,		F_NEEDOK | F_EXECDIR },
140	{ "-or",	c_simple,	f_or,		0 },
141	{ "-path", 	c_name,		f_path,		0 },
142	{ "-perm",	c_perm,		f_perm,		0 },
143	{ "-print",	c_print,	f_print,	0 },
144	{ "-print0",	c_print,	f_print0,	0 },
145// -printf
146	{ "-prune",	c_simple,	f_prune,	0 },
147	{ "-quit",	c_simple,	f_quit,		0 },
148	{ "-regex",	c_regex,	f_regex,	0 },
149	{ "-samefile",	c_samefile,	f_inum,		0 },
150	{ "-size",	c_size,		f_size,		0 },
151	{ "-true",	c_simple,	f_always_true,	0 },
152	{ "-type",	c_type,		f_type,		0 },
153	{ "-uid",	c_user,		f_user,		0 },
154	{ "-user",	c_user,		f_user,		0 },
155	{ "-wholename",	c_name,		f_path,		0 },
156	{ "-xdev",	c_xdev,		f_always_true,	0 },
157// -xtype
158};
159
160/*
161 * find_create --
162 *	create a node corresponding to a command line argument.
163 *
164 * TODO:
165 *	add create/process function pointers to node, so we can skip
166 *	this switch stuff.
167 */
168PLAN *
169find_create(char ***argvp)
170{
171	OPTION *p;
172	PLAN *new;
173	char **argv;
174
175	argv = *argvp;
176
177	if ((p = lookup_option(*argv)) == NULL)
178		errx(1, "%s: unknown option", *argv);
179	++argv;
180
181	new = (p->create)(p, &argv);
182	*argvp = argv;
183	return (new);
184}
185
186OPTION *
187lookup_option(const char *name)
188{
189	OPTION tmp;
190
191	tmp.name = name;
192	return ((OPTION *)bsearch(&tmp, options,
193	    sizeof(options)/sizeof(OPTION), sizeof(OPTION), typecompare));
194}
195
196int
197typecompare(const void *a, const void *b)
198{
199	return (strcmp(((const OPTION *)a)->name, ((const OPTION *)b)->name));
200}
201