main.c revision 92786
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 * 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 */
421590Srgrimes
431590Srgrimes#ifndef lint
4461575Sroberto#if 0
4523695Speterstatic char sccsid[] = "@(#)main.c	8.4 (Berkeley) 5/4/95";
4661575Sroberto#else
4761575Srobertostatic const char rcsid[] =
4861575Sroberto  "$FreeBSD: head/usr.bin/find/main.c 92786 2002-03-20 10:32:05Z markm $";
4961575Sroberto#endif
501590Srgrimes#endif /* not lint */
511590Srgrimes
521590Srgrimes#include <sys/types.h>
531590Srgrimes#include <sys/stat.h>
541590Srgrimes
551590Srgrimes#include <err.h>
561590Srgrimes#include <errno.h>
571590Srgrimes#include <fcntl.h>
581590Srgrimes#include <fts.h>
5917534Sache#include <locale.h>
6072945Sknu#include <regex.h>
611590Srgrimes#include <stdio.h>
621590Srgrimes#include <stdlib.h>
631590Srgrimes#include <time.h>
6423695Speter#include <unistd.h>
651590Srgrimes
661590Srgrimes#include "find.h"
671590Srgrimes
681590Srgrimestime_t now;			/* time find was run */
691590Srgrimesint dotfd;			/* starting directory */
701590Srgrimesint ftsoptions;			/* options for the ftsopen(3) call */
711590Srgrimesint isdeprecated;		/* using deprecated syntax */
721590Srgrimesint isdepth;			/* do directories on post-order visit */
731590Srgrimesint isoutput;			/* user specified output operator */
7441399Sbdeint issort;         		/* do hierarchies in lexicographical order */
751590Srgrimesint isxargs;			/* don't permit xargs delimiting chars */
7661575Srobertoint mindepth = -1, maxdepth = -1; /* minimum and maximum depth */
7772945Sknuint regexp_flags = REG_BASIC;	/* use the "basic" regexp by default*/
781590Srgrimes
7992786Smarkmstatic void usage(void);
801590Srgrimes
811590Srgrimesint
821590Srgrimesmain(argc, argv)
831590Srgrimes	int argc;
841590Srgrimes	char *argv[];
851590Srgrimes{
8691400Sdwmalone	char **p, **start;
8725942Sjdp	int Hflag, Lflag, ch;
881590Srgrimes
8917534Sache	(void)setlocale(LC_ALL, "");
9017534Sache
911590Srgrimes	(void)time(&now);	/* initialize the time-of-day */
921590Srgrimes
931590Srgrimes	p = start = argv;
9425942Sjdp	Hflag = Lflag = 0;
951590Srgrimes	ftsoptions = FTS_NOSTAT | FTS_PHYSICAL;
9672945Sknu	while ((ch = getopt(argc, argv, "EHLPXdf:sx")) != -1)
971590Srgrimes		switch (ch) {
9872945Sknu		case 'E':
9972945Sknu			regexp_flags |= REG_EXTENDED;
10072945Sknu			break;
1011590Srgrimes		case 'H':
1021590Srgrimes			Hflag = 1;
10325942Sjdp			Lflag = 0;
1041590Srgrimes			break;
1051590Srgrimes		case 'L':
1061590Srgrimes			Lflag = 1;
10725942Sjdp			Hflag = 0;
1081590Srgrimes			break;
1091590Srgrimes		case 'P':
1101590Srgrimes			Hflag = Lflag = 0;
1111590Srgrimes			break;
1121590Srgrimes		case 'X':
1131590Srgrimes			isxargs = 1;
1141590Srgrimes			break;
1151590Srgrimes		case 'd':
1161590Srgrimes			isdepth = 1;
1171590Srgrimes			break;
1181590Srgrimes		case 'f':
1191590Srgrimes			*p++ = optarg;
1201590Srgrimes			break;
12141391Swosch		case 's':
12241391Swosch			issort = 1;
12341391Swosch			break;
1241590Srgrimes		case 'x':
1251590Srgrimes			ftsoptions |= FTS_XDEV;
1261590Srgrimes			break;
1271590Srgrimes		case '?':
1281590Srgrimes		default:
1291590Srgrimes			break;
1301590Srgrimes		}
1311590Srgrimes
1328874Srgrimes	argc -= optind;
1331590Srgrimes	argv += optind;
1341590Srgrimes
1351590Srgrimes	if (Hflag)
1361590Srgrimes		ftsoptions |= FTS_COMFOLLOW;
1371590Srgrimes	if (Lflag) {
1381590Srgrimes		ftsoptions &= ~FTS_PHYSICAL;
1391590Srgrimes		ftsoptions |= FTS_LOGICAL;
1401590Srgrimes	}
1411590Srgrimes
1421590Srgrimes	/*
1431590Srgrimes	 * 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
1451590Srgrimes	 * 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
1591590Srgrimes	if ((dotfd = open(".", O_RDONLY, 0)) < 0)
1601590Srgrimes		err(1, ".");
1611590Srgrimes
1621590Srgrimes	exit(find_execute(find_formplan(argv), start));
1631590Srgrimes}
1641590Srgrimes
1651590Srgrimesstatic void
1661590Srgrimesusage()
1671590Srgrimes{
1681590Srgrimes	(void)fprintf(stderr,
16972945Sknu"usage: find [-H | -L | -P] [-EXdsx] [-f file] [file ...] [expression]\n");
1701590Srgrimes	exit(1);
1711590Srgrimes}
172