xref: /original-bsd/usr.bin/find/main.c (revision f3f8e977)
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.2 (Berkeley) 04/01/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 	/* Find first option to delimit the file list. */
99 	for (; *argv != NULL; *p++ = *argv++)
100 		if (option(*argv))
101 			break;
102 
103 	if (p == start)
104 		usage();
105 	*p = NULL;
106 
107 	if ((dotfd = open(".", O_RDONLY, 0)) < 0)
108 		err(1, ".");
109 
110 	exit(find_execute(find_formplan(argv), start));
111 }
112 
113 static void
114 usage()
115 {
116 	(void)fprintf(stderr,
117 "usage: find [-H | -L | -P] [-Xdx] [-f file] [file ...] [expression]\n");
118 	exit(1);
119 }
120