xref: /openbsd-src/usr.bin/find/main.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: main.c,v 1.9 2001/07/12 05:17:04 deraadt 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[] = "$OpenBSD: main.c,v 1.9 2001/07/12 05:17:04 deraadt 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 <signal.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <time.h>
53 
54 #include "find.h"
55 
56 time_t now;			/* time find was run */
57 int dotfd;			/* starting directory */
58 int ftsoptions;			/* options for the ftsopen(3) call */
59 int isdeprecated;		/* using deprecated syntax */
60 int isdepth;			/* do directories on post-order visit */
61 int isoutput;			/* user specified output operator */
62 int isxargs;			/* don't permit xargs delimiting chars */
63 
64 static void usage __P((void));
65 
66 int
67 main(argc, argv)
68 	int argc;
69 	char *argv[];
70 {
71 	struct sigaction sa;
72 	char **p, **paths;
73 	int ch;
74 
75 	memset(&sa, 0, sizeof sa);
76 	sa.sa_handler = show_path;
77 	sa.sa_flags = SA_RESTART;
78 
79 	(void)time(&now);	/* initialize the time-of-day */
80 
81 	p = paths = (char **) emalloc(sizeof(char *) * argc);
82 
83 	sigaction(SIGINFO, &sa, NULL);
84 
85 	ftsoptions = FTS_NOSTAT|FTS_PHYSICAL;
86 	while ((ch = getopt(argc, argv, "Hdf:hXxW")) != -1)
87 		switch(ch) {
88 		case 'H':
89 			ftsoptions |= FTS_COMFOLLOW;
90 			break;
91 		case 'd':
92 			isdepth = 1;
93 			break;
94 		case 'f':
95 			*p++ = optarg;
96 			break;
97 		case 'h':
98 			ftsoptions &= ~FTS_PHYSICAL;
99 			ftsoptions |= FTS_LOGICAL;
100 			break;
101 		case 'X':
102 			isxargs = 1;
103 			break;
104 		case 'x':
105 			ftsoptions &= ~FTS_NOSTAT;
106 			ftsoptions |= FTS_XDEV;
107 			break;
108 		case 'W':
109 			ftsoptions |= FTS_WHITEOUT;
110 			break;
111 		case '?':
112 		default:
113 			break;
114 		}
115 
116 	argc -= optind;
117 	argv += optind;
118 
119 	/* The first argument that starts with a -, or is a ! or a (, and all
120 	 * subsequent arguments shall be interpreted as an expression ...
121 	 * (POSIX.2).
122 	 */
123 	while (*argv) {
124 		if (**argv == '-' ||
125 		    ((**argv == '!' || **argv == '(') && (*argv)[1] == '\0'))
126 			break;
127 		*p++ = *argv++;
128 	}
129 
130 	if (p == paths)
131 		usage();
132 	*p = NULL;
133 
134 	if (!(paths = realloc(paths, sizeof(char *) * (p - paths + 1))))
135 		err(1, NULL);
136 
137 	if ((dotfd = open(".", O_RDONLY, 0)) < 0)
138 		err(1, ".:");
139 
140 	find_execute(find_formplan(argv), paths);
141 	exit(0);
142 }
143 
144 static void
145 usage()
146 {
147 	(void)fprintf(stderr,
148 	    "usage: find [-HdhXxW] [-f file] [file ...] expression\n");
149 	exit(1);
150 }
151