This commit was manufactured by cvs2svn to create tag 'FreeBSD-release/1.0'.
[unix-history] / usr.bin / find / option.c
CommitLineData
15637ed4 1/*-
78ed81a3 2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
15637ed4
RG
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
78ed81a3 38static char sccsid[] = "@(#)option.c 8.1 (Berkeley) 6/6/93";
15637ed4
RG
39#endif /* not lint */
40
41#include <sys/types.h>
42#include <sys/stat.h>
78ed81a3 43
44#include <err.h>
15637ed4
RG
45#include <fts.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
15637ed4 49
78ed81a3 50#include "find.h"
15637ed4 51
78ed81a3 52/* NB: the following table must be sorted lexically. */
53static OPTION options[] = {
54 { "!", N_NOT, c_not, O_ZERO },
55 { "(", N_OPENPAREN, c_openparen, O_ZERO },
56 { ")", N_CLOSEPAREN, c_closeparen, O_ZERO },
57 { "-a", N_AND, NULL, O_NONE },
58 { "-and", N_AND, NULL, O_NONE },
59 { "-atime", N_ATIME, c_atime, O_ARGV },
60 { "-ctime", N_CTIME, c_ctime, O_ARGV },
61 { "-depth", N_DEPTH, c_depth, O_ZERO },
62 { "-exec", N_EXEC, c_exec, O_ARGVP },
63 { "-follow", N_FOLLOW, c_follow, O_ZERO },
64 { "-fstype", N_FSTYPE, c_fstype, O_ARGV },
65 { "-group", N_GROUP, c_group, O_ARGV },
66 { "-inum", N_INUM, c_inum, O_ARGV },
67 { "-links", N_LINKS, c_links, O_ARGV },
68 { "-ls", N_LS, c_ls, O_ZERO },
69 { "-mtime", N_MTIME, c_mtime, O_ARGV },
70 { "-name", N_NAME, c_name, O_ARGV },
71 { "-newer", N_NEWER, c_newer, O_ARGV },
72 { "-nogroup", N_NOGROUP, c_nogroup, O_ZERO },
73 { "-nouser", N_NOUSER, c_nouser, O_ZERO },
74 { "-o", N_OR, c_or, O_ZERO },
75 { "-ok", N_OK, c_exec, O_ARGVP },
76 { "-or", N_OR, c_or, O_ZERO },
77 { "-path", N_PATH, c_path, O_ARGV },
78 { "-perm", N_PERM, c_perm, O_ARGV },
79 { "-print", N_PRINT, c_print, O_ZERO },
80 { "-prune", N_PRUNE, c_prune, O_ZERO },
81 { "-size", N_SIZE, c_size, O_ARGV },
82 { "-type", N_TYPE, c_type, O_ARGV },
83 { "-user", N_USER, c_user, O_ARGV },
84 { "-xdev", N_XDEV, c_xdev, O_ZERO },
15637ed4
RG
85};
86
87/*
88 * find_create --
89 * create a node corresponding to a command line argument.
90 *
91 * TODO:
92 * add create/process function pointers to node, so we can skip
93 * this switch stuff.
94 */
95PLAN *
96find_create(argvp)
97 char ***argvp;
98{
99 register OPTION *p;
100 PLAN *new;
101 char **argv;
15637ed4
RG
102
103 argv = *argvp;
104
78ed81a3 105 if ((p = option(*argv)) == NULL)
106 errx(1, "%s: unknown option", *argv);
15637ed4 107 ++argv;
78ed81a3 108 if (p->flags & (O_ARGV|O_ARGVP) && !*argv)
109 errx(1, "%s: requires additional arguments", *--argv);
15637ed4
RG
110
111 switch(p->flags) {
112 case O_NONE:
113 new = NULL;
114 break;
115 case O_ZERO:
116 new = (p->create)();
117 break;
118 case O_ARGV:
119 new = (p->create)(*argv++);
120 break;
121 case O_ARGVP:
122 new = (p->create)(&argv, p->token == N_OK);
123 break;
78ed81a3 124 default:
125 abort();
15637ed4
RG
126 }
127 *argvp = argv;
78ed81a3 128 return (new);
15637ed4
RG
129}
130
131OPTION *
132option(name)
133 char *name;
134{
135 OPTION tmp;
136 int typecompare __P((const void *, const void *));
137
138 tmp.name = name;
78ed81a3 139 return ((OPTION *)bsearch(&tmp, options,
15637ed4
RG
140 sizeof(options)/sizeof(OPTION), sizeof(OPTION), typecompare));
141}
78ed81a3 142
143int
15637ed4
RG
144typecompare(a, b)
145 const void *a, *b;
146{
78ed81a3 147 return (strcmp(((OPTION *)a)->name, ((OPTION *)b)->name));
15637ed4 148}