Home | History | Annotate | Line # | Download | only in code
locate.code.c revision 1.1.1.2
      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.2  jtc static char sccsid[] = "@(#)locate.code.c	8.1 (Berkeley) 6/6/93";
     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.2  jtc #include <err.h>
     84  1.1.1.2  jtc #include <errno.h>
     85  1.1.1.2  jtc #include <stdlib.h>
     86  1.1.1.2  jtc #include <string.h>
     87      1.1  cgd #include <stdio.h>
     88      1.1  cgd #include "locate.h"
     89      1.1  cgd 
     90  1.1.1.2  jtc #define	BGBUFSIZE	(NBG * 2)	/* size of bigram buffer */
     91      1.1  cgd 
     92      1.1  cgd char buf1[MAXPATHLEN] = " ";
     93      1.1  cgd char buf2[MAXPATHLEN];
     94      1.1  cgd char bigrams[BGBUFSIZE + 1] = { 0 };
     95      1.1  cgd 
     96  1.1.1.2  jtc int	bgindex __P((char *));
     97  1.1.1.2  jtc void	usage __P((void));
     98  1.1.1.2  jtc 
     99  1.1.1.2  jtc int
    100  1.1.1.2  jtc main(argc, argv)
    101  1.1.1.2  jtc 	int argc;
    102  1.1.1.2  jtc 	char *argv[];
    103      1.1  cgd {
    104  1.1.1.2  jtc 	register char *cp, *oldpath, *path;
    105  1.1.1.2  jtc 	int ch, code, count, diffcount, oldcount;
    106      1.1  cgd 	FILE *fp;
    107      1.1  cgd 
    108  1.1.1.2  jtc 	while ((ch = getopt(argc, argv, "")) != EOF)
    109  1.1.1.2  jtc 		switch(ch) {
    110  1.1.1.2  jtc 		case '?':
    111  1.1.1.2  jtc 		default:
    112  1.1.1.2  jtc 			usage();
    113  1.1.1.2  jtc 		}
    114  1.1.1.2  jtc 	argc -= optind;
    115  1.1.1.2  jtc 	argv += optind;
    116      1.1  cgd 
    117  1.1.1.2  jtc 	if (argc != 1)
    118  1.1.1.2  jtc 		usage();
    119  1.1.1.2  jtc 
    120  1.1.1.2  jtc 	if ((fp = fopen(argv[0], "r")) == NULL)
    121  1.1.1.2  jtc 		err(1, "%s", argv[0]);
    122  1.1.1.2  jtc 
    123  1.1.1.2  jtc 	/* First copy bigram array to stdout. */
    124  1.1.1.2  jtc 	(void)fgets(bigrams, BGBUFSIZE + 1, fp);
    125  1.1.1.2  jtc 	if (fwrite(bigrams, 1, BGBUFSIZE, stdout) != BGBUFSIZE)
    126  1.1.1.2  jtc 		err(1, "stdout");
    127  1.1.1.2  jtc 	(void)fclose(fp);
    128  1.1.1.2  jtc 
    129  1.1.1.2  jtc 	oldpath = buf1;
    130  1.1.1.2  jtc 	path = buf2;
    131  1.1.1.2  jtc 	oldcount = 0;
    132  1.1.1.2  jtc 	while (fgets(path, sizeof(buf2), stdin) != NULL) {
    133  1.1.1.2  jtc 		/* Truncate newline. */
    134      1.1  cgd 		cp = path + strlen(path) - 1;
    135      1.1  cgd 		if (cp > path && *cp == '\n')
    136      1.1  cgd 			*cp = '\0';
    137  1.1.1.2  jtc 
    138  1.1.1.2  jtc 		/* Squelch characters that would botch the decoding. */
    139  1.1.1.2  jtc 		for (cp = path; *cp != NULL; cp++) {
    140  1.1.1.2  jtc 			if ((u_char)*cp >= PARITY)
    141      1.1  cgd 				*cp &= PARITY-1;
    142  1.1.1.2  jtc 			else if (*cp <= SWITCH)
    143      1.1  cgd 				*cp = '?';
    144      1.1  cgd 		}
    145  1.1.1.2  jtc 
    146  1.1.1.2  jtc 		/* Skip longest common prefix. */
    147  1.1.1.2  jtc 		for (cp = path; *cp == *oldpath; cp++, oldpath++)
    148  1.1.1.2  jtc 			if (*oldpath == NULL)
    149      1.1  cgd 				break;
    150      1.1  cgd 		count = cp - path;
    151      1.1  cgd 		diffcount = count - oldcount + OFFSET;
    152      1.1  cgd 		oldcount = count;
    153  1.1.1.2  jtc 		if (diffcount < 0 || diffcount > 2 * OFFSET) {
    154  1.1.1.2  jtc 			if (putchar(SWITCH) == EOF ||
    155  1.1.1.2  jtc 			    putw(diffcount, stdout) == EOF)
    156  1.1.1.2  jtc 				err(1, "stdout");
    157  1.1.1.2  jtc 		} else
    158  1.1.1.2  jtc 			if (putchar(diffcount) == EOF)
    159  1.1.1.2  jtc 				err(1, "stdout");
    160  1.1.1.2  jtc 
    161  1.1.1.2  jtc 		while (*cp != NULL) {
    162  1.1.1.2  jtc 			if (*(cp + 1) == NULL) {
    163  1.1.1.2  jtc 				if (putchar(*cp) == EOF)
    164  1.1.1.2  jtc 					err(1, "stdout");
    165      1.1  cgd 				break;
    166      1.1  cgd 			}
    167  1.1.1.2  jtc 			if ((code = bgindex(cp)) < 0) {
    168  1.1.1.2  jtc 				if (putchar(*cp++) == EOF ||
    169  1.1.1.2  jtc 				    putchar(*cp++) == EOF)
    170  1.1.1.2  jtc 					err(1, "stdout");
    171  1.1.1.2  jtc 			} else {
    172  1.1.1.2  jtc 				/* Found, so mark byte with parity bit. */
    173  1.1.1.2  jtc 				if (putchar((code / 2) | PARITY) == EOF)
    174  1.1.1.2  jtc 					err(1, "stdout");
    175      1.1  cgd 				cp += 2;
    176      1.1  cgd 			}
    177      1.1  cgd 		}
    178  1.1.1.2  jtc 		if (path == buf1) {		/* swap pointers */
    179  1.1.1.2  jtc 			path = buf2;
    180  1.1.1.2  jtc 			oldpath = buf1;
    181  1.1.1.2  jtc 		} else {
    182  1.1.1.2  jtc 			path = buf1;
    183  1.1.1.2  jtc 			oldpath = buf2;
    184  1.1.1.2  jtc 		}
    185      1.1  cgd 	}
    186  1.1.1.2  jtc 	/* Non-zero status if there were errors */
    187  1.1.1.2  jtc 	if (fflush(stdout) != 0 || ferror(stdout))
    188  1.1.1.2  jtc 		exit(1);
    189  1.1.1.2  jtc 	exit(0);
    190      1.1  cgd }
    191      1.1  cgd 
    192  1.1.1.2  jtc int
    193  1.1.1.2  jtc bgindex(bg)			/* Return location of bg in bigrams or -1. */
    194      1.1  cgd 	char *bg;
    195      1.1  cgd {
    196  1.1.1.2  jtc 	register char bg0, bg1, *p;
    197      1.1  cgd 
    198  1.1.1.2  jtc 	bg0 = bg[0];
    199  1.1.1.2  jtc 	bg1 = bg[1];
    200  1.1.1.2  jtc 	for (p = bigrams; *p != NULL; p++)
    201  1.1.1.2  jtc 		if (*p++ == bg0 && *p == bg1)
    202      1.1  cgd 			break;
    203  1.1.1.2  jtc 	return (*p == NULL ? -1 : --p - bigrams);
    204  1.1.1.2  jtc }
    205  1.1.1.2  jtc 
    206  1.1.1.2  jtc void
    207  1.1.1.2  jtc usage()
    208  1.1.1.2  jtc {
    209  1.1.1.2  jtc 	(void)fprintf(stderr,
    210  1.1.1.2  jtc 	    "usage: locate.code common_bigrams < list > squozen_list\n");
    211  1.1.1.2  jtc 	exit(1);
    212      1.1  cgd }
    213