summaryrefslogtreecommitdiff
path: root/locate/code.c
diff options
context:
space:
mode:
Diffstat (limited to 'locate/code.c')
-rw-r--r--locate/code.c167
1 files changed, 47 insertions, 120 deletions
diff --git a/locate/code.c b/locate/code.c
index f48885b6..d79e72ce 100644
--- a/locate/code.c
+++ b/locate/code.c
@@ -1,19 +1,19 @@
/* code -- bigram- and front-encode filenames for locate
- Copyright (C) 1994, 2005, 2007 Free Software Foundation, Inc.
+ Copyright (C) 1994 Free Software Foundation, Inc.
- This program is free software: you can redistribute it and/or modify
+ This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
-
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
-
+
You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-*/
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
/* Compress a sorted list.
Works with `find' to encode a filename database to save space
@@ -37,54 +37,32 @@
0-28 likeliest differential counts + offset (14) to make nonnegative
30 escape code for out-of-range count to follow in next halfword
- 128-255 bigram codes (the 128 most common, as determined by `updatedb')
- 32-127 single character (printable) ASCII remainder
+ 128-255 bigram codes (the 128 most common, as determined by `updatedb')
+ 32-127 single character (printable) ASCII remainder
Written by James A. Woods <jwoods@adobe.com>.
- Modified by David MacKenzie <djm@gnu.org>. */
+ Modified by David MacKenzie <djm@gnu.ai.mit.edu>. */
#include <config.h>
#include <stdio.h>
#include <sys/types.h>
-#include <string.h>
-#include <errno.h>
-#include <stdbool.h>
+#if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
+#include <string.h>
+#else
+#include <strings.h>
+#endif
#ifdef STDC_HEADERS
#include <stdlib.h>
#endif
-#if ENABLE_NLS
-# include <libintl.h>
-# define _(Text) gettext (Text)
-#else
-# define _(Text) Text
-#define textdomain(Domain)
-#define bindtextdomain(Package, Directory)
-#endif
-#ifdef gettext_noop
-# define N_(String) gettext_noop (String)
-#else
-/* See locate.c for explanation as to why not use (String) */
-# define N_(String) String
-#endif
-
#include "locatedb.h"
-#include "closeout.h"
-#include "xalloc.h"
-#include "gnulib-version.h"
-#include "progname.h"
-#include "error.h"
-#include "findutils-version.h"
-
-#ifndef ATTRIBUTE_NORETURN
-# define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
-#endif
+char *xmalloc ();
/* The name this program was run with. */
-const char *program_name;
+char *program_name;
/* The 128 most common bigrams in the file list, padded with NULs
if there are fewer. */
@@ -93,7 +71,8 @@ static char bigrams[257] = {0};
/* Return the offset of PATTERN in STRING, or -1 if not found. */
static int
-strindex (char *string, char *pattern)
+strindex (string, pattern)
+ char *string, *pattern;
{
register char *s;
@@ -113,7 +92,8 @@ strindex (char *string, char *pattern)
/* Return the length of the longest common prefix of strings S1 and S2. */
static int
-prefix_length (char *s1, char *s2)
+prefix_length (s1, s2)
+ char *s1, *s2;
{
register char *start;
@@ -122,41 +102,10 @@ prefix_length (char *s1, char *s2)
return s1 - start;
}
-extern char *version_string;
-
-static void
-usage (FILE *stream)
-{
- fprintf (stream, _("\
-Usage: %s [--version | --help]\n\
-or %s most_common_bigrams < file-list > locate-database\n"),
- program_name, program_name);
- fputs (_("\nReport bugs to <bug-findutils@gnu.org>.\n"), stream);
-}
-
-
-static void inerr (const char *filename) ATTRIBUTE_NORETURN;
-static void outerr(void) ATTRIBUTE_NORETURN;
-
-static void
-inerr(const char *filename)
-{
- error(1, errno, "%s", filename);
- /*NOTREACHED*/
- abort();
-}
-
-static void
-outerr(void)
-{
- error(1, errno, _("write error"));
- /*NOTREACHED*/
- abort();
-}
-
-
-int
-main (int argc, char **argv)
+void
+main (argc, argv)
+ int argc;
+ char **argv;
{
char *path; /* The current input entry. */
char *oldpath; /* The previous input entry. */
@@ -167,56 +116,41 @@ main (int argc, char **argv)
FILE *fp; /* Most common bigrams file. */
int line_len; /* Length of input line. */
- set_program_name(argv[0]);
- atexit (close_stdout);
+ program_name = argv[0];
bigram[2] = '\0';
if (argc != 2)
{
- usage(stderr);
- return 2;
- }
-
- if (0 == strcmp(argv[1], "--help"))
- {
- usage(stdout);
- return 0;
- }
- else if (0 == strcmp(argv[1], "--version"))
- {
- display_findutils_version("code");
- return 0;
+ fprintf (stderr, "Usage: %s most_common_bigrams < list > coded_list\n",
+ argv[0]);
+ exit (2);
}
-
+
fp = fopen (argv[1], "r");
if (fp == NULL)
{
fprintf (stderr, "%s: ", argv[0]);
perror (argv[1]);
- return 1;
+ exit (1);
}
-
- pathsize = oldpathsize = 1026; /* Increased as necessary by getline. */
+
+ pathsize = oldpathsize = 1026; /* Increased as necessary by getstr. */
path = xmalloc (pathsize);
oldpath = xmalloc (oldpathsize);
- /* Set to empty string, to force the first prefix count to 0. */
- oldpath[0] = '\0';
+ /* Set to anything not starting with a slash, to force the first
+ prefix count to 0. */
+ strcpy (oldpath, " ");
oldcount = 0;
/* Copy the list of most common bigrams to the output,
padding with NULs if there are <128 of them. */
- if (NULL == fgets (bigrams, 257, fp))
- inerr(argv[1]);
-
- if (256 != fwrite (bigrams, 1, 256, stdout))
- outerr();
+ fgets (bigrams, 257, fp);
+ fwrite (bigrams, 1, 256, stdout);
+ fclose (fp);
- if (EOF == fclose (fp))
- inerr(argv[1]);
-
- while ((line_len = getline (&path, &pathsize, stdin)) > 0)
+ while ((line_len = getstr (&path, &pathsize, stdin, '\n', 0)) > 0)
{
char *pp;
@@ -225,7 +159,8 @@ main (int argc, char **argv)
/* Squelch unprintable chars in path so as not to botch decoding. */
for (pp = path; *pp != '\0'; pp++)
{
- if (!(*pp >= 040 && *pp < 0177))
+ *pp &= 0177;
+ if (*pp < 040 || *pp == 0177)
*pp = '?';
}
@@ -236,19 +171,11 @@ main (int argc, char **argv)
otherwise, two bytes plus a marker noting that fact. */
if (diffcount < -LOCATEDB_OLD_OFFSET || diffcount > LOCATEDB_OLD_OFFSET)
{
- if (EOF ==- putc (LOCATEDB_OLD_ESCAPE, stdout))
- outerr ();
-
- if (!putword (stdout,
- diffcount+LOCATEDB_OLD_OFFSET,
- GetwordEndianStateNative))
- outerr ();
+ putc (LOCATEDB_OLD_ESCAPE, stdout);
+ putw (diffcount + LOCATEDB_OLD_OFFSET, stdout);
}
else
- {
- if (EOF == putc (diffcount + LOCATEDB_OLD_OFFSET, stdout))
- outerr ();
- }
+ putc (diffcount + LOCATEDB_OLD_OFFSET, stdout);
/* Look for bigrams in the remainder of the path. */
for (pp = path + count; *pp != '\0'; pp += 2)
@@ -283,5 +210,5 @@ main (int argc, char **argv)
free (path);
free (oldpath);
- return 0;
+ exit (0);
}