From 4997b369b97d3ea57fe417aa4c0ea30e24ef3d90 Mon Sep 17 00:00:00 2001 From: ache Date: Fri, 14 Apr 2006 17:32:27 +0000 Subject: 1) Fix hang at the end of line buffer (PR 95715) 2) Localize PR: 95715 Submitted by: Li-Lun Wang (slightly edited by me) --- games/random/random.c | 4 ++++ games/random/randomize_fd.c | 15 +++++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) (limited to 'games') diff --git a/games/random/random.c b/games/random/random.c index 62c83ba..b7b732a 100644 --- a/games/random/random.c +++ b/games/random/random.c @@ -54,6 +54,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -84,6 +85,9 @@ main(int argc, char *argv[]) random_type = RANDOM_TYPE_UNSET; random_exit = randomize_lines = random_type = unbuffer_output = 0; unique_output = 1; + + (void)setlocale(LC_CTYPE, ""); + while ((ch = getopt(argc, argv, "ef:hlruUw")) != -1) switch (ch) { case 'e': diff --git a/games/random/randomize_fd.c b/games/random/randomize_fd.c index 33588b1..b60c53c 100644 --- a/games/random/randomize_fd.c +++ b/games/random/randomize_fd.c @@ -93,7 +93,7 @@ rand_node_append(struct rand_node *n) int randomize_fd(int fd, int type, int unique, double denom) { - u_char *buf, *p; + u_char *buf; u_int numnode, j, selected, slen; struct rand_node *n, *prev; int bufleft, eof, fndstr, ret; @@ -148,30 +148,29 @@ randomize_fd(int fd, int type, int unique, double denom) fndstr = 0; } } else { - p = (u_char *)realloc(buf, buflen * 2); - if (p == NULL) + buflen *= 2; + buf = (u_char *)realloc(buf, buflen); + if (buf == NULL) err(1, "realloc"); - buf = p; if (!eof) { - len = read(fd, &buf[i], buflen); + len = read(fd, &buf[i], buflen - i); if (len == -1) err(1, "read"); else if (len == 0) { eof++; break; } else if (len < (ssize_t)(buflen - i)) - buflen = (size_t)len; + buflen = i + (size_t)len; bufleft = (int)len; } - buflen *= 2; } } if ((type == RANDOM_TYPE_LINES && buf[i] == '\n') || - (type == RANDOM_TYPE_WORDS && isspace((int)buf[i])) || + (type == RANDOM_TYPE_WORDS && isspace(buf[i])) || (eof && i == buflen - 1)) { make_token: n = rand_node_allocate(); -- cgit v1.1