main.c revision 222697
169783Smsmith/*-
269783Smsmith * Copyright (c) 1990, 1993, 1994
369783Smsmith *	The Regents of the University of California.  All rights reserved.
469783Smsmith *
569783Smsmith * This code is derived from software contributed to Berkeley by
669783Smsmith * Cimarron D. Taylor of the University of California, Berkeley.
769783Smsmith *
869783Smsmith * Redistribution and use in source and binary forms, with or without
969783Smsmith * modification, are permitted provided that the following conditions
1069783Smsmith * are met:
1169783Smsmith * 1. Redistributions of source code must retain the above copyright
1269783Smsmith *    notice, this list of conditions and the following disclaimer.
1369783Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1469783Smsmith *    notice, this list of conditions and the following disclaimer in the
1569783Smsmith *    documentation and/or other materials provided with the distribution.
1669783Smsmith * 4. Neither the name of the University nor the names of its contributors
1769783Smsmith *    may be used to endorse or promote products derived from this software
1869783Smsmith *    without specific prior written permission.
1969783Smsmith *
2069783Smsmith * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2169783Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2269783Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2369783Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2469783Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2569783Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2669783Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2769783Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2869783Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2969783Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3069783Smsmith * SUCH DAMAGE.
31119418Sobrien */
32119418Sobrien
33119418Sobrien#ifndef lint
3469783Smsmithchar copyright[] =
3569783Smsmith"@(#) Copyright (c) 1990, 1993, 1994\n\
3669783Smsmith	The Regents of the University of California.  All rights reserved.\n";
3769783Smsmith#endif /* not lint */
3869783Smsmith
3969783Smsmith#ifndef lint
4069783Smsmith#if 0
41129876Sphkstatic char sccsid[] = "@(#)main.c	8.4 (Berkeley) 5/4/95";
4269783Smsmith#endif
43107546Simp#endif /* not lint */
44107546Simp
45106844Smdodd#include <sys/cdefs.h>
4669783Smsmith__FBSDID("$FreeBSD: head/usr.bin/find/main.c 222697 2011-06-04 21:59:55Z jilles $");
4769783Smsmith
4869783Smsmith#include <sys/types.h>
49119285Simp#include <sys/stat.h>
50119285Simp
51119285Simp#include <err.h>
5269783Smsmith#include <errno.h>
5369783Smsmith#include <fcntl.h>
5469783Smsmith#include <fts.h>
5569783Smsmith#include <locale.h>
5669783Smsmith#include <regex.h>
5769783Smsmith#include <stdio.h>
5869783Smsmith#include <stdlib.h>
5969783Smsmith#include <time.h>
6069783Smsmith#include <unistd.h>
61145661Simp
6269783Smsmith#include "find.h"
6369783Smsmith
6469783Smsmithtime_t now;			/* time find was run */
6569783Smsmithint dotfd;			/* starting directory */
6669783Smsmithint ftsoptions;			/* options for the ftsopen(3) call */
6769783Smsmithint isdeprecated;		/* using deprecated syntax */
6869783Smsmithint isdepth;			/* do directories on post-order visit */
6969783Smsmithint isoutput;			/* user specified output operator */
7069783Smsmithint issort;         		/* do hierarchies in lexicographical order */
7169783Smsmithint isxargs;			/* don't permit xargs delimiting chars */
7269783Smsmithint mindepth = -1, maxdepth = -1; /* minimum and maximum depth */
7369783Smsmithint regexp_flags = REG_BASIC;	/* use the "basic" regexp by default*/
7469783Smsmith
7569783Smsmithstatic void usage(void);
7669783Smsmith
7769783Smsmithint
7869783Smsmithmain(int argc, char *argv[])
7969783Smsmith{
8069783Smsmith	char **p, **start;
8169783Smsmith	int Hflag, Lflag, ch;
82164264Sjhb
83164264Sjhb	(void)setlocale(LC_ALL, "");
84164264Sjhb
85164264Sjhb	(void)time(&now);	/* initialize the time-of-day */
86169221Sjhb
8769783Smsmith	p = start = argv;
8869783Smsmith	Hflag = Lflag = 0;
8969783Smsmith	ftsoptions = FTS_NOSTAT | FTS_PHYSICAL;
9069783Smsmith	while ((ch = getopt(argc, argv, "EHLPXdf:sx")) != -1)
91154079Sjhb		switch (ch) {
9269783Smsmith		case 'E':
93154079Sjhb			regexp_flags |= REG_EXTENDED;
9469783Smsmith			break;
9569783Smsmith		case 'H':
9669783Smsmith			Hflag = 1;
97163805Simp			Lflag = 0;
98163805Simp			break;
99163805Simp		case 'L':
100163805Simp			Lflag = 1;
101163805Simp			Hflag = 0;
102163805Simp			break;
103163805Simp		case 'P':
104163805Simp			Hflag = Lflag = 0;
105163805Simp			break;
106163805Simp		case 'X':
107163805Simp			isxargs = 1;
108163805Simp			break;
109163805Simp		case 'd':
110163805Simp			isdepth = 1;
111163805Simp			break;
112163805Simp		case 'f':
113163805Simp			*p++ = optarg;
114163805Simp			break;
115163805Simp		case 's':
116163805Simp			issort = 1;
117163805Simp			break;
118163805Simp		case 'x':
119163805Simp			ftsoptions |= FTS_XDEV;
120163805Simp			break;
121163805Simp		case '?':
122163805Simp		default:
123163805Simp			usage();
12469783Smsmith		}
12569783Smsmith
12669783Smsmith	argc -= optind;
12769783Smsmith	argv += optind;
12869783Smsmith
12969783Smsmith	if (Hflag)
13069783Smsmith		ftsoptions |= FTS_COMFOLLOW;
13169783Smsmith	if (Lflag) {
13269783Smsmith		ftsoptions &= ~FTS_PHYSICAL;
13369783Smsmith		ftsoptions |= FTS_LOGICAL;
13469783Smsmith	}
13569783Smsmith
13669783Smsmith	/*
137102441Sjhb	 * Find first option to delimit the file list.  The first argument
138102441Sjhb	 * that starts with a -, or is a ! or a ( must be interpreted as a
13969783Smsmith	 * part of the find expression, according to POSIX .2.
14069783Smsmith	 */
141119266Simp	for (; *argv != NULL; *p++ = *argv++) {
142181789Simp		if (argv[0][0] == '-')
143181789Simp			break;
14469783Smsmith		if ((argv[0][0] == '!' || argv[0][0] == '(') &&
14569783Smsmith		    argv[0][1] == '\0')
14669783Smsmith			break;
14769783Smsmith	}
14869908Smsmith
14969908Smsmith	if (p == start)
15069908Smsmith		usage();
15169953Smsmith	*p = NULL;
152172394Smarius
153181789Simp	if ((dotfd = open(".", O_RDONLY, 0)) < 0)
15469908Smsmith		err(1, ".");
15569908Smsmith
15669908Smsmith	exit(find_execute(find_formplan(argv), start));
15769908Smsmith}
15869908Smsmith
15969783Smsmithstatic void
16069908Smsmithusage(void)
161181789Simp{
162181789Simp	(void)fprintf(stderr, "%s\n%s\n",
163181789Simp"usage: find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression]",
164181789Simp"       find [-H | -L | -P] [-EXdsx] -f path [path ...] [expression]");
165181789Simp	exit(1);
166181789Simp}
167181789Simp