2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1995 Wolfram Schneider <wosch@FreeBSD.org>. Berlin.
5 * Copyright (c) 1989, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 #ifndef _LOCATE_STATISTIC_
44 #define _LOCATE_STATISTIC_
47 statistic (fp, path_fcodes)
48 FILE *fp; /* open database */
49 char *path_fcodes; /* for error message */
51 register int lines, chars, size, big, zwerg;
52 register u_char *p, *s;
55 u_char bigram1[NBG], bigram2[NBG], path[MAXPATHLEN];
57 for (c = 0, p = bigram1, s = bigram2; c < NBG; c++) {
58 p[c] = check_bigram_char(getc(fp));
59 s[c] = check_bigram_char(getc(fp));
62 lines = chars = big = zwerg = umlaut = 0;
65 for (c = getc(fp), count = 0; c != EOF; size++) {
67 count += getwf(fp) - OFFSET;
73 for (p = path + count; (c = getc(fp)) > SWITCH; size++)
92 (void)printf("\nDatabase: %s\n", path_fcodes);
93 (void)printf("Compression: Front: %2.2f%%, ",
94 (size + big - (2 * NBG)) / (chars / (float)100));
95 (void)printf("Bigram: %2.2f%%, ", (size - big) / (size / (float)100));
96 (void)printf("Total: %2.2f%%\n",
97 (size - (2 * NBG)) / (chars / (float)100));
98 (void)printf("Filenames: %d, ", lines);
99 (void)printf("Characters: %d, ", chars);
100 (void)printf("Database size: %d\n", size);
101 (void)printf("Bigram characters: %d, ", big);
102 (void)printf("Integers: %d, ", zwerg);
103 (void)printf("8-Bit characters: %d\n", umlaut);
106 #endif /* _LOCATE_STATISTIC_ */
108 extern char separator;
118 #endif /* FF_ICASE */
119 (pathpart, paddr, len, database)
120 char *pathpart; /* search string */
121 caddr_t paddr; /* mmap pointer */
122 int len; /* length of database */
123 char *database; /* for error message */
133 #endif /* FF_ICASE */
135 (fp, pathpart, database)
136 FILE *fp; /* open database */
137 char *pathpart; /* search string */
138 char *database; /* for error message */
144 register u_char *p, *s, *patend, *q, *foundchar;
146 int count, found, globflag;
148 u_char bigram1[NBG], bigram2[NBG], path[MAXPATHLEN];
151 /* use a lookup table for case insensitive search */
152 u_char table[UCHAR_MAX + 1];
154 tolower_word(pathpart);
157 /* init bigram table */
159 for (c = 0, p = bigram1, s = bigram2; c < NBG; c++, len-= 2) {
160 p[c] = check_bigram_char(*paddr++);
161 s[c] = check_bigram_char(*paddr++);
164 for (c = 0, p = bigram1, s = bigram2; c < NBG; c++) {
165 p[c] = check_bigram_char(getc(fp));
166 s[c] = check_bigram_char(getc(fp));
170 /* find optimal (last) char for searching */
171 for (p = pathpart; *p != '\0'; p++)
172 if (strchr(LOCATE_REG, *p) != NULL)
185 /* set patend char to true */
186 for (c = 0; c < UCHAR_MAX + 1; c++)
189 table[TOLOWER(*patend)] = 1;
190 table[toupper(*patend)] = 1;
191 #endif /* FF_ICASE */
199 c = (u_char)*paddr++; len--;
206 /* go forward or backward */
207 if (c == SWITCH) { /* big step, an integer */
209 count += getwm(paddr) - OFFSET;
210 len -= INTSIZE; paddr += INTSIZE;
212 count += getwf(fp) - OFFSET;
214 } else { /* slow step, =< 14 chars */
218 if (count < 0 || count > MAXPATHLEN)
219 errx(1, "corrupted database: %s", database);
220 /* overlay old path */
226 c = (u_char)*paddr++;
233 * == UMLAUT: 8 bit char followed
236 * rest: single ascii char
238 * offset < SWITCH < UMLAUT < ascii < PARITY < bigram
244 c = (u_char)*paddr++;
257 #endif /* FF_ICASE */
262 /* bigrams are parity-marked */
266 if (bigram1[c] == cc ||
270 if (table[bigram1[c]] ||
272 #endif /* FF_ICASE */
280 if (found) { /* previous line matched */
284 } else if (foundchar >= path + count) { /* a char matched */
286 cutoff = path + count;
287 } else /* nothing to do */
291 for (s = foundchar; s >= cutoff; s--) {
295 #endif /* FF_ICASE */
296 ) { /* fast first char check */
297 for (p = patend - 1, q = s - 1; *p != '\0';
302 #endif /* FF_ICASE */
305 if (*p == '\0') { /* fast match success */
309 !fnmatch(pathpart, path, 0))
311 !fnmatch(pathpart, path,
313 #endif /* !FF_ICASE */
319 if (f_limit >= counter)
320 (void)printf("%s%c",path,separator);
322 errx(0, "[show only %d lines]", counter - 1);
324 (void)printf("%s%c",path,separator);