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, size_nbg, big, zwerg, umlaut; 48 u_char *p, *s; 49 int c; 50 int count, longest_path; 51 int error = 0; 52 u_char bigram1[NBG], bigram2[NBG], path[LOCATE_PATH_MAX]; 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 = longest_path = 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 >= LOCATE_PATH_MAX) { 71 /* stop on error and display the statstics anyway */ 72 warnx("corrupted database: %s %d", path_fcodes, count); 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 if ((p - path) > longest_path) 95 longest_path = p - path; 96 } 97 98 /* size without bigram db */ 99 size_nbg = size - (2 * NBG); 100 101 (void)printf("\nDatabase: %s\n", path_fcodes); 102 (void)printf("Compression: Front: %2.2f%%, ", chars > 0 ? (size_nbg + big) / (chars / (float)100) : 0); 103 (void)printf("Bigram: %2.2f%%, ", big > 0 ? (size_nbg - big) / (size_nbg / (float)100) : 0); 104 /* incl. bigram db overhead */ 105 (void)printf("Total: %2.2f%%\n", chars > 0 ? size / (chars / (float)100) : 0); 106 (void)printf("Filenames: %ld, ", lines); 107 (void)printf("Characters: %ld, ", chars); 108 (void)printf("Database size: %ld\n", size); 109 (void)printf("Bigram characters: %ld, ", big); 110 (void)printf("Integers: %ld, ", zwerg); 111 (void)printf("8-Bit characters: %ld\n", umlaut); 112 printf("Longest path: %d\n", longest_path > 0 ? longest_path - 1 : 0); 113 114 /* non zero exit on corrupt database */ 115 if (error) 116 exit(error); 117 } 118 #endif /* _LOCATE_STATISTIC_ */ 119 120 extern char separator; 121 122 void 123 #ifdef FF_MMAP 124 125 126 #ifdef FF_ICASE 127 fastfind_mmap_icase 128 #else 129 fastfind_mmap 130 #endif /* FF_ICASE */ 131 (pathpart, paddr, len, database) 132 char *pathpart; /* search string */ 133 caddr_t paddr; /* mmap pointer */ 134 off_t len; /* length of database */ 135 char *database; /* for error message */ 136 137 138 #else /* MMAP */ 139 140 141 #ifdef FF_ICASE 142 fastfind_icase 143 #else 144 fastfind 145 #endif /* FF_ICASE */ 146 147 (fp, pathpart, database) 148 FILE *fp; /* open database */ 149 char *pathpart; /* search string */ 150 char *database; /* for error message */ 151 152 153 #endif /* MMAP */ 154 155 { 156 u_char *p, *s, *patend, *q, *foundchar; 157 int c, cc; 158 int count, found, globflag; 159 u_char *cutoff; 160 u_char bigram1[NBG], bigram2[NBG], path[LOCATE_PATH_MAX + 2]; 161 162 #ifdef FF_ICASE 163 /* use a lookup table for case insensitive search */ 164 u_char table[UCHAR_MAX + 1]; 165 166 tolower_word(pathpart); 167 #endif /* FF_ICASE*/ 168 169 /* init bigram table */ 170 #ifdef FF_MMAP 171 for (c = 0, p = bigram1, s = bigram2; c < NBG; c++, len-= 2) { 172 p[c] = check_bigram_char(*paddr++); 173 s[c] = check_bigram_char(*paddr++); 174 } 175 #else 176 for (c = 0, p = bigram1, s = bigram2; c < NBG; c++) { 177 p[c] = check_bigram_char(getc(fp)); 178 s[c] = check_bigram_char(getc(fp)); 179 } 180 #endif /* FF_MMAP */ 181 182 /* find optimal (last) char for searching */ 183 for (p = pathpart; *p != '\0'; p++) 184 if (strchr(LOCATE_REG, *p) != NULL) 185 break; 186 187 if (*p == '\0') 188 globflag = 0; 189 else 190 globflag = 1; 191 192 p = pathpart; 193 patend = patprep(p); 194 cc = *patend; 195 196 #ifdef FF_ICASE 197 /* set patend char to true */ 198 for (c = 0; c < UCHAR_MAX + 1; c++) 199 table[c] = 0; 200 201 table[TOLOWER(*patend)] = 1; 202 table[toupper(*patend)] = 1; 203 #endif /* FF_ICASE */ 204 205 206 /* main loop */ 207 found = count = 0; 208 foundchar = 0; 209 210 #ifdef FF_MMAP 211 c = (u_char)*paddr++; 212 len--; 213 214 for (; len > 0; ) { 215 #else 216 c = getc(fp); 217 for (; c != EOF; ) { 218 #endif /* FF_MMAP */ 219 220 /* go forward or backward */ 221 if (c == SWITCH) { /* big step, an integer */ 222 #ifdef FF_MMAP 223 if (len < sizeof(int)) 224 errx(1, "corrupted database: %s", database); 225 226 count += getwm(paddr) - OFFSET; 227 len -= INTSIZE; 228 paddr += INTSIZE; 229 #else 230 count += getwf(fp) - OFFSET; 231 #endif /* FF_MMAP */ 232 } else { /* slow step, =< 14 chars */ 233 count += c - OFFSET; 234 } 235 236 if (count < 0 || count >= LOCATE_PATH_MAX) 237 errx(1, "corrupted database: %s %d", database, count); 238 239 /* overlay old path */ 240 p = path + count; 241 foundchar = p - 1; 242 243 #ifdef FF_MMAP 244 for (; len > 0;) { 245 c = (u_char)*paddr++; 246 len--; 247 #else 248 for (;;) { 249 c = getc(fp); 250 #endif /* FF_MMAP */ 251 /* 252 * == UMLAUT: 8 bit char followed 253 * <= SWITCH: offset 254 * >= PARITY: bigram 255 * rest: single ascii char 256 * 257 * offset < SWITCH < UMLAUT < ascii < PARITY < bigram 258 */ 259 if (c < PARITY) { 260 if (c <= UMLAUT) { 261 if (c == UMLAUT) { 262 #ifdef FF_MMAP 263 c = (u_char)*paddr++; 264 len--; 265 #else 266 c = getc(fp); 267 #endif /* FF_MMAP */ 268 269 } else 270 break; /* SWITCH */ 271 } 272 #ifdef FF_ICASE 273 if (table[c]) 274 #else 275 if (c == cc) 276 #endif /* FF_ICASE */ 277 foundchar = p; 278 *p++ = c; 279 } 280 else { 281 /* bigrams are parity-marked */ 282 TO7BIT(c); 283 284 #ifndef FF_ICASE 285 if (bigram1[c] == cc || 286 bigram2[c] == cc) 287 #else 288 289 if (table[bigram1[c]] || 290 table[bigram2[c]]) 291 #endif /* FF_ICASE */ 292 foundchar = p + 1; 293 294 *p++ = bigram1[c]; 295 *p++ = bigram2[c]; 296 } 297 298 if (p - path >= LOCATE_PATH_MAX) 299 errx(1, "corrupted database: %s %td", database, p - path); 300 301 } 302 303 if (found) { /* previous line matched */ 304 cutoff = path; 305 *p-- = '\0'; 306 foundchar = p; 307 } else if (foundchar >= path + count) { /* a char matched */ 308 *p-- = '\0'; 309 cutoff = path + count; 310 } else /* nothing to do */ 311 continue; 312 313 found = 0; 314 for (s = foundchar; s >= cutoff; s--) { 315 if (*s == cc 316 #ifdef FF_ICASE 317 || TOLOWER(*s) == cc 318 #endif /* FF_ICASE */ 319 ) { /* fast first char check */ 320 for (p = patend - 1, q = s - 1; *p != '\0'; 321 p--, q--) 322 if (*q != *p 323 #ifdef FF_ICASE 324 && TOLOWER(*q) != *p 325 #endif /* FF_ICASE */ 326 ) 327 break; 328 if (*p == '\0') { /* fast match success */ 329 found = 1; 330 if (!globflag || 331 #ifndef FF_ICASE 332 !fnmatch(pathpart, path, 0)) 333 #else 334 !fnmatch(pathpart, path, 335 FNM_CASEFOLD)) 336 #endif /* !FF_ICASE */ 337 { 338 if (f_silent) 339 counter++; 340 else if (f_limit) { 341 counter++; 342 if (f_limit >= counter) 343 (void)printf("%s%c",path,separator); 344 else 345 errx(0, "[show only %ld lines]", counter - 1); 346 } else 347 (void)printf("%s%c",path,separator); 348 } 349 break; 350 } 351 } 352 } 353 } 354 } 355