main.c revision 8874
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
441590Srgrimesstatic char sccsid[] = "@(#)main.c	8.3 (Berkeley) 4/16/94";
451590Srgrimes#endif /* not lint */
461590Srgrimes
471590Srgrimes#include <sys/types.h>
481590Srgrimes#include <sys/stat.h>
491590Srgrimes
501590Srgrimes#include <err.h>
511590Srgrimes#include <errno.h>
521590Srgrimes#include <fcntl.h>
531590Srgrimes#include <fts.h>
541590Srgrimes#include <stdio.h>
551590Srgrimes#include <stdlib.h>
561590Srgrimes#include <time.h>
571590Srgrimes
581590Srgrimes#include "find.h"
591590Srgrimes
601590Srgrimestime_t now;			/* time find was run */
611590Srgrimesint dotfd;			/* starting directory */
621590Srgrimesint ftsoptions;			/* options for the ftsopen(3) call */
631590Srgrimesint isdeprecated;		/* using deprecated syntax */
641590Srgrimesint isdepth;			/* do directories on post-order visit */
651590Srgrimesint isoutput;			/* user specified output operator */
661590Srgrimesint isxargs;			/* don't permit xargs delimiting chars */
671590Srgrimes
681590Srgrimesstatic void usage __P((void));
691590Srgrimes
701590Srgrimesint
711590Srgrimesmain(argc, argv)
721590Srgrimes	int argc;
731590Srgrimes	char *argv[];
741590Srgrimes{
751590Srgrimes	register char **p, **start;
761590Srgrimes	int Hflag, Lflag, Pflag, ch;
771590Srgrimes
781590Srgrimes	(void)time(&now);	/* initialize the time-of-day */
791590Srgrimes
801590Srgrimes	p = start = argv;
811590Srgrimes	Hflag = Lflag = Pflag = 0;
821590Srgrimes	ftsoptions = FTS_NOSTAT | FTS_PHYSICAL;
831590Srgrimes	while ((ch = getopt(argc, argv, "HLPXdf:x")) != EOF)
841590Srgrimes		switch (ch) {
851590Srgrimes		case 'H':
861590Srgrimes			Hflag = 1;
871590Srgrimes			Lflag = Pflag = 0;
881590Srgrimes			break;
891590Srgrimes		case 'L':
901590Srgrimes			Lflag = 1;
911590Srgrimes			Hflag = Pflag = 0;
921590Srgrimes			break;
931590Srgrimes		case 'P':
941590Srgrimes			Pflag = 1;
951590Srgrimes			Hflag = Lflag = 0;
961590Srgrimes			break;
971590Srgrimes		case 'X':
981590Srgrimes			isxargs = 1;
991590Srgrimes			break;
1001590Srgrimes		case 'd':
1011590Srgrimes			isdepth = 1;
1021590Srgrimes			break;
1031590Srgrimes		case 'f':
1041590Srgrimes			*p++ = optarg;
1051590Srgrimes			break;
1061590Srgrimes		case 'x':
1071590Srgrimes			ftsoptions |= FTS_XDEV;
1081590Srgrimes			break;
1091590Srgrimes		case '?':
1101590Srgrimes		default:
1111590Srgrimes			break;
1121590Srgrimes		}
1131590Srgrimes
1148874Srgrimes	argc -= optind;
1151590Srgrimes	argv += optind;
1161590Srgrimes
1171590Srgrimes	if (Hflag)
1181590Srgrimes		ftsoptions |= FTS_COMFOLLOW;
1191590Srgrimes	if (Lflag) {
1201590Srgrimes		ftsoptions &= ~FTS_PHYSICAL;
1211590Srgrimes		ftsoptions |= FTS_LOGICAL;
1221590Srgrimes	}
1231590Srgrimes
1241590Srgrimes	/*
1251590Srgrimes	 * Find first option to delimit the file list.  The first argument
1261590Srgrimes	 * that starts with a -, or is a ! or a ( must be interpreted as a
1271590Srgrimes	 * part of the find expression, according to POSIX .2.
1281590Srgrimes	 */
1291590Srgrimes	for (; *argv != NULL; *p++ = *argv++) {
1301590Srgrimes		if (argv[0][0] == '-')
1311590Srgrimes			break;
1321590Srgrimes		if ((argv[0][0] == '!' || argv[0][0] == '(') &&
1331590Srgrimes		    argv[0][1] == '\0')
1341590Srgrimes			break;
1351590Srgrimes	}
1361590Srgrimes
1371590Srgrimes	if (p == start)
1381590Srgrimes		usage();
1391590Srgrimes	*p = NULL;
1401590Srgrimes
1411590Srgrimes	if ((dotfd = open(".", O_RDONLY, 0)) < 0)
1421590Srgrimes		err(1, ".");
1431590Srgrimes
1441590Srgrimes	exit(find_execute(find_formplan(argv), start));
1451590Srgrimes}
1461590Srgrimes
1471590Srgrimesstatic void
1481590Srgrimesusage()
1491590Srgrimes{
1501590Srgrimes	(void)fprintf(stderr,
1511590Srgrimes"usage: find [-H | -L | -P] [-Xdx] [-f file] [file ...] [expression]\n");
1521590Srgrimes	exit(1);
1531590Srgrimes}
154