1 /* 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1995-2022 Wolfram Schneider <wosch@FreeBSD.org> 5 * Copyright (c) 1989, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * James A. Woods. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 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. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * $FreeBSD$ 36 */ 37 38 39 #ifndef _LOCATE_STATISTIC_ 40 #define _LOCATE_STATISTIC_ 41 42 void 43 statistic (fp, path_fcodes) 44 FILE *fp; /* open database */ 45 char *path_fcodes; /* for error message */ 46 { 47 long lines, chars, size, big, zwerg, umlaut; 48 register u_char *p, *s; 49 register int c; 50 int count; 51 int error = 0; 52 u_char bigram1[NBG], bigram2[NBG], path[MAXPATHLEN]; 53 54 for (c = 0, p = bigram1, s = bigram2; c < NBG; c++) { 55 p[c] = check_bigram_char(getc(fp)); 56 s[c] = check_bigram_char(getc(fp)); 57 } 58 59 lines = chars = big = zwerg = umlaut = 0; 60 size = NBG + NBG; 61 62 for (c = getc(fp), count = 0; c != EOF; size++) { 63 if (c == SWITCH) { 64 count += getwf(fp) - OFFSET; 65 size += sizeof(int); 66 zwerg++; 67 } else 68 count += c - OFFSET; 69 70 if (count < 0 || count > MAXPATHLEN) { 71 /* stop on error and display the statstics anyway */ 72 warnx("corrupted database: %s", path_fcodes); 73 error = 1; 74 break; 75 } 76 77 for (p = path + count; (c = getc(fp)) > SWITCH; size++) 78 if (c < PARITY) { 79 if (c == UMLAUT) { 80 c = getc(fp); 81 size++; 82 umlaut++; 83 } 84 p++; 85 } else { 86 /* bigram char */ 87 big++; 88 p += 2; 89 } 90 91 p++; 92 lines++; 93 chars += (p - path); 94 } 95 96 (void)printf("\nDatabase: %s\n", path_fcodes); 97 (void)printf("Compression: Front: %2.2f%%, ", 98 (size + big - (2 * NBG)) / (chars / (float)100)); 99 (void)printf("Bigram: %2.2f%%, ", (size - big) / (size / (float)100)); 100 (void)printf("Total: %2.2f%%\n", 101 (size - (2 * NBG)) / (chars / (float)100)); 102 (void)printf("Filenames: %ld, ", lines); 103 (void)printf("Characters: %ld, ", chars); 104 (void)printf("Database size: %ld\n", size); 105 (void)printf("Bigram characters: %ld, ", big); 106 (void)printf("Integers: %ld, ", zwerg); 107 (void)printf("8-Bit characters: %ld\n", umlaut); 108 109 /* non zero exit on corrupt database */ 110 if (error) 111 exit(error); 112 } 113 #endif /* _LOCATE_STATISTIC_ */ 114 115 extern char separator; 116 117 void 118 #ifdef FF_MMAP 119 120 121 #ifdef FF_ICASE 122 fastfind_mmap_icase 123 #else 124 fastfind_mmap 125 #endif /* FF_ICASE */ 126 (pathpart, paddr, len, database) 127 char *pathpart; /* search string */ 128 caddr_t paddr; /* mmap pointer */ 129 off_t len; /* length of database */ 130 char *database; /* for error message */ 131 132 133 #else /* MMAP */ 134 135 136 #ifdef FF_ICASE 137 fastfind_icase 138 #else 139 fastfind 140 #endif /* FF_ICASE */ 141 142 (fp, pathpart, database) 143 FILE *fp; /* open database */ 144 char *pathpart; /* search string */ 145 char *database; /* for error message */ 146 147 148 #endif /* MMAP */ 149 150 { 151 register u_char *p, *s, *patend, *q, *foundchar; 152 register int c, cc; 153 int count, found, globflag; 154 u_char *cutoff; 155 u_char bigram1[NBG], bigram2[NBG], path[MAXPATHLEN]; 156 157 #ifdef FF_ICASE 158 /* use a lookup table for case insensitive search */ 159 u_char table[UCHAR_MAX + 1]; 160 161 tolower_word(pathpart); 162 #endif /* FF_ICASE*/ 163 164 /* init bigram table */ 165 #ifdef FF_MMAP 166 for (c = 0, p = bigram1, s = bigram2; c < NBG; c++, len-= 2) { 167 p[c] = check_bigram_char(*paddr++); 168 s[c] = check_bigram_char(*paddr++); 169 } 170 #else 171 for (c = 0, p = bigram1, s = bigram2; c < NBG; c++) { 172 p[c] = check_bigram_char(getc(fp)); 173 s[c] = check_bigram_char(getc(fp)); 174 } 175 #endif /* FF_MMAP */ 176 177 /* find optimal (last) char for searching */ 178 for (p = pathpart; *p != '\0'; p++) 179 if (strchr(LOCATE_REG, *p) != NULL) 180 break; 181 182 if (*p == '\0') 183 globflag = 0; 184 else 185 globflag = 1; 186 187 p = pathpart; 188 patend = patprep(p); 189 cc = *patend; 190 191 #ifdef FF_ICASE 192 /* set patend char to true */ 193 for (c = 0; c < UCHAR_MAX + 1; c++) 194 table[c] = 0; 195 196 table[TOLOWER(*patend)] = 1; 197 table[toupper(*patend)] = 1; 198 #endif /* FF_ICASE */ 199 200 201 /* main loop */ 202 found = count = 0; 203 foundchar = 0; 204 205 #ifdef FF_MMAP 206 c = (u_char)*paddr++; len--; 207 for (; len > 0; ) { 208 #else 209 c = getc(fp); 210 for (; c != EOF; ) { 211 #endif /* FF_MMAP */ 212 213 /* go forward or backward */ 214 if (c == SWITCH) { /* big step, an integer */ 215 #ifdef FF_MMAP 216 count += getwm(paddr) - OFFSET; 217 len -= INTSIZE; paddr += INTSIZE; 218 #else 219 count += getwf(fp) - OFFSET; 220 #endif /* FF_MMAP */ 221 } else { /* slow step, =< 14 chars */ 222 count += c - OFFSET; 223 } 224 225 if (count < 0 || count > MAXPATHLEN) 226 errx(1, "corrupted database: %s", database); 227 /* overlay old path */ 228 p = path + count; 229 foundchar = p - 1; 230 231 #ifdef FF_MMAP 232 for (; len > 0;) { 233 c = (u_char)*paddr++; 234 len--; 235 #else 236 for (;;) { 237 c = getc(fp); 238 #endif /* FF_MMAP */ 239 /* 240 * == UMLAUT: 8 bit char followed 241 * <= SWITCH: offset 242 * >= PARITY: bigram 243 * rest: single ascii char 244 * 245 * offset < SWITCH < UMLAUT < ascii < PARITY < bigram 246 */ 247 if (c < PARITY) { 248 if (c <= UMLAUT) { 249 if (c == UMLAUT) { 250 #ifdef FF_MMAP 251 c = (u_char)*paddr++; 252 len--; 253 #else 254 c = getc(fp); 255 #endif /* FF_MMAP */ 256 257 } else 258 break; /* SWITCH */ 259 } 260 #ifdef FF_ICASE 261 if (table[c]) 262 #else 263 if (c == cc) 264 #endif /* FF_ICASE */ 265 foundchar = p; 266 *p++ = c; 267 } 268 else { 269 /* bigrams are parity-marked */ 270 TO7BIT(c); 271 272 #ifndef FF_ICASE 273 if (bigram1[c] == cc || 274 bigram2[c] == cc) 275 #else 276 277 if (table[bigram1[c]] || 278 table[bigram2[c]]) 279 #endif /* FF_ICASE */ 280 foundchar = p + 1; 281 282 *p++ = bigram1[c]; 283 *p++ = bigram2[c]; 284 } 285 } 286 287 if (found) { /* previous line matched */ 288 cutoff = path; 289 *p-- = '\0'; 290 foundchar = p; 291 } else if (foundchar >= path + count) { /* a char matched */ 292 *p-- = '\0'; 293 cutoff = path + count; 294 } else /* nothing to do */ 295 continue; 296 297 found = 0; 298 for (s = foundchar; s >= cutoff; s--) { 299 if (*s == cc 300 #ifdef FF_ICASE 301 || TOLOWER(*s) == cc 302 #endif /* FF_ICASE */ 303 ) { /* fast first char check */ 304 for (p = patend - 1, q = s - 1; *p != '\0'; 305 p--, q--) 306 if (*q != *p 307 #ifdef FF_ICASE 308 && TOLOWER(*q) != *p 309 #endif /* FF_ICASE */ 310 ) 311 break; 312 if (*p == '\0') { /* fast match success */ 313 found = 1; 314 if (!globflag || 315 #ifndef FF_ICASE 316 !fnmatch(pathpart, path, 0)) 317 #else 318 !fnmatch(pathpart, path, 319 FNM_CASEFOLD)) 320 #endif /* !FF_ICASE */ 321 { 322 if (f_silent) 323 counter++; 324 else if (f_limit) { 325 counter++; 326 if (f_limit >= counter) 327 (void)printf("%s%c",path,separator); 328 else 329 errx(0, "[show only %ld lines]", counter - 1); 330 } else 331 (void)printf("%s%c",path,separator); 332 } 333 break; 334 } 335 } 336 } 337 } 338 } 339