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 * $Id: fastfind.c,v 1.2 1996/08/29 22:39:41 wosch Exp wosch $ 38 */ 39 40 41 #ifndef _LOCATE_STATISTIC_ 42 #define _LOCATE_STATISTIC_ 43 44 void 45 statistic (fp, path_fcodes) 46 FILE *fp; /* open database */ 47 char *path_fcodes; /* for error message */ 48 { 49 register int lines, chars, size, big; 50 register u_char *p, *s; 51 register int c; 52 int count; 53 u_char bigram1[NBG], bigram2[NBG], path[MAXPATHLEN]; 54 55 for (c = 0, p = bigram1, s = bigram2; c < NBG; c++) { 56 p[c] = check_bigram_char(getc(fp)); 57 s[c] = check_bigram_char(getc(fp)); 58 } 59 60 lines = chars = big = 0; 61 size = NBG + NBG; 62 63 for (c = getc(fp), count = 0; c != EOF; size++) { 64 if (c == SWITCH) { 65 count += getwf(fp) - OFFSET; 66 size += sizeof(int); 67 } else 68 count += c - OFFSET; 69 70 for (p = path + count; (c = getc(fp)) > SWITCH; size++) 71 if (c < PARITY) 72 p++; 73 else { 74 big++; 75 p += 2; 76 } 77 78 p++; 79 lines++; 80 chars += (p - path); 81 } 82 83 (void)printf("\nDatabase: %s\n", path_fcodes); 84 (void)printf("Compression: Front: %2.2f%%, ", 85 (float)(100 * (size + big)) / chars); 86 (void)printf("Bigram: %2.2f%%, ", (float)(100 * (size - big)) / size); 87 (void)printf("Total: %2.2f%%\n", (float)(100 * size) / chars); 88 (void)printf("Filenames: %d, ", lines); 89 (void)printf("Chars: %d\n", chars); 90 (void)printf("Database size: %d, ", size); 91 (void)printf("Bigram chars: %d\n", big); 92 93 } 94 #endif /* _LOCATE_STATISTIC_ */ 95 96 97 void 98 #ifdef FF_MMAP 99 100 101 #ifdef FF_ICASE 102 fastfind_mmap_icase 103 #else 104 fastfind_mmap 105 #endif 106 (pathpart, paddr, len, database) 107 char *pathpart; /* search string */ 108 caddr_t paddr; /* mmap pointer */ 109 int len; /* length of database */ 110 char *database; /* for error message */ 111 112 113 #else /* MMAP */ 114 115 116 #ifdef FF_ICASE 117 fastfind_icase 118 #else /* !FF_ICASE */ 119 fastfind 120 #endif /* FF_ICASE */ 121 122 (fp, pathpart, database) 123 FILE *fp; /* open database */ 124 char *pathpart; /* search string */ 125 char *database; /* for error message */ 126 127 128 #endif /* MMAP */ 129 130 { 131 register u_char *p, *s, *patend, *q, *foundchar; 132 register int c, cc; 133 int count, found, globflag; 134 u_char *cutoff; 135 u_char bigram1[NBG], bigram2[NBG], path[MAXPATHLEN]; 136 137 #ifdef FF_ICASE 138 /* use a lookup table for case insensitive search */ 139 u_char table[UCHAR_MAX]; 140 141 tolower_word(pathpart); 142 #endif 143 144 /* init bigram table */ 145 #ifdef FF_MMAP 146 if (len < (2*NBG)) { 147 (void)fprintf(stderr, "database to small: %s\n", database); 148 exit(1); 149 } 150 151 for (c = 0, p = bigram1, s = bigram2; c < NBG; c++, len-= 2) { 152 p[c] = check_bigram_char(*paddr++); 153 s[c] = check_bigram_char(*paddr++); 154 } 155 #else 156 for (c = 0, p = bigram1, s = bigram2; c < NBG; c++) { 157 p[c] = check_bigram_char(getc(fp)); 158 s[c] = check_bigram_char(getc(fp)); 159 } 160 #endif 161 162 /* find optimal (last) char for searching */ 163 p = pathpart; 164 globflag = index(p, '*') || index(p, '?') || index(p, '['); 165 patend = patprep(p); 166 cc = *patend; 167 168 #ifdef FF_ICASE 169 /* set patend char to true */ 170 table[TOLOWER(*patend)] = 1; 171 table[toupper(*patend)] = 1; 172 #endif 173 174 175 /* main loop */ 176 found = count = 0; 177 foundchar = 0; 178 179 #ifdef FF_MMAP 180 for (c = (u_char)*paddr++; len-- > 0; ) { 181 #else 182 for (c = getc(fp); c != EOF; ) { 183 #endif 184 185 /* go forward or backward */ 186 if (c == SWITCH) { /* big step, an integer */ 187 #ifdef FF_MMAP 188 count += getwm(paddr) - OFFSET; 189 len -= INTSIZE; paddr += INTSIZE; 190 #else 191 count += getwf(fp) - OFFSET; 192 #endif 193 } else { /* slow step, =< 14 chars */ 194 count += c - OFFSET; 195 } 196 197 /* overlay old path */ 198 p = path + count; 199 foundchar = p - 1; 200 #ifdef FF_MMAP 201 for (; (c = (u_char)*paddr++) > SWITCH; len--) 202 #else 203 for (; (c = getc(fp)) > SWITCH; ) 204 #endif 205 206 if (c < PARITY) { 207 #ifdef FF_ICASE 208 if (table[c]) 209 #else 210 if (c == cc) 211 #endif 212 foundchar = p; 213 *p++ = c; 214 } 215 else { 216 /* bigrams are parity-marked */ 217 TO7BIT(c); 218 219 #ifndef FF_ICASE 220 if (bigram1[c] == cc || 221 bigram2[c] == cc) 222 #else 223 224 if (table[bigram1[c]] || 225 table[bigram2[c]]) 226 #endif 227 foundchar = p + 1; 228 229 *p++ = bigram1[c]; 230 *p++ = bigram2[c]; 231 } 232 233 234 if (found) { /* previous line matched */ 235 cutoff = path; 236 *p-- = '\0'; 237 foundchar = p; 238 } else if (foundchar >= path + count) { /* a char matched */ 239 *p-- = '\0'; 240 cutoff = path + count; 241 } else /* nothing to do */ 242 continue; 243 244 found = 0; 245 for (s = foundchar; s >= cutoff; s--) { 246 if (*s == cc 247 #ifdef FF_ICASE 248 || TOLOWER(*s) == cc 249 #endif 250 ) { /* fast first char check */ 251 for (p = patend - 1, q = s - 1; *p != '\0'; 252 p--, q--) 253 if (*q != *p 254 #ifdef FF_ICASE 255 && TOLOWER(*q) != *p 256 #endif 257 ) 258 break; 259 if (*p == '\0') { /* fast match success */ 260 found = 1; 261 if (!globflag || !fnmatch(pathpart, path, 0)) { 262 if (f_silent) 263 counter++; 264 else if (f_limit) { 265 counter++; 266 if (f_limit >= counter) 267 (void)puts(path); 268 else { 269 (void)fprintf(stderr, "[show only %d lines]\n", counter - 1); 270 exit(0); 271 } 272 } else 273 (void)puts(path); 274 } 275 break; 276 } 277 } 278 } 279 } 280 } 281