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.
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
37 * $FreeBSD: src/usr.bin/locate/locate/fastfind.c,v 1.12.2.1 2001/02/22 13:28:52 wosch Exp $
41 #ifndef _LOCATE_STATISTIC_
42 #define _LOCATE_STATISTIC_
45 statistic (FILE *fp
, char *path_fcodes
)
47 int lines
, chars
, size
, big
, zwerg
;
51 u_char bigram1
[NBG
], bigram2
[NBG
], path
[MAXPATHLEN
];
53 for (c
= 0, p
= bigram1
, s
= bigram2
; c
< NBG
; c
++) {
54 p
[c
] = check_bigram_char(getc(fp
));
55 s
[c
] = check_bigram_char(getc(fp
));
58 lines
= chars
= big
= zwerg
= umlaut
= 0;
61 for (c
= getc(fp
), count
= 0; c
!= EOF
; size
++) {
63 count
+= getwf(fp
) - OFFSET
;
69 for (p
= path
+ count
; (c
= getc(fp
)) > SWITCH
; size
++)
88 (void)printf("\nDatabase: %s\n", path_fcodes
);
89 (void)printf("Compression: Front: %2.2f%%, ",
90 (size
+ big
- (2 * NBG
)) / (chars
/ (float)100));
91 (void)printf("Bigram: %2.2f%%, ", (size
- big
) / (size
/ (float)100));
92 (void)printf("Total: %2.2f%%\n",
93 (size
- (2 * NBG
)) / (chars
/ (float)100));
94 (void)printf("Filenames: %d, ", lines
);
95 (void)printf("Characters: %d, ", chars
);
96 (void)printf("Database size: %d\n", size
);
97 (void)printf("Bigram characters: %d, ", big
);
98 (void)printf("Integers: %d, ", zwerg
);
99 (void)printf("8-Bit characters: %d\n", umlaut
);
102 #endif /* _LOCATE_STATISTIC_ */
113 #endif /* FF_ICASE */
114 (char *pathpart
, caddr_t paddr
, int len
, char *database
)
124 #endif /* FF_ICASE */
126 (FILE *fp
, char *pathpart
, char *database
)
132 u_char
*p
, *s
, *patend
, *q
, *foundchar
;
134 int count
, found
, globflag
;
136 u_char bigram1
[NBG
], bigram2
[NBG
], path
[MAXPATHLEN
];
139 /* use a lookup table for case insensitive search */
140 u_char table
[UCHAR_MAX
+ 1];
142 tolower_word(pathpart
);
145 /* init bigram table */
148 errx(1, "database too small: %s", database
);
150 for (c
= 0, p
= bigram1
, s
= bigram2
; c
< NBG
; c
++, len
-= 2) {
151 p
[c
] = check_bigram_char(*paddr
++);
152 s
[c
] = check_bigram_char(*paddr
++);
155 for (c
= 0, p
= bigram1
, s
= bigram2
; c
< NBG
; c
++) {
156 p
[c
] = check_bigram_char(getc(fp
));
157 s
[c
] = check_bigram_char(getc(fp
));
161 /* find optimal (last) char for searching */
162 for (p
= pathpart
; *p
!= '\0'; p
++)
163 if (index(LOCATE_REG
, *p
) != NULL
)
176 /* set patend char to true */
177 for (c
= 0; c
< UCHAR_MAX
+ 1; c
++)
180 table
[TOLOWER(*patend
)] = 1;
181 table
[toupper(*patend
)] = 1;
182 #endif /* FF_ICASE */
190 c
= (u_char
)*paddr
++; len
--;
197 /* go forward or backward */
198 if (c
== SWITCH
) { /* big step, an integer */
200 count
+= getwm(paddr
) - OFFSET
;
201 len
-= INTSIZE
; paddr
+= INTSIZE
;
203 count
+= getwf(fp
) - OFFSET
;
205 } else { /* slow step, =< 14 chars */
209 /* overlay old path */
215 c
= (u_char
)*paddr
++;
222 * == UMLAUT: 8 bit char followed
225 * rest: single ascii char
227 * offset < SWITCH < UMLAUT < ascii < PARITY < bigram
233 c
= (u_char
)*paddr
++;
246 #endif /* FF_ICASE */
251 /* bigrams are parity-marked */
255 if (bigram1
[c
] == cc
||
259 if (table
[bigram1
[c
]] ||
261 #endif /* FF_ICASE */
269 if (found
) { /* previous line matched */
273 } else if (foundchar
>= path
+ count
) { /* a char matched */
275 cutoff
= path
+ count
;
276 } else /* nothing to do */
280 for (s
= foundchar
; s
>= cutoff
; s
--) {
284 #endif /* FF_ICASE */
285 ) { /* fast first char check */
286 for (p
= patend
- 1, q
= s
- 1; *p
!= '\0';
291 #endif /* FF_ICASE */
294 if (*p
== '\0') { /* fast match success */
298 !fnmatch(pathpart
, path
, 0))
300 !fnmatch(pathpart
, path
,
302 #endif /* !FF_ICASE */
308 if (f_limit
>= counter
)
311 errx(0, "[show only %d lines]", counter
- 1);