1) Fix hang at the end of line buffer (PR 95715)

2) Localize

PR:             95715
Submitted by:   Li-Lun Wang <llwang@infor.org> (slightly edited by me)
This commit is contained in:
Andrey A. Chernov 2006-04-14 17:32:27 +00:00
parent 1fe5a490ab
commit 1107d0af30
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=157758
2 changed files with 11 additions and 8 deletions

View File

@ -54,6 +54,7 @@ __FBSDID("$FreeBSD$");
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <limits.h> #include <limits.h>
#include <locale.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -84,6 +85,9 @@ main(int argc, char *argv[])
random_type = RANDOM_TYPE_UNSET; random_type = RANDOM_TYPE_UNSET;
random_exit = randomize_lines = random_type = unbuffer_output = 0; random_exit = randomize_lines = random_type = unbuffer_output = 0;
unique_output = 1; unique_output = 1;
(void)setlocale(LC_CTYPE, "");
while ((ch = getopt(argc, argv, "ef:hlruUw")) != -1) while ((ch = getopt(argc, argv, "ef:hlruUw")) != -1)
switch (ch) { switch (ch) {
case 'e': case 'e':

View File

@ -93,7 +93,7 @@ rand_node_append(struct rand_node *n)
int int
randomize_fd(int fd, int type, int unique, double denom) randomize_fd(int fd, int type, int unique, double denom)
{ {
u_char *buf, *p; u_char *buf;
u_int numnode, j, selected, slen; u_int numnode, j, selected, slen;
struct rand_node *n, *prev; struct rand_node *n, *prev;
int bufleft, eof, fndstr, ret; int bufleft, eof, fndstr, ret;
@ -148,30 +148,29 @@ randomize_fd(int fd, int type, int unique, double denom)
fndstr = 0; fndstr = 0;
} }
} else { } else {
p = (u_char *)realloc(buf, buflen * 2); buflen *= 2;
if (p == NULL) buf = (u_char *)realloc(buf, buflen);
if (buf == NULL)
err(1, "realloc"); err(1, "realloc");
buf = p;
if (!eof) { if (!eof) {
len = read(fd, &buf[i], buflen); len = read(fd, &buf[i], buflen - i);
if (len == -1) if (len == -1)
err(1, "read"); err(1, "read");
else if (len == 0) { else if (len == 0) {
eof++; eof++;
break; break;
} else if (len < (ssize_t)(buflen - i)) } else if (len < (ssize_t)(buflen - i))
buflen = (size_t)len; buflen = i + (size_t)len;
bufleft = (int)len; bufleft = (int)len;
} }
buflen *= 2;
} }
} }
if ((type == RANDOM_TYPE_LINES && buf[i] == '\n') || 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)) { (eof && i == buflen - 1)) {
make_token: make_token:
n = rand_node_allocate(); n = rand_node_allocate();