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