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/code/locate.code.c,v 1.11.2.1 2001/03/04 08:46:46 kris Exp $
39 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
40 * @(#)locate.code.c 8.1 (Berkeley) 6/6/93
44 * PURPOSE: sorted list compressor (works with a modified 'find'
45 * to encode/decode a filename database)
47 * USAGE: bigram < list > bigrams
48 * process bigrams (see updatedb) > common_bigrams
49 * code common_bigrams < list > squozen_list
51 * METHOD: Uses 'front compression' (see ";login:", Volume 8, Number 1
52 * February/March 1983, p. 8). Output format is, per line, an
53 * offset differential count byte followed by a partially bigram-
54 * encoded ascii residue. A bigram is a two-character sequence,
55 * the first 128 most common of which are encoded in one byte.
57 * EXAMPLE: For simple front compression with no bigram encoding,
58 * if the input is... then the output is...
61 * /usr/src/cmd/aardvark.c 8 /cmd/aardvark.c
62 * /usr/src/cmd/armadillo.c 14 armadillo.c
63 * /usr/tmp/zoo 5 tmp/zoo
67 * 0-28 likeliest differential counts + offset to make nonnegative
68 * 30 switch code for out-of-range count to follow in next word
69 * 31 an 8 bit char followed
70 * 128-255 bigram codes (128 most common, as determined by 'updatedb')
71 * 32-127 single character (printable) ascii residue (ie, literal)
73 * The locate database store any character except newline ('\n')
74 * and NUL ('\0'). The 8-bit character support don't wast extra
75 * space until you have characters in file names less than 32
76 * or greather than 127.
79 * SEE ALSO: updatedb.sh, ../bigram/locate.bigram.c
81 * AUTHOR: James A. Woods, Informatics General Corp.,
82 * NASA Ames Research Center, 10/82
83 * 8-bit file names characters:
84 * Wolfram Schneider, Berlin September 1996
87 #include <sys/param.h>
96 #define BGBUFSIZE (NBG * 2) /* size of bigram buffer */
98 static u_char buf1
[MAXPATHLEN
] = " ";
99 static u_char buf2
[MAXPATHLEN
];
100 static u_char bigrams
[BGBUFSIZE
+ 1] = { 0 };
102 #define LOOKUP 1 /* use a lookup array instead a function, 3x faster */
105 #define BGINDEX(x) (big[(u_char)*x][(u_char)*(x + 1)])
107 static bg_t big
[UCHAR_MAX
+ 1][UCHAR_MAX
+ 1];
109 #define BGINDEX(x) bgindex(x)
111 static int bgindex(char *);
115 static void usage(void);
118 main(int argc
, char *argv
[])
120 u_char
*cp
, *oldpath
, *path
;
121 int ch
, code
, count
, diffcount
, oldcount
;
125 while ((ch
= getopt(argc
, argv
, "")) != -1)
136 if ((fp
= fopen(argv
[0], "r")) == NULL
)
137 err(1, "%s", argv
[0]);
139 /* First copy bigram array to stdout. */
140 (void)fgets(bigrams
, BGBUFSIZE
+ 1, fp
);
142 if (fwrite(bigrams
, 1, BGBUFSIZE
, stdout
) != BGBUFSIZE
)
147 /* init lookup table */
148 for (i
= 0; i
< UCHAR_MAX
+ 1; i
++)
149 for (j
= 0; j
< UCHAR_MAX
+ 1; j
++)
150 big
[i
][j
] = (bg_t
)-1;
152 for (cp
= bigrams
, i
= 0; *cp
!= '\0'; i
+= 2, cp
+= 2)
153 big
[(u_char
)*cp
][(u_char
)*(cp
+ 1)] = (bg_t
)i
;
161 while (fgets(path
, sizeof(buf2
), stdin
) != NULL
) {
163 /* skip empty lines */
168 for (cp
= path
; *cp
!= '\0'; cp
++) {
169 #ifndef LOCATE_CHAR30
170 /* old locate implementations core'd for char 30 */
174 #endif /* !LOCATE_CHAR30 */
181 /* Skip longest common prefix. */
182 for (cp
= path
; *cp
== *oldpath
; cp
++, oldpath
++)
187 diffcount
= count
- oldcount
+ OFFSET
;
189 if (diffcount
< 0 || diffcount
> 2 * OFFSET
) {
190 if (putchar(SWITCH
) == EOF
||
191 putw(diffcount
, stdout
) == EOF
)
194 if (putchar(diffcount
) == EOF
)
197 while (*cp
!= '\0') {
198 /* print *two* characters */
200 if ((code
= BGINDEX(cp
)) != (bg_t
)-1) {
202 * print *one* as bigram
203 * Found, so mark byte with
206 if (putchar((code
/ 2) | PARITY
) == EOF
)
212 for (i
= 0; i
< 2; i
++) {
216 /* print umlauts in file names */
217 if (*cp
< ASCII_MIN
||
219 if (putchar(UMLAUT
) == EOF
||
220 putchar(*cp
++) == EOF
)
225 /* normal character */
226 if(putchar(*cp
++) == EOF
)
234 if (path
== buf1
) { /* swap pointers */
242 /* Non-zero status if there were errors */
243 if (fflush(stdout
) != 0 || ferror(stdout
))
250 bgindex(bg
) /* Return location of bg in bigrams or -1. */
257 for (p
= bigrams
; *p
!= NULL
; p
++)
258 if (*p
++ == bg0
&& *p
== bg1
)
260 return (*p
== NULL
? -1 : (--p
- bigrams
));
267 (void)fprintf(stderr
,
268 "usage: locate.code common_bigrams < list > squozen_list\n");