xref: /openbsd-src/usr.bin/find/main.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: main.c,v 1.28 2011/06/14 21:04:05 naddy 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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 
35 #include <err.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <fts.h>
39 #include <signal.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <time.h>
44 
45 #include "find.h"
46 
47 time_t now;			/* time find was run */
48 int dotfd;			/* starting directory; may be -1 */
49 int ftsoptions;			/* options for the fts_open(3) call */
50 int isdepth;			/* do directories on post-order visit */
51 int isoutput;			/* user specified output operator */
52 int isxargs;			/* don't permit xargs delimiting chars */
53 
54 __dead static void usage(void);
55 
56 int
57 main(int argc, char *argv[])
58 {
59 	struct sigaction sa;
60 	char **p, **paths, **paths2;
61 	int ch;
62 
63 	memset(&sa, 0, sizeof sa);
64 	sa.sa_handler = show_path;
65 	sa.sa_flags = SA_RESTART;
66 
67 	(void)time(&now);	/* initialize the time-of-day */
68 
69 	p = paths = (char **) emalloc(sizeof(char *) * argc);
70 
71 	sigaction(SIGINFO, &sa, NULL);
72 
73 	ftsoptions = FTS_NOSTAT|FTS_PHYSICAL;
74 	while ((ch = getopt(argc, argv, "Hdf:hLXx")) != -1)
75 		switch(ch) {
76 		case 'H':
77 			ftsoptions |= FTS_COMFOLLOW;
78 			ftsoptions |= FTS_PHYSICAL;
79 			ftsoptions &= ~FTS_LOGICAL;
80 			break;
81 		case 'd':
82 			isdepth = 1;
83 			break;
84 		case 'f':
85 			*p++ = optarg;
86 			break;
87 		case 'h':
88 		case 'L':
89 			ftsoptions &= ~FTS_COMFOLLOW;
90 			ftsoptions &= ~FTS_PHYSICAL;
91 			ftsoptions |= FTS_LOGICAL;
92 			break;
93 		case 'X':
94 			isxargs = 1;
95 			break;
96 		case 'x':
97 			ftsoptions &= ~FTS_NOSTAT;
98 			ftsoptions |= FTS_XDEV;
99 			break;
100 		case '?':
101 		default:
102 			usage();
103 		}
104 
105 	argc -= optind;
106 	argv += optind;
107 
108 	/* The first argument that starts with a -, or is a ! or a (, and all
109 	 * subsequent arguments shall be interpreted as an expression ...
110 	 * (POSIX.2).
111 	 */
112 	while (*argv) {
113 		if (**argv == '-' ||
114 		    ((**argv == '!' || **argv == '(') && (*argv)[1] == '\0'))
115 			break;
116 		*p++ = *argv++;
117 	}
118 
119 	if (p == paths)
120 		usage();
121 	*p = NULL;
122 
123 	if (!(paths2 = realloc(paths, sizeof(char *) * (p - paths + 1))))
124 		err(1, NULL);
125 	paths = paths2;
126 
127 	dotfd = open(".", O_RDONLY, 0);
128 
129 	exit(find_execute(find_formplan(argv), paths));
130 }
131 
132 static void
133 usage(void)
134 {
135 	(void)fprintf(stderr,
136 	    "usage: find [-dHhLXx] [-f path] path ... [expression]\n");
137 	exit(1);
138 }
139