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
37 * @(#) Copyright (c) 1995-1996 Wolfram Schneider, Berlin. @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
38 * @(#)locate.c 8.1 (Berkeley) 6/6/93
39 * $FreeBSD: src/usr.bin/locate/locate/locate.c,v 1.12.2.1 2001/03/04 08:47:25 kris Exp $
40 * $DragonFly: src/usr.bin/locate/locate/locate.c,v 1.4 2003/11/06 19:30:04 eirikn Exp $
44 * Ref: Usenix ;login:, Vol 8, No 1, February/March, 1983, p. 8.
46 * Locate scans a file list for the full pathname of a file given only part
47 * of the name. The list has been processed with with "front-compression"
48 * and bigram coding. Front compression reduces space by a factor of 4-5,
49 * bigram coding by a further 20-25%.
53 * 0-28 likeliest differential counts + offset to make nonnegative
54 * 30 switch code for out-of-range count to follow in next word
55 * 31 an 8 bit char followed
56 * 128-255 bigram codes (128 most common, as determined by 'updatedb')
57 * 32-127 single character (printable) ascii residue (ie, literal)
59 * A novel two-tiered string search technique is employed:
61 * First, a metacharacter-free subpattern and partial pathname is matched
62 * BACKWARDS to avoid full expansion of the pathname list. The time savings
63 * is 40-50% over forward matching, which cannot efficiently handle
64 * overlapped search patterns and compressed path residue.
66 * Then, the actual shell glob-style regular expression (if in this form) is
67 * matched against the candidate pathnames using the slower routines provided
68 * in the standard 'find'.
71 #include <sys/param.h>
82 # include <sys/types.h>
83 # include <sys/stat.h>
84 # include <sys/mman.h>
90 #include <netinet/in.h> /* SunOS byteorder(3) htohl(3) */
94 #include "pathnames.h"
97 # include <sys/time.h>
98 # include <sys/types.h>
99 # include <sys/resource.h>
102 char *path_fcodes; /* locate database */
103 int f_mmap; /* use mmap */
104 int f_icase; /* ignore case */
105 int f_stdin; /* read database from stdin */
106 int f_statistic; /* print statistic */
107 int f_silent; /* suppress output, show only count of matches */
108 int f_limit; /* limit number of output lines, 0 == infinite */
109 u_int counter; /* counter for matches [-c] */
113 void statistic(FILE *, char *);
114 void fastfind(FILE *, char *, char *);
115 void fastfind_icase(FILE *, char *, char *);
116 void fastfind_mmap(char *, caddr_t, int, char *);
117 void fastfind_mmap_icase(char *, caddr_t, int, char *);
118 void search_mmap(char *, char **);
119 void search_fopen(char *, char **);
120 unsigned long cputime(void);
122 extern char **colon(char **, char*, char*);
123 extern void print_matches(u_int);
124 extern int getwm(caddr_t);
125 extern int getwf(FILE *);
126 extern u_char *tolower_word(u_char *);
127 extern int check_bigram_char(int);
128 extern char *patprep(char *);
138 f_mmap = 1; /* mmap is default */
140 (void) setlocale(LC_ALL, "");
142 while ((ch = getopt(argc, argv, "Scd:il:ms")) != -1)
144 case 'S': /* statistic lines */
147 case 'l': /* limit number of output lines, 0 == infinite */
148 f_limit = atoi(optarg);
150 case 'd': /* database */
151 dbv = colon(dbv, optarg, _PATH_FCODES);
153 case 'i': /* ignore case */
160 warnx("mmap(2) not implemented");
163 case 's': /* stdio lib */
166 case 'c': /* suppress output, show only count of matches */
175 /* to few arguments */
176 if (argc < 1 && !(f_statistic))
179 /* no (valid) database as argument */
180 if (dbv == NULL || *dbv == NULL) {
181 /* try to read database from enviroment */
182 if ((path_fcodes = getenv("LOCATE_PATH")) == NULL ||
183 *path_fcodes == '\0')
184 /* use default database */
185 dbv = colon(dbv, _PATH_FCODES, _PATH_FCODES);
186 else /* $LOCATE_PATH */
187 dbv = colon(dbv, path_fcodes, _PATH_FCODES);
190 if (f_icase && UCHAR_MAX < 4096) /* init tolower lookup table */
191 for (ch = 0; ch < UCHAR_MAX + 1; ch++)
192 myctype[ch] = tolower(ch);
194 /* foreach database ... */
195 while((path_fcodes = *dbv) != NULL) {
198 if (!strcmp(path_fcodes, "-"))
204 f_mmap = 0; /* be paranoid */
206 if (!f_mmap || f_stdin || f_statistic)
207 search_fopen(path_fcodes, argv);
209 search_mmap(path_fcodes, argv);
213 print_matches(counter);
220 char *db; /* database */
221 char **s; /* search strings */
228 /* can only read stdin once */
231 if (*(s+1) != NULL) {
232 warnx("read database from stdin, use only `%s' as pattern", *s);
236 else if ((fp = fopen(path_fcodes, "r")) == NULL)
237 err(1, "`%s'", path_fcodes);
239 /* count only chars or lines */
241 statistic(fp, path_fcodes);
246 /* foreach search string ... */
252 fseek(fp, (long)0, SEEK_SET) == -1)
253 err(1, "fseek to begin of ``%s''\n", path_fcodes);
256 fastfind_icase(fp, *s, path_fcodes);
258 fastfind(fp, *s, path_fcodes);
260 warnx("fastfind %ld ms", cputime () - t0);
270 char *db; /* database */
271 char **s; /* search strings */
280 if ((fd = open(path_fcodes, O_RDONLY)) == -1 ||
281 fstat(fd, &sb) == -1)
282 err(1, "`%s'", path_fcodes);
285 if ((p = mmap((caddr_t)0, (size_t)len,
286 PROT_READ, MAP_SHARED,
287 fd, (off_t)0)) == MAP_FAILED)
288 err(1, "mmap ``%s''", path_fcodes);
290 /* foreach search string ... */
296 fastfind_mmap_icase(*s, p, (int)len, path_fcodes);
298 fastfind_mmap(*s, p, (int)len, path_fcodes);
300 warnx("fastfind %ld ms", cputime () - t0);
305 if (munmap(p, (size_t)len) == -1)
306 warn("munmap %s\n", path_fcodes);
319 return(rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000);
326 (void)fprintf(stderr,
327 "usage: locate [-Scims] [-l limit] [-d database] pattern ...\n\n");
328 (void)fprintf(stderr,
329 "default database: `%s' or $LOCATE_PATH\n", _PATH_FCODES);
334 /* load fastfind functions */
337 /* fastfind_mmap, fastfind_mmap_icase */
343 #include "fastfind.c"
345 #include "fastfind.c"
349 /* fastfind, fastfind_icase */
352 #include "fastfind.c"
354 #include "fastfind.c"