Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.bin / locate / locate / locate.c
CommitLineData
984263bc
MD
1/*
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.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * James A. Woods.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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.
24 *
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
35 * SUCH DAMAGE.
36 */
37
38#ifndef lint
39static 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";
43#endif /* not lint */
44
45#ifndef lint
46#if 0
47static char sccsid[] = "@(#)locate.c 8.1 (Berkeley) 6/6/93";
48#endif
49static const char rcsid[] =
50 "$FreeBSD: src/usr.bin/locate/locate/locate.c,v 1.12.2.1 2001/03/04 08:47:25 kris Exp $";
51#endif /* not lint */
52
53/*
54 * Ref: Usenix ;login:, Vol 8, No 1, February/March, 1983, p. 8.
55 *
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%.
60 *
61 * The codes are:
62 *
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)
68 *
69 * A novel two-tiered string search technique is employed:
70 *
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.
75 *
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'.
79 */
80
81#include <sys/param.h>
82#include <ctype.h>
83#include <err.h>
84#include <fnmatch.h>
85#include <locale.h>
86#include <stdio.h>
87#include <stdlib.h>
88#include <string.h>
89#include <unistd.h>
90
91#ifdef MMAP
92# include <sys/types.h>
93# include <sys/stat.h>
94# include <sys/mman.h>
95# include <fcntl.h>
96#endif
97
98
99#ifdef sun
100#include <netinet/in.h> /* SunOS byteorder(3) htohl(3) */
101#ifndef __P
102#define __P(x) x
103#endif
104#endif
105
106#include "locate.h"
107#include "pathnames.h"
108
109#ifdef DEBUG
110# include <sys/time.h>
111# include <sys/types.h>
112# include <sys/resource.h>
113#endif
114
115char *path_fcodes; /* locate database */
116int f_mmap; /* use mmap */
117int f_icase; /* ignore case */
118int f_stdin; /* read database from stdin */
119int f_statistic; /* print statistic */
120int f_silent; /* suppress output, show only count of matches */
121int f_limit; /* limit number of output lines, 0 == infinite */
122u_int counter; /* counter for matches [-c] */
123
124
125void usage __P((void));
126void statistic __P((FILE *, char *));
127void fastfind __P((FILE *, char *, char *));
128void fastfind_icase __P((FILE *, char *, char *));
129void fastfind_mmap __P((char *, caddr_t, int, char *));
130void fastfind_mmap_icase __P((char *, caddr_t, int, char *));
131void search_mmap __P((char *, char **));
132void search_fopen __P((char *, char **));
133unsigned long cputime __P((void));
134
135extern char **colon __P((char **, char*, char*));
136extern void print_matches __P((u_int));
137extern int getwm __P((caddr_t));
138extern int getwf __P((FILE *));
139extern u_char *tolower_word __P((u_char *));
140extern int check_bigram_char __P((int));
141extern char *patprep __P((char *));
142
143int
144main(argc, argv)
145 int argc;
146 char **argv;
147{
148 register int ch;
149 char **dbv = NULL;
150#ifdef MMAP
151 f_mmap = 1; /* mmap is default */
152#endif
153 (void) setlocale(LC_ALL, "");
154
155 while ((ch = getopt(argc, argv, "Scd:il:ms")) != -1)
156 switch(ch) {
157 case 'S': /* statistic lines */
158 f_statistic = 1;
159 break;
160 case 'l': /* limit number of output lines, 0 == infinite */
161 f_limit = atoi(optarg);
162 break;
163 case 'd': /* database */
164 dbv = colon(dbv, optarg, _PATH_FCODES);
165 break;
166 case 'i': /* ignore case */
167 f_icase = 1;
168 break;
169 case 'm': /* mmap */
170#ifdef MMAP
171 f_mmap = 1;
172#else
173 warnx("mmap(2) not implemented");
174#endif
175 break;
176 case 's': /* stdio lib */
177 f_mmap = 0;
178 break;
179 case 'c': /* suppress output, show only count of matches */
180 f_silent = 1;
181 break;
182 default:
183 usage();
184 }
185 argv += optind;
186 argc -= optind;
187
188 /* to few arguments */
189 if (argc < 1 && !(f_statistic))
190 usage();
191
192 /* no (valid) database as argument */
193 if (dbv == NULL || *dbv == NULL) {
194 /* try to read database from enviroment */
195 if ((path_fcodes = getenv("LOCATE_PATH")) == NULL ||
196 *path_fcodes == '\0')
197 /* use default database */
198 dbv = colon(dbv, _PATH_FCODES, _PATH_FCODES);
199 else /* $LOCATE_PATH */
200 dbv = colon(dbv, path_fcodes, _PATH_FCODES);
201 }
202
203 if (f_icase && UCHAR_MAX < 4096) /* init tolower lookup table */
204 for (ch = 0; ch < UCHAR_MAX + 1; ch++)
205 myctype[ch] = tolower(ch);
206
207 /* foreach database ... */
208 while((path_fcodes = *dbv) != NULL) {
209 dbv++;
210
211 if (!strcmp(path_fcodes, "-"))
212 f_stdin = 1;
213 else
214 f_stdin = 0;
215
216#ifndef MMAP
217 f_mmap = 0; /* be paranoid */
218#endif
219 if (!f_mmap || f_stdin || f_statistic)
220 search_fopen(path_fcodes, argv);
221 else
222 search_mmap(path_fcodes, argv);
223 }
224
225 if (f_silent)
226 print_matches(counter);
227 exit(0);
228}
229
230
231void
232search_fopen(db, s)
233 char *db; /* database */
234 char **s; /* search strings */
235{
236 FILE *fp;
237#ifdef DEBUG
238 long t0;
239#endif
240
241 /* can only read stdin once */
242 if (f_stdin) {
243 fp = stdin;
244 if (*(s+1) != NULL) {
245 warnx("read database from stdin, use only `%s' as pattern", *s);
246 *(s+1) = NULL;
247 }
248 }
249 else if ((fp = fopen(path_fcodes, "r")) == NULL)
250 err(1, "`%s'", path_fcodes);
251
252 /* count only chars or lines */
253 if (f_statistic) {
254 statistic(fp, path_fcodes);
255 (void)fclose(fp);
256 return;
257 }
258
259 /* foreach search string ... */
260 while(*s != NULL) {
261#ifdef DEBUG
262 t0 = cputime();
263#endif
264 if (!f_stdin &&
265 fseek(fp, (long)0, SEEK_SET) == -1)
266 err(1, "fseek to begin of ``%s''\n", path_fcodes);
267
268 if (f_icase)
269 fastfind_icase(fp, *s, path_fcodes);
270 else
271 fastfind(fp, *s, path_fcodes);
272#ifdef DEBUG
273 warnx("fastfind %ld ms", cputime () - t0);
274#endif
275 s++;
276 }
277 (void)fclose(fp);
278}
279
280#ifdef MMAP
281void
282search_mmap(db, s)
283 char *db; /* database */
284 char **s; /* search strings */
285{
286 struct stat sb;
287 int fd;
288 caddr_t p;
289 off_t len;
290#ifdef DEBUG
291 long t0;
292#endif
293 if ((fd = open(path_fcodes, O_RDONLY)) == -1 ||
294 fstat(fd, &sb) == -1)
295 err(1, "`%s'", path_fcodes);
296 len = sb.st_size;
297
298 if ((p = mmap((caddr_t)0, (size_t)len,
299 PROT_READ, MAP_SHARED,
300 fd, (off_t)0)) == MAP_FAILED)
301 err(1, "mmap ``%s''", path_fcodes);
302
303 /* foreach search string ... */
304 while (*s != NULL) {
305#ifdef DEBUG
306 t0 = cputime();
307#endif
308 if (f_icase)
309 fastfind_mmap_icase(*s, p, (int)len, path_fcodes);
310 else
311 fastfind_mmap(*s, p, (int)len, path_fcodes);
312#ifdef DEBUG
313 warnx("fastfind %ld ms", cputime () - t0);
314#endif
315 s++;
316 }
317
318 if (munmap(p, (size_t)len) == -1)
319 warn("munmap %s\n", path_fcodes);
320
321 (void)close(fd);
322}
323#endif /* MMAP */
324
325#ifdef DEBUG
326unsigned long
327cputime ()
328{
329 struct rusage rus;
330
331 getrusage(0, &rus);
332 return(rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000);
333}
334#endif /* DEBUG */
335
336void
337usage ()
338{
339 (void)fprintf(stderr,
340 "usage: locate [-Scims] [-l limit] [-d database] pattern ...\n\n");
341 (void)fprintf(stderr,
342 "default database: `%s' or $LOCATE_PATH\n", _PATH_FCODES);
343 exit(1);
344}
345
346
347/* load fastfind functions */
348
349/* statistic */
350/* fastfind_mmap, fastfind_mmap_icase */
351#ifdef MMAP
352#undef FF_MMAP
353#undef FF_ICASE
354
355#define FF_MMAP
356#include "fastfind.c"
357#define FF_ICASE
358#include "fastfind.c"
359#endif /* MMAP */
360
361/* fopen */
362/* fastfind, fastfind_icase */
363#undef FF_MMAP
364#undef FF_ICASE
365#include "fastfind.c"
366#define FF_ICASE
367#include "fastfind.c"