locate.code.c revision 1.1.1.3 1 1.1 cgd /*
2 1.1.1.2 jtc * Copyright (c) 1989, 1993
3 1.1.1.2 jtc * The Regents of the University of California. All rights reserved.
4 1.1 cgd *
5 1.1 cgd * This code is derived from software contributed to Berkeley by
6 1.1 cgd * James A. Woods.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd #ifndef lint
38 1.1.1.2 jtc static char copyright[] =
39 1.1.1.2 jtc "@(#) Copyright (c) 1989, 1993\n\
40 1.1.1.2 jtc The Regents of the University of California. All rights reserved.\n";
41 1.1 cgd #endif /* not lint */
42 1.1 cgd
43 1.1 cgd #ifndef lint
44 1.1.1.3 jtc static char sccsid[] = "@(#)locate.code.c 8.4 (Berkeley) 5/4/95";
45 1.1 cgd #endif /* not lint */
46 1.1 cgd
47 1.1 cgd /*
48 1.1 cgd * PURPOSE: sorted list compressor (works with a modified 'find'
49 1.1 cgd * to encode/decode a filename database)
50 1.1 cgd *
51 1.1 cgd * USAGE: bigram < list > bigrams
52 1.1 cgd * process bigrams (see updatedb) > common_bigrams
53 1.1 cgd * code common_bigrams < list > squozen_list
54 1.1 cgd *
55 1.1 cgd * METHOD: Uses 'front compression' (see ";login:", Volume 8, Number 1
56 1.1.1.2 jtc * February/March 1983, p. 8). Output format is, per line, an
57 1.1 cgd * offset differential count byte followed by a partially bigram-
58 1.1 cgd * encoded ascii residue. A bigram is a two-character sequence,
59 1.1 cgd * the first 128 most common of which are encoded in one byte.
60 1.1 cgd *
61 1.1 cgd * EXAMPLE: For simple front compression with no bigram encoding,
62 1.1 cgd * if the input is... then the output is...
63 1.1 cgd *
64 1.1 cgd * /usr/src 0 /usr/src
65 1.1 cgd * /usr/src/cmd/aardvark.c 8 /cmd/aardvark.c
66 1.1 cgd * /usr/src/cmd/armadillo.c 14 armadillo.c
67 1.1 cgd * /usr/tmp/zoo 5 tmp/zoo
68 1.1 cgd *
69 1.1.1.2 jtc * The codes are:
70 1.1 cgd *
71 1.1.1.2 jtc * 0-28 likeliest differential counts + offset to make nonnegative
72 1.1 cgd * 30 switch code for out-of-range count to follow in next word
73 1.1 cgd * 128-255 bigram codes (128 most common, as determined by 'updatedb')
74 1.1 cgd * 32-127 single character (printable) ascii residue (ie, literal)
75 1.1 cgd *
76 1.1.1.2 jtc * SEE ALSO: updatedb.csh, bigram.c
77 1.1.1.2 jtc *
78 1.1 cgd * AUTHOR: James A. Woods, Informatics General Corp.,
79 1.1 cgd * NASA Ames Research Center, 10/82
80 1.1 cgd */
81 1.1 cgd
82 1.1 cgd #include <sys/param.h>
83 1.1.1.3 jtc
84 1.1.1.2 jtc #include <err.h>
85 1.1.1.2 jtc #include <errno.h>
86 1.1.1.3 jtc #include <stdio.h>
87 1.1.1.2 jtc #include <stdlib.h>
88 1.1.1.2 jtc #include <string.h>
89 1.1.1.3 jtc #include <unistd.h>
90 1.1.1.3 jtc
91 1.1 cgd #include "locate.h"
92 1.1 cgd
93 1.1.1.2 jtc #define BGBUFSIZE (NBG * 2) /* size of bigram buffer */
94 1.1 cgd
95 1.1 cgd char buf1[MAXPATHLEN] = " ";
96 1.1 cgd char buf2[MAXPATHLEN];
97 1.1 cgd char bigrams[BGBUFSIZE + 1] = { 0 };
98 1.1 cgd
99 1.1.1.2 jtc int bgindex __P((char *));
100 1.1.1.2 jtc void usage __P((void));
101 1.1.1.2 jtc
102 1.1.1.2 jtc int
103 1.1.1.2 jtc main(argc, argv)
104 1.1.1.2 jtc int argc;
105 1.1.1.2 jtc char *argv[];
106 1.1 cgd {
107 1.1.1.2 jtc register char *cp, *oldpath, *path;
108 1.1.1.2 jtc int ch, code, count, diffcount, oldcount;
109 1.1 cgd FILE *fp;
110 1.1 cgd
111 1.1.1.2 jtc while ((ch = getopt(argc, argv, "")) != EOF)
112 1.1.1.2 jtc switch(ch) {
113 1.1.1.2 jtc case '?':
114 1.1.1.2 jtc default:
115 1.1.1.2 jtc usage();
116 1.1.1.2 jtc }
117 1.1.1.2 jtc argc -= optind;
118 1.1.1.2 jtc argv += optind;
119 1.1 cgd
120 1.1.1.2 jtc if (argc != 1)
121 1.1.1.2 jtc usage();
122 1.1.1.2 jtc
123 1.1.1.2 jtc if ((fp = fopen(argv[0], "r")) == NULL)
124 1.1.1.2 jtc err(1, "%s", argv[0]);
125 1.1.1.2 jtc
126 1.1.1.2 jtc /* First copy bigram array to stdout. */
127 1.1.1.2 jtc (void)fgets(bigrams, BGBUFSIZE + 1, fp);
128 1.1.1.2 jtc if (fwrite(bigrams, 1, BGBUFSIZE, stdout) != BGBUFSIZE)
129 1.1.1.2 jtc err(1, "stdout");
130 1.1.1.2 jtc (void)fclose(fp);
131 1.1.1.2 jtc
132 1.1.1.2 jtc oldpath = buf1;
133 1.1.1.2 jtc path = buf2;
134 1.1.1.2 jtc oldcount = 0;
135 1.1.1.2 jtc while (fgets(path, sizeof(buf2), stdin) != NULL) {
136 1.1.1.2 jtc /* Truncate newline. */
137 1.1 cgd cp = path + strlen(path) - 1;
138 1.1 cgd if (cp > path && *cp == '\n')
139 1.1 cgd *cp = '\0';
140 1.1.1.2 jtc
141 1.1.1.2 jtc /* Squelch characters that would botch the decoding. */
142 1.1.1.3 jtc for (cp = path; *cp != '\0'; cp++) {
143 1.1.1.3 jtc if ((u_char)*cp >= PARITY || *cp <= SWITCH)
144 1.1 cgd *cp = '?';
145 1.1 cgd }
146 1.1.1.2 jtc
147 1.1.1.2 jtc /* Skip longest common prefix. */
148 1.1.1.2 jtc for (cp = path; *cp == *oldpath; cp++, oldpath++)
149 1.1.1.3 jtc if (*oldpath == '\0')
150 1.1 cgd break;
151 1.1 cgd count = cp - path;
152 1.1 cgd diffcount = count - oldcount + OFFSET;
153 1.1 cgd oldcount = count;
154 1.1.1.2 jtc if (diffcount < 0 || diffcount > 2 * OFFSET) {
155 1.1.1.2 jtc if (putchar(SWITCH) == EOF ||
156 1.1.1.2 jtc putw(diffcount, stdout) == EOF)
157 1.1.1.2 jtc err(1, "stdout");
158 1.1.1.2 jtc } else
159 1.1.1.2 jtc if (putchar(diffcount) == EOF)
160 1.1.1.2 jtc err(1, "stdout");
161 1.1.1.2 jtc
162 1.1.1.3 jtc while (*cp != '\0') {
163 1.1.1.3 jtc if (*(cp + 1) == '\0') {
164 1.1.1.2 jtc if (putchar(*cp) == EOF)
165 1.1.1.2 jtc err(1, "stdout");
166 1.1 cgd break;
167 1.1 cgd }
168 1.1.1.2 jtc if ((code = bgindex(cp)) < 0) {
169 1.1.1.2 jtc if (putchar(*cp++) == EOF ||
170 1.1.1.2 jtc putchar(*cp++) == EOF)
171 1.1.1.2 jtc err(1, "stdout");
172 1.1.1.2 jtc } else {
173 1.1.1.2 jtc /* Found, so mark byte with parity bit. */
174 1.1.1.2 jtc if (putchar((code / 2) | PARITY) == EOF)
175 1.1.1.2 jtc err(1, "stdout");
176 1.1 cgd cp += 2;
177 1.1 cgd }
178 1.1 cgd }
179 1.1.1.2 jtc if (path == buf1) { /* swap pointers */
180 1.1.1.2 jtc path = buf2;
181 1.1.1.2 jtc oldpath = buf1;
182 1.1.1.2 jtc } else {
183 1.1.1.2 jtc path = buf1;
184 1.1.1.2 jtc oldpath = buf2;
185 1.1.1.2 jtc }
186 1.1 cgd }
187 1.1.1.2 jtc /* Non-zero status if there were errors */
188 1.1.1.2 jtc if (fflush(stdout) != 0 || ferror(stdout))
189 1.1.1.2 jtc exit(1);
190 1.1.1.2 jtc exit(0);
191 1.1 cgd }
192 1.1 cgd
193 1.1.1.2 jtc int
194 1.1.1.2 jtc bgindex(bg) /* Return location of bg in bigrams or -1. */
195 1.1 cgd char *bg;
196 1.1 cgd {
197 1.1.1.2 jtc register char bg0, bg1, *p;
198 1.1 cgd
199 1.1.1.2 jtc bg0 = bg[0];
200 1.1.1.2 jtc bg1 = bg[1];
201 1.1.1.3 jtc for (p = bigrams; *p != '\0'; p++)
202 1.1.1.2 jtc if (*p++ == bg0 && *p == bg1)
203 1.1 cgd break;
204 1.1.1.3 jtc return (*p == '\0' ? -1 : --p - bigrams);
205 1.1.1.2 jtc }
206 1.1.1.2 jtc
207 1.1.1.2 jtc void
208 1.1.1.2 jtc usage()
209 1.1.1.2 jtc {
210 1.1.1.2 jtc (void)fprintf(stderr,
211 1.1.1.2 jtc "usage: locate.code common_bigrams < list > squozen_list\n");
212 1.1.1.2 jtc exit(1);
213 1.1 cgd }
214