Annotation of src/usr.bin/find/main.c, Revision 1.4
1.4 ! millert 1: /* $OpenBSD: main.c,v 1.3 1996/10/24 03:46:04 tholo 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.4 ! millert 38: static char rcsid[] = "$OpenBSD: main.c,v 1.3 1996/10/24 03:46:04 tholo 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>
51: #include <time.h>
52:
53: #include "find.h"
54:
55: time_t now; /* time find was run */
56: int dotfd; /* starting directory */
57: int ftsoptions; /* options for the ftsopen(3) call */
58: int isdeprecated; /* using deprecated syntax */
59: int isdepth; /* do directories on post-order visit */
60: int isoutput; /* user specified output operator */
61: int isxargs; /* don't permit xargs delimiting chars */
62:
63: static void usage __P((void));
64:
65: int
66: main(argc, argv)
67: int argc;
68: char *argv[];
69: {
1.3 tholo 70: struct sigaction sa = { show_path, SA_RESTART, NULL };
1.1 deraadt 71: register char **p, **start;
72: int ch;
73:
74: (void)time(&now); /* initialize the time-of-day */
1.3 tholo 75:
76: sigaction(SIGINFO, &sa, NULL);
1.1 deraadt 77:
78: p = start = argv;
79: ftsoptions = FTS_NOSTAT|FTS_PHYSICAL;
1.4 ! millert 80: while ((ch = getopt(argc, argv, "Hdf:hXx")) != -1)
1.1 deraadt 81: switch(ch) {
82: case 'H':
83: ftsoptions |= FTS_COMFOLLOW;
84: break;
85: case 'd':
86: isdepth = 1;
87: break;
88: case 'f':
89: *p++ = optarg;
90: break;
91: case 'h':
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;
102: case '?':
103: default:
104: break;
105: }
106:
107: argc -= optind;
108: 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: while (*argv) {
115: if (**argv == '-' ||
116: ((**argv == '!' || **argv == '(') && (*argv)[1] == '\0'))
117: break;
118: *p++ = *argv++;
119: }
120:
121: if (p == start)
122: usage();
123: *p = NULL;
124:
125: if ((dotfd = open(".", O_RDONLY, 0)) < 0)
126: err(1, ".:");
127:
128: find_execute(find_formplan(argv), start);
129: exit(0);
130: }
131:
132: static void
133: usage()
134: {
135: (void)fprintf(stderr,
136: "usage: find [-HdhXx] [-f file] [file ...] expression\n");
137: exit(1);
138: }