- caesar(6) is not a SETGIDGAME, so we don't need to revoke any privileges.
- replaced direct POSIX read/write with stdio, which made the code simpler. - added even more error checking. - restructured the code to make each function fit on one screen (well, except one). - now the code reflects the intended purpose of the program. - return 0 instead of EXIT_FAILURE on success.
This commit is contained in:
parent
15bc25dc45
commit
d590f7d069
@ -1,4 +1,4 @@
|
|||||||
/* $NetBSD: caesar.c,v 1.15 2005/05/23 23:02:30 rillig Exp $ */
|
/* $NetBSD: caesar.c,v 1.16 2005/07/22 11:52:23 rillig Exp $ */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 1989, 1993
|
* Copyright (c) 1989, 1993
|
||||||
@ -48,7 +48,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
|
|||||||
#if 0
|
#if 0
|
||||||
static char sccsid[] = "@(#)caesar.c 8.1 (Berkeley) 5/31/93";
|
static char sccsid[] = "@(#)caesar.c 8.1 (Berkeley) 5/31/93";
|
||||||
#else
|
#else
|
||||||
__RCSID("$NetBSD: caesar.c,v 1.15 2005/05/23 23:02:30 rillig Exp $");
|
__RCSID("$NetBSD: caesar.c,v 1.16 2005/07/22 11:52:23 rillig Exp $");
|
||||||
#endif
|
#endif
|
||||||
#endif /* not lint */
|
#endif /* not lint */
|
||||||
|
|
||||||
@ -60,7 +60,6 @@ __RCSID("$NetBSD: caesar.c,v 1.15 2005/05/23 23:02:30 rillig Exp $");
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#define NCHARS (1 << CHAR_BIT)
|
#define NCHARS (1 << CHAR_BIT)
|
||||||
#define LETTERS (26)
|
#define LETTERS (26)
|
||||||
@ -85,7 +84,7 @@ init_rottbl(int rot)
|
|||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
rot %= LETTERS;
|
rot %= LETTERS; /* prevent integer overflow */
|
||||||
|
|
||||||
for (i = 0; i < NCHARS; i++)
|
for (i = 0; i < NCHARS; i++)
|
||||||
rottbl[i] = (unsigned char)i;
|
rottbl[i] = (unsigned char)i;
|
||||||
@ -97,51 +96,56 @@ init_rottbl(int rot)
|
|||||||
rottbl[lower[i]] = lower[(i + rot) % LETTERS];
|
rottbl[lower[i]] = lower[(i + rot) % LETTERS];
|
||||||
}
|
}
|
||||||
|
|
||||||
static void __attribute__((__noreturn__))
|
static void
|
||||||
printrot(const char *arg)
|
print_file(void)
|
||||||
{
|
{
|
||||||
int ch;
|
int ch;
|
||||||
|
|
||||||
|
while ((ch = getchar()) != EOF) {
|
||||||
|
if (putchar(rottbl[ch]) == EOF) {
|
||||||
|
err(EXIT_FAILURE, "<stdout>");
|
||||||
|
/* NOTREACHED */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
print_array(const unsigned char *a, size_t len)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
for (i = 0; i < len; i++) {
|
||||||
|
if (putchar(rottbl[a[i]]) == EOF) {
|
||||||
|
err(EXIT_FAILURE, "<stdout>");
|
||||||
|
/* NOTREACHED */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
get_rotation(const char *arg)
|
||||||
|
{
|
||||||
long rot;
|
long rot;
|
||||||
char *endp;
|
char *endp;
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
rot = strtol(arg, &endp, 10);
|
rot = strtol(arg, &endp, 10);
|
||||||
if (*endp != '\0') {
|
if (!(errno == 0 && *endp == '\0' && 0 <= rot && rot <= INT_MAX)) {
|
||||||
errx(EXIT_FAILURE, "bad rotation value: %s", arg);
|
errx(EXIT_FAILURE, "bad rotation value: %s", arg);
|
||||||
/* NOTREACHED */
|
/* NOTREACHED */
|
||||||
}
|
}
|
||||||
if (errno == ERANGE || rot < 0 || rot > INT_MAX) {
|
return (int) rot;
|
||||||
errx(EXIT_FAILURE, "rotation value out of range: %s", arg);
|
|
||||||
/* NOTREACHED */
|
|
||||||
}
|
|
||||||
init_rottbl((int)rot);
|
|
||||||
|
|
||||||
while ((ch = getchar()) != EOF) {
|
|
||||||
if (putchar(rottbl[ch]) == EOF) {
|
|
||||||
err(EXIT_FAILURE, "writing to stdout");
|
|
||||||
/* NOTREACHED */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
exit(EXIT_SUCCESS);
|
|
||||||
/* NOTREACHED */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
static void
|
||||||
main(int argc, char **argv)
|
guess_and_rotate(void)
|
||||||
{
|
{
|
||||||
ssize_t i, nread, ntotal;
|
|
||||||
double dot, winnerdot;
|
|
||||||
int try, winner;
|
|
||||||
unsigned char inbuf[2048];
|
unsigned char inbuf[2048];
|
||||||
unsigned int obs[NCHARS];
|
unsigned int obs[NCHARS];
|
||||||
|
size_t i, nread;
|
||||||
/* revoke setgid privileges */
|
double dot, winnerdot;
|
||||||
(void)setgid(getgid());
|
int try, winner;
|
||||||
|
int ch;
|
||||||
if (argc > 1) {
|
|
||||||
printrot(argv[1]);
|
|
||||||
/* NOTREACHED */
|
|
||||||
}
|
|
||||||
|
|
||||||
/* adjust frequency table to weight low probs REAL low */
|
/* adjust frequency table to weight low probs REAL low */
|
||||||
for (i = 0; i < LETTERS; i++)
|
for (i = 0; i < LETTERS; i++)
|
||||||
@ -150,18 +154,13 @@ main(int argc, char **argv)
|
|||||||
/* zero out observation table */
|
/* zero out observation table */
|
||||||
(void)memset(obs, 0, sizeof(obs));
|
(void)memset(obs, 0, sizeof(obs));
|
||||||
|
|
||||||
for (ntotal = 0; (size_t) ntotal < sizeof(inbuf); ntotal += nread) {
|
for (nread = 0; nread < sizeof(inbuf); nread++) {
|
||||||
nread = read(STDIN_FILENO, &inbuf[ntotal],
|
if ((ch = getchar()) == EOF)
|
||||||
sizeof(inbuf) - ntotal);
|
|
||||||
if (nread < 0) {
|
|
||||||
err(EXIT_FAILURE, "reading from stdin");
|
|
||||||
/* NOTREACHED */
|
|
||||||
}
|
|
||||||
if (nread == 0)
|
|
||||||
break;
|
break;
|
||||||
|
inbuf[nread] = (unsigned char) ch;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < ntotal; i++)
|
for (i = 0; i < nread; i++)
|
||||||
obs[inbuf[i]]++;
|
obs[inbuf[i]]++;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -182,20 +181,36 @@ main(int argc, char **argv)
|
|||||||
winnerdot = dot;
|
winnerdot = dot;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
init_rottbl(winner);
|
|
||||||
|
|
||||||
while (ntotal > 0) {
|
init_rottbl(winner);
|
||||||
for (i = 0; i < ntotal; i++) {
|
print_array(inbuf, nread);
|
||||||
if (putchar(rottbl[inbuf[i]]) == EOF) {
|
print_file();
|
||||||
err(EXIT_FAILURE, "writing to stdout");
|
}
|
||||||
/* NOTREACHED */
|
|
||||||
}
|
int
|
||||||
}
|
main(int argc, char **argv)
|
||||||
if ((ntotal = read(STDIN_FILENO, inbuf, sizeof(inbuf))) < 0) {
|
{
|
||||||
err(EXIT_FAILURE, "reading from stdin");
|
if (argc == 1) {
|
||||||
/* NOTREACHED */
|
guess_and_rotate();
|
||||||
}
|
} else if (argc == 2) {
|
||||||
}
|
init_rottbl(get_rotation(argv[1]));
|
||||||
exit(EXIT_FAILURE);
|
print_file();
|
||||||
/* NOTREACHED */
|
} else {
|
||||||
|
(void)fprintf(stderr, "usage: caesar [rot]\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
/* NOTREACHED */
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ferror(stdin)) {
|
||||||
|
errx(EXIT_FAILURE, "<stdin>");
|
||||||
|
/* NOTREACHED */
|
||||||
|
}
|
||||||
|
|
||||||
|
(void)fflush(stdout);
|
||||||
|
if (ferror(stdout)) {
|
||||||
|
err(EXIT_FAILURE, "<stdout>");
|
||||||
|
/* NOTREACHED */
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user