xref: /freebsd/usr.bin/find/main.c (revision 13464e4a44fc58490a03bb8bfc7e3c972e9c30b2)
1 /*-
2  * Copyright (c) 1990, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Cimarron D. Taylor of the University of California, Berkeley.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 static const char copyright[] =
35 "@(#) Copyright (c) 1990, 1993, 1994\n\
36 	The Regents of the University of California.  All rights reserved.\n";
37 #endif /* not lint */
38 
39 #ifndef lint
40 #if 0
41 static char sccsid[] = "@(#)main.c	8.4 (Berkeley) 5/4/95";
42 #endif
43 #endif /* not lint */
44 
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47 
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 
51 #include <err.h>
52 #include <errno.h>
53 #include <fcntl.h>
54 #include <fts.h>
55 #include <locale.h>
56 #include <regex.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <time.h>
60 #include <unistd.h>
61 
62 #include "find.h"
63 
64 time_t now;			/* time find was run */
65 int dotfd;			/* starting directory */
66 int ftsoptions;			/* options for the ftsopen(3) call */
67 int ignore_readdir_race;	/* ignore readdir race */
68 int isdeprecated;		/* using deprecated syntax */
69 int isdepth;			/* do directories on post-order visit */
70 int isoutput;			/* user specified output operator */
71 int issort;         		/* do hierarchies in lexicographical order */
72 int isxargs;			/* don't permit xargs delimiting chars */
73 int mindepth = -1, maxdepth = -1; /* minimum and maximum depth */
74 int regexp_flags = REG_BASIC;	/* use the "basic" regexp by default*/
75 int exitstatus;
76 
77 static void usage(void);
78 
79 int
80 main(int argc, char *argv[])
81 {
82 	char **p, **start;
83 	int Hflag, Lflag, ch;
84 
85 	(void)setlocale(LC_ALL, "");
86 
87 	(void)time(&now);	/* initialize the time-of-day */
88 
89 	p = start = argv;
90 	Hflag = Lflag = 0;
91 	ftsoptions = FTS_NOSTAT | FTS_PHYSICAL;
92 	while ((ch = getopt(argc, argv, "EHLPXdf:sx")) != -1)
93 		switch (ch) {
94 		case 'E':
95 			regexp_flags |= REG_EXTENDED;
96 			break;
97 		case 'H':
98 			Hflag = 1;
99 			Lflag = 0;
100 			break;
101 		case 'L':
102 			Lflag = 1;
103 			Hflag = 0;
104 			break;
105 		case 'P':
106 			Hflag = Lflag = 0;
107 			break;
108 		case 'X':
109 			isxargs = 1;
110 			break;
111 		case 'd':
112 			isdepth = 1;
113 			break;
114 		case 'f':
115 			*p++ = optarg;
116 			break;
117 		case 's':
118 			issort = 1;
119 			break;
120 		case 'x':
121 			ftsoptions |= FTS_XDEV;
122 			break;
123 		case '?':
124 		default:
125 			usage();
126 		}
127 
128 	argc -= optind;
129 	argv += optind;
130 
131 	if (Hflag)
132 		ftsoptions |= FTS_COMFOLLOW;
133 	if (Lflag) {
134 		ftsoptions &= ~FTS_PHYSICAL;
135 		ftsoptions |= FTS_LOGICAL;
136 	}
137 
138 	/*
139 	 * Find first option to delimit the file list.  The first argument
140 	 * that starts with a -, or is a ! or a ( must be interpreted as a
141 	 * part of the find expression, according to POSIX .2.
142 	 */
143 	for (; *argv != NULL; *p++ = *argv++) {
144 		if (argv[0][0] == '-')
145 			break;
146 		if ((argv[0][0] == '!' || argv[0][0] == '(') &&
147 		    argv[0][1] == '\0')
148 			break;
149 	}
150 
151 	if (p == start)
152 		usage();
153 	*p = NULL;
154 
155 	if ((dotfd = open(".", O_RDONLY | O_CLOEXEC, 0)) < 0)
156 		ftsoptions |= FTS_NOCHDIR;
157 
158 	exit(find_execute(find_formplan(argv), start));
159 }
160 
161 static void
162 usage(void)
163 {
164 	(void)fprintf(stderr, "%s\n%s\n",
165 "usage: find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression]",
166 "       find [-H | -L | -P] [-EXdsx] -f path [path ...] [expression]");
167 	exit(1);
168 }
169