main.c revision 8874
1104964Sjeff/*-
2104964Sjeff * Copyright (c) 1990, 1993, 1994
3104964Sjeff *	The Regents of the University of California.  All rights reserved.
4104964Sjeff *
5104964Sjeff * This code is derived from software contributed to Berkeley by
6104964Sjeff * Cimarron D. Taylor of the University of California, Berkeley.
7104964Sjeff *
8104964Sjeff * Redistribution and use in source and binary forms, with or without
9104964Sjeff * modification, are permitted provided that the following conditions
10104964Sjeff * are met:
11104964Sjeff * 1. Redistributions of source code must retain the above copyright
12104964Sjeff *    notice, this list of conditions and the following disclaimer.
13104964Sjeff * 2. Redistributions in binary form must reproduce the above copyright
14104964Sjeff *    notice, this list of conditions and the following disclaimer in the
15104964Sjeff *    documentation and/or other materials provided with the distribution.
16104964Sjeff * 3. All advertising materials mentioning features or use of this software
17104964Sjeff *    must display the following acknowledgement:
18104964Sjeff *	This product includes software developed by the University of
19104964Sjeff *	California, Berkeley and its contributors.
20104964Sjeff * 4. Neither the name of the University nor the names of its contributors
21104964Sjeff *    may be used to endorse or promote products derived from this software
22104964Sjeff *    without specific prior written permission.
23104964Sjeff *
24104964Sjeff * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25104964Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26104964Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27104964Sjeff * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28104964Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29104964Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30104964Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31104964Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32104964Sjeff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33104964Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34104964Sjeff * SUCH DAMAGE.
35116182Sobrien */
36116182Sobrien
37116182Sobrien#ifndef lint
38147565Speterchar copyright[] =
39177418Sjeff"@(#) Copyright (c) 1990, 1993, 1994\n\
40179297Sjb	The Regents of the University of California.  All rights reserved.\n";
41147565Speter#endif /* not lint */
42104964Sjeff
43104964Sjeff#ifndef lint
44176750Smarcelstatic char sccsid[] = "@(#)main.c	8.3 (Berkeley) 4/16/94";
45104964Sjeff#endif /* not lint */
46104964Sjeff
47104964Sjeff#include <sys/types.h>
48123871Sjhb#include <sys/stat.h>
49104964Sjeff
50104964Sjeff#include <err.h>
51104964Sjeff#include <errno.h>
52104964Sjeff#include <fcntl.h>
53235459Srstone#include <fts.h>
54104964Sjeff#include <stdio.h>
55104964Sjeff#include <stdlib.h>
56104964Sjeff#include <time.h>
57139453Sjhb
58161599Sdavidxu#include "find.h"
59160039Sobrien
60134689Sjuliantime_t now;			/* time find was run */
61104964Sjeffint dotfd;			/* starting directory */
62145256Sjkoshyint ftsoptions;			/* options for the ftsopen(3) call */
63145256Sjkoshyint isdeprecated;		/* using deprecated syntax */
64145256Sjkoshyint isdepth;			/* do directories on post-order visit */
65145256Sjkoshyint isoutput;			/* user specified output operator */
66179297Sjbint isxargs;			/* don't permit xargs delimiting chars */
67179297Sjb
68179297Sjbstatic void usage __P((void));
69179297Sjb
70179297Sjbint
71179297Sjbmain(argc, argv)
72107135Sjeff	int argc;
73107135Sjeff	char *argv[];
74107135Sjeff{
75107135Sjeff	register char **p, **start;
76107135Sjeff	int Hflag, Lflag, Pflag, ch;
77107135Sjeff
78107135Sjeff	(void)time(&now);	/* initialize the time-of-day */
79122355Sbde
80122355Sbde	p = start = argv;
81122355Sbde	Hflag = Lflag = Pflag = 0;
82107135Sjeff	ftsoptions = FTS_NOSTAT | FTS_PHYSICAL;
83122355Sbde	while ((ch = getopt(argc, argv, "HLPXdf:x")) != EOF)
84107135Sjeff		switch (ch) {
85107135Sjeff		case 'H':
86187679Sjeff			Hflag = 1;
87187357Sjeff			Lflag = Pflag = 0;
88134791Sjulian			break;
89163709Sjb		case 'L':
90164936Sjulian			Lflag = 1;
91164936Sjulian			Hflag = Pflag = 0;
92163709Sjb			break;
93164936Sjulian		case 'P':
94164936Sjulian			Pflag = 1;
95164936Sjulian			Hflag = Lflag = 0;
96172264Sjeff			break;
97239153Smav		case 'X':
98180923Sjhb			isxargs = 1;
99164936Sjulian			break;
100187357Sjeff		case 'd':
101187357Sjeff			isdepth = 1;
102187357Sjeff			break;
103109145Sjeff		case 'f':
104109145Sjeff			*p++ = optarg;
105134791Sjulian			break;
106164936Sjulian		case 'x':
107177435Sjeff			ftsoptions |= FTS_XDEV;
108239157Smav			break;
109134791Sjulian		case '?':
110180923Sjhb		default:
111180923Sjhb			break;
112180923Sjhb		}
113164936Sjulian
114164936Sjulian	argc -= optind;
115124955Sjeff	argv += optind;
116180923Sjhb
117180923Sjhb	if (Hflag)
118180923Sjhb		ftsoptions |= FTS_COMFOLLOW;
119164936Sjulian	if (Lflag) {
120171488Sjeff		ftsoptions &= ~FTS_PHYSICAL;
121134791Sjulian		ftsoptions |= FTS_LOGICAL;
122239185Smav	}
123125288Sjeff
124239185Smav	/*
125104964Sjeff	 * Find first option to delimit the file list.  The first argument
126124955Sjeff	 * that starts with a -, or is a ! or a ( must be interpreted as a
127123871Sjhb	 * part of the find expression, according to POSIX .2.
128124955Sjeff	 */
129139453Sjhb	for (; *argv != NULL; *p++ = *argv++) {
130104964Sjeff		if (argv[0][0] == '-')
131104964Sjeff			break;
132163709Sjb		if ((argv[0][0] == '!' || argv[0][0] == '(') &&
133163709Sjb		    argv[0][1] == '\0')
134163709Sjb			break;
135134694Sjulian	}
136180923Sjhb
137180879Sjhb	if (p == start)
138180879Sjhb		usage();
139134694Sjulian	*p = NULL;
140104964Sjeff
141124955Sjeff	if ((dotfd = open(".", O_RDONLY, 0)) < 0)
142124955Sjeff		err(1, ".");
143124955Sjeff
144124955Sjeff	exit(find_execute(find_formplan(argv), start));
145124955Sjeff}
146253604Savg
147177253Srwatsonstatic void
148177253Srwatsonusage()
149104964Sjeff{
150239153Smav	(void)fprintf(stderr,
151239153Smav"usage: find [-H | -L | -P] [-Xdx] [-f file] [file ...] [expression]\n");
152239153Smav	exit(1);
153239153Smav}
154104964Sjeff