* find/find.1: -D option (for Solaris door files) is documented
[findutils.git] / locate / bigram.c
blobfa69e8e0b5f1f9e313c4d158474ecadc2c67c6ad
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
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 #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
36 #include <string.h>
37 #else
38 #include <strings.h>
39 #endif
41 #ifdef STDC_HEADERS
42 #include <stdlib.h>
43 #endif
44 #include <sys/types.h>
46 #include <getline.h>
47 #include <xalloc.h>
49 /* The name this program was run with. */
50 char *program_name;
52 /* Return the length of the longest common prefix of strings S1 and S2. */
54 static int
55 prefix_length (char *s1, char *s2)
57 register char *start;
59 for (start = s1; *s1 == *s2 && *s1 != '\0'; s1++, s2++)
61 return s1 - start;
64 int
65 main (int argc, char **argv)
67 char *path; /* The current input entry. */
68 char *oldpath; /* The previous input entry. */
69 size_t pathsize, oldpathsize; /* Amounts allocated for them. */
70 int line_len; /* Length of input line. */
72 program_name = argv[0];
74 pathsize = oldpathsize = 1026; /* Increased as necessary by getline. */
75 path = xmalloc (pathsize);
76 oldpath = xmalloc (oldpathsize);
78 /* Set to anything not starting with a slash, to force the first
79 prefix count to 0. */
80 strcpy (oldpath, " ");
82 while ((line_len = getline (&path, &pathsize, stdin)) > 0)
84 register int count; /* The prefix length. */
85 register int j; /* Index into input line. */
87 path[line_len - 1] = '\0'; /* Remove the newline. */
89 /* Output bigrams in the remainder only. */
90 count = prefix_length (oldpath, path);
91 for (j = count; path[j] != '\0' && path[j + 1] != '\0'; j += 2)
93 putchar (path[j]);
94 putchar (path[j + 1]);
95 putchar ('\n');
99 /* Swap path and oldpath and their sizes. */
100 char *tmppath = oldpath;
101 size_t tmppathsize = oldpathsize;
102 oldpath = path;
103 oldpathsize = pathsize;
104 path = tmppath;
105 pathsize = tmppathsize;
109 free (path);
110 free (oldpath);
112 exit (0);