option.c revision 91400
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/*
39static char sccsid[] = "@(#)option.c	8.2 (Berkeley) 4/16/94";
40*/
41static const char rcsid[] =
42  "$FreeBSD: head/usr.bin/find/option.c 91400 2002-02-27 17:57:00Z dwmalone $";
43#endif /* not lint */
44
45#include <sys/types.h>
46#include <sys/stat.h>
47
48#include <err.h>
49#include <fts.h>
50#include <regex.h>
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
54
55#include "find.h"
56
57int typecompare __P((const void *, const void *));
58
59/* NB: the following table must be sorted lexically. */
60static OPTION const options[] = {
61	{ "!",		c_simple,	f_not,		0 },
62	{ "(",		c_simple,	f_openparen,	0 },
63	{ ")",		c_simple,	f_closeparen,	0 },
64	{ "-a",		c_and,		NULL,		0 },
65	{ "-amin",	c_Xmin,		f_Xmin,		F_TIME_A },
66	{ "-and",	c_and,		NULL,		0 },
67	{ "-anewer",	c_newer,	f_newer,	F_TIME_A },
68	{ "-atime",	c_Xtime,	f_Xtime,	F_TIME_A },
69	{ "-cmin",	c_Xmin,		f_Xmin,		F_TIME_C },
70	{ "-cnewer",	c_newer,	f_newer,	F_TIME_C },
71	{ "-ctime",	c_Xtime,	f_Xtime,	F_TIME_C },
72	{ "-delete",	c_delete,	f_delete,	0 },
73	{ "-depth",	c_depth,	f_always_true,	0 },
74	{ "-empty",	c_empty,	f_empty,	0 },
75	{ "-exec",	c_exec,		f_exec,		0 },
76	{ "-execdir",	c_exec,		f_exec,		F_EXECDIR },
77	{ "-flags",	c_flags,	f_flags,	0 },
78	{ "-follow",	c_follow,	f_always_true,	0 },
79/*
80 * NetBSD doesn't provide a getvfsbyname(), so this option
81 * is not available if using a NetBSD kernel.
82 */
83#if !defined(__NetBSD__)
84	{ "-fstype",	c_fstype,	f_fstype,	0 },
85#endif
86	{ "-group",	c_group,	f_group,	0 },
87	{ "-iname",	c_name,		f_name,		F_IGNCASE },
88	{ "-inum",	c_inum,		f_inum,		0 },
89	{ "-ipath",	c_name,		f_path,		F_IGNCASE },
90	{ "-iregex",	c_regex,	f_regex,	F_IGNCASE },
91	{ "-links",	c_links,	f_links,	0 },
92	{ "-ls",	c_ls,		f_ls,		0 },
93	{ "-maxdepth",	c_mXXdepth,	f_always_true,	F_MAXDEPTH },
94	{ "-mindepth",	c_mXXdepth,	f_always_true,	0 },
95	{ "-mmin",	c_Xmin,		f_Xmin,		0 },
96	{ "-mnewer",	c_newer,	f_newer,	0 },
97	{ "-mtime",	c_Xtime,	f_Xtime,	0 },
98	{ "-name",	c_name,		f_name,		0 },
99	{ "-newer",	c_newer,	f_newer,	0 },
100	{ "-neweraa",	c_newer,	f_newer,	F_TIME_A | F_TIME2_A },
101	{ "-newerac",	c_newer,	f_newer,	F_TIME_A | F_TIME2_C },
102	{ "-neweram",	c_newer,	f_newer,	F_TIME_A },
103	{ "-newerat",	c_newer,	f_newer,	F_TIME_A | F_TIME2_T },
104	{ "-newerca",	c_newer,	f_newer,	F_TIME_C | F_TIME2_A },
105	{ "-newercc",	c_newer,	f_newer,	F_TIME_C | F_TIME2_C },
106	{ "-newercm",	c_newer,	f_newer,	F_TIME_C },
107	{ "-newerct",	c_newer,	f_newer,	F_TIME_C | F_TIME2_T },
108	{ "-newerma",	c_newer,	f_newer,	F_TIME2_A },
109	{ "-newermc",	c_newer,	f_newer,	F_TIME2_C },
110	{ "-newermm",	c_newer,	f_newer,	0 },
111	{ "-newermt",	c_newer,	f_newer,	F_TIME2_T },
112	{ "-nogroup",	c_nogroup,	f_nogroup,	0 },
113	{ "-nouser",	c_nouser,	f_nouser,	0 },
114	{ "-o",		c_simple,	f_or,		0 },
115	{ "-ok",	c_exec,		f_exec,		F_NEEDOK },
116	{ "-okdir",	c_exec,		f_exec,		F_NEEDOK | F_EXECDIR },
117	{ "-or",	c_simple,	f_or,		0 },
118	{ "-path", 	c_name,		f_path,		0 },
119	{ "-perm",	c_perm,		f_perm,		0 },
120	{ "-print",	c_print,	f_print,	0 },
121	{ "-print0",	c_print,	f_print0,	0 },
122	{ "-prune",	c_simple,	f_prune,	0 },
123	{ "-regex",	c_regex,	f_regex,	0 },
124	{ "-size",	c_size,		f_size,		0 },
125	{ "-type",	c_type,		f_type,		0 },
126	{ "-user",	c_user,		f_user,		0 },
127	{ "-xdev",	c_xdev,		f_always_true,	0 },
128};
129
130/*
131 * find_create --
132 *	create a node corresponding to a command line argument.
133 *
134 * TODO:
135 *	add create/process function pointers to node, so we can skip
136 *	this switch stuff.
137 */
138PLAN *
139find_create(argvp)
140	char ***argvp;
141{
142	OPTION *p;
143	PLAN *new;
144	char **argv;
145
146	argv = *argvp;
147
148	if ((p = lookup_option(*argv)) == NULL)
149		errx(1, "%s: unknown option", *argv);
150	++argv;
151
152	new = (p->create)(p, &argv);
153	*argvp = argv;
154	return (new);
155}
156
157OPTION *
158lookup_option(name)
159	const char *name;
160{
161	OPTION tmp;
162
163	tmp.name = name;
164	return ((OPTION *)bsearch(&tmp, options,
165	    sizeof(options)/sizeof(OPTION), sizeof(OPTION), typecompare));
166}
167
168int
169typecompare(a, b)
170	const void *a, *b;
171{
172	return (strcmp(((const OPTION *)a)->name, ((const OPTION *)b)->name));
173}
174