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