]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/locate/locate/locate.c
This commit was generated by cvs2svn to compensate for changes in r52878,
[FreeBSD/FreeBSD.git] / usr.bin / locate / locate / locate.c
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
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";
43 #endif /* not lint */
44
45 #ifndef lint
46 #if 0
47 static char sccsid[] = "@(#)locate.c    8.1 (Berkeley) 6/6/93";
48 #endif
49 static const char rcsid[] =
50   "$FreeBSD$";
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
115 char *path_fcodes;      /* locate database */
116 int f_mmap;             /* use mmap */
117 int f_icase;            /* ignore case */
118 int f_stdin;            /* read database from stdin */
119 int f_statistic;        /* print statistic */
120 int f_silent;           /* suppress output, show only count of matches */
121 int f_limit;            /* limit number of output lines, 0 == infinite */
122 u_int counter;          /* counter for matches [-c] */
123
124
125 void    usage __P((void));
126 void    statistic __P((FILE *, char *));
127 void    fastfind __P((FILE *, char *, char *));
128 void    fastfind_icase __P((FILE *, char *, char *));
129 void    fastfind_mmap __P((char *, caddr_t, int, char *));
130 void    fastfind_mmap_icase __P((char *, caddr_t, int, char *));
131 void    search_mmap __P((char *, char **));
132 void    search_fopen __P((char *, char **));
133 unsigned long cputime __P((void));
134
135 extern char     **colon __P((char **, char*, char*));
136 extern void     print_matches __P((u_int));
137 extern int      getwm __P((caddr_t));
138 extern int      getwf __P((FILE *));
139 extern u_char   *tolower_word __P((u_char *));
140 extern int      check_bigram_char __P((int));
141 extern char     *patprep __P((char *));
142
143 extern char     *optarg;
144 extern int      optind;
145
146
147 int
148 main(argc, argv)
149         int argc;
150         char **argv;
151 {
152         register int ch;
153         char **dbv = NULL;
154 #ifdef MMAP
155         f_mmap = 1;             /* mmap is default */
156 #endif
157         (void) setlocale(LC_ALL, "");
158
159         while ((ch = getopt(argc, argv, "Scd:il:ms")) != -1)
160                 switch(ch) {
161                 case 'S':       /* statistic lines */   
162                         f_statistic = 1;
163                         break;
164                 case 'l': /* limit number of output lines, 0 == infinite */
165                         f_limit = atoi(optarg);
166                         break;
167                 case 'd':       /* database */
168                         dbv = colon(dbv, optarg, _PATH_FCODES);
169                         break;
170                 case 'i':       /* ignore case */
171                         f_icase = 1;
172                         break;
173                 case 'm':       /* mmap */
174 #ifdef MMAP
175                         f_mmap = 1;
176 #else
177                                                 warnx("mmap(2) not implemented");
178 #endif
179                         break;
180                 case 's':       /* stdio lib */
181                         f_mmap = 0;
182                         break;
183                 case 'c': /* suppress output, show only count of matches */
184                         f_silent = 1;
185                         break;
186                 default:
187                         usage();
188                 }
189         argv += optind;
190         argc -= optind;
191
192         /* to few arguments */
193         if (argc < 1 && !(f_statistic))
194                 usage();
195
196         /* no (valid) database as argument */
197         if (dbv == NULL || *dbv == NULL) {
198                 /* try to read database from enviroment */
199                 if ((path_fcodes = getenv("LOCATE_PATH")) == NULL ||
200                      *path_fcodes == '\0')
201                         /* use default database */
202                         dbv = colon(dbv, _PATH_FCODES, _PATH_FCODES);
203                 else            /* $LOCATE_PATH */
204                         dbv = colon(dbv, path_fcodes, _PATH_FCODES);
205         }
206
207         if (f_icase && UCHAR_MAX < 4096) /* init tolower lookup table */
208                 for (ch = 0; ch < UCHAR_MAX + 1; ch++)
209                         myctype[ch] = tolower(ch);
210
211         /* foreach database ... */
212         while((path_fcodes = *dbv) != NULL) {
213                 dbv++;
214
215                 if (!strcmp(path_fcodes, "-"))
216                         f_stdin = 1;
217                 else
218                         f_stdin = 0;
219
220 #ifndef MMAP
221                 f_mmap = 0;     /* be paranoid */
222 #endif
223                 if (!f_mmap || f_stdin || f_statistic) 
224                         search_fopen(path_fcodes, argv);
225                 else 
226                         search_mmap(path_fcodes, argv);
227         }
228
229         if (f_silent)
230                 print_matches(counter);
231         exit(0);
232 }
233
234
235 void
236 search_fopen(db, s)
237         char *db; /* database */
238         char **s; /* search strings */
239 {
240         FILE *fp;
241 #ifdef DEBUG
242         long t0;
243 #endif
244                
245         /* can only read stdin once */
246         if (f_stdin) { 
247                 fp = stdin;
248                 if (*(s+1) != NULL) {
249                         warnx("read database from stdin, use only `%s' as pattern", *s);
250                         *(s+1) = NULL;
251                 }
252         } 
253         else if ((fp = fopen(path_fcodes, "r")) == NULL)
254                 err(1,  "`%s'", path_fcodes);
255
256         /* count only chars or lines */
257         if (f_statistic) {
258                 statistic(fp, path_fcodes);
259                 (void)fclose(fp);
260                 return;
261         }
262
263         /* foreach search string ... */
264         while(*s != NULL) {
265 #ifdef DEBUG
266                 t0 = cputime();
267 #endif
268                 if (!f_stdin &&
269                     fseek(fp, (long)0, SEEK_SET) == -1)
270                         err(1, "fseek to begin of ``%s''\n", path_fcodes);
271
272                 if (f_icase)
273                         fastfind_icase(fp, *s, path_fcodes);
274                 else
275                         fastfind(fp, *s, path_fcodes);
276 #ifdef DEBUG
277                 warnx("fastfind %ld ms", cputime () - t0);
278 #endif
279                 s++;
280         } 
281         (void)fclose(fp);
282
283
284 #ifdef MMAP
285 void
286 search_mmap(db, s)
287         char *db; /* database */
288         char **s; /* search strings */
289 {
290         struct stat sb;
291         int fd;
292         caddr_t p;
293         off_t len;
294 #ifdef DEBUG
295         long t0;
296 #endif
297         if ((fd = open(path_fcodes, O_RDONLY)) == -1 ||
298             fstat(fd, &sb) == -1)
299                 err(1, "`%s'", path_fcodes);
300         len = sb.st_size;
301
302         if ((p = mmap((caddr_t)0, (size_t)len,
303                       PROT_READ, MAP_SHARED,
304                       fd, (off_t)0)) == MAP_FAILED)
305                 err(1, "mmap ``%s''", path_fcodes);
306
307         /* foreach search string ... */
308         while (*s != NULL) {
309 #ifdef DEBUG
310                 t0 = cputime();
311 #endif
312                 if (f_icase)
313                         fastfind_mmap_icase(*s, p, (int)len, path_fcodes);
314                 else
315                         fastfind_mmap(*s, p, (int)len, path_fcodes);
316 #ifdef DEBUG
317                 warnx("fastfind %ld ms", cputime () - t0);
318 #endif
319                 s++;
320         }
321
322         if (munmap(p, (size_t)len) == -1)
323                 warn("munmap %s\n", path_fcodes);
324         
325         (void)close(fd);
326 }
327 #endif /* MMAP */
328
329 #ifdef DEBUG
330 unsigned long
331 cputime ()
332 {
333         struct rusage rus;
334
335         getrusage(0, &rus);
336         return(rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000);
337 }
338 #endif /* DEBUG */
339
340 void
341 usage ()
342 {
343         (void)fprintf(stderr,
344         "usage: locate [-Scims] [-l limit] [-d database] pattern ...\n\n");
345         (void)fprintf(stderr,
346         "default database: `%s' or $LOCATE_PATH\n", _PATH_FCODES);
347         exit(1);
348 }
349
350
351 /* load fastfind functions */
352
353 /* statistic */
354 /* fastfind_mmap, fastfind_mmap_icase */
355 #ifdef MMAP
356 #undef FF_MMAP
357 #undef FF_ICASE
358
359 #define FF_MMAP
360 #include "fastfind.c"
361 #define FF_ICASE
362 #include "fastfind.c"
363 #endif /* MMAP */
364
365 /* fopen */
366 /* fastfind, fastfind_icase */
367 #undef FF_MMAP
368 #undef FF_ICASE
369 #include "fastfind.c"
370 #define FF_ICASE
371 #include "fastfind.c"