1 |
|
|
/* $OpenBSD: main.c,v 1.31 2017/01/03 21:31:16 tedu 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 |
|
|
#include <unistd.h> |
45 |
|
|
|
46 |
|
|
#include "find.h" |
47 |
|
|
|
48 |
|
|
time_t now; /* time find was run */ |
49 |
|
|
int dotfd; /* starting directory; may be -1 */ |
50 |
|
|
int ftsoptions; /* options for the fts_open(3) call */ |
51 |
|
|
int isdelete; /* user specified -delete operator */ |
52 |
|
|
int isdepth; /* do directories on post-order visit */ |
53 |
|
|
int isoutput; /* user specified output operator */ |
54 |
|
|
int isxargs; /* don't permit xargs delimiting chars */ |
55 |
|
|
|
56 |
|
|
__dead static void usage(void); |
57 |
|
|
|
58 |
|
|
int |
59 |
|
|
main(int argc, char *argv[]) |
60 |
|
|
{ |
61 |
|
226 |
struct sigaction sa; |
62 |
|
|
char **p, **paths, **paths2; |
63 |
|
|
int ch; |
64 |
|
|
|
65 |
|
113 |
memset(&sa, 0, sizeof sa); |
66 |
|
113 |
sa.sa_handler = show_path; |
67 |
|
113 |
sa.sa_flags = SA_RESTART; |
68 |
|
|
|
69 |
|
113 |
(void)time(&now); /* initialize the time-of-day */ |
70 |
|
|
|
71 |
|
113 |
p = paths = ereallocarray(NULL, argc, sizeof(char *)); |
72 |
|
|
|
73 |
|
113 |
sigaction(SIGINFO, &sa, NULL); |
74 |
|
|
|
75 |
|
113 |
ftsoptions = FTS_NOSTAT|FTS_PHYSICAL; |
76 |
✓✓ |
228 |
while ((ch = getopt(argc, argv, "Hdf:hLXx")) != -1) |
77 |
✗✗✗✗ ✗✗✓✗
|
2 |
switch(ch) { |
78 |
|
|
case 'H': |
79 |
|
|
ftsoptions |= FTS_COMFOLLOW; |
80 |
|
|
ftsoptions |= FTS_PHYSICAL; |
81 |
|
|
ftsoptions &= ~FTS_LOGICAL; |
82 |
|
|
break; |
83 |
|
|
case 'd': |
84 |
|
|
isdepth = 1; |
85 |
|
|
break; |
86 |
|
|
case 'f': |
87 |
|
|
*p++ = optarg; |
88 |
|
|
break; |
89 |
|
|
case 'h': |
90 |
|
|
case 'L': |
91 |
|
|
ftsoptions &= ~FTS_COMFOLLOW; |
92 |
|
|
ftsoptions &= ~FTS_PHYSICAL; |
93 |
|
|
ftsoptions |= FTS_LOGICAL; |
94 |
|
|
break; |
95 |
|
|
case 'X': |
96 |
|
|
isxargs = 1; |
97 |
|
|
break; |
98 |
|
|
case 'x': |
99 |
|
2 |
ftsoptions &= ~FTS_NOSTAT; |
100 |
|
2 |
ftsoptions |= FTS_XDEV; |
101 |
|
2 |
break; |
102 |
|
|
case '?': |
103 |
|
|
default: |
104 |
|
|
usage(); |
105 |
|
|
} |
106 |
|
|
|
107 |
|
113 |
argc -= optind; |
108 |
|
113 |
argv += optind; |
109 |
|
|
|
110 |
|
|
/* The first argument that starts with a -, or is a ! or a (, and all |
111 |
|
|
* subsequent arguments shall be interpreted as an expression ... |
112 |
|
|
* (POSIX.2). |
113 |
|
|
*/ |
114 |
✓✗ |
516 |
while (*argv) { |
115 |
✓✓✗✓
|
261 |
if (**argv == '-' || |
116 |
✓✗✓✓
|
299 |
((**argv == '!' || **argv == '(') && (*argv)[1] == '\0')) |
117 |
|
|
break; |
118 |
|
145 |
*p++ = *argv++; |
119 |
|
|
} |
120 |
|
|
|
121 |
✗✓ |
113 |
if (p == paths) |
122 |
|
|
usage(); |
123 |
|
113 |
*p = NULL; |
124 |
|
|
|
125 |
✗✓ |
113 |
if (!(paths2 = reallocarray(paths, p - paths + 1, sizeof(char *)))) |
126 |
|
|
err(1, NULL); |
127 |
|
|
paths = paths2; |
128 |
|
|
|
129 |
|
|
dotfd = open(".", O_RDONLY, 0); |
130 |
|
|
|
131 |
|
|
exit(find_execute(find_formplan(argv), paths)); |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
static void |
135 |
|
|
usage(void) |
136 |
|
|
{ |
137 |
|
|
(void)fprintf(stderr, |
138 |
|
|
"usage: find [-dHhLXx] [-f path] path ... [expression]\n"); |
139 |
|
|
exit(1); |
140 |
|
|
} |