/usr.bin/locate/code/locate.code.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 278 lines · 145 code · 36 blank · 97 comment · 65 complexity · 4e33fe891220161054572b17dd301b98 MD5 · raw file

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