WIP: silence build warnings
[findutils/ericb.git] / locate / bigram.c
blob7aca0215c8138d288ae0b8c05ac68ca6923f8232
1 /* bigram -- list bigrams for locate
2 Copyright (C) 1994, 2007, 2009, 2010 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /* Usage: bigram < text > bigrams
19 Use `code' to encode a file using this output.
21 Read a file from stdin and write out the bigrams (pairs of
22 adjacent characters), one bigram per line, to stdout. To reduce
23 needless duplication in the output, it starts finding the
24 bigrams on each input line at the character where that line
25 first differs from the previous line (i.e., in the ASCII
26 remainder). Therefore, the input should be sorted in order to
27 get the least redundant output.
29 Written by James A. Woods <jwoods@adobe.com>.
30 Modified by David MacKenzie <djm@gnu.ai.mit.edu>. */
32 #include <config.h>
33 #include <stdio.h>
35 #include <string.h>
36 #include <stdlib.h>
38 #include <sys/types.h>
40 #include <xalloc.h>
41 #include "progname.h"
42 #include "closeout.h"
44 /* Return the length of the longest common prefix of strings S1 and S2. */
46 static int
47 prefix_length (char *s1, char *s2)
49 register char *start;
51 for (start = s1; *s1 == *s2 && *s1 != '\0'; s1++, s2++)
53 return s1 - start;
56 int
57 main (int argc, char **argv)
59 char *path; /* The current input entry. */
60 char *oldpath; /* The previous input entry. */
61 size_t pathsize, oldpathsize; /* Amounts allocated for them. */
62 int line_len; /* Length of input line. */
64 if (argv[0])
65 set_program_name (argv[0]);
66 else
67 set_program_name ("bigram");
69 (void) argc;
70 atexit (close_stdout);
72 pathsize = oldpathsize = 1026; /* Increased as necessary by getline. */
73 path = xmalloc (pathsize);
74 oldpath = xmalloc (oldpathsize);
76 /* Set to empty string, to force the first prefix count to 0. */
77 oldpath[0] = '\0';
79 while ((line_len = getline (&path, &pathsize, stdin)) > 0)
81 register int count; /* The prefix length. */
82 register int j; /* Index into input line. */
84 path[line_len - 1] = '\0'; /* Remove the newline. */
86 /* Output bigrams in the remainder only. */
87 count = prefix_length (oldpath, path);
88 for (j = count; path[j] != '\0' && path[j + 1] != '\0'; j += 2)
90 putchar (path[j]);
91 putchar (path[j + 1]);
92 putchar ('\n');
96 /* Swap path and oldpath and their sizes. */
97 char *tmppath = oldpath;
98 size_t tmppathsize = oldpathsize;
99 oldpath = path;
100 oldpathsize = pathsize;
101 path = tmppath;
102 pathsize = tmppathsize;
106 free (path);
107 free (oldpath);
109 return 0;