xref: /freebsd/usr.bin/find/option.c (revision 315ee00f)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1990, 1993, 1994
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. Neither the name of the University nor the names of its contributors
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 /*
36 static char sccsid[] = "@(#)option.c	8.2 (Berkeley) 4/16/94";
37 */
38 
39 #include <sys/cdefs.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 
43 #include <err.h>
44 #include <fts.h>
45 #include <regex.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 
50 #include "find.h"
51 
52 static int typecompare(const void *, const void *);
53 
54 /* NB: the following table must be sorted lexically. */
55 /* Options listed with C++ comments are in gnu find, but not our find */
56 static OPTION const options[] = {
57 	{ "!",		c_simple,	f_not,		0 },
58 	{ "(",		c_simple,	f_openparen,	0 },
59 	{ ")",		c_simple,	f_closeparen,	0 },
60 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
61 	{ "-Bmin",	c_Xmin,		f_Xmin,		F_TIME_B },
62 	{ "-Bnewer",	c_newer,	f_newer,	F_TIME_B },
63 	{ "-Btime",	c_Xtime,	f_Xtime,	F_TIME_B },
64 #endif
65 	{ "-a",		c_and,		NULL,		0 },
66 #ifdef ACL_TYPE_NFS4
67 	{ "-acl",	c_acl,		f_acl,		0 },
68 #endif
69 	{ "-amin",	c_Xmin,		f_Xmin,		F_TIME_A },
70 	{ "-and",	c_and,		NULL,		0 },
71 	{ "-anewer",	c_newer,	f_newer,	F_TIME_A },
72 	{ "-atime",	c_Xtime,	f_Xtime,	F_TIME_A },
73 	{ "-cmin",	c_Xmin,		f_Xmin,		F_TIME_C },
74 	{ "-cnewer",	c_newer,	f_newer,	F_TIME_C },
75 	{ "-ctime",	c_Xtime,	f_Xtime,	F_TIME_C },
76 	{ "-d",		c_depth,	f_depth,	0 },
77 // -daystart
78 	{ "-delete",	c_delete,	f_delete,	0 },
79 	{ "-depth",	c_depth,	f_depth,	0 },
80 	{ "-empty",	c_empty,	f_empty,	0 },
81 	{ "-exec",	c_exec,		f_exec,		0 },
82 	{ "-execdir",	c_exec,		f_exec,		F_EXECDIR },
83 	{ "-false",	c_simple,	f_false,	0 },
84 #if HAVE_STRUCT_STAT_ST_FLAGS
85 	{ "-flags",	c_flags,	f_flags,	0 },
86 #endif
87 // -fls
88 	{ "-follow",	c_follow,	f_always_true,	0 },
89 // -fprint
90 // -fprint0
91 // -fprintf
92 #if HAVE_STRUCT_STATFS_F_FSTYPENAME
93 	{ "-fstype",	c_fstype,	f_fstype,	0 },
94 #endif
95 	{ "-gid",	c_group,	f_group,	0 },
96 	{ "-group",	c_group,	f_group,	0 },
97 	{ "-ignore_readdir_race",c_ignore_readdir_race, f_always_true,0 },
98 	{ "-ilname",	c_name,		f_name,		F_LINK | F_IGNCASE },
99 	{ "-iname",	c_name,		f_name,		F_IGNCASE },
100 	{ "-inum",	c_inum,		f_inum,		0 },
101 	{ "-ipath",	c_name,		f_path,		F_IGNCASE },
102 	{ "-iregex",	c_regex,	f_regex,	F_IGNCASE },
103 	{ "-iwholename",c_name,		f_path,		F_IGNCASE },
104 	{ "-links",	c_links,	f_links,	0 },
105 	{ "-lname",	c_name,		f_name,		F_LINK },
106 	{ "-ls",	c_ls,		f_ls,		0 },
107 	{ "-maxdepth",	c_mXXdepth,	f_always_true,	F_MAXDEPTH },
108 	{ "-mindepth",	c_mXXdepth,	f_always_true,	0 },
109 	{ "-mmin",	c_Xmin,		f_Xmin,		0 },
110 	{ "-mnewer",	c_newer,	f_newer,	0 },
111 	{ "-mount",	c_xdev,		f_always_true,	0 },
112 	{ "-mtime",	c_Xtime,	f_Xtime,	0 },
113 	{ "-name",	c_name,		f_name,		0 },
114 	{ "-newer",	c_newer,	f_newer,	0 },
115 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
116 	{ "-newerBB",	c_newer,	f_newer,	F_TIME_B | F_TIME2_B },
117 	{ "-newerBa",	c_newer,	f_newer,	F_TIME_B | F_TIME2_A },
118 	{ "-newerBc",	c_newer,	f_newer,	F_TIME_B | F_TIME2_C },
119 	{ "-newerBm",	c_newer,	f_newer,	F_TIME_B },
120 	{ "-newerBt",	c_newer,	f_newer,	F_TIME_B | F_TIME2_T },
121 	{ "-neweraB",	c_newer,	f_newer,	F_TIME_A | F_TIME2_B },
122 #endif
123 	{ "-neweraa",	c_newer,	f_newer,	F_TIME_A | F_TIME2_A },
124 	{ "-newerac",	c_newer,	f_newer,	F_TIME_A | F_TIME2_C },
125 	{ "-neweram",	c_newer,	f_newer,	F_TIME_A },
126 	{ "-newerat",	c_newer,	f_newer,	F_TIME_A | F_TIME2_T },
127 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
128 	{ "-newercB",	c_newer,	f_newer,	F_TIME_C | F_TIME2_B },
129 #endif
130 	{ "-newerca",	c_newer,	f_newer,	F_TIME_C | F_TIME2_A },
131 	{ "-newercc",	c_newer,	f_newer,	F_TIME_C | F_TIME2_C },
132 	{ "-newercm",	c_newer,	f_newer,	F_TIME_C },
133 	{ "-newerct",	c_newer,	f_newer,	F_TIME_C | F_TIME2_T },
134 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
135 	{ "-newermB",	c_newer,	f_newer,	F_TIME2_B },
136 #endif
137 	{ "-newerma",	c_newer,	f_newer,	F_TIME2_A },
138 	{ "-newermc",	c_newer,	f_newer,	F_TIME2_C },
139 	{ "-newermm",	c_newer,	f_newer,	0 },
140 	{ "-newermt",	c_newer,	f_newer,	F_TIME2_T },
141 	{ "-nogroup",	c_nogroup,	f_nogroup,	0 },
142 	{ "-noignore_readdir_race",c_ignore_readdir_race, f_always_true,0 },
143 	{ "-noleaf",	c_simple,	f_always_true,	0 },
144 	{ "-not",	c_simple,	f_not,		0 },
145 	{ "-nouser",	c_nouser,	f_nouser,	0 },
146 	{ "-o",		c_simple,	f_or,		0 },
147 	{ "-ok",	c_exec,		f_exec,		F_NEEDOK },
148 	{ "-okdir",	c_exec,		f_exec,		F_NEEDOK | F_EXECDIR },
149 	{ "-or",	c_simple,	f_or,		0 },
150 	{ "-path", 	c_name,		f_path,		0 },
151 	{ "-perm",	c_perm,		f_perm,		0 },
152 	{ "-print",	c_print,	f_print,	0 },
153 	{ "-print0",	c_print,	f_print0,	0 },
154 // -printf
155 	{ "-prune",	c_simple,	f_prune,	0 },
156 	{ "-quit",	c_simple,	f_quit,		0 },
157 	{ "-regex",	c_regex,	f_regex,	0 },
158 	{ "-samefile",	c_samefile,	f_inum,		0 },
159 	{ "-size",	c_size,		f_size,		0 },
160 	{ "-sparse",	c_sparse,	f_sparse,	0 },
161 	{ "-true",	c_simple,	f_always_true,	0 },
162 	{ "-type",	c_type,		f_type,		0 },
163 	{ "-uid",	c_user,		f_user,		0 },
164 	{ "-user",	c_user,		f_user,		0 },
165 	{ "-wholename",	c_name,		f_path,		0 },
166 	{ "-xdev",	c_xdev,		f_always_true,	0 },
167 // -xtype
168 };
169 
170 /*
171  * find_create --
172  *	create a node corresponding to a command line argument.
173  *
174  * TODO:
175  *	add create/process function pointers to node, so we can skip
176  *	this switch stuff.
177  */
178 PLAN *
179 find_create(char ***argvp)
180 {
181 	OPTION *p;
182 	PLAN *new;
183 	char **argv;
184 
185 	argv = *argvp;
186 
187 	if ((p = lookup_option(*argv)) == NULL)
188 		errx(1, "%s: unknown primary or operator", *argv);
189 	++argv;
190 
191 	new = (p->create)(p, &argv);
192 	*argvp = argv;
193 	return (new);
194 }
195 
196 OPTION *
197 lookup_option(const char *name)
198 {
199 	OPTION tmp;
200 
201 	tmp.name = name;
202 	return ((OPTION *)bsearch(&tmp, options,
203 	    sizeof(options)/sizeof(OPTION), sizeof(OPTION), typecompare));
204 }
205 
206 static int
207 typecompare(const void *a, const void *b)
208 {
209 	return (strcmp(((const OPTION *)a)->name, ((const OPTION *)b)->name));
210 }
211