xref: /original-bsd/usr.bin/find/main.c (revision 35d77a20)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)main.c	5.9 (Berkeley) 05/24/91";
10 #endif /* not lint */
11 
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <time.h>
16 #include <fts.h>
17 #include <errno.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include "find.h"
21 
22 time_t now;			/* time find was run */
23 int dotfd;			/* starting directory */
24 int ftsoptions;			/* options for the ftsopen(3) call */
25 int isdeprecated;		/* using deprecated syntax */
26 int isdepth;			/* do directories on post-order visit */
27 int isoutput;			/* user specified output operator */
28 int isxargs;			/* don't permit xargs delimiting chars */
29 
30 static void usage();
31 
32 main(argc, argv)
33 	int argc;
34 	char **argv;
35 {
36 	register char **p, **start;
37 	PLAN *find_formplan();
38 	int ch;
39 
40 	(void)time(&now);	/* initialize the time-of-day */
41 
42 	p = start = argv;
43 	ftsoptions = FTS_NOSTAT|FTS_PHYSICAL;
44 	while ((ch = getopt(argc, argv, "df:sXx")) != EOF)
45 		switch(ch) {
46 		case 'd':
47 			isdepth = 1;
48 			break;
49 		case 'f':
50 			*p++ = optarg;
51 			break;
52 		case 's':
53 			ftsoptions &= ~FTS_PHYSICAL;
54 			ftsoptions |= FTS_LOGICAL;
55 			break;
56 		case 'X':
57 			isxargs = 1;
58 			break;
59 		case 'x':
60 			ftsoptions &= ~FTS_NOSTAT;
61 			ftsoptions |= FTS_XDEV;
62 			break;
63 		case '?':
64 		default:
65 			break;
66 		}
67 
68 	argc -= optind;
69 	argv += optind;
70 
71 	/* Find first option to delimit the file list. */
72 	while (*argv) {
73 		if (option(*argv))
74 			break;
75 		*p++ = *argv++;
76 	}
77 
78 	if (p == start)
79 		usage();
80 	*p = NULL;
81 
82 	if ((dotfd = open(".", O_RDONLY, 0)) < 0)
83 		err(".: %s", strerror(errno));
84 
85 	find_execute(find_formplan(argv), start);
86 }
87 
88 static void
89 usage()
90 {
91 	(void)fprintf(stderr,
92 	    "usage: find [-dsXx] [-f file] [file ...] expression\n");
93 	exit(1);
94 }
95