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
331590Srgrimes#ifndef lint
34228394Sedstatic const char copyright[] =
351590Srgrimes"@(#) Copyright (c) 1990, 1993, 1994\n\
361590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
371590Srgrimes#endif /* not lint */
381590Srgrimes
39207705Sdelphij#ifndef lint
40207705Sdelphij#if 0
41207705Sdelphijstatic char sccsid[] = "@(#)main.c	8.4 (Berkeley) 5/4/95";
42207705Sdelphij#endif
43207705Sdelphij#endif /* not lint */
44207705Sdelphij
4593604Sobrien#include <sys/cdefs.h>
4693604Sobrien__FBSDID("$FreeBSD$");
471590Srgrimes
481590Srgrimes#include <sys/types.h>
491590Srgrimes#include <sys/stat.h>
501590Srgrimes
511590Srgrimes#include <err.h>
52200462Sdelphij#include <errno.h>
531590Srgrimes#include <fcntl.h>
541590Srgrimes#include <fts.h>
5517534Sache#include <locale.h>
5672945Sknu#include <regex.h>
571590Srgrimes#include <stdio.h>
581590Srgrimes#include <stdlib.h>
591590Srgrimes#include <time.h>
6023695Speter#include <unistd.h>
611590Srgrimes
621590Srgrimes#include "find.h"
631590Srgrimes
641590Srgrimestime_t now;			/* time find was run */
651590Srgrimesint dotfd;			/* starting directory */
661590Srgrimesint ftsoptions;			/* options for the ftsopen(3) call */
67238948Sjillesint ignore_readdir_race;	/* ignore readdir race */
681590Srgrimesint isdeprecated;		/* using deprecated syntax */
691590Srgrimesint isdepth;			/* do directories on post-order visit */
701590Srgrimesint isoutput;			/* user specified output operator */
7141399Sbdeint issort;         		/* do hierarchies in lexicographical order */
721590Srgrimesint isxargs;			/* don't permit xargs delimiting chars */
7361575Srobertoint mindepth = -1, maxdepth = -1; /* minimum and maximum depth */
7472945Sknuint regexp_flags = REG_BASIC;	/* use the "basic" regexp by default*/
75264387Sjillesint exitstatus;
761590Srgrimes
7792786Smarkmstatic void usage(void);
781590Srgrimes
791590Srgrimesint
80116333Smarkmmain(int argc, char *argv[])
811590Srgrimes{
8291400Sdwmalone	char **p, **start;
8325942Sjdp	int Hflag, Lflag, ch;
841590Srgrimes
8517534Sache	(void)setlocale(LC_ALL, "");
8617534Sache
871590Srgrimes	(void)time(&now);	/* initialize the time-of-day */
881590Srgrimes
891590Srgrimes	p = start = argv;
9025942Sjdp	Hflag = Lflag = 0;
911590Srgrimes	ftsoptions = FTS_NOSTAT | FTS_PHYSICAL;
9272945Sknu	while ((ch = getopt(argc, argv, "EHLPXdf:sx")) != -1)
931590Srgrimes		switch (ch) {
9472945Sknu		case 'E':
9572945Sknu			regexp_flags |= REG_EXTENDED;
9672945Sknu			break;
971590Srgrimes		case 'H':
981590Srgrimes			Hflag = 1;
9925942Sjdp			Lflag = 0;
1001590Srgrimes			break;
1011590Srgrimes		case 'L':
1021590Srgrimes			Lflag = 1;
10325942Sjdp			Hflag = 0;
1041590Srgrimes			break;
1051590Srgrimes		case 'P':
1061590Srgrimes			Hflag = Lflag = 0;
1071590Srgrimes			break;
1081590Srgrimes		case 'X':
1091590Srgrimes			isxargs = 1;
1101590Srgrimes			break;
1111590Srgrimes		case 'd':
1121590Srgrimes			isdepth = 1;
1131590Srgrimes			break;
1141590Srgrimes		case 'f':
1151590Srgrimes			*p++ = optarg;
1161590Srgrimes			break;
11741391Swosch		case 's':
11841391Swosch			issort = 1;
11941391Swosch			break;
1201590Srgrimes		case 'x':
1211590Srgrimes			ftsoptions |= FTS_XDEV;
1221590Srgrimes			break;
1231590Srgrimes		case '?':
1241590Srgrimes		default:
125222697Sjilles			usage();
1261590Srgrimes		}
1271590Srgrimes
1288874Srgrimes	argc -= optind;
1291590Srgrimes	argv += optind;
1301590Srgrimes
1311590Srgrimes	if (Hflag)
1321590Srgrimes		ftsoptions |= FTS_COMFOLLOW;
1331590Srgrimes	if (Lflag) {
1341590Srgrimes		ftsoptions &= ~FTS_PHYSICAL;
1351590Srgrimes		ftsoptions |= FTS_LOGICAL;
1361590Srgrimes	}
1371590Srgrimes
1381590Srgrimes	/*
1391590Srgrimes	 * Find first option to delimit the file list.  The first argument
1401590Srgrimes	 * that starts with a -, or is a ! or a ( must be interpreted as a
1411590Srgrimes	 * part of the find expression, according to POSIX .2.
1421590Srgrimes	 */
1431590Srgrimes	for (; *argv != NULL; *p++ = *argv++) {
1441590Srgrimes		if (argv[0][0] == '-')
1451590Srgrimes			break;
1461590Srgrimes		if ((argv[0][0] == '!' || argv[0][0] == '(') &&
1471590Srgrimes		    argv[0][1] == '\0')
1481590Srgrimes			break;
1491590Srgrimes	}
1501590Srgrimes
1511590Srgrimes	if (p == start)
1521590Srgrimes		usage();
1531590Srgrimes	*p = NULL;
1541590Srgrimes
155240973Sjilles	if ((dotfd = open(".", O_RDONLY | O_CLOEXEC, 0)) < 0)
156246628Sjilles		ftsoptions |= FTS_NOCHDIR;
1571590Srgrimes
1581590Srgrimes	exit(find_execute(find_formplan(argv), start));
1591590Srgrimes}
1601590Srgrimes
1611590Srgrimesstatic void
162116333Smarkmusage(void)
1631590Srgrimes{
164176761Sru	(void)fprintf(stderr, "%s\n%s\n",
165176761Sru"usage: find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression]",
166176761Sru"       find [-H | -L | -P] [-EXdsx] -f path [path ...] [expression]");
1671590Srgrimes	exit(1);
1681590Srgrimes}
169