main.c revision 200462
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
3861575Srobertochar copyright[] =
3923695Speter"@(#) Copyright (c) 1990, 1993, 1994\n\
4061575Sroberto	The Regents of the University of California.  All rights reserved.\n";
4161575Sroberto#endif /* not lint */
421590Srgrimes
4393604Sobrien#ifndef lint
4493604Sobrien#if 0
451590Srgrimesstatic char sccsid[] = "@(#)main.c	8.4 (Berkeley) 5/4/95";
461590Srgrimes#endif
471590Srgrimes#endif /* not lint */
481590Srgrimes
491590Srgrimes#include <sys/cdefs.h>
501590Srgrimes__FBSDID("$FreeBSD: head/usr.bin/find/main.c 200462 2009-12-13 03:14:06Z delphij $");
511590Srgrimes
5272945Sknu#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>
5992786Smarkm#include <locale.h>
6041391Swosch#include <regex.h>
611590Srgrimes#include <stdio.h>
6241398Sbde#include <stdlib.h>
6341398Sbde#include <time.h>
6441399Sbde#include <unistd.h>
6541399Sbde
6641398Sbde#include "find.h"
6741398Sbde
6841398Sbdetime_t now;			/* time find was run */
6941398Sbdeint dotfd;			/* starting directory */
7041398Sbdeint ftsoptions;			/* options for the ftsopen(3) call */
7141398Sbdeint isdeprecated;		/* using deprecated syntax */
7241398Sbdeint isdepth;			/* do directories on post-order visit */
7341398Sbdeint isoutput;			/* user specified output operator */
7441398Sbdeint issort;         		/* do hierarchies in lexicographical order */
7541398Sbdeint isxargs;			/* don't permit xargs delimiting chars */
761590Srgrimesint mindepth = -1, maxdepth = -1; /* minimum and maximum depth */
771590Srgrimesint regexp_flags = REG_BASIC;	/* use the "basic" regexp by default*/
781590Srgrimes
791590Srgrimesstatic void usage(void);
801590Srgrimes
811590Srgrimesint
821590Srgrimesmain(int argc, char *argv[])
831590Srgrimes{
841590Srgrimes	char **p, **start;
851590Srgrimes	int Hflag, Lflag, ch;
861590Srgrimes
871590Srgrimes	(void)setlocale(LC_ALL, "");
881590Srgrimes
891590Srgrimes	(void)time(&now);	/* initialize the time-of-day */
901590Srgrimes
911590Srgrimes	p = start = argv;
921590Srgrimes	Hflag = Lflag = 0;
931590Srgrimes	ftsoptions = FTS_NOSTAT | FTS_PHYSICAL;
941590Srgrimes	while ((ch = getopt(argc, argv, "EHLPXdf:sx")) != -1)
951590Srgrimes		switch (ch) {
961590Srgrimes		case 'E':
971590Srgrimes			regexp_flags |= REG_EXTENDED;
981590Srgrimes			break;
991590Srgrimes		case 'H':
1001590Srgrimes			Hflag = 1;
1011590Srgrimes			Lflag = 0;
1021590Srgrimes			break;
1031590Srgrimes		case 'L':
1041590Srgrimes			Lflag = 1;
1051590Srgrimes			Hflag = 0;
1061590Srgrimes			break;
1071590Srgrimes		case 'P':
1081590Srgrimes			Hflag = Lflag = 0;
1091590Srgrimes			break;
1101590Srgrimes		case 'X':
1111590Srgrimes			isxargs = 1;
1128874Srgrimes			break;
1131590Srgrimes		case 'd':
1141590Srgrimes			isdepth = 1;
11523695Speter			break;
11623695Speter		case 'f':
1171590Srgrimes			*p++ = optarg;
1181590Srgrimes			break;
11976250Sphk		case 's':
12091400Sdwmalone			issort = 1;
12176250Sphk			break;
12223695Speter		case 'x':
12391400Sdwmalone			ftsoptions |= FTS_XDEV;
12491400Sdwmalone			break;
1251590Srgrimes		case '?':
12623695Speter		default:
12791400Sdwmalone			break;
12891400Sdwmalone		}
1296776Sguido
1306776Sguido	argc -= optind;
13191400Sdwmalone	argv += optind;
13291400Sdwmalone
1331590Srgrimes	if (Hflag)
1341590Srgrimes		ftsoptions |= FTS_COMFOLLOW;
13591400Sdwmalone	if (Lflag) {
13691400Sdwmalone		ftsoptions &= ~FTS_PHYSICAL;
1376776Sguido		ftsoptions |= FTS_LOGICAL;
1386776Sguido	}
1391590Srgrimes
1401590Srgrimes	/*
1418874Srgrimes	 * Find first option to delimit the file list.  The first argument
1421590Srgrimes	 * that starts with a -, or is a ! or a ( must be interpreted as a
1431590Srgrimes	 * part of the find expression, according to POSIX .2.
1441590Srgrimes	 */
1451590Srgrimes	for (; *argv != NULL; *p++ = *argv++) {
1461590Srgrimes		if (argv[0][0] == '-')
1471590Srgrimes			break;
1481590Srgrimes		if ((argv[0][0] == '!' || argv[0][0] == '(') &&
1491590Srgrimes		    argv[0][1] == '\0')
1501590Srgrimes			break;
1511590Srgrimes	}
1521590Srgrimes
1531590Srgrimes	if (p == start)
1541590Srgrimes		usage();
1551590Srgrimes	*p = NULL;
1561590Srgrimes
1571590Srgrimes	if ((dotfd = open(".", O_RDONLY, 0)) < 0)
1581590Srgrimes		err(1, ".");
1591590Srgrimes
1601590Srgrimes	exit(find_execute(find_formplan(argv), start));
1611590Srgrimes}
1621590Srgrimes
1631590Srgrimesstatic void
1641590Srgrimesusage(void)
1651590Srgrimes{
1661590Srgrimes	(void)fprintf(stderr, "%s\n%s\n",
1671590Srgrimes"usage: find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression]",
1681590Srgrimes"       find [-H | -L | -P] [-EXdsx] -f path [path ...] [expression]");
1691590Srgrimes	exit(1);
1708874Srgrimes}
1711590Srgrimes