]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/find/option.c
This commit was generated by cvs2svn to compensate for changes in r68651,
[FreeBSD/FreeBSD.git] / usr.bin / find / option.c
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 /*
39 static char sccsid[] = "@(#)option.c    8.2 (Berkeley) 4/16/94";
40 */
41 static const char rcsid[] =
42   "$FreeBSD$";
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 <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53
54 #include "find.h"
55
56 static OPTION *option __P((char *));
57
58 /* NB: the following table must be sorted lexically. */
59 static OPTION const options[] = {
60         { "!",          N_NOT,          c_not,          O_ZERO },
61         { "(",          N_OPENPAREN,    c_openparen,    O_ZERO },
62         { ")",          N_CLOSEPAREN,   c_closeparen,   O_ZERO },
63         { "-a",         N_AND,          NULL,           O_NONE },
64         { "-amin",      N_AMIN,         c_amin,         O_ARGV },
65         { "-and",       N_AND,          NULL,           O_NONE },
66         { "-atime",     N_ATIME,        c_atime,        O_ARGV },
67         { "-cmin",      N_CMIN,         c_cmin,         O_ARGV },
68         { "-ctime",     N_CTIME,        c_ctime,        O_ARGV },
69         { "-delete",    N_DELETE,       c_delete,       O_ZERO },
70         { "-depth",     N_DEPTH,        c_depth,        O_ZERO },
71         { "-exec",      N_EXEC,         c_exec,         O_ARGVP },
72         { "-execdir",   N_EXECDIR,      c_execdir,      O_ARGVP },
73         { "-flags",     N_FLAGS,        c_flags,        O_ARGV },
74         { "-follow",    N_FOLLOW,       c_follow,       O_ZERO },
75
76 /*
77  * NetBSD doesn't provide a getvfsbyname(), so this option
78  * is not available if using a NetBSD kernel.
79  */
80 #if !defined(__NetBSD__)
81         { "-fstype",    N_FSTYPE,       c_fstype,       O_ARGV },
82 #endif
83         { "-group",     N_GROUP,        c_group,        O_ARGV },
84         { "-inum",      N_INUM,         c_inum,         O_ARGV },
85         { "-links",     N_LINKS,        c_links,        O_ARGV },
86         { "-ls",        N_LS,           c_ls,           O_ZERO },
87         { "-maxdepth",  N_MAXDEPTH,     c_maxdepth,     O_ARGV },
88         { "-mindepth",  N_MINDEPTH,     c_mindepth,     O_ARGV },
89         { "-mmin",      N_MMIN,         c_mmin,         O_ARGV },
90         { "-mtime",     N_MTIME,        c_mtime,        O_ARGV },
91         { "-name",      N_NAME,         c_name,         O_ARGV },
92         { "-newer",     N_NEWER,        c_newer,        O_ARGV },
93         { "-nogroup",   N_NOGROUP,      c_nogroup,      O_ZERO },
94         { "-nouser",    N_NOUSER,       c_nouser,       O_ZERO },
95         { "-o",         N_OR,           c_or,           O_ZERO },
96         { "-ok",        N_OK,           c_exec,         O_ARGVP },
97         { "-or",        N_OR,           c_or,           O_ZERO },
98         { "-path",      N_PATH,         c_path,         O_ARGV },
99         { "-perm",      N_PERM,         c_perm,         O_ARGV },
100         { "-print",     N_PRINT,        c_print,        O_ZERO },
101         { "-print0",    N_PRINT0,       c_print0,       O_ZERO },
102         { "-prune",     N_PRUNE,        c_prune,        O_ZERO },
103         { "-size",      N_SIZE,         c_size,         O_ARGV },
104         { "-type",      N_TYPE,         c_type,         O_ARGV },
105         { "-user",      N_USER,         c_user,         O_ARGV },
106         { "-xdev",      N_XDEV,         c_xdev,         O_ZERO },
107 };
108
109 /*
110  * find_create --
111  *      create a node corresponding to a command line argument.
112  *
113  * TODO:
114  *      add create/process function pointers to node, so we can skip
115  *      this switch stuff.
116  */
117 PLAN *
118 find_create(argvp)
119         char ***argvp;
120 {
121         register OPTION *p;
122         PLAN *new;
123         char **argv;
124
125         argv = *argvp;
126
127         if ((p = option(*argv)) == NULL)
128                 errx(1, "%s: unknown option", *argv);
129         ++argv;
130         if (p->flags & (O_ARGV|O_ARGVP) && !*argv)
131                 errx(1, "%s: requires additional arguments", *--argv);
132
133         switch(p->flags) {
134         case O_NONE:
135                 new = NULL;
136                 break;
137         case O_ZERO:
138                 new = (p->create)();
139                 break;
140         case O_ARGV:
141                 new = (p->create)(*argv++);
142                 break;
143         case O_ARGVP:
144                 new = (p->create)(&argv, p->token == N_OK);
145                 break;
146         default:
147                 abort();
148         }
149         *argvp = argv;
150         return (new);
151 }
152
153 static OPTION *
154 option(name)
155         char *name;
156 {
157         OPTION tmp;
158         int typecompare __P((const void *, const void *));
159
160         tmp.name = name;
161         return ((OPTION *)bsearch(&tmp, options,
162             sizeof(options)/sizeof(OPTION), sizeof(OPTION), typecompare));
163 }
164
165 int
166 typecompare(a, b)
167         const void *a, *b;
168 {
169         return (strcmp(((OPTION *)a)->name, ((OPTION *)b)->name));
170 }