xref: /netbsd-src/usr.bin/find/main.c (revision d0fed6c87ddc40a8bffa6f99e7433ddfc864dd83)
1 /*	$NetBSD: main.c,v 1.6 1997/01/09 20:19:14 tls 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 #ifndef lint
37 /*static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 6/6/93";*/
38 static char rcsid[] = "$NetBSD: main.c,v 1.6 1997/01/09 20:19:14 tls Exp $";
39 #endif /* not lint */
40 
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 
44 #include <err.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <fts.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <time.h>
51 
52 #include "find.h"
53 
54 time_t now;			/* time find was run */
55 int dotfd;			/* starting directory */
56 int ftsoptions;			/* options for the ftsopen(3) call */
57 int isdeprecated;		/* using deprecated syntax */
58 int isdepth;			/* do directories on post-order visit */
59 int isoutput;			/* user specified output operator */
60 int isxargs;			/* don't permit xargs delimiting chars */
61 
62 static void usage __P((void));
63 
64 int
65 main(argc, argv)
66 	int argc;
67 	char *argv[];
68 {
69 	register char **p, **start;
70 	int ch;
71 
72 	(void)time(&now);	/* initialize the time-of-day */
73 
74 	p = start = argv;
75 	ftsoptions = FTS_NOSTAT|FTS_PHYSICAL;
76 	while ((ch = getopt(argc, argv, "Hdf:hXx")) != EOF)
77 		switch(ch) {
78 		case 'H':
79 			ftsoptions |= FTS_COMFOLLOW;
80 			break;
81 		case 'd':
82 			isdepth = 1;
83 			break;
84 		case 'f':
85 			*p++ = optarg;
86 			break;
87 		case 'h':
88 			ftsoptions &= ~FTS_PHYSICAL;
89 			ftsoptions |= FTS_LOGICAL;
90 			break;
91 		case 'X':
92 			isxargs = 1;
93 			break;
94 		case 'x':
95 			ftsoptions &= ~FTS_NOSTAT;
96 			ftsoptions |= FTS_XDEV;
97 			break;
98 		case '?':
99 		default:
100 			break;
101 		}
102 
103 	argc -= optind;
104 	argv += optind;
105 
106 	/* The first argument that starts with a -, or is a ! or a (, and all
107 	 * subsequent arguments shall be interpreted as an expression ...
108 	 * (POSIX.2).
109 	 */
110 	while (*argv) {
111 		if (**argv == '-' ||
112 		    ((**argv == '!' || **argv == '(') && (*argv)[1] == '\0'))
113 			break;
114 		*p++ = *argv++;
115 	}
116 
117 	if (p == start)
118 		usage();
119 	*p = NULL;
120 
121 	if ((dotfd = open(".", O_RDONLY, 0)) < 0)
122 		err(1, ".:");
123 
124 	find_execute(find_formplan(argv), start);
125 	exit(0);
126 }
127 
128 static void
129 usage()
130 {
131 	(void)fprintf(stderr,
132 	    "usage: find [-HdhXx] [-f file] [file ...] expression\n");
133 	exit(1);
134 }
135