new copyright notice
[unix-history] / usr / src / games / caesar / caesar.c
CommitLineData
989956f7 1/*
fd9714d7
KB
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
989956f7 4 *
fd9714d7
KB
5 * This code is derived from software contributed to Berkeley by
6 * Rick Adams.
989956f7 7 *
fd9714d7
KB
8 * Authors:
9 * Stan King, John Eldridge, based on algorithm suggested by
10 * Bob Morris
989956f7
KB
11 * 29-Sep-82
12 *
a6547b1d 13 * %sccs.include.redist.c%
989956f7
KB
14 */
15
fd9714d7
KB
16#ifndef lint
17char copyright[] =
18"@(#) Copyright (c) 1989 The Regents of the University of California.\n\
19 All rights reserved.\n";
20#endif /* not lint */
21
22#ifndef lint
a6547b1d 23static char sccsid[] = "@(#)caesar.c 5.4 (Berkeley) %G%";
fd9714d7 24#endif /* not lint */
989956f7 25
fd9714d7 26#include <math.h>
989956f7
KB
27#include <stdio.h>
28#include <ctype.h>
7b8a3929 29#include <unistd.h>
fd9714d7
KB
30
31#define LINELENGTH 2048
32#define ROTATE(ch, perm) \
7b8a3929 33 isupper(ch) ? ('A' + (ch - 'A' + perm) % 26) : \
fd9714d7 34 islower(ch) ? ('a' + (ch - 'a' + perm) % 26) : ch
989956f7 35
7b8a3929
KB
36/*
37 * letter frequencies (taken from some unix(tm) documentation)
38 * (unix is a trademark of Bell Laboratories)
39 */
40double stdf[26] = {
41 7.97, 1.35, 3.61, 4.78, 12.37, 2.01, 1.46, 4.49, 6.39, 0.04,
42 0.42, 3.81, 2.69, 5.92, 6.96, 2.91, 0.08, 6.63, 8.77, 9.68,
43 2.62, 0.81, 1.88, 0.23, 2.07, 0.06,
44};
45
989956f7 46main(argc, argv)
fd9714d7
KB
47 int argc;
48 char **argv;
989956f7 49{
7b8a3929
KB
50 extern int errno;
51 register int ch, dot, i, nread, winnerdot;
fd9714d7 52 register char *inbuf;
7b8a3929
KB
53 int obs[26], try, winner;
54 char *malloc(), *strerror();
989956f7 55
7b8a3929
KB
56 if (argc > 1)
57 printit(argv[1]);
989956f7 58
fd9714d7
KB
59 if (!(inbuf = malloc(LINELENGTH))) {
60 (void)fprintf(stderr, "caesar: out of memory.\n");
61 exit(1);
989956f7
KB
62 }
63
fd9714d7
KB
64 /* adjust frequency table to weight low probs REAL low */
65 for (i = 0; i < 26; ++i)
66 stdf[i] = log(stdf[i]) + log(26.0 / 100.0);
67
7b8a3929
KB
68 /* zero out observation table */
69 bzero(obs, 26 * sizeof(int));
989956f7 70
7b8a3929
KB
71 if ((nread = read(STDIN_FILENO, inbuf, LINELENGTH)) < 0) {
72 (void)fprintf(stderr, "caesar: %s\n", strerror(errno));
73 exit(1);
74 }
75 for (i = nread; i--;) {
76 ch = inbuf[i];
77 if (islower(ch))
78 ++obs[ch - 'a'];
79 else if (isupper(ch))
80 ++obs[ch - 'A'];
81 }
989956f7 82
7b8a3929
KB
83 /*
84 * now "dot" the freqs with the observed letter freqs
85 * and keep track of best fit
86 */
87 for (try = winner = 0; try < 26; ++try) { /* += 13) { */
88 dot = 0;
89 for (i = 0; i < 26; i++)
90 dot += obs[i] * stdf[(i + try) % 26];
91 /* initialize winning score */
92 if (try == 0)
93 winnerdot = dot;
94 if (dot > winnerdot) {
95 /* got a new winner! */
96 winner = try;
97 winnerdot = dot;
989956f7 98 }
7b8a3929 99 }
989956f7 100
7b8a3929
KB
101 for (;;) {
102 for (i = 0; i < nread; ++i) {
103 ch = inbuf[i];
fd9714d7
KB
104 putchar(ROTATE(ch, winner));
105 }
7b8a3929
KB
106 if (nread < LINELENGTH)
107 break;
108 if ((nread = read(STDIN_FILENO, inbuf, LINELENGTH)) < 0) {
109 (void)fprintf(stderr, "caesar: %s\n", strerror(errno));
110 exit(1);
111 }
989956f7 112 }
7b8a3929 113 exit(0);
fd9714d7 114}
989956f7 115
7b8a3929
KB
116printit(arg)
117 char *arg;
989956f7 118{
7b8a3929 119 register int ch, rot;
fd9714d7 120
7b8a3929
KB
121 if ((rot = atoi(arg)) < 0) {
122 (void)fprintf(stderr, "caesar: bad rotation value.\n");
123 exit(1);
124 }
fd9714d7
KB
125 while ((ch = getchar()) != EOF)
126 putchar(ROTATE(ch, rot));
7b8a3929 127 exit(0);
989956f7 128}