11590Srgrimes/*-
21590Srgrimes * Copyright (c) 1990, 1993, 1994
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * This code is derived from software contributed to Berkeley by
61590Srgrimes * Cimarron D. Taylor of the University of California, Berkeley.
71590Srgrimes *
81590Srgrimes * Redistribution and use in source and binary forms, with or without
91590Srgrimes * modification, are permitted provided that the following conditions
101590Srgrimes * are met:
111590Srgrimes * 1. Redistributions of source code must retain the above copyright
121590Srgrimes *    notice, this list of conditions and the following disclaimer.
131590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141590Srgrimes *    notice, this list of conditions and the following disclaimer in the
151590Srgrimes *    documentation and/or other materials provided with the distribution.
161590Srgrimes * 4. Neither the name of the University nor the names of its contributors
171590Srgrimes *    may be used to endorse or promote products derived from this software
181590Srgrimes *    without specific prior written permission.
191590Srgrimes *
201590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301590Srgrimes * SUCH DAMAGE.
311590Srgrimes */
321590Srgrimes
33207705Sdelphij#ifndef lint
34207705Sdelphij/*
35207705Sdelphijstatic char sccsid[] = "@(#)option.c	8.2 (Berkeley) 4/16/94";
36207705Sdelphij*/
37207705Sdelphij#endif /* not lint */
38207705Sdelphij
3993604Sobrien#include <sys/cdefs.h>
4093604Sobrien__FBSDID("$FreeBSD$");
411590Srgrimes
421590Srgrimes#include <sys/types.h>
431590Srgrimes#include <sys/stat.h>
441590Srgrimes
451590Srgrimes#include <err.h>
461590Srgrimes#include <fts.h>
4772945Sknu#include <regex.h>
48200462Sdelphij#include <stdio.h>
491590Srgrimes#include <stdlib.h>
501590Srgrimes#include <string.h>
511590Srgrimes
521590Srgrimes#include "find.h"
531590Srgrimes
54207705Sdelphijstatic int typecompare(const void *, const void *);
5591400Sdwmalone
561590Srgrimes/* NB: the following table must be sorted lexically. */
57176478Simp/* Options listed with C++ comments are in gnu find, but not our find */
581590Srgrimesstatic OPTION const options[] = {
5976250Sphk	{ "!",		c_simple,	f_not,		0 },
6076250Sphk	{ "(",		c_simple,	f_openparen,	0 },
6176250Sphk	{ ")",		c_simple,	f_closeparen,	0 },
62157555Sceri	{ "-Bmin",	c_Xmin,		f_Xmin,		F_TIME_B },
63157555Sceri	{ "-Bnewer",	c_newer,	f_newer,	F_TIME_B },
64157555Sceri	{ "-Btime",	c_Xtime,	f_Xtime,	F_TIME_B },
6576250Sphk	{ "-a",		c_and,		NULL,		0 },
66127796Sbmilekic	{ "-acl",	c_acl,		f_acl,		0 },
6776250Sphk	{ "-amin",	c_Xmin,		f_Xmin,		F_TIME_A },
6876250Sphk	{ "-and",	c_and,		NULL,		0 },
6976250Sphk	{ "-anewer",	c_newer,	f_newer,	F_TIME_A },
7076250Sphk	{ "-atime",	c_Xtime,	f_Xtime,	F_TIME_A },
7176250Sphk	{ "-cmin",	c_Xmin,		f_Xmin,		F_TIME_C },
7276250Sphk	{ "-cnewer",	c_newer,	f_newer,	F_TIME_C },
7376250Sphk	{ "-ctime",	c_Xtime,	f_Xtime,	F_TIME_C },
74176478Simp	{ "-d",		c_depth,	f_depth,	0 },
75176478Simp// -daystart
7676250Sphk	{ "-delete",	c_delete,	f_delete,	0 },
77129812Seik	{ "-depth",	c_depth,	f_depth,	0 },
7876250Sphk	{ "-empty",	c_empty,	f_empty,	0 },
7976250Sphk	{ "-exec",	c_exec,		f_exec,		0 },
8076250Sphk	{ "-execdir",	c_exec,		f_exec,		F_EXECDIR },
81176478Simp	{ "-false",	c_simple,	f_false,	0 },
8276250Sphk	{ "-flags",	c_flags,	f_flags,	0 },
83176478Simp// -fls
8476250Sphk	{ "-follow",	c_follow,	f_always_true,	0 },
85176478Simp// -fprint
86176478Simp// -fprint0
87176478Simp// -fprintf
8876250Sphk	{ "-fstype",	c_fstype,	f_fstype,	0 },
89176478Simp	{ "-gid",	c_group,	f_group,	0 },
9076250Sphk	{ "-group",	c_group,	f_group,	0 },
91238780Sjilles	{ "-ignore_readdir_race",c_ignore_readdir_race, f_always_true,0 },
92176478Simp	{ "-ilname",	c_name,		f_name,		F_LINK | F_IGNCASE },
9376250Sphk	{ "-iname",	c_name,		f_name,		F_IGNCASE },
9476250Sphk	{ "-inum",	c_inum,		f_inum,		0 },
9576250Sphk	{ "-ipath",	c_name,		f_path,		F_IGNCASE },
9676250Sphk	{ "-iregex",	c_regex,	f_regex,	F_IGNCASE },
97176478Simp	{ "-iwholename",c_name,		f_path,		F_IGNCASE },
9876250Sphk	{ "-links",	c_links,	f_links,	0 },
99176478Simp	{ "-lname",	c_name,		f_name,		F_LINK },
10076250Sphk	{ "-ls",	c_ls,		f_ls,		0 },
10176250Sphk	{ "-maxdepth",	c_mXXdepth,	f_always_true,	F_MAXDEPTH },
10276250Sphk	{ "-mindepth",	c_mXXdepth,	f_always_true,	0 },
10376250Sphk	{ "-mmin",	c_Xmin,		f_Xmin,		0 },
10476250Sphk	{ "-mnewer",	c_newer,	f_newer,	0 },
105176478Simp	{ "-mount",	c_xdev,		f_always_true,	0 },
10676250Sphk	{ "-mtime",	c_Xtime,	f_Xtime,	0 },
10776250Sphk	{ "-name",	c_name,		f_name,		0 },
10876250Sphk	{ "-newer",	c_newer,	f_newer,	0 },
109157555Sceri	{ "-newerBB",	c_newer,	f_newer,	F_TIME_B | F_TIME2_B },
110157555Sceri	{ "-newerBa",	c_newer,	f_newer,	F_TIME_B | F_TIME2_A },
111157555Sceri	{ "-newerBc",	c_newer,	f_newer,	F_TIME_B | F_TIME2_C },
112157555Sceri	{ "-newerBm",	c_newer,	f_newer,	F_TIME_B },
113157555Sceri	{ "-newerBt",	c_newer,	f_newer,	F_TIME_B | F_TIME2_T },
114157555Sceri	{ "-neweraB",	c_newer,	f_newer,	F_TIME_A | F_TIME2_B },
11576250Sphk	{ "-neweraa",	c_newer,	f_newer,	F_TIME_A | F_TIME2_A },
11676250Sphk	{ "-newerac",	c_newer,	f_newer,	F_TIME_A | F_TIME2_C },
11776250Sphk	{ "-neweram",	c_newer,	f_newer,	F_TIME_A },
11876250Sphk	{ "-newerat",	c_newer,	f_newer,	F_TIME_A | F_TIME2_T },
119157555Sceri	{ "-newercB",	c_newer,	f_newer,	F_TIME_C | F_TIME2_B },
12076250Sphk	{ "-newerca",	c_newer,	f_newer,	F_TIME_C | F_TIME2_A },
12176250Sphk	{ "-newercc",	c_newer,	f_newer,	F_TIME_C | F_TIME2_C },
12276250Sphk	{ "-newercm",	c_newer,	f_newer,	F_TIME_C },
12376250Sphk	{ "-newerct",	c_newer,	f_newer,	F_TIME_C | F_TIME2_T },
124157555Sceri	{ "-newermB",	c_newer,	f_newer,	F_TIME2_B },
12576250Sphk	{ "-newerma",	c_newer,	f_newer,	F_TIME2_A },
12676250Sphk	{ "-newermc",	c_newer,	f_newer,	F_TIME2_C },
12776250Sphk	{ "-newermm",	c_newer,	f_newer,	0 },
12876250Sphk	{ "-newermt",	c_newer,	f_newer,	F_TIME2_T },
12976250Sphk	{ "-nogroup",	c_nogroup,	f_nogroup,	0 },
130238780Sjilles	{ "-noignore_readdir_race",c_ignore_readdir_race, f_always_true,0 },
131176478Simp	{ "-noleaf",	c_simple,	f_always_true,	0 },
132111084Sdes	{ "-not",	c_simple,	f_not,		0 },
13376250Sphk	{ "-nouser",	c_nouser,	f_nouser,	0 },
13476250Sphk	{ "-o",		c_simple,	f_or,		0 },
13576250Sphk	{ "-ok",	c_exec,		f_exec,		F_NEEDOK },
13676250Sphk	{ "-okdir",	c_exec,		f_exec,		F_NEEDOK | F_EXECDIR },
13776250Sphk	{ "-or",	c_simple,	f_or,		0 },
13876250Sphk	{ "-path", 	c_name,		f_path,		0 },
13976250Sphk	{ "-perm",	c_perm,		f_perm,		0 },
14076250Sphk	{ "-print",	c_print,	f_print,	0 },
14176250Sphk	{ "-print0",	c_print,	f_print0,	0 },
142176478Simp// -printf
14376250Sphk	{ "-prune",	c_simple,	f_prune,	0 },
144176478Simp	{ "-quit",	c_simple,	f_quit,		0 },
14576250Sphk	{ "-regex",	c_regex,	f_regex,	0 },
146176478Simp	{ "-samefile",	c_samefile,	f_inum,		0 },
14776250Sphk	{ "-size",	c_size,		f_size,		0 },
148247730Sdwmalone	{ "-sparse",	c_sparse,	f_sparse,	0 },
149176478Simp	{ "-true",	c_simple,	f_always_true,	0 },
15076250Sphk	{ "-type",	c_type,		f_type,		0 },
151176478Simp	{ "-uid",	c_user,		f_user,		0 },
15276250Sphk	{ "-user",	c_user,		f_user,		0 },
153176478Simp	{ "-wholename",	c_name,		f_path,		0 },
15476250Sphk	{ "-xdev",	c_xdev,		f_always_true,	0 },
155176478Simp// -xtype
1561590Srgrimes};
1571590Srgrimes
1581590Srgrimes/*
1591590Srgrimes * find_create --
1601590Srgrimes *	create a node corresponding to a command line argument.
1611590Srgrimes *
1621590Srgrimes * TODO:
1631590Srgrimes *	add create/process function pointers to node, so we can skip
1641590Srgrimes *	this switch stuff.
1651590Srgrimes */
1661590SrgrimesPLAN *
167116333Smarkmfind_create(char ***argvp)
1681590Srgrimes{
16991400Sdwmalone	OPTION *p;
1701590Srgrimes	PLAN *new;
1711590Srgrimes	char **argv;
1721590Srgrimes
1731590Srgrimes	argv = *argvp;
1741590Srgrimes
17591400Sdwmalone	if ((p = lookup_option(*argv)) == NULL)
176222390Sjilles		errx(1, "%s: unknown primary or operator", *argv);
1771590Srgrimes	++argv;
1781590Srgrimes
17976250Sphk	new = (p->create)(p, &argv);
1801590Srgrimes	*argvp = argv;
1811590Srgrimes	return (new);
1821590Srgrimes}
1831590Srgrimes
18476250SphkOPTION *
185116333Smarkmlookup_option(const char *name)
1861590Srgrimes{
1871590Srgrimes	OPTION tmp;
1881590Srgrimes
1891590Srgrimes	tmp.name = name;
1901590Srgrimes	return ((OPTION *)bsearch(&tmp, options,
1911590Srgrimes	    sizeof(options)/sizeof(OPTION), sizeof(OPTION), typecompare));
1921590Srgrimes}
1931590Srgrimes
194207705Sdelphijstatic int
195116333Smarkmtypecompare(const void *a, const void *b)
1961590Srgrimes{
19791400Sdwmalone	return (strcmp(((const OPTION *)a)->name, ((const OPTION *)b)->name));
1981590Srgrimes}
199