xref: /netbsd-src/usr.bin/locate/code/locate.code.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: locate.code.c,v 1.10 2008/07/21 14:19:23 lukem Exp $	*/
2 
3 /*
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * James A. Woods.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
21  *
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
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
38  The Regents of the University of California.  All rights reserved.");
39 #endif /* not lint */
40 
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)locate.code.c	8.4 (Berkeley) 5/4/95";
44 #endif
45 __RCSID("$NetBSD: locate.code.c,v 1.10 2008/07/21 14:19:23 lukem Exp $");
46 #endif /* not lint */
47 
48 /*
49  * PURPOSE:	sorted list compressor (works with a modified 'find'
50  *		to encode/decode a filename database)
51  *
52  * USAGE:	bigram < list > bigrams
53  *		process bigrams (see updatedb) > common_bigrams
54  *		code common_bigrams < list > squozen_list
55  *
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.
61  *
62  * EXAMPLE:	For simple front compression with no bigram encoding,
63  *		if the input is...		then the output is...
64  *
65  *		/usr/src			 0 /usr/src
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
69  *
70  *	The codes are:
71  *
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)
76  *
77  * SEE ALSO:	updatedb.csh, bigram.c
78  *
79  * AUTHOR:	James A. Woods, Informatics General Corp.,
80  *		NASA Ames Research Center, 10/82
81  */
82 
83 #include <sys/param.h>
84 
85 #include <err.h>
86 #include <errno.h>
87 #include <stdio.h>
88 #include <stdlib.h>
89 #include <string.h>
90 #include <unistd.h>
91 
92 #include "locate.h"
93 
94 #define	BGBUFSIZE	(NBG * 2)	/* size of bigram buffer */
95 
96 char buf1[MAXPATHLEN] = " ";
97 char buf2[MAXPATHLEN];
98 char bigrams[BGBUFSIZE + 1] = { 0 };
99 
100 int	bgindex __P((char *));
101 int	main __P((int, char **));
102 void	usage __P((void));
103 
104 int
105 main(argc, argv)
106 	int argc;
107 	char *argv[];
108 {
109 	char *cp, *oldpath, *path;
110 	int ch, code, count, diffcount, oldcount;
111 
112 	while ((ch = getopt(argc, argv, "")) != -1)
113 		switch(ch) {
114 		case '?':
115 		default:
116 			usage();
117 		}
118 	argc -= optind;
119 	argv += optind;
120 
121 	if (argc != 1)
122 		usage();
123 
124 	/* First copy bigram array to stdout. */
125 	strncpy(bigrams, argv[0], BGBUFSIZE + 1);
126 	if (fwrite(bigrams, 1, BGBUFSIZE, stdout) != BGBUFSIZE)
127 		err(1, "stdout");
128 
129 	oldpath = buf1;
130 	path = buf2;
131 	oldcount = 0;
132 	while (fgets(path, sizeof(buf2), stdin) != NULL) {
133 		/* Truncate newline. */
134 		cp = path + strlen(path) - 1;
135 		if (cp > path && *cp == '\n')
136 			*cp = '\0';
137 
138 		/* Squelch characters that would botch the decoding. */
139 		for (cp = path; *cp != '\0'; cp++) {
140 			*cp &= PARITY-1;
141 			if (*cp <= SWITCH)
142 				*cp = '?';
143 		}
144 
145 		/* Skip longest common prefix. */
146 		for (cp = path; *cp == *oldpath; cp++, oldpath++)
147 			if (*oldpath == '\0')
148 				break;
149 		count = cp - path;
150 		diffcount = count - oldcount + OFFSET;
151 		oldcount = count;
152 		if (diffcount < 0 || diffcount > 2 * OFFSET) {
153 			if (putchar(SWITCH) == EOF ||
154 			    putw(diffcount, stdout) == EOF)
155 				err(1, "stdout");
156 		} else
157 			if (putchar(diffcount) == EOF)
158 				err(1, "stdout");
159 
160 		while (*cp != '\0') {
161 			if (*(cp + 1) == '\0') {
162 				if (putchar(*cp) == EOF)
163 					err(1, "stdout");
164 				break;
165 			}
166 			if ((code = bgindex(cp)) < 0) {
167 				if (putchar(*cp++) == EOF ||
168 				    putchar(*cp++) == EOF)
169 					err(1, "stdout");
170 			} else {
171 				/* Found, so mark byte with parity bit. */
172 				if (putchar((code / 2) | PARITY) == EOF)
173 					err(1, "stdout");
174 				cp += 2;
175 			}
176 		}
177 		if (path == buf1) {		/* swap pointers */
178 			path = buf2;
179 			oldpath = buf1;
180 		} else {
181 			path = buf1;
182 			oldpath = buf2;
183 		}
184 	}
185 	/* Non-zero status if there were errors */
186 	if (fflush(stdout) != 0 || ferror(stdout))
187 		exit(1);
188 	exit(0);
189 }
190 
191 int
192 bgindex(bg)			/* Return location of bg in bigrams or -1. */
193 	char *bg;
194 {
195 	char bg0, bg1, *p;
196 
197 	bg0 = bg[0];
198 	bg1 = bg[1];
199 	for (p = bigrams; *p != '\0'; p += 2)
200 		if (p[0] == bg0 && p[1] == bg1)
201 			break;
202 	return (*p == '\0' ? -1 : p - bigrams);
203 }
204 
205 void
206 usage()
207 {
208 	(void)fprintf(stderr,
209 	    "usage: locate.code common_bigrams < list > squozen_list\n");
210 	exit(1);
211 }
212