Home | History | Annotate | Line # | Download | only in find
option.c revision 1.7
      1 /*	$NetBSD: option.c,v 1.7 1997/10/19 11:52:59 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1990, 1993
      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.1 (Berkeley) 6/6/93";
     43 #else
     44 __RCSID("$NetBSD: option.c,v 1.7 1997/10/19 11:52:59 lukem 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 
     61 /* NB: the following table must be sorted lexically. */
     62 static OPTION options[] = {
     63 	{ "!",		N_NOT,		c_not,		O_ZERO },
     64 	{ "(",		N_OPENPAREN,	c_openparen,	O_ZERO },
     65 	{ ")",		N_CLOSEPAREN,	c_closeparen,	O_ZERO },
     66 	{ "-a",		N_AND,		NULL,		O_NONE },
     67 	{ "-and",	N_AND,		NULL,		O_NONE },
     68 	{ "-atime",	N_ATIME,	c_atime,	O_ARGV },
     69 	{ "-ctime",	N_CTIME,	c_ctime,	O_ARGV },
     70 	{ "-depth",	N_DEPTH,	c_depth,	O_ZERO },
     71 	{ "-exec",	N_EXEC,		c_exec,		O_ARGVP },
     72 	{ "-follow",	N_FOLLOW,	c_follow,	O_ZERO },
     73 	{ "-fstype",	N_FSTYPE,	c_fstype,	O_ARGV },
     74 	{ "-group",	N_GROUP,	c_group,	O_ARGV },
     75 	{ "-inum",	N_INUM,		c_inum,		O_ARGV },
     76 	{ "-links",	N_LINKS,	c_links,	O_ARGV },
     77 	{ "-ls",	N_LS,		c_ls,		O_ZERO },
     78 	{ "-mtime",	N_MTIME,	c_mtime,	O_ARGV },
     79 	{ "-name",	N_NAME,		c_name,		O_ARGV },
     80 	{ "-newer",	N_NEWER,	c_newer,	O_ARGV },
     81 	{ "-nogroup",	N_NOGROUP,	c_nogroup,	O_ZERO },
     82 	{ "-nouser",	N_NOUSER,	c_nouser,	O_ZERO },
     83 	{ "-o",		N_OR,		c_or,		O_ZERO },
     84 	{ "-ok",	N_OK,		c_exec,		O_ARGVP },
     85 	{ "-or",	N_OR,		c_or,		O_ZERO },
     86 	{ "-path", 	N_PATH,		c_path,		O_ARGV },
     87 	{ "-perm",	N_PERM,		c_perm,		O_ARGV },
     88 	{ "-print",	N_PRINT,	c_print,	O_ZERO },
     89 	{ "-print0",	N_PRINT0,	c_print0,	O_ZERO },
     90 	{ "-prune",	N_PRUNE,	c_prune,	O_ZERO },
     91 	{ "-size",	N_SIZE,		c_size,		O_ARGV },
     92 	{ "-type",	N_TYPE,		c_type,		O_ARGV },
     93 	{ "-user",	N_USER,		c_user,		O_ARGV },
     94 	{ "-xdev",	N_XDEV,		c_xdev,		O_ZERO },
     95 };
     96 
     97 /*
     98  * find_create --
     99  *	create a node corresponding to a command line argument.
    100  *
    101  * TODO:
    102  *	add create/process function pointers to node, so we can skip
    103  *	this switch stuff.
    104  */
    105 PLAN *
    106 find_create(argvp)
    107 	char ***argvp;
    108 {
    109 	OPTION *p;
    110 	PLAN *new;
    111 	char **argv;
    112 
    113 	argv = *argvp;
    114 
    115 	if ((p = option(*argv)) == NULL)
    116 		errx(1, "%s: unknown option", *argv);
    117 	++argv;
    118 	if (p->flags & (O_ARGV|O_ARGVP) && !*argv)
    119 		errx(1, "%s: requires additional arguments", *--argv);
    120 
    121 	switch(p->flags) {
    122 	case O_NONE:
    123 		new = NULL;
    124 		break;
    125 	case O_ZERO:
    126 		new = (p->create)();
    127 		break;
    128 	case O_ARGV:
    129 		new = (p->create)(*argv++);
    130 		break;
    131 	case O_ARGVP:
    132 		new = (p->create)(&argv, p->token == N_OK);
    133 		break;
    134 	default:
    135 		abort();
    136 	}
    137 	*argvp = argv;
    138 	return (new);
    139 }
    140 
    141 OPTION *
    142 option(name)
    143 	char *name;
    144 {
    145 	OPTION tmp;
    146 
    147 	tmp.name = name;
    148 	return ((OPTION *)bsearch(&tmp, options,
    149 	    sizeof(options)/sizeof(OPTION), sizeof(OPTION), typecompare));
    150 }
    151 
    152 int
    153 typecompare(a, b)
    154 	const void *a, *b;
    155 {
    156 	return (strcmp(((OPTION *)a)->name, ((OPTION *)b)->name));
    157 }
    158