Annotation of src/usr.bin/find/main.c, Revision 1.14
1.14 ! millert 1: /* $OpenBSD: main.c,v 1.13 2002/02/16 21:27:46 millert 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.
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";*/
1.14 ! millert 38: static char rcsid[] = "$OpenBSD: main.c,v 1.13 2002/02/16 21:27:46 millert Exp $";
1.1 deraadt 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>
1.3 tholo 48: #include <signal.h>
1.1 deraadt 49: #include <stdio.h>
50: #include <stdlib.h>
1.9 deraadt 51: #include <string.h>
1.1 deraadt 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:
1.13 millert 64: static void usage(void);
1.1 deraadt 65:
66: int
67: main(argc, argv)
68: int argc;
69: char *argv[];
70: {
1.9 deraadt 71: struct sigaction sa;
1.5 millert 72: char **p, **paths;
1.1 deraadt 73: int ch;
1.9 deraadt 74:
75: memset(&sa, 0, sizeof sa);
76: sa.sa_handler = show_path;
77: sa.sa_flags = SA_RESTART;
1.1 deraadt 78:
79: (void)time(&now); /* initialize the time-of-day */
1.3 tholo 80:
1.7 millert 81: p = paths = (char **) emalloc(sizeof(char *) * argc);
1.5 millert 82:
1.3 tholo 83: sigaction(SIGINFO, &sa, NULL);
1.1 deraadt 84:
85: ftsoptions = FTS_NOSTAT|FTS_PHYSICAL;
1.14 ! millert 86: while ((ch = getopt(argc, argv, "Hdf:hLXxW")) != -1)
1.1 deraadt 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':
1.14 ! millert 98: case 'L':
1.1 deraadt 99: ftsoptions &= ~FTS_PHYSICAL;
100: ftsoptions |= FTS_LOGICAL;
101: break;
102: case 'X':
103: isxargs = 1;
104: break;
105: case 'x':
106: ftsoptions &= ~FTS_NOSTAT;
107: ftsoptions |= FTS_XDEV;
108: break;
1.8 espie 109: case 'W':
110: ftsoptions |= FTS_WHITEOUT;
111: break;
1.1 deraadt 112: case '?':
113: default:
114: break;
115: }
116:
117: argc -= optind;
118: argv += optind;
119:
120: /* The first argument that starts with a -, or is a ! or a (, and all
121: * subsequent arguments shall be interpreted as an expression ...
122: * (POSIX.2).
123: */
124: while (*argv) {
125: if (**argv == '-' ||
126: ((**argv == '!' || **argv == '(') && (*argv)[1] == '\0'))
127: break;
128: *p++ = *argv++;
129: }
130:
1.6 millert 131: if (p == paths)
1.7 millert 132: usage();
1.5 millert 133: *p = NULL;
1.6 millert 134:
1.5 millert 135: if (!(paths = realloc(paths, sizeof(char *) * (p - paths + 1))))
136: err(1, NULL);
1.1 deraadt 137:
138: if ((dotfd = open(".", O_RDONLY, 0)) < 0)
139: err(1, ".:");
140:
1.5 millert 141: find_execute(find_formplan(argv), paths);
1.1 deraadt 142: exit(0);
143: }
144:
145: static void
146: usage()
147: {
148: (void)fprintf(stderr,
1.14 ! millert 149: "usage: find [-dHhLXxW] [-f file] file [...] [expression]\n");
1.1 deraadt 150: exit(1);
151: }