Home | History | Annotate | Line # | Download | only in find
option.c revision 1.13
      1 /*	$NetBSD: option.c,v 1.13 1999/07/20 01:28:42 cgd Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1990, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Cimarron D. Taylor of the University of California, Berkeley.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #ifndef lint
     41 #if 0
     42 static char sccsid[] = "from: @(#)option.c	8.2 (Berkeley) 4/16/94";
     43 #else
     44 __RCSID("$NetBSD: option.c,v 1.13 1999/07/20 01:28:42 cgd Exp $");
     45 #endif
     46 #endif /* not lint */
     47 
     48 #include <sys/types.h>
     49 #include <sys/stat.h>
     50 
     51 #include <err.h>
     52 #include <fts.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 
     57 #include "find.h"
     58 
     59 int typecompare __P((const void *, const void *));
     60 static OPTION *option __P((char *));
     61 
     62 /* NB: the following table must be sorted lexically. */
     63 static OPTION const options[] = {
     64 	{ "!",		N_NOT,		c_not,		0 },
     65 	{ "(",		N_OPENPAREN,	c_openparen,	0 },
     66 	{ ")",		N_CLOSEPAREN,	c_closeparen,	0 },
     67 	{ "-a",		N_AND,		c_null,		0 },
     68 	{ "-and",	N_AND,		c_null,		0 },
     69 	{ "-amin",	N_AMIN,		c_amin,		1 },
     70 	{ "-atime",	N_ATIME,	c_atime,	1 },
     71 	{ "-cmin",	N_CMIN,		c_cmin,		1 },
     72 	{ "-ctime",	N_CTIME,	c_ctime,	1 },
     73 	{ "-depth",	N_DEPTH,	c_depth,	0 },
     74 	{ "-exec",	N_EXEC,		c_exec,		1 },
     75 	{ "-flags",	N_FLAGS,	c_flags,	1 },
     76 	{ "-follow",	N_FOLLOW,	c_follow,	0 },
     77 	{ "-fstype",	N_FSTYPE,	c_fstype,	1 },
     78 	{ "-group",	N_GROUP,	c_group,	1 },
     79 	{ "-inum",	N_INUM,		c_inum,		1 },
     80 	{ "-iregex",	N_IREGEX,	c_iregex,	1 },
     81 	{ "-links",	N_LINKS,	c_links,	1 },
     82 	{ "-ls",	N_LS,		c_ls,		0 },
     83 	{ "-mmin",	N_MMIN,		c_mmin,		1 },
     84 	{ "-mtime",	N_MTIME,	c_mtime,	1 },
     85 	{ "-name",	N_NAME,		c_name,		1 },
     86 	{ "-newer",	N_NEWER,	c_newer,	1 },
     87 	{ "-nogroup",	N_NOGROUP,	c_nogroup,	0 },
     88 	{ "-nouser",	N_NOUSER,	c_nouser,	0 },
     89 	{ "-o",		N_OR,		c_or,		0 },
     90 	{ "-ok",	N_OK,		c_exec,		1 },
     91 	{ "-or",	N_OR,		c_or,		0 },
     92 	{ "-path", 	N_PATH,		c_path,		1 },
     93 	{ "-perm",	N_PERM,		c_perm,		1 },
     94 	{ "-print",	N_PRINT,	c_print,	0 },
     95 	{ "-print0",	N_PRINT0,	c_print0,	0 },
     96 	{ "-printx",	N_PRINTX,	c_printx,	0 },
     97 	{ "-prune",	N_PRUNE,	c_prune,	0 },
     98 	{ "-regex",	N_REGEX,	c_regex,	1 },
     99 	{ "-size",	N_SIZE,		c_size,		1 },
    100 	{ "-type",	N_TYPE,		c_type,		1 },
    101 	{ "-user",	N_USER,		c_user,		1 },
    102 	{ "-xdev",	N_XDEV,		c_xdev,		0 }
    103 };
    104 
    105 /*
    106  * find_create --
    107  *	create a node corresponding to a command line argument.
    108  *
    109  * TODO:
    110  *	add create/process function pointers to node, so we can skip
    111  *	this switch stuff.
    112  */
    113 PLAN *
    114 find_create(argvp)
    115 	char ***argvp;
    116 {
    117 	OPTION *p;
    118 	PLAN *new;
    119 	char **argv;
    120 
    121 	argv = *argvp;
    122 
    123 	if ((p = option(*argv)) == NULL)
    124 		errx(1, "%s: unknown option", *argv);
    125 	++argv;
    126 	if (p->arg && !*argv)
    127 		errx(1, "%s: requires additional arguments", *--argv);
    128 
    129 	new = (p->create)(&argv, p->token == N_OK);
    130 
    131 	*argvp = argv;
    132 	return (new);
    133 }
    134 
    135 static OPTION *
    136 option(name)
    137 	char *name;
    138 {
    139 	OPTION tmp;
    140 
    141 	tmp.name = name;
    142 	return ((OPTION *)bsearch(&tmp, options,
    143 	    sizeof(options)/sizeof(OPTION), sizeof(OPTION), typecompare));
    144 }
    145 
    146 int
    147 typecompare(a, b)
    148 	const void *a, *b;
    149 {
    150 	return (strcmp(((OPTION *)a)->name, ((OPTION *)b)->name));
    151 }
    152