xref: /netbsd-src/usr.bin/find/main.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: main.c,v 1.8 1997/10/19 11:52:43 lukem Exp $	*/
2 
3 /*-
4  * Copyright (c) 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 6/6/93";
40 #else
41 __RCSID("$NetBSD: main.c,v 1.8 1997/10/19 11:52:43 lukem Exp $");
42 #endif
43 #endif /* not lint */
44 
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 
48 #include <err.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <fts.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <time.h>
55 
56 #include "find.h"
57 
58 time_t now;			/* time find was run */
59 int dotfd;			/* starting directory */
60 int ftsoptions;			/* options for the ftsopen(3) call */
61 int isdeprecated;		/* using deprecated syntax */
62 int isdepth;			/* do directories on post-order visit */
63 int isoutput;			/* user specified output operator */
64 int isxargs;			/* don't permit xargs delimiting chars */
65 
66 int main __P((int, char **));
67 static void usage __P((void));
68 
69 int
70 main(argc, argv)
71 	int argc;
72 	char *argv[];
73 {
74 	char **p, **start;
75 	int ch;
76 
77 	(void)time(&now);	/* initialize the time-of-day */
78 
79 	p = start = argv;
80 	ftsoptions = FTS_NOSTAT|FTS_PHYSICAL;
81 	while ((ch = getopt(argc, argv, "Hdf:hXx")) != -1)
82 		switch(ch) {
83 		case 'H':
84 			ftsoptions |= FTS_COMFOLLOW;
85 			break;
86 		case 'd':
87 			isdepth = 1;
88 			break;
89 		case 'f':
90 			*p++ = optarg;
91 			break;
92 		case 'h':
93 			ftsoptions &= ~FTS_PHYSICAL;
94 			ftsoptions |= FTS_LOGICAL;
95 			break;
96 		case 'X':
97 			isxargs = 1;
98 			break;
99 		case 'x':
100 			ftsoptions &= ~FTS_NOSTAT;
101 			ftsoptions |= FTS_XDEV;
102 			break;
103 		case '?':
104 		default:
105 			break;
106 		}
107 
108 	argc -= optind;
109 	argv += optind;
110 
111 	/* The first argument that starts with a -, or is a ! or a (, and all
112 	 * subsequent arguments shall be interpreted as an expression ...
113 	 * (POSIX.2).
114 	 */
115 	while (*argv) {
116 		if (**argv == '-' ||
117 		    ((**argv == '!' || **argv == '(') && (*argv)[1] == '\0'))
118 			break;
119 		*p++ = *argv++;
120 	}
121 
122 	if (p == start)
123 		usage();
124 	*p = NULL;
125 
126 	if ((dotfd = open(".", O_RDONLY, 0)) < 0)
127 		err(1, ".:");
128 
129 	find_execute(find_formplan(argv), start);
130 	exit(0);
131 }
132 
133 static void
134 usage()
135 {
136 	(void)fprintf(stderr,
137 	    "usage: find [-HdhXx] [-f file] [file ...] expression\n");
138 	exit(1);
139 }
140