2 * Copyright (c) 1995 Wolfram Schneider <wosch@FreeBSD.org>. Berlin.
3 * Copyright (c) 1989, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static const char copyright[] =
40 "@(#) Copyright (c) 1995-1996 Wolfram Schneider, Berlin.\n\
41 @(#) Copyright (c) 1989, 1993\n\
42 The Regents of the University of California. All rights reserved.\n";
47 static char sccsid[] = "@(#)locate.c 8.1 (Berkeley) 6/6/93";
49 static const char rcsid[] =
54 * Ref: Usenix ;login:, Vol 8, No 1, February/March, 1983, p. 8.
56 * Locate scans a file list for the full pathname of a file given only part
57 * of the name. The list has been processed with with "front-compression"
58 * and bigram coding. Front compression reduces space by a factor of 4-5,
59 * bigram coding by a further 20-25%.
63 * 0-28 likeliest differential counts + offset to make nonnegative
64 * 30 switch code for out-of-range count to follow in next word
65 * 31 an 8 bit char followed
66 * 128-255 bigram codes (128 most common, as determined by 'updatedb')
67 * 32-127 single character (printable) ascii residue (ie, literal)
69 * A novel two-tiered string search technique is employed:
71 * First, a metacharacter-free subpattern and partial pathname is matched
72 * BACKWARDS to avoid full expansion of the pathname list. The time savings
73 * is 40-50% over forward matching, which cannot efficiently handle
74 * overlapped search patterns and compressed path residue.
76 * Then, the actual shell glob-style regular expression (if in this form) is
77 * matched against the candidate pathnames using the slower routines provided
78 * in the standard 'find'.
81 #include <sys/param.h>
92 # include <sys/types.h>
93 # include <sys/stat.h>
94 # include <sys/mman.h>
100 #include "pathnames.h"
103 # include <sys/time.h>
104 # include <sys/types.h>
105 # include <sys/resource.h>
108 int f_mmap; /* use mmap */
109 int f_icase; /* ignore case */
110 int f_stdin; /* read database from stdin */
111 int f_statistic; /* print statistic */
112 int f_silent; /* suppress output, show only count of matches */
113 int f_limit; /* limit number of output lines, 0 == infinite */
114 u_int counter; /* counter for matches [-c] */
115 char separator='\n'; /* line separator */
119 void statistic(FILE *, char *);
120 void fastfind(FILE *, char *, char *);
121 void fastfind_icase(FILE *, char *, char *);
122 void fastfind_mmap(char *, caddr_t, int, char *);
123 void fastfind_mmap_icase(char *, caddr_t, int, char *);
124 void search_mmap(char *, char **);
125 void search_fopen(char *, char **);
126 unsigned long cputime(void);
128 extern char **colon(char **, char*, char*);
129 extern void print_matches(u_int);
130 extern int getwm(caddr_t);
131 extern int getwf(FILE *);
132 extern u_char *tolower_word(u_char *);
133 extern int check_bigram_char(int);
134 extern char *patprep(char *);
143 char *path_fcodes; /* locate database */
145 f_mmap = 1; /* mmap is default */
147 (void) setlocale(LC_ALL, "");
149 while ((ch = getopt(argc, argv, "0Scd:il:ms")) != -1)
151 case '0': /* 'find -print0' style */
154 case 'S': /* statistic lines */
157 case 'l': /* limit number of output lines, 0 == infinite */
158 f_limit = atoi(optarg);
160 case 'd': /* database */
161 dbv = colon(dbv, optarg, _PATH_FCODES);
163 case 'i': /* ignore case */
170 warnx("mmap(2) not implemented");
173 case 's': /* stdio lib */
176 case 'c': /* suppress output, show only count of matches */
185 /* to few arguments */
186 if (argc < 1 && !(f_statistic))
189 /* no (valid) database as argument */
190 if (dbv == NULL || *dbv == NULL) {
191 /* try to read database from enviroment */
192 if ((path_fcodes = getenv("LOCATE_PATH")) == NULL ||
193 *path_fcodes == '\0')
194 /* use default database */
195 dbv = colon(dbv, _PATH_FCODES, _PATH_FCODES);
196 else /* $LOCATE_PATH */
197 dbv = colon(dbv, path_fcodes, _PATH_FCODES);
200 if (f_icase && UCHAR_MAX < 4096) /* init tolower lookup table */
201 for (ch = 0; ch < UCHAR_MAX + 1; ch++)
202 myctype[ch] = tolower(ch);
204 /* foreach database ... */
205 while((path_fcodes = *dbv) != NULL) {
208 if (!strcmp(path_fcodes, "-"))
214 f_mmap = 0; /* be paranoid */
216 if (!f_mmap || f_stdin || f_statistic)
217 search_fopen(path_fcodes, argv);
219 search_mmap(path_fcodes, argv);
223 print_matches(counter);
230 char *db; /* database */
231 char **s; /* search strings */
238 /* can only read stdin once */
241 if (*(s+1) != NULL) {
242 warnx("read database from stdin, use only `%s' as pattern", *s);
246 else if ((fp = fopen(db, "r")) == NULL)
249 /* count only chars or lines */
256 /* foreach search string ... */
262 fseek(fp, (long)0, SEEK_SET) == -1)
263 err(1, "fseek to begin of ``%s''\n", db);
266 fastfind_icase(fp, *s, db);
268 fastfind(fp, *s, db);
270 warnx("fastfind %ld ms", cputime () - t0);
280 char *db; /* database */
281 char **s; /* search strings */
290 if ((fd = open(db, O_RDONLY)) == -1 ||
291 fstat(fd, &sb) == -1)
295 errx(1, "database too small: %s", db);
297 if ((p = mmap((caddr_t)0, (size_t)len,
298 PROT_READ, MAP_SHARED,
299 fd, (off_t)0)) == MAP_FAILED)
300 err(1, "mmap ``%s''", db);
302 /* foreach search string ... */
308 fastfind_mmap_icase(*s, p, (int)len, db);
310 fastfind_mmap(*s, p, (int)len, db);
312 warnx("fastfind %ld ms", cputime () - t0);
317 if (munmap(p, (size_t)len) == -1)
318 warn("munmap %s\n", db);
330 getrusage(RUSAGE_SELF, &rus);
331 return(rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000);
338 (void)fprintf(stderr,
339 "usage: locate [-0Scims] [-l limit] [-d database] pattern ...\n\n");
340 (void)fprintf(stderr,
341 "default database: `%s' or $LOCATE_PATH\n", _PATH_FCODES);
346 /* load fastfind functions */
349 /* fastfind_mmap, fastfind_mmap_icase */
355 #include "fastfind.c"
357 #include "fastfind.c"
361 /* fastfind, fastfind_icase */
364 #include "fastfind.c"
366 #include "fastfind.c"