main.c revision 116333
1204076Spjd/*-
2204076Spjd * Copyright (c) 1990, 1993, 1994
3204076Spjd *	The Regents of the University of California.  All rights reserved.
4204076Spjd *
5204076Spjd * This code is derived from software contributed to Berkeley by
6204076Spjd * Cimarron D. Taylor of the University of California, Berkeley.
7204076Spjd *
8204076Spjd * Redistribution and use in source and binary forms, with or without
9204076Spjd * modification, are permitted provided that the following conditions
10204076Spjd * are met:
11204076Spjd * 1. Redistributions of source code must retain the above copyright
12204076Spjd *    notice, this list of conditions and the following disclaimer.
13204076Spjd * 2. Redistributions in binary form must reproduce the above copyright
14204076Spjd *    notice, this list of conditions and the following disclaimer in the
15204076Spjd *    documentation and/or other materials provided with the distribution.
16204076Spjd * 3. All advertising materials mentioning features or use of this software
17204076Spjd *    must display the following acknowledgement:
18204076Spjd *	This product includes software developed by the University of
19204076Spjd *	California, Berkeley and its contributors.
20204076Spjd * 4. Neither the name of the University nor the names of its contributors
21204076Spjd *    may be used to endorse or promote products derived from this software
22204076Spjd *    without specific prior written permission.
23204076Spjd *
24204076Spjd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25204076Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26204076Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27204076Spjd * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28204076Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29204076Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30204076Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31204076Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32204076Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33204076Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34204076Spjd * SUCH DAMAGE.
35204076Spjd */
36219620Strociny
37204076Spjd#ifndef lint
38204076Spjdchar copyright[] =
39204076Spjd"@(#) Copyright (c) 1990, 1993, 1994\n\
40204076Spjd	The Regents of the University of California.  All rights reserved.\n";
41204076Spjd#endif /* not lint */
42204076Spjd
43204076Spjd#ifndef lint
44204076Spjd#if 0
45204076Spjdstatic char sccsid[] = "@(#)main.c	8.4 (Berkeley) 5/4/95";
46204076Spjd#endif
47204076Spjd#endif /* not lint */
48204076Spjd
49204076Spjd#include <sys/cdefs.h>
50204076Spjd__FBSDID("$FreeBSD: head/usr.bin/find/main.c 116333 2003-06-14 13:00:21Z markm $");
51204076Spjd
52204076Spjd#include <sys/types.h>
53204076Spjd#include <sys/stat.h>
54204076Spjd
55204076Spjd#include <err.h>
56204076Spjd#include <errno.h>
57204076Spjd#include <fcntl.h>
58204076Spjd#include <fts.h>
59204076Spjd#include <locale.h>
60204076Spjd#include <regex.h>
61204076Spjd#include <stdio.h>
62204076Spjd#include <stdlib.h>
63251025Smarck#include <time.h>
64251025Smarck#include <unistd.h>
65204076Spjd
66204076Spjd#include "find.h"
67204076Spjd
68204076Spjdtime_t now;			/* time find was run */
69204076Spjdint dotfd;			/* starting directory */
70204076Spjdint ftsoptions;			/* options for the ftsopen(3) call */
71204076Spjdint isdeprecated;		/* using deprecated syntax */
72204076Spjdint isdepth;			/* do directories on post-order visit */
73204076Spjdint isoutput;			/* user specified output operator */
74204076Spjdint issort;         		/* do hierarchies in lexicographical order */
75204076Spjdint isxargs;			/* don't permit xargs delimiting chars */
76204076Spjdint mindepth = -1, maxdepth = -1; /* minimum and maximum depth */
77204076Spjdint regexp_flags = REG_BASIC;	/* use the "basic" regexp by default*/
78204076Spjd
79251025Smarckstatic void usage(void);
80251025Smarck
81251025Smarckint
82204076Spjdmain(int argc, char *argv[])
83204076Spjd{
84204076Spjd	char **p, **start;
85204076Spjd	int Hflag, Lflag, ch;
86204076Spjd
87204076Spjd	(void)setlocale(LC_ALL, "");
88204076Spjd
89204076Spjd	(void)time(&now);	/* initialize the time-of-day */
90204076Spjd
91204076Spjd	p = start = argv;
92204076Spjd	Hflag = Lflag = 0;
93204076Spjd	ftsoptions = FTS_NOSTAT | FTS_PHYSICAL;
94204076Spjd	while ((ch = getopt(argc, argv, "EHLPXdf:sx")) != -1)
95204076Spjd		switch (ch) {
96204076Spjd		case 'E':
97204076Spjd			regexp_flags |= REG_EXTENDED;
98204076Spjd			break;
99204076Spjd		case 'H':
100204076Spjd			Hflag = 1;
101231017Strociny			Lflag = 0;
102204076Spjd			break;
103204076Spjd		case 'L':
104204076Spjd			Lflag = 1;
105204076Spjd			Hflag = 0;
106204076Spjd			break;
107204076Spjd		case 'P':
108204076Spjd			Hflag = Lflag = 0;
109204076Spjd			break;
110204076Spjd		case 'X':
111204076Spjd			isxargs = 1;
112204076Spjd			break;
113204076Spjd		case 'd':
114204076Spjd			isdepth = 1;
115204076Spjd			break;
116204076Spjd		case 'f':
117204076Spjd			*p++ = optarg;
118204076Spjd			break;
119204076Spjd		case 's':
120204076Spjd			issort = 1;
121204076Spjd			break;
122204076Spjd		case 'x':
123204076Spjd			ftsoptions |= FTS_XDEV;
124204076Spjd			break;
125204076Spjd		case '?':
126204076Spjd		default:
127204076Spjd			break;
128204076Spjd		}
129204076Spjd
130204076Spjd	argc -= optind;
131204076Spjd	argv += optind;
132204076Spjd
133204076Spjd	if (Hflag)
134204076Spjd		ftsoptions |= FTS_COMFOLLOW;
135204076Spjd	if (Lflag) {
136204076Spjd		ftsoptions &= ~FTS_PHYSICAL;
137204076Spjd		ftsoptions |= FTS_LOGICAL;
138204076Spjd	}
139204076Spjd
140204076Spjd	/*
141204076Spjd	 * Find first option to delimit the file list.  The first argument
142204076Spjd	 * that starts with a -, or is a ! or a ( must be interpreted as a
143231017Strociny	 * part of the find expression, according to POSIX .2.
144204076Spjd	 */
145204076Spjd	for (; *argv != NULL; *p++ = *argv++) {
146204076Spjd		if (argv[0][0] == '-')
147204076Spjd			break;
148204076Spjd		if ((argv[0][0] == '!' || argv[0][0] == '(') &&
149204076Spjd		    argv[0][1] == '\0')
150204076Spjd			break;
151204076Spjd	}
152204076Spjd
153204076Spjd	if (p == start)
154204076Spjd		usage();
155204076Spjd	*p = NULL;
156204076Spjd
157204076Spjd	if ((dotfd = open(".", O_RDONLY, 0)) < 0)
158204076Spjd		err(1, ".");
159204076Spjd
160204076Spjd	exit(find_execute(find_formplan(argv), start));
161204076Spjd}
162204076Spjd
163204076Spjdstatic void
164204076Spjdusage(void)
165204076Spjd{
166204076Spjd	(void)fprintf(stderr,
167204076Spjd"usage: find [-H | -L | -P] [-EXdsx] [-f file] [file ...] [expression]\n");
168204076Spjd	exit(1);
169204076Spjd}
170204076Spjd