~ubuntu-branches/ubuntu/raring/findutils/raring

« back to all changes in this revision

Viewing changes to locate/code.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2008-05-06 11:32:24 UTC
  • mfrom: (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 23.
  • Revision ID: james.westby@ubuntu.com-20080506113224-gy4ecmxu48tnvva4
Tags: upstream-4.4.0
ImportĀ upstreamĀ versionĀ 4.4.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* code -- bigram- and front-encode filenames for locate
2
 
   Copyright (C) 1994, 2000, 2003, 2004, 2005, 2007 Free Software
3
 
   Foundation, Inc.
 
2
   Copyright (C) 1994, 2005, 2007, 2008 Free Software Foundation, Inc.
4
3
 
5
4
   This program is free software: you can redistribute it and/or modify
6
5
   it under the terms of the GNU General Public License as published by
33
32
   followed by a (partially bigram-encoded) ASCII remainder.
34
33
   The output lines have no terminating byte; the start of the next line
35
34
   is indicated by its first byte having a value <= 30.
36
 
 
 
35
   
37
36
   The encoding of the output bytes is:
38
37
 
39
38
   0-28         likeliest differential counts + offset (14) to make nonnegative
40
39
   30           escape code for out-of-range count to follow in next halfword
41
 
   128-255      bigram codes (the 128 most common, as determined by `updatedb')
42
 
   32-127       single character (printable) ASCII remainder
 
40
   128-255      bigram codes (the 128 most common, as determined by `updatedb')
 
41
   32-127       single character (printable) ASCII remainder
43
42
 
44
43
   Written by James A. Woods <jwoods@adobe.com>.
45
44
   Modified by David MacKenzie <djm@gnu.org>.  */
47
46
#include <config.h>
48
47
#include <stdio.h>
49
48
#include <sys/types.h>
50
 
 
51
 
#if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
52
49
#include <string.h>
53
 
#else
54
 
#include <strings.h>
55
 
#endif
 
50
#include <errno.h>
 
51
#include <stdbool.h>
 
52
 
56
53
 
57
54
#ifdef STDC_HEADERS
58
55
#include <stdlib.h>
75
72
 
76
73
#include "locatedb.h"
77
74
#include "closeout.h"
 
75
#include "xalloc.h"
78
76
#include "gnulib-version.h"
79
 
 
80
 
char *xmalloc PARAMS((size_t));
 
77
#include "progname.h"
 
78
#include "error.h"
 
79
#include "findutils-version.h"
 
80
 
 
81
#ifndef ATTRIBUTE_NORETURN
 
82
# define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
 
83
#endif
 
84
 
81
85
 
82
86
/* The name this program was run with.  */
83
 
char *program_name;
 
87
extern const char *program_name;
84
88
 
85
89
/* The 128 most common bigrams in the file list, padded with NULs
86
90
   if there are fewer.  */
131
135
}
132
136
 
133
137
 
 
138
static void inerr (const char *filename) ATTRIBUTE_NORETURN;
 
139
static void outerr(void)                 ATTRIBUTE_NORETURN;
 
140
 
 
141
static void
 
142
inerr(const char *filename) 
 
143
{
 
144
  error(1, errno, "%s", filename);
 
145
  /*NOTREACHED*/
 
146
  abort();
 
147
}
 
148
 
 
149
static void
 
150
outerr(void) 
 
151
{
 
152
  error(1, errno, _("write error"));
 
153
  /*NOTREACHED*/
 
154
  abort();
 
155
}
 
156
 
 
157
 
134
158
int
135
159
main (int argc, char **argv)
136
160
{
143
167
  FILE *fp;                     /* Most common bigrams file.  */
144
168
  int line_len;                 /* Length of input line.  */
145
169
 
146
 
  program_name = argv[0];
 
170
  set_program_name(argv[0]);
147
171
  atexit (close_stdout);
148
172
 
149
173
  bigram[2] = '\0';
153
177
      usage(stderr);
154
178
      return 2;
155
179
    }
156
 
 
 
180
  
157
181
  if (0 == strcmp(argv[1], "--help"))
158
182
    {
159
183
      usage(stdout);
161
185
    }
162
186
  else if (0 == strcmp(argv[1], "--version"))
163
187
    {
164
 
      printf (_("GNU findutils version %s\n"), version_string);
165
 
      printf (_("Built using GNU gnulib version %s\n"), gnulib_version);
 
188
      display_findutils_version("code");
166
189
      return 0;
167
190
    }
168
 
 
 
191
  
169
192
  fp = fopen (argv[1], "r");
170
193
  if (fp == NULL)
171
194
    {
173
196
      perror (argv[1]);
174
197
      return 1;
175
198
    }
176
 
 
 
199
  
177
200
  pathsize = oldpathsize = 1026; /* Increased as necessary by getline.  */
178
201
  path = xmalloc (pathsize);
179
202
  oldpath = xmalloc (oldpathsize);
184
207
 
185
208
  /* Copy the list of most common bigrams to the output,
186
209
     padding with NULs if there are <128 of them.  */
187
 
  fgets (bigrams, 257, fp);
188
 
  fwrite (bigrams, 1, 256, stdout);
189
 
  fclose (fp);
 
210
  if (NULL == fgets (bigrams, 257, fp))
 
211
    inerr(argv[1]);
 
212
  
 
213
  if (256 != fwrite (bigrams, 1, 256, stdout))
 
214
     outerr();
 
215
 
 
216
  if (EOF == fclose (fp))
 
217
     inerr(argv[1]);
190
218
 
191
219
  while ((line_len = getline (&path, &pathsize, stdin)) > 0)
192
220
    {
208
236
         otherwise, two bytes plus a marker noting that fact.  */
209
237
      if (diffcount < -LOCATEDB_OLD_OFFSET || diffcount > LOCATEDB_OLD_OFFSET)
210
238
        {
211
 
          putc (LOCATEDB_OLD_ESCAPE, stdout);
212
 
          putw (diffcount + LOCATEDB_OLD_OFFSET, stdout);
 
239
          if (EOF ==- putc (LOCATEDB_OLD_ESCAPE, stdout))
 
240
            outerr ();
 
241
 
 
242
          if (!putword (stdout,
 
243
                        diffcount+LOCATEDB_OLD_OFFSET,
 
244
                        GetwordEndianStateNative))
 
245
            outerr ();
213
246
        }
214
247
      else
215
 
        putc (diffcount + LOCATEDB_OLD_OFFSET, stdout);
 
248
        {
 
249
          if (EOF == putc (diffcount + LOCATEDB_OLD_OFFSET, stdout))
 
250
            outerr ();
 
251
        }
216
252
 
217
253
      /* Look for bigrams in the remainder of the path.  */
218
254
      for (pp = path + count; *pp != '\0'; pp += 2)