2 * SPDX-License-Identifier: BSD-4-Clause
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.
8 * This code is derived from software contributed to Berkeley by
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 static char copyright[] =
45 "@(#) Copyright (c) 1989, 1993\n\
46 The Regents of the University of California. All rights reserved.\n";
50 static char sccsid[] = "@(#)locate.code.c 8.1 (Berkeley) 6/6/93";
55 * PURPOSE: sorted list compressor (works with a modified 'find'
56 * to encode/decode a filename database)
58 * USAGE: bigram < list > bigrams
59 * process bigrams (see updatedb) > common_bigrams
60 * code common_bigrams < list > squozen_list
62 * METHOD: Uses 'front compression' (see ";login:", Volume 8, Number 1
63 * February/March 1983, p. 8). Output format is, per line, an
64 * offset differential count byte followed by a partially bigram-
65 * encoded ascii residue. A bigram is a two-character sequence,
66 * the first 128 most common of which are encoded in one byte.
68 * EXAMPLE: For simple front compression with no bigram encoding,
69 * if the input is... then the output is...
72 * /usr/src/cmd/aardvark.c 8 /cmd/aardvark.c
73 * /usr/src/cmd/armadillo.c 14 armadillo.c
74 * /usr/tmp/zoo 5 tmp/zoo
78 * 0-28 likeliest differential counts + offset to make nonnegative
79 * 30 switch code for out-of-range count to follow in next word
80 * 31 an 8 bit char followed
81 * 128-255 bigram codes (128 most common, as determined by 'updatedb')
82 * 32-127 single character (printable) ascii residue (ie, literal)
84 * The locate database store any character except newline ('\n')
85 * and NUL ('\0'). The 8-bit character support don't wast extra
86 * space until you have characters in file names less than 32
87 * or greather than 127.
90 * SEE ALSO: updatedb.sh, ../bigram/locate.bigram.c
92 * AUTHOR: James A. Woods, Informatics General Corp.,
93 * NASA Ames Research Center, 10/82
94 * 8-bit file names characters:
95 * Wolfram Schneider, Berlin September 1996
98 #include <sys/param.h>
107 #define BGBUFSIZE (NBG * 2) /* size of bigram buffer */
109 u_char buf1[MAXPATHLEN] = " ";
110 u_char buf2[MAXPATHLEN];
111 u_char bigrams[BGBUFSIZE + 1] = { 0 };
113 #define LOOKUP 1 /* use a lookup array instead a function, 3x faster */
116 #define BGINDEX(x) (big[(u_char)*x][(u_char)*(x + 1)])
118 bg_t big[UCHAR_MAX + 1][UCHAR_MAX + 1];
120 #define BGINDEX(x) bgindex(x)
129 main(int argc, char *argv[])
131 u_char *cp, *oldpath, *path;
132 int ch, code, count, diffcount, oldcount;
136 while ((ch = getopt(argc, argv, "")) != -1)
147 if ((fp = fopen(argv[0], "r")) == NULL)
148 err(1, "%s", argv[0]);
150 /* First copy bigram array to stdout. */
151 (void)fgets(bigrams, BGBUFSIZE + 1, fp);
153 if (fwrite(bigrams, 1, BGBUFSIZE, stdout) != BGBUFSIZE)
158 /* init lookup table */
159 for (i = 0; i < UCHAR_MAX + 1; i++)
160 for (j = 0; j < UCHAR_MAX + 1; j++)
161 big[i][j] = (bg_t)-1;
163 for (cp = bigrams, i = 0; *cp != '\0'; i += 2, cp += 2)
164 big[(u_char)*cp][(u_char)*(cp + 1)] = (bg_t)i;
172 while (fgets(path, sizeof(buf2), stdin) != NULL) {
174 /* skip empty lines */
179 for (cp = path; *cp != '\0'; cp++) {
180 #ifndef LOCATE_CHAR30
181 /* old locate implementations core'd for char 30 */
185 #endif /* !LOCATE_CHAR30 */
192 /* Skip longest common prefix. */
193 for (cp = path; *cp == *oldpath; cp++, oldpath++)
198 diffcount = count - oldcount + OFFSET;
200 if (diffcount < 0 || diffcount > 2 * OFFSET) {
201 if (putchar(SWITCH) == EOF ||
202 putw(diffcount, stdout) == EOF)
205 if (putchar(diffcount) == EOF)
208 while (*cp != '\0') {
209 /* print *two* characters */
211 if ((code = BGINDEX(cp)) != (bg_t)-1) {
213 * print *one* as bigram
214 * Found, so mark byte with
217 if (putchar((code / 2) | PARITY) == EOF)
223 for (i = 0; i < 2; i++) {
227 /* print umlauts in file names */
228 if (*cp < ASCII_MIN ||
230 if (putchar(UMLAUT) == EOF ||
231 putchar(*cp++) == EOF)
236 /* normal character */
237 if(putchar(*cp++) == EOF)
245 if (path == buf1) { /* swap pointers */
253 /* Non-zero status if there were errors */
254 if (fflush(stdout) != 0 || ferror(stdout))
261 bgindex(char *bg) /* Return location of bg in bigrams or -1. */
267 for (p = bigrams; *p != NULL; p++)
268 if (*p++ == bg0 && *p == bg1)
270 return (*p == NULL ? -1 : (--p - bigrams));
277 (void)fprintf(stderr,
278 "usage: locate.code common_bigrams < list > squozen_list\n");