Annotation of src/usr.bin/find/main.c, Revision 1.19
1.19 ! deraadt 1: /* $OpenBSD: main.c,v 1.18 2004/02/24 07:20:38 jmc Exp $ */
1.2 deraadt 2:
1.1 deraadt 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.
1.15 millert 15: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 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: #ifndef lint
33: /*static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";*/
1.19 ! deraadt 34: static char rcsid[] = "$OpenBSD: main.c,v 1.18 2004/02/24 07:20:38 jmc Exp $";
1.1 deraadt 35: #endif /* not lint */
36:
37: #include <sys/types.h>
38: #include <sys/stat.h>
39:
40: #include <err.h>
41: #include <errno.h>
42: #include <fcntl.h>
43: #include <fts.h>
1.3 tholo 44: #include <signal.h>
1.1 deraadt 45: #include <stdio.h>
46: #include <stdlib.h>
1.9 deraadt 47: #include <string.h>
1.1 deraadt 48: #include <time.h>
49:
50: #include "find.h"
51:
52: time_t now; /* time find was run */
53: int dotfd; /* starting directory */
54: int ftsoptions; /* options for the ftsopen(3) call */
55: int isdepth; /* do directories on post-order visit */
56: int isoutput; /* user specified output operator */
57: int isxargs; /* don't permit xargs delimiting chars */
58:
1.13 millert 59: static void usage(void);
1.1 deraadt 60:
61: int
1.16 deraadt 62: main(int argc, char *argv[])
1.1 deraadt 63: {
1.9 deraadt 64: struct sigaction sa;
1.17 tedu 65: char **p, **paths, **paths2;
1.1 deraadt 66: int ch;
1.9 deraadt 67:
68: memset(&sa, 0, sizeof sa);
69: sa.sa_handler = show_path;
70: sa.sa_flags = SA_RESTART;
1.1 deraadt 71:
72: (void)time(&now); /* initialize the time-of-day */
1.3 tholo 73:
1.7 millert 74: p = paths = (char **) emalloc(sizeof(char *) * argc);
1.5 millert 75:
1.3 tholo 76: sigaction(SIGINFO, &sa, NULL);
1.1 deraadt 77:
78: ftsoptions = FTS_NOSTAT|FTS_PHYSICAL;
1.14 millert 79: while ((ch = getopt(argc, argv, "Hdf:hLXxW")) != -1)
1.1 deraadt 80: switch(ch) {
81: case 'H':
82: ftsoptions |= FTS_COMFOLLOW;
83: break;
84: case 'd':
85: isdepth = 1;
86: break;
87: case 'f':
88: *p++ = optarg;
89: break;
90: case 'h':
1.14 millert 91: case 'L':
1.1 deraadt 92: ftsoptions &= ~FTS_PHYSICAL;
93: ftsoptions |= FTS_LOGICAL;
94: break;
95: case 'X':
96: isxargs = 1;
97: break;
98: case 'x':
99: ftsoptions &= ~FTS_NOSTAT;
100: ftsoptions |= FTS_XDEV;
101: break;
1.8 espie 102: case 'W':
103: ftsoptions |= FTS_WHITEOUT;
104: break;
1.1 deraadt 105: case '?':
106: default:
107: break;
108: }
109:
110: argc -= optind;
111: argv += optind;
112:
113: /* The first argument that starts with a -, or is a ! or a (, and all
114: * subsequent arguments shall be interpreted as an expression ...
115: * (POSIX.2).
116: */
117: while (*argv) {
118: if (**argv == '-' ||
119: ((**argv == '!' || **argv == '(') && (*argv)[1] == '\0'))
120: break;
121: *p++ = *argv++;
122: }
123:
1.6 millert 124: if (p == paths)
1.7 millert 125: usage();
1.5 millert 126: *p = NULL;
1.6 millert 127:
1.17 tedu 128: if (!(paths2 = realloc(paths, sizeof(char *) * (p - paths + 1))))
1.5 millert 129: err(1, NULL);
1.17 tedu 130: paths = paths2;
1.1 deraadt 131:
132: if ((dotfd = open(".", O_RDONLY, 0)) < 0)
133: err(1, ".:");
134:
1.5 millert 135: find_execute(find_formplan(argv), paths);
1.1 deraadt 136: exit(0);
137: }
138:
139: static void
1.16 deraadt 140: usage(void)
1.1 deraadt 141: {
142: (void)fprintf(stderr,
1.18 jmc 143: "usage: find [-dHhLWXx] [-f file] file [...] [expression]\n");
1.1 deraadt 144: exit(1);
145: }