File: | src/usr.bin/locate/locate/fastfind.c |
Warning: | line 70, column 6 Value stored to 'c' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* $OpenBSD: fastfind.c,v 1.16 2019/01/17 06:15:44 tedu Exp $ */ |
2 | |
3 | /* |
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. |
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 | |
36 | #ifndef _LOCATE_STATISTIC_ |
37 | #define _LOCATE_STATISTIC_ |
38 | |
39 | void |
40 | statistic (fp, path_fcodes) |
41 | FILE *fp; /* open database */ |
42 | char *path_fcodes; /* for error message */ |
43 | { |
44 | int lines, chars, size, big, zwerg; |
45 | u_char *p, *s; |
46 | int c; |
47 | int count, umlaut; |
48 | u_char bigram1[NBG128], bigram2[NBG128], path[PATH_MAX1024]; |
49 | |
50 | for (c = 0, p = bigram1, s = bigram2; c < NBG128; c++) { |
51 | p[c] = check_bigram_char(getc(fp)(!__isthreaded ? (--(fp)->_r < 0 ? __srget(fp) : (int)( *(fp)->_p++)) : (getc)(fp))); |
52 | s[c] = check_bigram_char(getc(fp)(!__isthreaded ? (--(fp)->_r < 0 ? __srget(fp) : (int)( *(fp)->_p++)) : (getc)(fp))); |
53 | } |
54 | |
55 | lines = chars = big = zwerg = umlaut = 0; |
56 | size = NBG128 + NBG128; |
57 | |
58 | for (c = getc(fp)(!__isthreaded ? (--(fp)->_r < 0 ? __srget(fp) : (int)( *(fp)->_p++)) : (getc)(fp)), count = 0; c != EOF(-1); size++) { |
59 | if (c == SWITCH30) { |
60 | count += getwf(fp) - OFFSET14; |
61 | size += sizeof(int); |
62 | zwerg++; |
63 | } else |
64 | count += c - OFFSET14; |
65 | |
66 | sane_count(count); |
67 | for (p = path + count; (c = getc(fp)(!__isthreaded ? (--(fp)->_r < 0 ? __srget(fp) : (int)( *(fp)->_p++)) : (getc)(fp))) > SWITCH30; size++) |
68 | if (c < PARITY0200) { |
69 | if (c == UMLAUT31) { |
70 | c = getc(fp)(!__isthreaded ? (--(fp)->_r < 0 ? __srget(fp) : (int)( *(fp)->_p++)) : (getc)(fp)); |
Value stored to 'c' is never read | |
71 | size++; |
72 | umlaut++; |
73 | } |
74 | p++; |
75 | } else { |
76 | /* bigram char */ |
77 | big++; |
78 | p += 2; |
79 | } |
80 | |
81 | p++; |
82 | lines++; |
83 | chars += (p - path); |
84 | } |
85 | |
86 | (void)printf("\nDatabase: %s\n", path_fcodes); |
87 | (void)printf("Compression: Front: %2.2f%%, ", |
88 | (float)(100 * (size + big - (2 * NBG128))) / chars); |
89 | (void)printf("Bigram: %2.2f%%, ", (float)(100 * (size - big)) / size); |
90 | (void)printf("Total: %2.2f%%\n", |
91 | (float)(100 * (size - (2 * NBG128))) / chars); |
92 | (void)printf("Filenames: %d, ", lines); |
93 | (void)printf("Characters: %d, ", chars); |
94 | (void)printf("Database size: %d\n", size); |
95 | (void)printf("Bigram characters: %d, ", big); |
96 | (void)printf("Integers: %d, ", zwerg); |
97 | (void)printf("8-Bit characters: %d\n", umlaut); |
98 | |
99 | } |
100 | #endif /* _LOCATE_STATISTIC_ */ |
101 | |
102 | |
103 | void |
104 | |
105 | |
106 | #ifdef FF_ICASE |
107 | fastfind_mmap_icase |
108 | #else |
109 | fastfind_mmap |
110 | #endif /* FF_ICASE */ |
111 | (pathpart, paddr, len, database) |
112 | char *pathpart; /* search string */ |
113 | caddr_t paddr; /* mmap pointer */ |
114 | int len; /* length of database */ |
115 | char *database; /* for error message */ |
116 | |
117 | |
118 | |
119 | { |
120 | u_char *p, *s, *patend, *q, *foundchar; |
121 | int c, cc; |
122 | int count, found, globflag; |
123 | u_char *cutoff; |
124 | u_char bigram1[NBG128], bigram2[NBG128], path[PATH_MAX1024]; |
125 | |
126 | #ifdef FF_ICASE |
127 | for (p = pathpart; *p != '\0'; p++) |
128 | *p = tolower(*p); |
129 | #endif /* FF_ICASE*/ |
130 | |
131 | /* init bigram table */ |
132 | if (len < (2*NBG128)) { |
133 | (void)fprintf(stderr(&__sF[2]), "database too small: %s\n", database); |
134 | exit(1); |
135 | } |
136 | |
137 | for (c = 0, p = bigram1, s = bigram2; c < NBG128; c++, len-= 2) { |
138 | p[c] = check_bigram_char(*paddr++); |
139 | s[c] = check_bigram_char(*paddr++); |
140 | } |
141 | |
142 | /* find optimal (last) char for searching */ |
143 | for (p = pathpart; *p != '\0'; p++) |
144 | if (strchr(LOCATE_REG"*?[]\\", *p) != NULL((void *)0)) |
145 | break; |
146 | |
147 | if (*p == '\0') |
148 | globflag = 0; |
149 | else |
150 | globflag = 1; |
151 | |
152 | p = pathpart; |
153 | patend = patprep(p); |
154 | cc = *patend; |
155 | #ifdef FF_ICASE |
156 | cc = tolower(cc); |
157 | #endif /* FF_ICASE */ |
158 | |
159 | |
160 | /* main loop */ |
161 | found = count = 0; |
162 | foundchar = 0; |
163 | |
164 | c = (u_char)*paddr++; len--; |
165 | for (; len > 0; ) { |
166 | |
167 | /* go forward or backward */ |
168 | if (c == SWITCH30) { /* big step, an integer */ |
169 | if (len < sizeof(int)) |
170 | break; |
171 | count += getwm(paddr) - OFFSET14; |
172 | len -= sizeof(int); |
173 | paddr += sizeof(int); |
174 | } else { /* slow step, =< 14 chars */ |
175 | count += c - OFFSET14; |
176 | } |
177 | |
178 | sane_count(count); |
179 | /* overlay old path */ |
180 | p = path + count; |
181 | foundchar = p - 1; |
182 | |
183 | for (; len > 0; ) { |
184 | c = (u_char)*paddr++; |
185 | len--; |
186 | /* |
187 | * == UMLAUT: 8 bit char followed |
188 | * <= SWITCH: offset |
189 | * >= PARITY: bigram |
190 | * rest: single ascii char |
191 | * |
192 | * offset < SWITCH < UMLAUT < ascii < PARITY < bigram |
193 | */ |
194 | if (c < PARITY0200) { |
195 | if (c <= UMLAUT31) { |
196 | if (c == UMLAUT31 && len > 0) { |
197 | c = (u_char)*paddr++; |
198 | len--; |
199 | |
200 | } else |
201 | break; /* SWITCH */ |
202 | } |
203 | #ifdef FF_ICASE |
204 | if (tolower(c) == cc) |
205 | #else |
206 | if (c == cc) |
207 | #endif /* FF_ICASE */ |
208 | foundchar = p; |
209 | *p++ = c; |
210 | } else { |
211 | /* bigrams are parity-marked */ |
212 | c &= ASCII_MAX127; |
213 | #ifdef FF_ICASE |
214 | if (tolower(bigram1[c]) == cc || |
215 | tolower(bigram2[c]) == cc) |
216 | #else |
217 | if (bigram1[c] == cc || |
218 | bigram2[c] == cc) |
219 | #endif /* FF_ICASE */ |
220 | foundchar = p + 1; |
221 | |
222 | *p++ = bigram1[c]; |
223 | *p++ = bigram2[c]; |
224 | } |
225 | } |
226 | |
227 | if (found) { /* previous line matched */ |
228 | cutoff = path; |
229 | *p-- = '\0'; |
230 | foundchar = p; |
231 | } else if (foundchar >= path + count) { /* a char matched */ |
232 | *p-- = '\0'; |
233 | cutoff = path + count; |
234 | } else /* nothing to do */ |
235 | continue; |
236 | |
237 | found = 0; |
238 | for (s = foundchar; s >= cutoff; s--) { |
239 | if (*s == cc |
240 | #ifdef FF_ICASE |
241 | || tolower(*s) == cc |
242 | #endif /* FF_ICASE */ |
243 | ) { /* fast first char check */ |
244 | for (p = patend - 1, q = s - 1; *p != '\0'; |
245 | p--, q--) |
246 | if (*q != *p |
247 | #ifdef FF_ICASE |
248 | && tolower(*q) != *p |
249 | #endif /* FF_ICASE */ |
250 | ) |
251 | break; |
252 | if (*p == '\0') { /* fast match success */ |
253 | char *shortpath; |
254 | |
255 | found = 1; |
256 | shortpath = path; |
257 | if (f_basename) |
258 | shortpath = basename(path); |
259 | |
260 | if ((!f_basename && (!globflag || |
261 | #ifdef FF_ICASE |
262 | !fnmatch(pathpart, shortpath, |
263 | FNM_CASEFOLD0x10))) |
264 | #else |
265 | !fnmatch(pathpart, shortpath, 0))) |
266 | #endif /* FF_ICASE */ |
267 | || (strstr(shortpath, pathpart) != |
268 | NULL((void *)0))) { |
269 | if (f_silent) |
270 | counter++; |
271 | else if (f_limit) { |
272 | counter++; |
273 | if (f_limit >= counter) |
274 | (void)puts(path); |
275 | else { |
276 | fprintf(stderr(&__sF[2]), "[show only %u lines]\n", counter - 1); |
277 | exit(0); |
278 | } |
279 | } else |
280 | (void)puts(path); |
281 | } |
282 | break; |
283 | } |
284 | } |
285 | } |
286 | } |
287 | } |