main.c revision 41391
1139738Simp/*- 283364Sdfr * Copyright (c) 1990, 1993, 1994 383364Sdfr * The Regents of the University of California. All rights reserved. 483364Sdfr * 583364Sdfr * This code is derived from software contributed to Berkeley by 683364Sdfr * Cimarron D. Taylor of the University of California, Berkeley. 783364Sdfr * 883364Sdfr * Redistribution and use in source and binary forms, with or without 983364Sdfr * modification, are permitted provided that the following conditions 1083364Sdfr * are met: 1183364Sdfr * 1. Redistributions of source code must retain the above copyright 1283364Sdfr * notice, this list of conditions and the following disclaimer. 1383364Sdfr * 2. Redistributions in binary form must reproduce the above copyright 1483364Sdfr * notice, this list of conditions and the following disclaimer in the 1583364Sdfr * documentation and/or other materials provided with the distribution. 1683364Sdfr * 3. All advertising materials mentioning features or use of this software 1783364Sdfr * must display the following acknowledgement: 1883364Sdfr * This product includes software developed by the University of 1983364Sdfr * California, Berkeley and its contributors. 2083364Sdfr * 4. Neither the name of the University nor the names of its contributors 2183364Sdfr * may be used to endorse or promote products derived from this software 2283364Sdfr * without specific prior written permission. 2383364Sdfr * 2483364Sdfr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2583364Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2683364Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2783364Sdfr * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2883364Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2983364Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3083364Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3183364Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3283364Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3383364Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34113038Sobrien * SUCH DAMAGE. 35113038Sobrien */ 3683364Sdfr 3783364Sdfr#ifndef lint 3883364Sdfrchar copyright[] = 3983364Sdfr"@(#) Copyright (c) 1990, 1993, 1994\n\ 4083364Sdfr The Regents of the University of California. All rights reserved.\n"; 4183364Sdfr#endif /* not lint */ 4283364Sdfr 4383364Sdfr#ifndef lint 4483364Sdfrstatic char sccsid[] = "@(#)main.c 8.4 (Berkeley) 5/4/95"; 4583364Sdfr#endif /* not lint */ 4683364Sdfr 4783364Sdfr#include <sys/types.h> 4883364Sdfr#include <sys/stat.h> 4983364Sdfr 5083364Sdfr#include <err.h> 5183364Sdfr#include <errno.h> 5283364Sdfr#include <fcntl.h> 5383364Sdfr#include <fts.h> 5483364Sdfr#include <locale.h> 5583364Sdfr#include <stdio.h> 5683364Sdfr#include <stdlib.h> 5783364Sdfr#include <time.h> 5883364Sdfr#include <unistd.h> 59108100Sjake 6083364Sdfr#include "find.h" 6183364Sdfr 6283364Sdfrtime_t now; /* time find was run */ 6383364Sdfrint dotfd; /* starting directory */ 6483364Sdfrint ftsoptions; /* options for the ftsopen(3) call */ 6583364Sdfrint isdeprecated; /* using deprecated syntax */ 6683364Sdfrint isdepth; /* do directories on post-order visit */ 6783364Sdfrint isoutput; /* user specified output operator */ 6883364Sdfrint isxargs; /* don't permit xargs delimiting chars */ 6983364Sdfrint issort; /* travel the file hierarchy lexicographical order */ 7083364Sdfr 7183364Sdfrstatic void usage __P((void)); 7283364Sdfr 7383364Sdfrint 7483364Sdfrmain(argc, argv) 7583364Sdfr int argc; 7683364Sdfr char *argv[]; 7783364Sdfr{ 78158467Sjhb register char **p, **start; 7983364Sdfr int Hflag, Lflag, ch; 8083364Sdfr 8183364Sdfr (void)setlocale(LC_ALL, ""); 8283364Sdfr 8383364Sdfr (void)time(&now); /* initialize the time-of-day */ 8483364Sdfr 8583364Sdfr p = start = argv; 8683364Sdfr Hflag = Lflag = 0; 87 ftsoptions = FTS_NOSTAT | FTS_PHYSICAL; 88 while ((ch = getopt(argc, argv, "HLPXdf:sx")) != -1) 89 switch (ch) { 90 case 'H': 91 Hflag = 1; 92 Lflag = 0; 93 break; 94 case 'L': 95 Lflag = 1; 96 Hflag = 0; 97 break; 98 case 'P': 99 Hflag = Lflag = 0; 100 break; 101 case 'X': 102 isxargs = 1; 103 break; 104 case 'd': 105 isdepth = 1; 106 break; 107 case 'f': 108 *p++ = optarg; 109 break; 110 case 's': 111 issort = 1; 112 break; 113 case 'x': 114 ftsoptions |= FTS_XDEV; 115 break; 116 case '?': 117 default: 118 break; 119 } 120 121 argc -= optind; 122 argv += optind; 123 124 if (Hflag) 125 ftsoptions |= FTS_COMFOLLOW; 126 if (Lflag) { 127 ftsoptions &= ~FTS_PHYSICAL; 128 ftsoptions |= FTS_LOGICAL; 129 } 130 131 /* 132 * Find first option to delimit the file list. The first argument 133 * that starts with a -, or is a ! or a ( must be interpreted as a 134 * part of the find expression, according to POSIX .2. 135 */ 136 for (; *argv != NULL; *p++ = *argv++) { 137 if (argv[0][0] == '-') 138 break; 139 if ((argv[0][0] == '!' || argv[0][0] == '(') && 140 argv[0][1] == '\0') 141 break; 142 } 143 144 if (p == start) 145 usage(); 146 *p = NULL; 147 148 if ((dotfd = open(".", O_RDONLY, 0)) < 0) 149 err(1, "."); 150 151 exit(find_execute(find_formplan(argv), start)); 152} 153 154static void 155usage() 156{ 157 (void)fprintf(stderr, 158"usage: find [-H | -L | -P] [-Xdx] [-f file] [file ...] [expression]\n"); 159 exit(1); 160} 161