From 7b638af8acb02ec4fb6007a3c7959c076391077f Mon Sep 17 00:00:00 2001 From: ache Date: Sun, 10 Aug 2008 11:31:56 +0000 Subject: All cosmetic. 1) Rename RANDOM_MAX to RANDOM_MAX_PLUS1 to not confuse with random()'s max 2) Use calloc() instead of zeroing fields explicitly 3) "too many lines" -> "too many delimiters" for err() --- games/random/random.c | 6 +++--- games/random/randomize_fd.c | 13 +++++-------- games/random/randomize_fd.h | 2 +- 3 files changed, 9 insertions(+), 12 deletions(-) (limited to 'games') diff --git a/games/random/random.c b/games/random/random.c index 122ce45..2580348 100644 --- a/games/random/random.c +++ b/games/random/random.c @@ -162,7 +162,7 @@ main(int argc, char *argv[]) /* Compute a random exit status between 0 and denom - 1. */ if (random_exit) - return (int)(denom * random() / RANDOM_MAX); + return (int)(denom * random() / RANDOM_MAX_PLUS1); /* * Select whether to print the first line. (Prime the pump.) @@ -170,7 +170,7 @@ main(int argc, char *argv[]) * 0 (which has a 1 / denom chance of being true), we select the * line. */ - selected = (int)(denom * random() / RANDOM_MAX) == 0; + selected = (int)(denom * random() / RANDOM_MAX_PLUS1) == 0; while ((ch = getchar()) != EOF) { if (selected) (void)putchar(ch); @@ -180,7 +180,7 @@ main(int argc, char *argv[]) err(2, "stdout"); /* Now see if the next line is to be printed. */ - selected = (int)(denom * random() / RANDOM_MAX) == 0; + selected = (int)(denom * random() / RANDOM_MAX_PLUS1) == 0; } } if (ferror(stdin)) diff --git a/games/random/randomize_fd.c b/games/random/randomize_fd.c index c625d14..960c4b9 100644 --- a/games/random/randomize_fd.c +++ b/games/random/randomize_fd.c @@ -48,13 +48,10 @@ rand_node_allocate(void) { struct rand_node *n; - n = (struct rand_node *)malloc(sizeof(struct rand_node)); + n = (struct rand_node *)calloc(1, sizeof(struct rand_node)); if (n == NULL) - err(1, "malloc"); + err(1, "calloc"); - n->len = 0; - n->cp = NULL; - n->next = NULL; return(n); } @@ -175,9 +172,9 @@ randomize_fd(int fd, int type, int unique, double denom) (type == RANDOM_TYPE_WORDS && isspace(buf[i])) || (eof && i == buflen - 1)) { make_token: - if (numnode == RANDOM_MAX) { + if (numnode == RANDOM_MAX_PLUS1) { errno = EFBIG; - err(1, "too many lines"); + err(1, "too many delimiters"); } numnode++; n = rand_node_allocate(); @@ -215,7 +212,7 @@ randomize_fd(int fd, int type, int unique, double denom) if (n->cp == NULL) break; - if ((int)(denom * random() / RANDOM_MAX) == 0) { + if ((int)(denom * random() / RANDOM_MAX_PLUS1) == 0) { ret = printf("%.*s", (int)n->len - 1, n->cp); if (ret < 0) err(1, "printf"); diff --git a/games/random/randomize_fd.h b/games/random/randomize_fd.h index 6f9fac2..de3f873 100644 --- a/games/random/randomize_fd.h +++ b/games/random/randomize_fd.h @@ -33,7 +33,7 @@ * The random() function is defined to return values between 0 and * 2^31 - 1 inclusive in random(3). */ -#define RANDOM_MAX 0x80000000UL +#define RANDOM_MAX_PLUS1 0x80000000UL #define RANDOM_TYPE_UNSET 0 #define RANDOM_TYPE_LINES 1 -- cgit v1.1