]>
Commit | Line | Data |
---|---|---|
44bd5ea7 A |
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. | |
44bd5ea7 A |
16 | * 4. Neither the name of the University nor the names of its contributors |
17 | * may be used to endorse or promote products derived from this software | |
18 | * without specific prior written permission. | |
19 | * | |
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
30 | * SUCH DAMAGE. | |
31 | */ | |
32 | ||
44bd5ea7 | 33 | #ifndef lint |
c0fcf4e1 A |
34 | /* |
35 | static char sccsid[] = "@(#)option.c 8.2 (Berkeley) 4/16/94"; | |
36 | */ | |
44bd5ea7 A |
37 | #endif /* not lint */ |
38 | ||
e1a085ba | 39 | #include <sys/cdefs.h> |
1e9ba8f2 | 40 | __FBSDID("$FreeBSD: src/usr.bin/find/option.c,v 1.32 2011/05/27 22:14:49 jilles Exp $"); |
e1a085ba | 41 | |
44bd5ea7 A |
42 | #include <sys/types.h> |
43 | #include <sys/stat.h> | |
44 | ||
45 | #include <err.h> | |
46 | #include <fts.h> | |
c0fcf4e1 | 47 | #include <regex.h> |
44bd5ea7 A |
48 | #include <stdio.h> |
49 | #include <stdlib.h> | |
50 | #include <string.h> | |
51 | ||
52 | #include "find.h" | |
53 | ||
1e9ba8f2 | 54 | static int typecompare(const void *, const void *); |
e1a085ba | 55 | |
44bd5ea7 | 56 | /* NB: the following table must be sorted lexically. */ |
ddb4a88b | 57 | /* Options listed with C++ comments are in gnu find, but not our find */ |
44bd5ea7 | 58 | static OPTION const options[] = { |
c0fcf4e1 A |
59 | { "!", c_simple, f_not, 0 }, |
60 | { "(", c_simple, f_openparen, 0 }, | |
61 | { ")", c_simple, f_closeparen, 0 }, | |
e1a085ba A |
62 | { "-Bmin", c_Xmin, f_Xmin, F_TIME_B }, |
63 | { "-Bnewer", c_newer, f_newer, F_TIME_B }, | |
64 | { "-Btime", c_Xtime, f_Xtime, F_TIME_B }, | |
c0fcf4e1 | 65 | { "-a", c_and, NULL, 0 }, |
e1a085ba | 66 | { "-acl", c_acl, f_acl, 0 }, |
c0fcf4e1 A |
67 | { "-amin", c_Xmin, f_Xmin, F_TIME_A }, |
68 | { "-and", c_and, NULL, 0 }, | |
69 | { "-anewer", c_newer, f_newer, F_TIME_A }, | |
70 | { "-atime", c_Xtime, f_Xtime, F_TIME_A }, | |
71 | { "-cmin", c_Xmin, f_Xmin, F_TIME_C }, | |
72 | { "-cnewer", c_newer, f_newer, F_TIME_C }, | |
73 | { "-ctime", c_Xtime, f_Xtime, F_TIME_C }, | |
ddb4a88b A |
74 | { "-d", c_depth, f_depth, 0 }, |
75 | // -daystart | |
c0fcf4e1 | 76 | { "-delete", c_delete, f_delete, 0 }, |
e1a085ba | 77 | { "-depth", c_depth, f_depth, 0 }, |
c0fcf4e1 A |
78 | { "-empty", c_empty, f_empty, 0 }, |
79 | { "-exec", c_exec, f_exec, 0 }, | |
80 | { "-execdir", c_exec, f_exec, F_EXECDIR }, | |
ddb4a88b | 81 | { "-false", c_simple, f_false, 0 }, |
c0fcf4e1 | 82 | { "-flags", c_flags, f_flags, 0 }, |
ddb4a88b | 83 | // -fls |
c0fcf4e1 | 84 | { "-follow", c_follow, f_always_true, 0 }, |
ddb4a88b A |
85 | // -fprint |
86 | // -fprint0 | |
87 | // -fprintf | |
c0fcf4e1 | 88 | { "-fstype", c_fstype, f_fstype, 0 }, |
ddb4a88b | 89 | { "-gid", c_group, f_group, 0 }, |
c0fcf4e1 | 90 | { "-group", c_group, f_group, 0 }, |
ddb4a88b A |
91 | { "-ignore_readdir_race",c_simple, f_always_true,0 }, |
92 | { "-ilname", c_name, f_name, F_LINK | F_IGNCASE }, | |
c0fcf4e1 A |
93 | { "-iname", c_name, f_name, F_IGNCASE }, |
94 | { "-inum", c_inum, f_inum, 0 }, | |
95 | { "-ipath", c_name, f_path, F_IGNCASE }, | |
96 | { "-iregex", c_regex, f_regex, F_IGNCASE }, | |
ddb4a88b | 97 | { "-iwholename",c_name, f_path, F_IGNCASE }, |
c0fcf4e1 | 98 | { "-links", c_links, f_links, 0 }, |
ddb4a88b | 99 | { "-lname", c_name, f_name, F_LINK }, |
c0fcf4e1 A |
100 | { "-ls", c_ls, f_ls, 0 }, |
101 | { "-maxdepth", c_mXXdepth, f_always_true, F_MAXDEPTH }, | |
102 | { "-mindepth", c_mXXdepth, f_always_true, 0 }, | |
103 | { "-mmin", c_Xmin, f_Xmin, 0 }, | |
104 | { "-mnewer", c_newer, f_newer, 0 }, | |
ddb4a88b | 105 | { "-mount", c_xdev, f_always_true, 0 }, |
c0fcf4e1 A |
106 | { "-mtime", c_Xtime, f_Xtime, 0 }, |
107 | { "-name", c_name, f_name, 0 }, | |
108 | { "-newer", c_newer, f_newer, 0 }, | |
e1a085ba A |
109 | { "-newerBB", c_newer, f_newer, F_TIME_B | F_TIME2_B }, |
110 | { "-newerBa", c_newer, f_newer, F_TIME_B | F_TIME2_A }, | |
111 | { "-newerBc", c_newer, f_newer, F_TIME_B | F_TIME2_C }, | |
112 | { "-newerBm", c_newer, f_newer, F_TIME_B }, | |
113 | { "-newerBt", c_newer, f_newer, F_TIME_B | F_TIME2_T }, | |
114 | { "-neweraB", c_newer, f_newer, F_TIME_A | F_TIME2_B }, | |
c0fcf4e1 A |
115 | { "-neweraa", c_newer, f_newer, F_TIME_A | F_TIME2_A }, |
116 | { "-newerac", c_newer, f_newer, F_TIME_A | F_TIME2_C }, | |
117 | { "-neweram", c_newer, f_newer, F_TIME_A }, | |
118 | { "-newerat", c_newer, f_newer, F_TIME_A | F_TIME2_T }, | |
e1a085ba | 119 | { "-newercB", c_newer, f_newer, F_TIME_C | F_TIME2_B }, |
c0fcf4e1 A |
120 | { "-newerca", c_newer, f_newer, F_TIME_C | F_TIME2_A }, |
121 | { "-newercc", c_newer, f_newer, F_TIME_C | F_TIME2_C }, | |
122 | { "-newercm", c_newer, f_newer, F_TIME_C }, | |
123 | { "-newerct", c_newer, f_newer, F_TIME_C | F_TIME2_T }, | |
e1a085ba | 124 | { "-newermB", c_newer, f_newer, F_TIME2_B }, |
c0fcf4e1 A |
125 | { "-newerma", c_newer, f_newer, F_TIME2_A }, |
126 | { "-newermc", c_newer, f_newer, F_TIME2_C }, | |
127 | { "-newermm", c_newer, f_newer, 0 }, | |
128 | { "-newermt", c_newer, f_newer, F_TIME2_T }, | |
129 | { "-nogroup", c_nogroup, f_nogroup, 0 }, | |
ddb4a88b A |
130 | { "-noignore_readdir_race",c_simple, f_always_true,0 }, |
131 | { "-noleaf", c_simple, f_always_true, 0 }, | |
9bafe280 | 132 | { "-not", c_simple, f_not, 0 }, |
c0fcf4e1 A |
133 | { "-nouser", c_nouser, f_nouser, 0 }, |
134 | { "-o", c_simple, f_or, 0 }, | |
135 | { "-ok", c_exec, f_exec, F_NEEDOK }, | |
136 | { "-okdir", c_exec, f_exec, F_NEEDOK | F_EXECDIR }, | |
137 | { "-or", c_simple, f_or, 0 }, | |
138 | { "-path", c_name, f_path, 0 }, | |
139 | { "-perm", c_perm, f_perm, 0 }, | |
140 | { "-print", c_print, f_print, 0 }, | |
141 | { "-print0", c_print, f_print0, 0 }, | |
ddb4a88b | 142 | // -printf |
c0fcf4e1 | 143 | { "-prune", c_simple, f_prune, 0 }, |
ddb4a88b | 144 | { "-quit", c_simple, f_quit, 0 }, |
c0fcf4e1 | 145 | { "-regex", c_regex, f_regex, 0 }, |
ddb4a88b | 146 | { "-samefile", c_samefile, f_inum, 0 }, |
c0fcf4e1 | 147 | { "-size", c_size, f_size, 0 }, |
ddb4a88b | 148 | { "-true", c_simple, f_always_true, 0 }, |
c0fcf4e1 | 149 | { "-type", c_type, f_type, 0 }, |
ddb4a88b | 150 | { "-uid", c_user, f_user, 0 }, |
c0fcf4e1 | 151 | { "-user", c_user, f_user, 0 }, |
ddb4a88b | 152 | { "-wholename", c_name, f_path, 0 }, |
f14763b6 A |
153 | #ifdef __APPLE__ |
154 | { "-xattr", c_simple, f_xattr, 0 }, | |
155 | { "-xattrname", c_name, f_xattrname, 0 }, | |
156 | #endif /* __APPLE__ */ | |
c0fcf4e1 | 157 | { "-xdev", c_xdev, f_always_true, 0 }, |
ddb4a88b | 158 | // -xtype |
44bd5ea7 A |
159 | }; |
160 | ||
161 | /* | |
162 | * find_create -- | |
163 | * create a node corresponding to a command line argument. | |
164 | * | |
165 | * TODO: | |
166 | * add create/process function pointers to node, so we can skip | |
167 | * this switch stuff. | |
168 | */ | |
169 | PLAN * | |
e1a085ba | 170 | find_create(char ***argvp) |
44bd5ea7 | 171 | { |
e1a085ba | 172 | OPTION *p; |
44bd5ea7 A |
173 | PLAN *new; |
174 | char **argv; | |
175 | ||
176 | argv = *argvp; | |
177 | ||
e1a085ba | 178 | if ((p = lookup_option(*argv)) == NULL) |
1e9ba8f2 | 179 | errx(1, "%s: unknown primary or operator", *argv); |
44bd5ea7 | 180 | ++argv; |
44bd5ea7 | 181 | |
c0fcf4e1 | 182 | new = (p->create)(p, &argv); |
44bd5ea7 A |
183 | *argvp = argv; |
184 | return (new); | |
185 | } | |
186 | ||
c0fcf4e1 | 187 | OPTION * |
e1a085ba | 188 | lookup_option(const char *name) |
44bd5ea7 A |
189 | { |
190 | OPTION tmp; | |
191 | ||
192 | tmp.name = name; | |
193 | return ((OPTION *)bsearch(&tmp, options, | |
194 | sizeof(options)/sizeof(OPTION), sizeof(OPTION), typecompare)); | |
195 | } | |
196 | ||
1e9ba8f2 | 197 | static int |
e1a085ba | 198 | typecompare(const void *a, const void *b) |
44bd5ea7 | 199 | { |
e1a085ba | 200 | return (strcmp(((const OPTION *)a)->name, ((const OPTION *)b)->name)); |
44bd5ea7 | 201 | } |