]>
git.saurik.com Git - apple/shell_cmds.git/blob - locate/locate/fastfind.c
9383f0735219f341b6ced219d478871d0c5f70e8
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.14 2005/12/07 12:22:46 des Exp $
41 #ifndef _LOCATE_STATISTIC_
42 #define _LOCATE_STATISTIC_
45 statistic (fp
, path_fcodes
)
46 FILE *fp
; /* open database */
47 char *path_fcodes
; /* for error message */
49 register int lines
, chars
, size
, big
, zwerg
;
50 register u_char
*p
, *s
;
53 u_char bigram1
[NBG
], bigram2
[NBG
], path
[MAXPATHLEN
];
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
));
60 lines
= chars
= big
= zwerg
= umlaut
= 0;
63 for (c
= getc(fp
), count
= 0; c
!= EOF
; size
++) {
65 count
+= getwf(fp
) - OFFSET
;
71 for (p
= path
+ count
; (c
= getc(fp
)) > SWITCH
; size
++)
90 (void)printf("\nDatabase: %s\n", path_fcodes
);
91 (void)printf("Compression: Front: %2.2f%%, ",
92 (size
+ big
- (2 * NBG
)) / (chars
/ (float)100));
93 (void)printf("Bigram: %2.2f%%, ", (size
- big
) / (size
/ (float)100));
94 (void)printf("Total: %2.2f%%\n",
95 (size
- (2 * NBG
)) / (chars
/ (float)100));
96 (void)printf("Filenames: %d, ", lines
);
97 (void)printf("Characters: %d, ", chars
);
98 (void)printf("Database size: %d\n", size
);
99 (void)printf("Bigram characters: %d, ", big
);
100 (void)printf("Integers: %d, ", zwerg
);
101 (void)printf("8-Bit characters: %d\n", umlaut
);
104 #endif /* _LOCATE_STATISTIC_ */
106 extern char separator
;
116 #endif /* FF_ICASE */
117 (pathpart
, paddr
, len
, database
)
118 char *pathpart
; /* search string */
119 caddr_t paddr
; /* mmap pointer */
120 int len
; /* length of database */
121 char *database
; /* for error message */
131 #endif /* FF_ICASE */
133 (fp
, pathpart
, database
)
134 FILE *fp
; /* open database */
135 char *pathpart
; /* search string */
136 char *database
; /* for error message */
142 register u_char
*p
, *s
, *patend
, *q
, *foundchar
;
144 int count
, found
, globflag
;
146 u_char bigram1
[NBG
], bigram2
[NBG
], path
[MAXPATHLEN
];
149 /* use a lookup table for case insensitive search */
150 u_char table
[UCHAR_MAX
+ 1];
152 tolower_word(pathpart
);
155 /* init bigram table */
158 errx(1, "database too small: %s", database
);
160 for (c
= 0, p
= bigram1
, s
= bigram2
; c
< NBG
; c
++, len
-= 2) {
161 p
[c
] = check_bigram_char(*paddr
++);
162 s
[c
] = check_bigram_char(*paddr
++);
165 for (c
= 0, p
= bigram1
, s
= bigram2
; c
< NBG
; c
++) {
166 p
[c
] = check_bigram_char(getc(fp
));
167 s
[c
] = check_bigram_char(getc(fp
));
171 /* find optimal (last) char for searching */
172 for (p
= pathpart
; *p
!= '\0'; p
++)
173 if (index(LOCATE_REG
, *p
) != NULL
)
186 /* set patend char to true */
187 for (c
= 0; c
< UCHAR_MAX
+ 1; c
++)
190 table
[TOLOWER(*patend
)] = 1;
191 table
[toupper(*patend
)] = 1;
192 #endif /* FF_ICASE */
200 c
= (u_char
)*paddr
++; len
--;
207 /* go forward or backward */
208 if (c
== SWITCH
) { /* big step, an integer */
210 count
+= getwm(paddr
) - OFFSET
;
211 len
-= INTSIZE
; paddr
+= INTSIZE
;
213 count
+= getwf(fp
) - OFFSET
;
215 } else { /* slow step, =< 14 chars */
219 /* overlay old path */
225 c
= (u_char
)*paddr
++;
232 * == UMLAUT: 8 bit char followed
235 * rest: single ascii char
237 * offset < SWITCH < UMLAUT < ascii < PARITY < bigram
243 c
= (u_char
)*paddr
++;
256 #endif /* FF_ICASE */
261 /* bigrams are parity-marked */
265 if (bigram1
[c
] == cc
||
269 if (table
[bigram1
[c
]] ||
271 #endif /* FF_ICASE */
279 if (found
) { /* previous line matched */
283 } else if (foundchar
>= path
+ count
) { /* a char matched */
285 cutoff
= path
+ count
;
286 } else /* nothing to do */
290 for (s
= foundchar
; s
>= cutoff
; s
--) {
294 #endif /* FF_ICASE */
295 ) { /* fast first char check */
296 for (p
= patend
- 1, q
= s
- 1; *p
!= '\0';
301 #endif /* FF_ICASE */
304 if (*p
== '\0') { /* fast match success */
308 !fnmatch(pathpart
, path
, 0))
310 !fnmatch(pathpart
, path
,
312 #endif /* !FF_ICASE */
318 if (f_limit
>= counter
)
319 (void)printf("%s%c",path
,separator
);
321 errx(0, "[show only %d lines]", counter
- 1);
323 (void)printf("%s%c",path
,separator
);