main.c revision 91400
11590Srgrimes/*-
21590Srgrimes * Copyright (c) 1990, 1993, 1994
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
496196Sdes *
596196Sdes * This code is derived from software contributed to Berkeley by
61590Srgrimes * Cimarron D. Taylor of the University of California, Berkeley.
796196Sdes *
896196Sdes * Redistribution and use in source and binary forms, with or without
996196Sdes * modification, are permitted provided that the following conditions
1096196Sdes * are met:
1196196Sdes * 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 * 3. All advertising materials mentioning features or use of this software
171590Srgrimes *    must display the following acknowledgement:
181590Srgrimes *	This product includes software developed by the University of
191590Srgrimes *	California, Berkeley and its contributors.
201590Srgrimes * 4. Neither the name of the University nor the names of its contributors
211590Srgrimes *    may be used to endorse or promote products derived from this software
221590Srgrimes *    without specific prior written permission.
231590Srgrimes *
241590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341590Srgrimes * SUCH DAMAGE.
351590Srgrimes */
361590Srgrimes
371590Srgrimes#ifndef lint
381590Srgrimeschar copyright[] =
391590Srgrimes"@(#) Copyright (c) 1990, 1993, 1994\n\
401590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
411590Srgrimes#endif /* not lint */
4227919Scharnier
431590Srgrimes#ifndef lint
441590Srgrimes#if 0
451590Srgrimesstatic char sccsid[] = "@(#)main.c	8.4 (Berkeley) 5/4/95";
461590Srgrimes#else
47105235Scharnierstatic const char rcsid[] =
481590Srgrimes  "$FreeBSD: head/usr.bin/find/main.c 91400 2002-02-27 17:57:00Z dwmalone $";
4929922Smarkm#endif
501590Srgrimes#endif /* not lint */
51105235Scharnier
521590Srgrimes#include <sys/types.h>
53105235Scharnier#include <sys/stat.h>
54105235Scharnier
55105235Scharnier#include <err.h>
561590Srgrimes#include <errno.h>
571590Srgrimes#include <fcntl.h>
581590Srgrimes#include <fts.h>
5995621Smarkm#include <locale.h>
601590Srgrimes#include <regex.h>
61120547Stjr#include <stdio.h>
621590Srgrimes#include <stdlib.h>
631590Srgrimes#include <time.h>
641590Srgrimes#include <unistd.h>
651590Srgrimes
661590Srgrimes#include "find.h"
671590Srgrimes
681590Srgrimestime_t now;			/* time find was run */
691590Srgrimesint dotfd;			/* starting directory */
708232Sdgint ftsoptions;			/* options for the ftsopen(3) call */
711590Srgrimesint isdeprecated;		/* using deprecated syntax */
7227919Scharnierint isdepth;			/* do directories on post-order visit */
731590Srgrimesint isoutput;			/* user specified output operator */
741590Srgrimesint issort;         		/* do hierarchies in lexicographical order */
751590Srgrimesint isxargs;			/* don't permit xargs delimiting chars */
76200462Sdelphijint mindepth = -1, maxdepth = -1; /* minimum and maximum depth */
771590Srgrimesint regexp_flags = REG_BASIC;	/* use the "basic" regexp by default*/
781590Srgrimes
79120547Stjrstatic void usage __P((void));
801590Srgrimes
811590Srgrimesint
821590Srgrimesmain(argc, argv)
831590Srgrimes	int argc;
841590Srgrimes	char *argv[];
851590Srgrimes{
861590Srgrimes	char **p, **start;
871590Srgrimes	int Hflag, Lflag, ch;
881590Srgrimes
891590Srgrimes	(void)setlocale(LC_ALL, "");
901590Srgrimes
911590Srgrimes	(void)time(&now);	/* initialize the time-of-day */
921590Srgrimes
931590Srgrimes	p = start = argv;
941590Srgrimes	Hflag = Lflag = 0;
95120547Stjr	ftsoptions = FTS_NOSTAT | FTS_PHYSICAL;
96120547Stjr	while ((ch = getopt(argc, argv, "EHLPXdf:sx")) != -1)
97120547Stjr		switch (ch) {
9857232Sshin		case 'E':
991590Srgrimes			regexp_flags |= REG_EXTENDED;
1001590Srgrimes			break;
1011590Srgrimes		case 'H':
1021590Srgrimes			Hflag = 1;
1031590Srgrimes			Lflag = 0;
1041590Srgrimes			break;
1051590Srgrimes		case 'L':
10692921Simp			Lflag = 1;
10792921Simp			Hflag = 0;
10892921Simp			break;
10992921Simp		case 'P':
11092921Simp			Hflag = Lflag = 0;
111105268Smarkm			break;
11292921Simp		case 'X':
11392921Simp			isxargs = 1;
11495621Smarkm			break;
11592921Simp		case 'd':
11692921Simp			isdepth = 1;
11792921Simp			break;
11892921Simp		case 'f':
11992921Simp			*p++ = optarg;
12092921Simp			break;
12192921Simp		case 's':
12292921Simp			issort = 1;
12392921Simp			break;
1241590Srgrimes		case 'x':
1251590Srgrimes			ftsoptions |= FTS_XDEV;
12693057Simp			break;
1271590Srgrimes		case '?':
1281590Srgrimes		default:
1291590Srgrimes			break;
130120547Stjr		}
1311590Srgrimes
132105268Smarkm	argc -= optind;
133105268Smarkm	argv += optind;
13447549Sbde
135120547Stjr	if (Hflag)
13696196Sdes		ftsoptions |= FTS_COMFOLLOW;
137148726Sstefanf	if (Lflag) {
138152397Sdwmalone		ftsoptions &= ~FTS_PHYSICAL;
139152397Sdwmalone		ftsoptions |= FTS_LOGICAL;
1401590Srgrimes	}
1418232Sdg
1421590Srgrimes	/*
14347549Sbde	 * Find first option to delimit the file list.  The first argument
1441590Srgrimes	 * that starts with a -, or is a ! or a ( must be interpreted as a
14529922Smarkm	 * part of the find expression, according to POSIX .2.
1461590Srgrimes	 */
1471590Srgrimes	for (; *argv != NULL; *p++ = *argv++) {
1481590Srgrimes		if (argv[0][0] == '-')
1491590Srgrimes			break;
1501590Srgrimes		if ((argv[0][0] == '!' || argv[0][0] == '(') &&
1511590Srgrimes		    argv[0][1] == '\0')
1521590Srgrimes			break;
1531590Srgrimes	}
1541590Srgrimes
1551590Srgrimes	if (p == start)
1561590Srgrimes		usage();
1571590Srgrimes	*p = NULL;
1581590Srgrimes
159140569Sru	if ((dotfd = open(".", O_RDONLY, 0)) < 0)
16024360Simp		err(1, ".");
1611590Srgrimes
16257232Sshin	exit(find_execute(find_formplan(argv), start));
16357232Sshin}
16457232Sshin
16557232Sshinstatic void
16657232Sshinusage()
16757232Sshin{
16857232Sshin	(void)fprintf(stderr,
16957232Sshin"usage: find [-H | -L | -P] [-EXdsx] [-f file] [file ...] [expression]\n");
1701590Srgrimes	exit(1);
1711590Srgrimes}
1721590Srgrimes