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