xref: /original-bsd/usr.bin/find/option.c (revision a69cfb4b)
1 /*-
2  * Copyright (c) 1990, 1993
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  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)option.c	8.1 (Berkeley) 06/06/93";
13 #endif /* not lint */
14 
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 
18 #include <err.h>
19 #include <fts.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "find.h"
25 
26 /* NB: the following table must be sorted lexically. */
27 static OPTION options[] = {
28 	{ "!",		N_NOT,		c_not,		O_ZERO },
29 	{ "(",		N_OPENPAREN,	c_openparen,	O_ZERO },
30 	{ ")",		N_CLOSEPAREN,	c_closeparen,	O_ZERO },
31 	{ "-a",		N_AND,		NULL,		O_NONE },
32 	{ "-and",	N_AND,		NULL,		O_NONE },
33 	{ "-atime",	N_ATIME,	c_atime,	O_ARGV },
34 	{ "-ctime",	N_CTIME,	c_ctime,	O_ARGV },
35 	{ "-depth",	N_DEPTH,	c_depth,	O_ZERO },
36 	{ "-exec",	N_EXEC,		c_exec,		O_ARGVP },
37 	{ "-follow",	N_FOLLOW,	c_follow,	O_ZERO },
38 	{ "-fstype",	N_FSTYPE,	c_fstype,	O_ARGV },
39 	{ "-group",	N_GROUP,	c_group,	O_ARGV },
40 	{ "-inum",	N_INUM,		c_inum,		O_ARGV },
41 	{ "-links",	N_LINKS,	c_links,	O_ARGV },
42 	{ "-ls",	N_LS,		c_ls,		O_ZERO },
43 	{ "-mtime",	N_MTIME,	c_mtime,	O_ARGV },
44 	{ "-name",	N_NAME,		c_name,		O_ARGV },
45 	{ "-newer",	N_NEWER,	c_newer,	O_ARGV },
46 	{ "-nogroup",	N_NOGROUP,	c_nogroup,	O_ZERO },
47 	{ "-nouser",	N_NOUSER,	c_nouser,	O_ZERO },
48 	{ "-o",		N_OR,		c_or,		O_ZERO },
49 	{ "-ok",	N_OK,		c_exec,		O_ARGVP },
50 	{ "-or",	N_OR,		c_or,		O_ZERO },
51 	{ "-path", 	N_PATH,		c_path,		O_ARGV },
52 	{ "-perm",	N_PERM,		c_perm,		O_ARGV },
53 	{ "-print",	N_PRINT,	c_print,	O_ZERO },
54 	{ "-prune",	N_PRUNE,	c_prune,	O_ZERO },
55 	{ "-size",	N_SIZE,		c_size,		O_ARGV },
56 	{ "-type",	N_TYPE,		c_type,		O_ARGV },
57 	{ "-user",	N_USER,		c_user,		O_ARGV },
58 	{ "-xdev",	N_XDEV,		c_xdev,		O_ZERO },
59 };
60 
61 /*
62  * find_create --
63  *	create a node corresponding to a command line argument.
64  *
65  * TODO:
66  *	add create/process function pointers to node, so we can skip
67  *	this switch stuff.
68  */
69 PLAN *
70 find_create(argvp)
71 	char ***argvp;
72 {
73 	register OPTION *p;
74 	PLAN *new;
75 	char **argv;
76 
77 	argv = *argvp;
78 
79 	if ((p = option(*argv)) == NULL)
80 		errx(1, "%s: unknown option", *argv);
81 	++argv;
82 	if (p->flags & (O_ARGV|O_ARGVP) && !*argv)
83 		errx(1, "%s: requires additional arguments", *--argv);
84 
85 	switch(p->flags) {
86 	case O_NONE:
87 		new = NULL;
88 		break;
89 	case O_ZERO:
90 		new = (p->create)();
91 		break;
92 	case O_ARGV:
93 		new = (p->create)(*argv++);
94 		break;
95 	case O_ARGVP:
96 		new = (p->create)(&argv, p->token == N_OK);
97 		break;
98 	default:
99 		abort();
100 	}
101 	*argvp = argv;
102 	return (new);
103 }
104 
105 OPTION *
106 option(name)
107 	char *name;
108 {
109 	OPTION tmp;
110 	int typecompare __P((const void *, const void *));
111 
112 	tmp.name = name;
113 	return ((OPTION *)bsearch(&tmp, options,
114 	    sizeof(options)/sizeof(OPTION), sizeof(OPTION), typecompare));
115 }
116 
117 int
118 typecompare(a, b)
119 	const void *a, *b;
120 {
121 	return (strcmp(((OPTION *)a)->name, ((OPTION *)b)->name));
122 }
123