Sun Nov 1 05:38:29 2020 UTC ()
add support for '-not': GNU and thus worldly scripts compatibility.


(mrg)
diff -r1.27 -r1.28 src/usr.bin/find/option.c

cvs diff -r1.27 -r1.28 src/usr.bin/find/option.c (switch to unified diff)

--- src/usr.bin/find/option.c 2016/06/13 00:04:40 1.27
+++ src/usr.bin/find/option.c 2020/11/01 05:38:29 1.28
@@ -1,194 +1,196 @@ @@ -1,194 +1,196 @@
1/* $NetBSD: option.c,v 1.27 2016/06/13 00:04:40 pgoyette Exp $ */ 1/* $NetBSD: option.c,v 1.28 2020/11/01 05:38:29 mrg Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1990, 1993, 1994 4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * Cimarron D. Taylor of the University of California, Berkeley. 8 * Cimarron D. Taylor of the University of California, Berkeley.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the 16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution. 17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors 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 19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission. 20 * without specific prior written permission.
21 * 21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 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 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 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 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 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 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE. 32 * SUCH DAMAGE.
33 */ 33 */
34 34
35#include <sys/cdefs.h> 35#include <sys/cdefs.h>
36#ifndef lint 36#ifndef lint
37#if 0 37#if 0
38static char sccsid[] = "from: @(#)option.c 8.2 (Berkeley) 4/16/94"; 38static char sccsid[] = "from: @(#)option.c 8.2 (Berkeley) 4/16/94";
39#else 39#else
40__RCSID("$NetBSD: option.c,v 1.27 2016/06/13 00:04:40 pgoyette Exp $"); 40__RCSID("$NetBSD: option.c,v 1.28 2020/11/01 05:38:29 mrg Exp $");
41#endif 41#endif
42#endif /* not lint */ 42#endif /* not lint */
43 43
44#include <sys/types.h> 44#include <sys/types.h>
45#include <sys/stat.h> 45#include <sys/stat.h>
46 46
47#include <err.h> 47#include <err.h>
48#include <fts.h> 48#include <fts.h>
49#include <stdio.h> 49#include <stdio.h>
50#include <stdlib.h> 50#include <stdlib.h>
51#include <string.h> 51#include <string.h>
52 52
53#include "find.h" 53#include "find.h"
54 54
55int typecompare(const void *, const void *); 55int typecompare(const void *, const void *);
56static OPTION *option(char *); 56static OPTION *option(char *);
57 57
58/* NB: the following table must be sorted lexically. */ 58/* NB: the following table must be sorted lexically. */
59static OPTION const options[] = { 59static OPTION const options[] = {
60 { "!", N_NOT, c_not, 0 }, 60 { "!", N_NOT, c_not, 0 },
61 { "(", N_OPENPAREN, c_openparen, 0 }, 61 { "(", N_OPENPAREN, c_openparen, 0 },
62 { ")", N_CLOSEPAREN, c_closeparen, 0 }, 62 { ")", N_CLOSEPAREN, c_closeparen, 0 },
63 { "-a", N_AND, c_null, 0 }, 63 { "-a", N_AND, c_null, 0 },
64 { "-amin", N_AMIN, c_amin, 1 }, 64 { "-amin", N_AMIN, c_amin, 1 },
65 { "-and", N_AND, c_null, 0 }, 65 { "-and", N_AND, c_null, 0 },
66 { "-anewer", N_ANEWER, c_anewer, 1 }, 66 { "-anewer", N_ANEWER, c_anewer, 1 },
67 { "-asince", N_ASINCE, c_asince, 1 }, 67 { "-asince", N_ASINCE, c_asince, 1 },
68 { "-atime", N_ATIME, c_atime, 1 }, 68 { "-atime", N_ATIME, c_atime, 1 },
69 { "-cmin", N_CMIN, c_cmin, 1 }, 69 { "-cmin", N_CMIN, c_cmin, 1 },
70 { "-cnewer", N_CNEWER, c_cnewer, 1 }, 70 { "-cnewer", N_CNEWER, c_cnewer, 1 },
71 { "-csince", N_CSINCE, c_csince, 1 }, 71 { "-csince", N_CSINCE, c_csince, 1 },
72 { "-ctime", N_CTIME, c_ctime, 1 }, 72 { "-ctime", N_CTIME, c_ctime, 1 },
73 { "-delete", N_DELETE, c_delete, 0 }, 73 { "-delete", N_DELETE, c_delete, 0 },
74 { "-depth", N_DEPTH, c_depth, 0 }, 74 { "-depth", N_DEPTH, c_depth, 0 },
75 { "-empty", N_EMPTY, c_empty, 0 }, 75 { "-empty", N_EMPTY, c_empty, 0 },
76 { "-exec", N_EXEC, c_exec, 1 }, 76 { "-exec", N_EXEC, c_exec, 1 },
77 { "-execdir", N_EXECDIR, c_execdir, 1 }, 77 { "-execdir", N_EXECDIR, c_execdir, 1 },
78 { "-exit", N_EXIT, c_exit, 0 }, 78 { "-exit", N_EXIT, c_exit, 0 },
79 { "-false", N_FALSE, c_false, 0 }, 79 { "-false", N_FALSE, c_false, 0 },
80 { "-flags", N_FLAGS, c_flags, 1 }, 80 { "-flags", N_FLAGS, c_flags, 1 },
81 { "-follow", N_FOLLOW, c_follow, 0 }, 81 { "-follow", N_FOLLOW, c_follow, 0 },
82 { "-fprint", N_FPRINT, c_fprint, 1 }, 82 { "-fprint", N_FPRINT, c_fprint, 1 },
83 { "-fstype", N_FSTYPE, c_fstype, 1 }, 83 { "-fstype", N_FSTYPE, c_fstype, 1 },
84 { "-group", N_GROUP, c_group, 1 }, 84 { "-group", N_GROUP, c_group, 1 },
85 { "-iname", N_INAME, c_iname, 1 }, 85 { "-iname", N_INAME, c_iname, 1 },
86 { "-inum", N_INUM, c_inum, 1 }, 86 { "-inum", N_INUM, c_inum, 1 },
87 { "-iregex", N_IREGEX, c_iregex, 1 }, 87 { "-iregex", N_IREGEX, c_iregex, 1 },
88 { "-links", N_LINKS, c_links, 1 }, 88 { "-links", N_LINKS, c_links, 1 },
89 { "-ls", N_LS, c_ls, 0 }, 89 { "-ls", N_LS, c_ls, 0 },
90 { "-maxdepth", N_MAXDEPTH, c_maxdepth, 1 }, 90 { "-maxdepth", N_MAXDEPTH, c_maxdepth, 1 },
91 { "-mindepth", N_MINDEPTH, c_mindepth, 1 }, 91 { "-mindepth", N_MINDEPTH, c_mindepth, 1 },
92 { "-mmin", N_MMIN, c_mmin, 1 }, 92 { "-mmin", N_MMIN, c_mmin, 1 },
93 { "-mtime", N_MTIME, c_mtime, 1 }, 93 { "-mtime", N_MTIME, c_mtime, 1 },
94 { "-name", N_NAME, c_name, 1 }, 94 { "-name", N_NAME, c_name, 1 },
95 { "-newer", N_NEWER, c_newer, 1 }, 95 { "-newer", N_NEWER, c_newer, 1 },
96 96
97/* Aliases for compatability with Gnu findutils */ 97/* Aliases for compatability with Gnu findutils */
98 { "-neweraa", N_ANEWER, c_anewer, 1 }, 98 { "-neweraa", N_ANEWER, c_anewer, 1 },
99 { "-newerat", N_ASINCE, c_asince, 1 }, 99 { "-newerat", N_ASINCE, c_asince, 1 },
100 { "-newercc", N_CNEWER, c_cnewer, 1 }, 100 { "-newercc", N_CNEWER, c_cnewer, 1 },
101 { "-newerct", N_CSINCE, c_csince, 1 }, 101 { "-newerct", N_CSINCE, c_csince, 1 },
102 { "-newermm", N_NEWER, c_newer, 1 }, 102 { "-newermm", N_NEWER, c_newer, 1 },
103 { "-newermt", N_SINCE, c_since, 1 }, 103 { "-newermt", N_SINCE, c_since, 1 },
104 104
105/* 105/*
106 * Unimplemented Gnu findutils options  106 * Unimplemented Gnu findutils options
107 * 107 *
108 * If you implement any of these, be sure to re-sort the table 108 * If you implement any of these, be sure to re-sort the table
109 * in ascii(7) order! 109 * in ascii(7) order!
110 * 110 *
111 { "-newerBB", N_UNIMPL, c_unimpl, 1 }, 111 { "-newerBB", N_UNIMPL, c_unimpl, 1 },
112 { "-newerBa", N_UNIMPL, c_unimpl, 1 }, 112 { "-newerBa", N_UNIMPL, c_unimpl, 1 },
113 { "-newerBc", N_UNIMPL, c_unimpl, 1 }, 113 { "-newerBc", N_UNIMPL, c_unimpl, 1 },
114 { "-newerBm", N_UNIMPL, c_unimpl, 1 }, 114 { "-newerBm", N_UNIMPL, c_unimpl, 1 },
115 { "-newerBt", N_UNIMPL, c_unimpl, 1 }, 115 { "-newerBt", N_UNIMPL, c_unimpl, 1 },
116 { "-neweraB", N_UNIMPL, c_unimpl, 1 }, 116 { "-neweraB", N_UNIMPL, c_unimpl, 1 },
117 { "-newerac", N_UNIMPL, c_unimpl, 1 }, 117 { "-newerac", N_UNIMPL, c_unimpl, 1 },
118 { "-neweram", N_UNIMPL, c_unimpl, 1 }, 118 { "-neweram", N_UNIMPL, c_unimpl, 1 },
119 { "-newerca", N_UNIMPL, c_unimpl, 1 }, 119 { "-newerca", N_UNIMPL, c_unimpl, 1 },
120 { "-newercm", N_UNIMPL, c_unimpl, 1 }, 120 { "-newercm", N_UNIMPL, c_unimpl, 1 },
121 { "-newercB", N_UNIMPL, c_unimpl, 1 }, 121 { "-newercB", N_UNIMPL, c_unimpl, 1 },
122 { "-newermB", N_UNIMPL, c_unimpl, 1 }, 122 { "-newermB", N_UNIMPL, c_unimpl, 1 },
123 { "-newerma", N_UNIMPL, c_unimpl, 1 }, 123 { "-newerma", N_UNIMPL, c_unimpl, 1 },
124 { "-newermc", N_UNIMPL, c_unimpl, 1 }, 124 { "-newermc", N_UNIMPL, c_unimpl, 1 },
125 * 125 *
126 */ 126 */
127 127
128 { "-nogroup", N_NOGROUP, c_nogroup, 0 }, 128 { "-nogroup", N_NOGROUP, c_nogroup, 0 },
 129/* Alias for compatability with Gnu findutils */
 130 { "-not", N_NOT, c_not, 0 },
129 { "-nouser", N_NOUSER, c_nouser, 0 }, 131 { "-nouser", N_NOUSER, c_nouser, 0 },
130 { "-o", N_OR, c_or, 0 }, 132 { "-o", N_OR, c_or, 0 },
131 { "-ok", N_OK, c_exec, 1 }, 133 { "-ok", N_OK, c_exec, 1 },
132 { "-or", N_OR, c_or, 0 }, 134 { "-or", N_OR, c_or, 0 },
133 { "-path", N_PATH, c_path, 1 }, 135 { "-path", N_PATH, c_path, 1 },
134 { "-perm", N_PERM, c_perm, 1 }, 136 { "-perm", N_PERM, c_perm, 1 },
135 { "-print", N_PRINT, c_print, 0 }, 137 { "-print", N_PRINT, c_print, 0 },
136 { "-print0", N_PRINT0, c_print0, 0 }, 138 { "-print0", N_PRINT0, c_print0, 0 },
137 { "-printx", N_PRINTX, c_printx, 0 }, 139 { "-printx", N_PRINTX, c_printx, 0 },
138 { "-prune", N_PRUNE, c_prune, 0 }, 140 { "-prune", N_PRUNE, c_prune, 0 },
139 { "-regex", N_REGEX, c_regex, 1 }, 141 { "-regex", N_REGEX, c_regex, 1 },
140 { "-rm", N_DELETE, c_delete, 0 }, 142 { "-rm", N_DELETE, c_delete, 0 },
141 { "-since", N_SINCE, c_since, 1 }, 143 { "-since", N_SINCE, c_since, 1 },
142 { "-size", N_SIZE, c_size, 1 }, 144 { "-size", N_SIZE, c_size, 1 },
143 { "-type", N_TYPE, c_type, 1 }, 145 { "-type", N_TYPE, c_type, 1 },
144 { "-user", N_USER, c_user, 1 }, 146 { "-user", N_USER, c_user, 1 },
145 { "-xdev", N_XDEV, c_xdev, 0 } 147 { "-xdev", N_XDEV, c_xdev, 0 }
146}; 148};
147 149
148/* 150/*
149 * find_create -- 151 * find_create --
150 * create a node corresponding to a command line argument. 152 * create a node corresponding to a command line argument.
151 * 153 *
152 * TODO: 154 * TODO:
153 * add create/process function pointers to node, so we can skip 155 * add create/process function pointers to node, so we can skip
154 * this switch stuff. 156 * this switch stuff.
155 */ 157 */
156PLAN * 158PLAN *
157find_create(char ***argvp) 159find_create(char ***argvp)
158{ 160{
159 OPTION *p; 161 OPTION *p;
160 PLAN *new; 162 PLAN *new;
161 char **argv; 163 char **argv;
162 char *opt; 164 char *opt;
163 165
164 argv = *argvp; 166 argv = *argvp;
165 opt = *argv; 167 opt = *argv;
166 168
167 if ((p = option(opt)) == NULL) 169 if ((p = option(opt)) == NULL)
168 errx(1, "%s: unknown option", opt); 170 errx(1, "%s: unknown option", opt);
169 ++argv; 171 ++argv;
170 if (p->arg && !*argv) 172 if (p->arg && !*argv)
171 errx(1, "%s: requires additional arguments", opt); 173 errx(1, "%s: requires additional arguments", opt);
172 174
173 new = (p->create)(&argv, p->token == N_OK, opt); 175 new = (p->create)(&argv, p->token == N_OK, opt);
174 176
175 *argvp = argv; 177 *argvp = argv;
176 return (new); 178 return (new);
177} 179}
178 180
179static OPTION * 181static OPTION *
180option(char *name) 182option(char *name)
181{ 183{
182 OPTION tmp; 184 OPTION tmp;
183 185
184 tmp.name = name; 186 tmp.name = name;
185 return ((OPTION *)bsearch(&tmp, options, 187 return ((OPTION *)bsearch(&tmp, options,
186 sizeof(options)/sizeof(OPTION), sizeof(OPTION), typecompare)); 188 sizeof(options)/sizeof(OPTION), sizeof(OPTION), typecompare));
187} 189}
188 190
189int 191int
190typecompare(const void *a, const void *b) 192typecompare(const void *a, const void *b)
191{ 193{
192 194
193 return (strcmp(((const OPTION *)a)->name, ((const OPTION *)b)->name)); 195 return (strcmp(((const OPTION *)a)->name, ((const OPTION *)b)->name));
194} 196}