xref: /original-bsd/usr.bin/find/option.c (revision abd50c55)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * 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	5.1 (Berkeley) 04/16/90";
13 #endif /* not lint */
14 
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fts.h>
18 #include <stdio.h>
19 #include "find.h"
20 
21 typedef struct _option {
22 	char *name;		/* option name */
23 	int token;		/* token value */
24 	PLAN *(*create)();	/* create function */
25 #define	O_NONE		0x01	/* no call required */
26 #define	O_ZERO		0x02	/* pass: nothing */
27 #define	O_ARGV		0x04	/* pass: argv, increment argv */
28 #define	O_ARGVP		0x08	/* pass: *argv, T_OK || T_EXEC */
29 	int flags;
30 } OPTION;
31 
32 PLAN	*c_atime(), *c_ctime(), *c_depth(), *c_exec(), *c_follow(),
33 	*c_fstype(), *c_group(), *c_inum(), *c_links(), *c_ls(),
34 	*c_mtime(), *c_name(), *c_newer(), *c_nogroup(), *c_nouser(),
35 	*c_ok(), *c_perm(), *c_print(), *c_prune(), *c_size(), *c_type(),
36 	*c_user(), *c_xdev(), *c_openparen(), *c_closeparen(), *c_not(),
37 	*c_or();
38 
39 OPTION options[] = {
40 	"!",		T_NOT,		c_not,		O_ZERO,
41 	"(",		T_OPENPAREN,	c_openparen,	O_ZERO,
42 	")",		T_CLOSEPAREN,	c_closeparen,	O_ZERO,
43 	"-a",		T_AND,		(PLAN *(*)())-1,O_NONE,
44 	"-atime",	T_ATIME,	c_atime,	O_ARGV,
45 	"-ctime",	T_CTIME,	c_ctime,	O_ARGV,
46 	"-depth",	T_DEPTH,	c_depth,	O_ZERO,
47 	"-exec",	T_EXEC,		c_exec,		O_ARGVP,
48 	"-follow",	T_FOLLOW,	c_follow,	O_ZERO,
49 	"-fstype",	T_FSTYPE,	c_fstype,	O_ARGV,
50 	"-group",	T_GROUP,	c_group,	O_ARGV,
51 	"-inum",	T_INUM,		c_inum,		O_ARGV,
52 	"-links",	T_LINKS,	c_links,	O_ARGV,
53 	"-ls",		T_LS,		c_ls,		O_ZERO,
54 	"-mtime",	T_MTIME,	c_mtime,	O_ARGV,
55 	"-name",	T_NAME,		c_name,		O_ARGV,
56 	"-newer",	T_NEWER,	c_newer,	O_ARGV,
57 	"-nogroup",	T_NOGROUP,	c_nogroup,	O_ZERO,
58 	"-nouser",	T_NOUSER,	c_nouser,	O_ZERO,
59 	"-o",		T_OR,		c_or,		O_ZERO,
60 	"-ok",		T_OK,		c_exec,		O_ARGVP,
61 	"-perm",	T_PERM,		c_perm,		O_ARGV,
62 	"-print",	T_PRINT,	c_print,	O_ZERO,
63 	"-prune",	T_PRUNE,	c_prune,	O_ZERO,
64 	"-size",	T_SIZE,		c_size,		O_ARGV,
65 	"-type",	T_TYPE,		c_type,		O_ARGV,
66 	"-user",	T_USER,		c_user,		O_ARGV,
67 	"-xdev",	T_XDEV,		c_xdev,		O_ZERO,
68 	{ NULL },
69 };
70 
71 /*
72  * find_create --
73  *	create a node corresponding to a command line argument.
74  *
75  * TODO:
76  *	add create/process function pointers to node, so we can skip
77  *	this switch stuff.
78  */
79 PLAN *
80 find_create(argvp)
81 	char ***argvp;
82 {
83 	register OPTION *p;
84 	OPTION tmp;
85 	PLAN *new;
86 	char **argv;
87 	OPTION *find_typelookup();
88 	int typecompare();
89 
90 	argv = *argvp;
91 	tmp.name = *argv++;
92 
93 	p = (OPTION *)bsearch(&tmp, options, sizeof(options)/sizeof(OPTION),
94 	    sizeof(OPTION), typecompare);
95 	if (!p) {
96 		(void)fprintf(stderr, "find: unknown option %s.\n", *--argv);
97 		exit(1);
98 	}
99 	if (p->flags & (O_ARGV|O_ARGVP) && !*argv) {
100 		(void)fprintf(stderr,
101 		    "find: %s requires additional arguments.\n", *--argv);
102 		exit(1);
103 	}
104 
105 	switch(p->flags) {
106 	case O_ZERO:
107 		new = (p->create)();
108 		break;
109 	case O_ARGV:
110 		new = (p->create)(*argv++);
111 		break;
112 	case O_ARGVP:
113 		new = (p->create)(&argv, p->token == T_OK);
114 		break;
115 	}
116 	*argvp = argv;
117 	return(new);
118 }
119 
120 typecompare(a, b)
121 	OPTION *a, *b;
122 {
123 	return(strcmp(a->name, b->name));
124 }
125