main.c revision 25942
1116743Ssam/*- 2178354Ssam * Copyright (c) 1990, 1993, 1994 3116743Ssam * The Regents of the University of California. All rights reserved. 4116743Ssam * 5116743Ssam * This code is derived from software contributed to Berkeley by 6116743Ssam * Cimarron D. Taylor of the University of California, Berkeley. 7116743Ssam * 8116743Ssam * Redistribution and use in source and binary forms, with or without 9116743Ssam * modification, are permitted provided that the following conditions 10116743Ssam * are met: 11116743Ssam * 1. Redistributions of source code must retain the above copyright 12116743Ssam * notice, this list of conditions and the following disclaimer. 13116743Ssam * 2. Redistributions in binary form must reproduce the above copyright 14116743Ssam * notice, this list of conditions and the following disclaimer in the 15116743Ssam * documentation and/or other materials provided with the distribution. 16116743Ssam * 3. All advertising materials mentioning features or use of this software 17116743Ssam * must display the following acknowledgement: 18116743Ssam * This product includes software developed by the University of 19116743Ssam * California, Berkeley and its contributors. 20116743Ssam * 4. Neither the name of the University nor the names of its contributors 21116743Ssam * may be used to endorse or promote products derived from this software 22116743Ssam * without specific prior written permission. 23116743Ssam * 24116743Ssam * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25116743Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26116743Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27116743Ssam * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28116743Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29116743Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30116743Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31116743Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32116743Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33116743Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34116743Ssam * SUCH DAMAGE. 35116743Ssam */ 36116743Ssam 37116743Ssam#ifndef lint 38116743Ssamchar copyright[] = 39165185Ssam"@(#) Copyright (c) 1990, 1993, 1994\n\ 40119783Ssam The Regents of the University of California. All rights reserved.\n"; 41116743Ssam#endif /* not lint */ 42138570Ssam 43116743Ssam#ifndef lint 44116743Ssamstatic char sccsid[] = "@(#)main.c 8.4 (Berkeley) 5/4/95"; 45116743Ssam#endif /* not lint */ 46155481Ssam 47116743Ssam#include <sys/types.h> 48155481Ssam#include <sys/stat.h> 49155481Ssam 50170530Ssam#include <err.h> 51155481Ssam#include <errno.h> 52178354Ssam#include <fcntl.h> 53178354Ssam#include <fts.h> 54140438Ssam#include <locale.h> 55138570Ssam#include <stdio.h> 56155480Ssam#include <stdlib.h> 57138570Ssam#include <time.h> 58116743Ssam#include <unistd.h> 59147067Ssam 60147067Ssam#include "find.h" 61147067Ssam 62147067Ssamtime_t now; /* time find was run */ 63147057Ssamint dotfd; /* starting directory */ 64147057Ssamint ftsoptions; /* options for the ftsopen(3) call */ 65147057Ssamint isdeprecated; /* using deprecated syntax */ 66147057Ssamint isdepth; /* do directories on post-order visit */ 67147057Ssamint isoutput; /* user specified output operator */ 68147057Ssamint isxargs; /* don't permit xargs delimiting chars */ 69147057Ssam 70147057Ssamstatic void usage __P((void)); 71147057Ssam 72147057Ssamint 73147057Ssammain(argc, argv) 74170530Ssam int argc; 75170530Ssam char *argv[]; 76170530Ssam{ 77170530Ssam register char **p, **start; 78170530Ssam int Hflag, Lflag, ch; 79170530Ssam 80170530Ssam (void)setlocale(LC_ALL, ""); 81170530Ssam 82138570Ssam (void)time(&now); /* initialize the time-of-day */ 83116743Ssam 84119150Ssam p = start = argv; 85178354Ssam Hflag = Lflag = 0; 86178354Ssam ftsoptions = FTS_NOSTAT | FTS_PHYSICAL; 87178354Ssam while ((ch = getopt(argc, argv, "HLPXdf:x")) != -1) 88170530Ssam switch (ch) { 89138570Ssam case 'H': 90116743Ssam Hflag = 1; 91138570Ssam Lflag = 0; 92138570Ssam break; 93116743Ssam case 'L': 94138570Ssam Lflag = 1; 95138570Ssam Hflag = 0; 96138570Ssam break; 97138570Ssam case 'P': 98138570Ssam Hflag = Lflag = 0; 99138570Ssam break; 100138570Ssam case 'X': 101138570Ssam isxargs = 1; 102138570Ssam break; 103138570Ssam case 'd': 104138570Ssam isdepth = 1; 105116743Ssam break; 106138570Ssam case 'f': 107170530Ssam *p++ = optarg; 108170530Ssam break; 109116743Ssam case 'x': 110156073Ssam ftsoptions |= FTS_XDEV; 111116743Ssam break; 112165185Ssam case '?': 113116743Ssam default: 114138570Ssam break; 115116743Ssam } 116116743Ssam 117116743Ssam argc -= optind; 118140438Ssam argv += optind; 119116743Ssam 120116743Ssam if (Hflag) 121138570Ssam ftsoptions |= FTS_COMFOLLOW; 122116743Ssam if (Lflag) { 123138570Ssam ftsoptions &= ~FTS_PHYSICAL; 124138570Ssam ftsoptions |= FTS_LOGICAL; 125138570Ssam } 126138570Ssam 127138570Ssam /* 128138570Ssam * Find first option to delimit the file list. The first argument 129138570Ssam * that starts with a -, or is a ! or a ( must be interpreted as a 130158298Ssam * part of the find expression, according to POSIX .2. 131138570Ssam */ 132138570Ssam for (; *argv != NULL; *p++ = *argv++) { 133138570Ssam if (argv[0][0] == '-') 134138570Ssam break; 135138570Ssam if ((argv[0][0] == '!' || argv[0][0] == '(') && 136138570Ssam argv[0][1] == '\0') 137138570Ssam break; 138138570Ssam } 139138570Ssam 140138570Ssam if (p == start) 141138570Ssam usage(); 142138570Ssam *p = NULL; 143138570Ssam 144138570Ssam if ((dotfd = open(".", O_RDONLY, 0)) < 0) 145138570Ssam err(1, "."); 146138570Ssam 147138570Ssam exit(find_execute(find_formplan(argv), start)); 148178354Ssam} 149156073Ssam 150138570Ssamstatic void 151138570Ssamusage() 152138570Ssam{ 153138570Ssam (void)fprintf(stderr, 154155482Ssam"usage: find [-H | -L | -P] [-Xdx] [-f file] [file ...] [expression]\n"); 155170530Ssam exit(1); 156170530Ssam} 157170530Ssam