+/* $NetBSD: caesar.c,v 1.10 2000/01/09 17:17:20 jsm Exp $ */
+
/*
- * Copyright (c) 1989 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1989, 1993
+ * The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Rick Adams.
* SUCH DAMAGE.
*/
+#include <sys/cdefs.h>
#ifndef lint
-char copyright[] =
-"@(#) Copyright (c) 1989 The Regents of the University of California.\n\
- All rights reserved.\n";
+__COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
+ The Regents of the University of California. All rights reserved.\n");
#endif /* not lint */
#ifndef lint
-/*static char sccsid[] = "from: @(#)caesar.c 5.4 (Berkeley) 6/1/90";*/
-static char rcsid[] = "$Id: caesar.c,v 1.2 1993/08/01 18:55:37 mycroft Exp $";
+#if 0
+static char sccsid[] = "@(#)caesar.c 8.1 (Berkeley) 5/31/93";
+#else
+__RCSID("$NetBSD: caesar.c,v 1.10 2000/01/09 17:17:20 jsm Exp $");
+#endif
#endif /* not lint */
+#include <ctype.h>
+#include <err.h>
+#include <errno.h>
#include <math.h>
#include <stdio.h>
-#include <ctype.h>
+#include <string.h>
+#include <stdlib.h>
#include <unistd.h>
#define LINELENGTH 2048
2.62, 0.81, 1.88, 0.23, 2.07, 0.06,
};
+
+int main __P((int, char *[]));
+void printit __P((const char *)) __attribute__((__noreturn__));
+
+int
main(argc, argv)
int argc;
char **argv;
{
- extern int errno;
- register int ch, dot, i, nread, winnerdot;
- register char *inbuf;
+ int ch, dot, i, nread, winnerdot;
+ char *inbuf;
int obs[26], try, winner;
- char *malloc(), *strerror();
+ /* revoke setgid privileges */
+ setregid(getgid(), getgid());
+
+ winnerdot = 0;
if (argc > 1)
printit(argv[1]);
- if (!(inbuf = malloc(LINELENGTH))) {
- (void)fprintf(stderr, "caesar: out of memory.\n");
- exit(1);
- }
+ if (!(inbuf = malloc(LINELENGTH)))
+ err(1, NULL);
/* adjust frequency table to weight low probs REAL low */
for (i = 0; i < 26; ++i)
stdf[i] = log(stdf[i]) + log(26.0 / 100.0);
/* zero out observation table */
- bzero(obs, 26 * sizeof(int));
+ memset(obs, 0, 26 * sizeof(int));
- if ((nread = read(STDIN_FILENO, inbuf, LINELENGTH)) < 0) {
- (void)fprintf(stderr, "caesar: %s\n", strerror(errno));
- exit(1);
- }
+ if ((nread = read(STDIN_FILENO, inbuf, LINELENGTH)) < 0)
+ err(1, "reading from stdin");
for (i = nread; i--;) {
ch = inbuf[i];
if (islower(ch))
}
if (nread < LINELENGTH)
break;
- if ((nread = read(STDIN_FILENO, inbuf, LINELENGTH)) < 0) {
- (void)fprintf(stderr, "caesar: %s\n", strerror(errno));
- exit(1);
- }
+ if ((nread = read(STDIN_FILENO, inbuf, LINELENGTH)) < 0)
+ err(1, "reading from stdin");
}
exit(0);
}
+void
printit(arg)
- char *arg;
+ const char *arg;
{
- register int ch, rot;
+ int ch, rot;
- if ((rot = atoi(arg)) < 0) {
- (void)fprintf(stderr, "caesar: bad rotation value.\n");
- exit(1);
- }
+ if ((rot = atoi(arg)) < 0)
+ errx(1, "bad rotation value.");
while ((ch = getchar()) != EOF)
putchar(ROTATE(ch, rot));
exit(0);