]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - caesar/caesar.c
minor KNFify
[bsdgames-darwin.git] / caesar / caesar.c
1 /* $NetBSD: caesar.c,v 1.6 1997/10/11 02:40:39 lukem Exp $ */
2
3 /*
4 * Copyright (c) 1989, 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 * Rick Adams.
9 *
10 * Authors:
11 * Stan King, John Eldridge, based on algorithm suggested by
12 * Bob Morris
13 * 29-Sep-82
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. All advertising materials mentioning features or use of this software
24 * must display the following acknowledgement:
25 * This product includes software developed by the University of
26 * California, Berkeley and its contributors.
27 * 4. Neither the name of the University nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * SUCH DAMAGE.
42 */
43
44 #include <sys/cdefs.h>
45 #ifndef lint
46 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
47 The Regents of the University of California. All rights reserved.\n");
48 #endif /* not lint */
49
50 #ifndef lint
51 #if 0
52 static char sccsid[] = "@(#)caesar.c 8.1 (Berkeley) 5/31/93";
53 #else
54 __RCSID("$NetBSD: caesar.c,v 1.6 1997/10/11 02:40:39 lukem Exp $");
55 #endif
56 #endif /* not lint */
57
58 #include <ctype.h>
59 #include <err.h>
60 #include <errno.h>
61 #include <math.h>
62 #include <stdio.h>
63 #include <string.h>
64 #include <stdlib.h>
65 #include <unistd.h>
66
67 #define LINELENGTH 2048
68 #define ROTATE(ch, perm) \
69 isupper(ch) ? ('A' + (ch - 'A' + perm) % 26) : \
70 islower(ch) ? ('a' + (ch - 'a' + perm) % 26) : ch
71
72 /*
73 * letter frequencies (taken from some unix(tm) documentation)
74 * (unix is a trademark of Bell Laboratories)
75 */
76 double stdf[26] = {
77 7.97, 1.35, 3.61, 4.78, 12.37, 2.01, 1.46, 4.49, 6.39, 0.04,
78 0.42, 3.81, 2.69, 5.92, 6.96, 2.91, 0.08, 6.63, 8.77, 9.68,
79 2.62, 0.81, 1.88, 0.23, 2.07, 0.06,
80 };
81
82
83 int main __P((int, char *[]));
84 void printit __P((char *));
85
86 int
87 main(argc, argv)
88 int argc;
89 char **argv;
90 {
91 int ch, dot, i, nread, winnerdot;
92 char *inbuf;
93 int obs[26], try, winner;
94
95 winnerdot = 0;
96 if (argc > 1)
97 printit(argv[1]);
98
99 if (!(inbuf = malloc(LINELENGTH)))
100 errx(1, "out of memory");
101
102 /* adjust frequency table to weight low probs REAL low */
103 for (i = 0; i < 26; ++i)
104 stdf[i] = log(stdf[i]) + log(26.0 / 100.0);
105
106 /* zero out observation table */
107 memset(obs, 0, 26 * sizeof(int));
108
109 if ((nread = read(STDIN_FILENO, inbuf, LINELENGTH)) < 0)
110 err(1, "reading from stdin");
111 for (i = nread; i--;) {
112 ch = inbuf[i];
113 if (islower(ch))
114 ++obs[ch - 'a'];
115 else if (isupper(ch))
116 ++obs[ch - 'A'];
117 }
118
119 /*
120 * now "dot" the freqs with the observed letter freqs
121 * and keep track of best fit
122 */
123 for (try = winner = 0; try < 26; ++try) { /* += 13) { */
124 dot = 0;
125 for (i = 0; i < 26; i++)
126 dot += obs[i] * stdf[(i + try) % 26];
127 /* initialize winning score */
128 if (try == 0)
129 winnerdot = dot;
130 if (dot > winnerdot) {
131 /* got a new winner! */
132 winner = try;
133 winnerdot = dot;
134 }
135 }
136
137 for (;;) {
138 for (i = 0; i < nread; ++i) {
139 ch = inbuf[i];
140 putchar(ROTATE(ch, winner));
141 }
142 if (nread < LINELENGTH)
143 break;
144 if ((nread = read(STDIN_FILENO, inbuf, LINELENGTH)) < 0)
145 err(1, "reading from stdin");
146 }
147 exit(0);
148 }
149
150 void
151 printit(arg)
152 char *arg;
153 {
154 int ch, rot;
155
156 if ((rot = atoi(arg)) < 0)
157 errx(1, "bad rotation value.");
158 while ((ch = getchar()) != EOF)
159 putchar(ROTATE(ch, rot));
160 exit(0);
161 }