xref: /original-bsd/usr.bin/find/main.c (revision 0f81f0ee)
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  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 char copyright[] =
13 "@(#) Copyright (c) 1990, 1993, 1994\n\
14 	The Regents of the University of California.  All rights reserved.\n";
15 #endif /* not lint */
16 
17 #ifndef lint
18 static char sccsid[] = "@(#)main.c	8.4 (Berkeley) 05/04/95";
19 #endif /* not lint */
20 
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 
24 #include <err.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <fts.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <time.h>
31 #include <unistd.h>
32 
33 #include "find.h"
34 
35 time_t now;			/* time find was run */
36 int dotfd;			/* starting directory */
37 int ftsoptions;			/* options for the ftsopen(3) call */
38 int isdeprecated;		/* using deprecated syntax */
39 int isdepth;			/* do directories on post-order visit */
40 int isoutput;			/* user specified output operator */
41 int isxargs;			/* don't permit xargs delimiting chars */
42 
43 static void usage __P((void));
44 
45 int
46 main(argc, argv)
47 	int argc;
48 	char *argv[];
49 {
50 	register char **p, **start;
51 	int Hflag, Lflag, Pflag, ch;
52 
53 	(void)time(&now);	/* initialize the time-of-day */
54 
55 	p = start = argv;
56 	Hflag = Lflag = Pflag = 0;
57 	ftsoptions = FTS_NOSTAT | FTS_PHYSICAL;
58 	while ((ch = getopt(argc, argv, "HLPXdf:x")) != EOF)
59 		switch (ch) {
60 		case 'H':
61 			Hflag = 1;
62 			Lflag = Pflag = 0;
63 			break;
64 		case 'L':
65 			Lflag = 1;
66 			Hflag = Pflag = 0;
67 			break;
68 		case 'P':
69 			Pflag = 1;
70 			Hflag = Lflag = 0;
71 			break;
72 		case 'X':
73 			isxargs = 1;
74 			break;
75 		case 'd':
76 			isdepth = 1;
77 			break;
78 		case 'f':
79 			*p++ = optarg;
80 			break;
81 		case 'x':
82 			ftsoptions |= FTS_XDEV;
83 			break;
84 		case '?':
85 		default:
86 			break;
87 		}
88 
89 	argc -= optind;
90 	argv += optind;
91 
92 	if (Hflag)
93 		ftsoptions |= FTS_COMFOLLOW;
94 	if (Lflag) {
95 		ftsoptions &= ~FTS_PHYSICAL;
96 		ftsoptions |= FTS_LOGICAL;
97 	}
98 
99 	/*
100 	 * Find first option to delimit the file list.  The first argument
101 	 * that starts with a -, or is a ! or a ( must be interpreted as a
102 	 * part of the find expression, according to POSIX .2.
103 	 */
104 	for (; *argv != NULL; *p++ = *argv++) {
105 		if (argv[0][0] == '-')
106 			break;
107 		if ((argv[0][0] == '!' || argv[0][0] == '(') &&
108 		    argv[0][1] == '\0')
109 			break;
110 	}
111 
112 	if (p == start)
113 		usage();
114 	*p = NULL;
115 
116 	if ((dotfd = open(".", O_RDONLY, 0)) < 0)
117 		err(1, ".");
118 
119 	exit(find_execute(find_formplan(argv), start));
120 }
121 
122 static void
123 usage()
124 {
125 	(void)fprintf(stderr,
126 "usage: find [-H | -L | -P] [-Xdx] [-f file] [file ...] [expression]\n");
127 	exit(1);
128 }
129