]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - boggle/mkdict/mkdict.c
Move UCB-licensed code from 4-clause to 3-clause licence.
[bsdgames-darwin.git] / boggle / mkdict / mkdict.c
1 /* $NetBSD: mkdict.c,v 1.9 2003/08/07 09:37:06 agc Exp $ */
2
3 /*-
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Barry Brachman.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #ifndef lint
36 static const char copyright[] =
37 "@(#) Copyright (c) 1993\n\
38 The Regents of the University of California. All rights reserved.\n";
39 #if 0
40 static char sccsid[] = "@(#)mkdict.c 8.1 (Berkeley) 6/11/93";
41 #else
42 static const char rcsid[] =
43 "$NetBSD: mkdict.c,v 1.9 2003/08/07 09:37:06 agc Exp $";
44 #endif
45 #endif /* not lint */
46
47 /*
48 * Filter out words that:
49 * 1) Are not completely made up of lower case letters
50 * 2) Contain a 'q' not immediately followed by a 'u'
51 * 3) Are less that 3 characters long
52 * 4) Are greater than MAXWORDLEN characters long
53 */
54
55 #include <ctype.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59
60 #include "bog.h"
61
62 int main(int, char *[]);
63
64 int
65 main(argc, argv)
66 int argc;
67 char *argv[];
68 {
69 char *p, *q;
70 int ch, common, nwords;
71 int current, len, prev, qcount;
72 char buf[2][MAXWORDLEN + 1];
73
74 prev = 0;
75 current = 1;
76 buf[prev][0] = '\0';
77
78 for (nwords = 1;
79 fgets(buf[current], MAXWORDLEN + 1, stdin) != NULL; ++nwords) {
80 if ((p = strchr(buf[current], '\n')) == NULL) {
81 fprintf(stderr, "word too long: %s\n", buf[current]);
82 while ((ch = getc(stdin)) != EOF && ch != '\n')
83 ;
84 if (ch == EOF)
85 break;
86 continue;
87 }
88 len = 0;
89 for (p = buf[current]; *p != '\n'; p++) {
90 if (!islower(*p))
91 break;
92 if (*p == 'q') {
93 q = p + 1;
94 if (*q != 'u')
95 break;
96 else {
97 while ((*q = *(q + 1)))
98 q++;
99 }
100 len++;
101 }
102 len++;
103 }
104 if (*p != '\n' || len < 3 || len > MAXWORDLEN)
105 continue;
106 if (argc == 2 && nwords % atoi(argv[1]))
107 continue;
108
109 *p = '\0';
110 p = buf[current];
111 q = buf[prev];
112 qcount = 0;
113 while ((ch = *p++) == *q++ && ch != '\0')
114 if (ch == 'q')
115 qcount++;
116 common = p - buf[current] - 1;
117 printf("%c%s", common + qcount, p - 1);
118 prev = !prev;
119 current = !current;
120 }
121 fprintf(stderr, "%d words\n", nwords);
122 fflush(stdout);
123 if (ferror(stdout)) {
124 perror("error writing standard output");
125 exit(1);
126 }
127 exit(0);
128 }