1 /* $NetBSD: locate.code.c,v 1.9 2003/08/07 11:14:20 agc Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
37 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
38 The Regents of the University of California. All rights reserved.");
43 static char sccsid
[] = "@(#)locate.code.c 8.4 (Berkeley) 5/4/95";
45 __RCSID("$NetBSD: locate.code.c,v 1.9 2003/08/07 11:14:20 agc Exp $");
49 * PURPOSE: sorted list compressor (works with a modified 'find'
50 * to encode/decode a filename database)
52 * USAGE: bigram < list > bigrams
53 * process bigrams (see updatedb) > common_bigrams
54 * code common_bigrams < list > squozen_list
56 * METHOD: Uses 'front compression' (see ";login:", Volume 8, Number 1
57 * February/March 1983, p. 8). Output format is, per line, an
58 * offset differential count byte followed by a partially bigram-
59 * encoded ascii residue. A bigram is a two-character sequence,
60 * the first 128 most common of which are encoded in one byte.
62 * EXAMPLE: For simple front compression with no bigram encoding,
63 * if the input is... then the output is...
66 * /usr/src/cmd/aardvark.c 8 /cmd/aardvark.c
67 * /usr/src/cmd/armadillo.c 14 armadillo.c
68 * /usr/tmp/zoo 5 tmp/zoo
72 * 0-28 likeliest differential counts + offset to make nonnegative
73 * 30 switch code for out-of-range count to follow in next word
74 * 128-255 bigram codes (128 most common, as determined by 'updatedb')
75 * 32-127 single character (printable) ascii residue (ie, literal)
77 * SEE ALSO: updatedb.csh, bigram.c
79 * AUTHOR: James A. Woods, Informatics General Corp.,
80 * NASA Ames Research Center, 10/82
83 #include <sys/param.h>
94 #define BGBUFSIZE (NBG * 2) /* size of bigram buffer */
96 char buf1
[MAXPATHLEN
] = " ";
97 char buf2
[MAXPATHLEN
];
98 char bigrams
[BGBUFSIZE
+ 1] = { 0 };
100 int bgindex
__P((char *));
101 int main
__P((int, char **));
102 void usage
__P((void));
109 char *cp
, *oldpath
, *path
;
110 int ch
, code
, count
, diffcount
, oldcount
;
112 while ((ch
= getopt(argc
, argv
, "")) != -1)
124 /* First copy bigram array to stdout. */
125 strncpy(bigrams
, argv
[0], BGBUFSIZE
+ 1);
126 if (fwrite(bigrams
, 1, BGBUFSIZE
, stdout
) != BGBUFSIZE
)
132 while (fgets(path
, sizeof(buf2
), stdin
) != NULL
) {
133 /* Truncate newline. */
134 cp
= path
+ strlen(path
) - 1;
135 if (cp
> path
&& *cp
== '\n')
138 /* Squelch characters that would botch the decoding. */
139 for (cp
= path
; *cp
!= '\0'; cp
++) {
145 /* Skip longest common prefix. */
146 for (cp
= path
; *cp
== *oldpath
; cp
++, oldpath
++)
147 if (*oldpath
== '\0')
150 diffcount
= count
- oldcount
+ OFFSET
;
152 if (diffcount
< 0 || diffcount
> 2 * OFFSET
) {
153 if (putchar(SWITCH
) == EOF
||
154 putw(diffcount
, stdout
) == EOF
)
157 if (putchar(diffcount
) == EOF
)
160 while (*cp
!= '\0') {
161 if (*(cp
+ 1) == '\0') {
162 if (putchar(*cp
) == EOF
)
166 if ((code
= bgindex(cp
)) < 0) {
167 if (putchar(*cp
++) == EOF
||
168 putchar(*cp
++) == EOF
)
171 /* Found, so mark byte with parity bit. */
172 if (putchar((code
/ 2) | PARITY
) == EOF
)
177 if (path
== buf1
) { /* swap pointers */
185 /* Non-zero status if there were errors */
186 if (fflush(stdout
) != 0 || ferror(stdout
))
192 bgindex(bg
) /* Return location of bg in bigrams or -1. */
199 for (p
= bigrams
; *p
!= '\0'; p
+= 2)
200 if (p
[0] == bg0
&& p
[1] == bg1
)
202 return (*p
== '\0' ? -1 : p
- bigrams
);
208 (void)fprintf(stderr
,
209 "usage: locate.code common_bigrams < list > squozen_list\n");