1/* $NetBSD: locate.code.c,v 1.12 2019/10/13 19:39:15 christos 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 43static char sccsid[] = "@(#)locate.code.c 8.4 (Berkeley) 5/4/95"; 44#endif 45__RCSID("$NetBSD: locate.code.c,v 1.12 2019/10/13 19:39:15 christos 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 96static char buf1[MAXPATHLEN] = " "; 97static char buf2[MAXPATHLEN]; 98static char bigrams[BGBUFSIZE + 1] = { 0 }; 99 100static int bgindex(char *); 101__dead static void usage(void); 102 103int 104main(int argc, char *argv[]) 105{ 106 char *cp, *oldpath, *path; 107 int ch, code, count, diffcount, oldcount; 108 109 while ((ch = getopt(argc, argv, "")) != -1) 110 switch(ch) { 111 case '?': 112 default: 113 usage(); 114 } 115 argc -= optind; 116 argv += optind; 117 118 if (argc != 1) 119 usage(); 120 121 /* First copy bigram array to stdout. */ 122 strncpy(bigrams, argv[0], BGBUFSIZE); 123 bigrams[BGBUFSIZE] = '\0'; 124 if (fwrite(bigrams, 1, BGBUFSIZE, stdout) != BGBUFSIZE) 125 err(1, "stdout"); 126 127 oldpath = buf1; 128 path = buf2; 129 oldcount = 0; 130 while (fgets(path, sizeof(buf2), stdin) != NULL) { 131 /* Truncate newline. */ 132 cp = path + strlen(path) - 1; 133 if (cp > path && *cp == '\n') 134 *cp = '\0'; 135 136 /* Squelch characters that would botch the decoding. */ 137 for (cp = path; *cp != '\0'; cp++) { 138 *cp &= PARITY-1; 139 if (*cp <= SWITCH) 140 *cp = '?'; 141 } 142 143 /* Skip longest common prefix. */ 144 for (cp = path; *cp == *oldpath; cp++, oldpath++) 145 if (*oldpath == '\0') 146 break; 147 count = cp - path; 148 diffcount = count - oldcount + OFFSET; 149 oldcount = count; 150 if (diffcount < 0 || diffcount > 2 * OFFSET) { 151 if (putchar(SWITCH) == EOF || 152 putw(diffcount, stdout) == EOF) 153 err(1, "stdout"); 154 } else 155 if (putchar(diffcount) == EOF) 156 err(1, "stdout"); 157 158 while (*cp != '\0') { 159 if (*(cp + 1) == '\0') { 160 if (putchar(*cp) == EOF) 161 err(1, "stdout"); 162 break; 163 } 164 if ((code = bgindex(cp)) < 0) { 165 if (putchar(*cp++) == EOF || 166 putchar(*cp++) == EOF) 167 err(1, "stdout"); 168 } else { 169 /* Found, so mark byte with parity bit. */ 170 if (putchar((code / 2) | PARITY) == EOF) 171 err(1, "stdout"); 172 cp += 2; 173 } 174 } 175 if (path == buf1) { /* swap pointers */ 176 path = buf2; 177 oldpath = buf1; 178 } else { 179 path = buf1; 180 oldpath = buf2; 181 } 182 } 183 /* Non-zero status if there were errors */ 184 if (fflush(stdout) != 0 || ferror(stdout)) 185 exit(1); 186 exit(0); 187} 188 189static int 190bgindex(char *bg) /* Return location of bg in bigrams or -1. */ 191{ 192 char bg0, bg1, *p; 193 194 bg0 = bg[0]; 195 bg1 = bg[1]; 196 for (p = bigrams; *p != '\0'; p += 2) 197 if (p[0] == bg0 && p[1] == bg1) 198 break; 199 return (*p == '\0' ? -1 : p - bigrams); 200} 201 202static void 203usage(void) 204{ 205 (void)fprintf(stderr, 206 "usage: locate.code common_bigrams < list > squozen_list\n"); 207 exit(1); 208} 209