Merged changes made for version 4.1.20 onto the trunk
[findutils.git] / locate / bigram.c
bloba4734fe898f4eaac3a7505c13c2f9507db97fb37
1 /* bigram -- list bigrams for locate
2 Copyright (C) 1994 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 2, or (at your option)
7 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, write to the Free Software
16 Foundation, Inc., 9 Temple Place - Suite 330, Boston, MA 02111-1307,
17 USA.
20 /* Usage: bigram < text > bigrams
21 Use `code' to encode a file using this output.
23 Read a file from stdin and write out the bigrams (pairs of
24 adjacent characters), one bigram per line, to stdout. To reduce
25 needless duplication in the output, it starts finding the
26 bigrams on each input line at the character where that line
27 first differs from the previous line (i.e., in the ASCII
28 remainder). Therefore, the input should be sorted in order to
29 get the least redundant output.
31 Written by James A. Woods <jwoods@adobe.com>.
32 Modified by David MacKenzie <djm@gnu.ai.mit.edu>. */
34 #include <gnulib/config.h>
35 #undef VERSION
36 #undef PACKAGE_VERSION
37 #undef PACKAGE_TARNAME
38 #undef PACKAGE_STRING
39 #undef PACKAGE_NAME
40 #undef PACKAGE
41 #include <config.h>
42 #include <stdio.h>
44 #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
45 #include <string.h>
46 #else
47 #include <strings.h>
48 #endif
50 #ifdef STDC_HEADERS
51 #include <stdlib.h>
52 #endif
53 #include <sys/types.h>
55 #include <getline.h>
56 #include <xalloc.h>
58 /* The name this program was run with. */
59 char *program_name;
61 /* Return the length of the longest common prefix of strings S1 and S2. */
63 static int
64 prefix_length (char *s1, char *s2)
66 register char *start;
68 for (start = s1; *s1 == *s2 && *s1 != '\0'; s1++, s2++)
70 return s1 - start;
73 int
74 main (int argc, char **argv)
76 char *path; /* The current input entry. */
77 char *oldpath; /* The previous input entry. */
78 size_t pathsize, oldpathsize; /* Amounts allocated for them. */
79 int line_len; /* Length of input line. */
81 program_name = argv[0];
83 pathsize = oldpathsize = 1026; /* Increased as necessary by getline. */
84 path = xmalloc (pathsize);
85 oldpath = xmalloc (oldpathsize);
87 /* Set to anything not starting with a slash, to force the first
88 prefix count to 0. */
89 strcpy (oldpath, " ");
91 while ((line_len = getline (&path, &pathsize, stdin)) > 0)
93 register int count; /* The prefix length. */
94 register int j; /* Index into input line. */
96 path[line_len - 1] = '\0'; /* Remove the newline. */
98 /* Output bigrams in the remainder only. */
99 count = prefix_length (oldpath, path);
100 for (j = count; path[j] != '\0' && path[j + 1] != '\0'; j += 2)
102 putchar (path[j]);
103 putchar (path[j + 1]);
104 putchar ('\n');
108 /* Swap path and oldpath and their sizes. */
109 char *tmppath = oldpath;
110 size_t tmppathsize = oldpathsize;
111 oldpath = path;
112 oldpathsize = pathsize;
113 path = tmppath;
114 pathsize = tmppathsize;
118 free (path);
119 free (oldpath);
121 exit (0);