From a9464f41539f4800744b4185c6a4d4c5e38eeadf Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Wed, 28 Nov 2012 03:12:02 -0600 Subject: The previous wc -m didn't handle multibyte characters that crossed a buffer boundary, so take a guess at making that work. (I haven't got a test case for this. I also don't know how to handle invalid sequences so just don't count them.) --- toys/posix/wc.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'toys') diff --git a/toys/posix/wc.c b/toys/posix/wc.c index d2eb306d..d6029b6e 100644 --- a/toys/posix/wc.c +++ b/toys/posix/wc.c @@ -51,24 +51,30 @@ static void do_wc(int fd, char *name) unsigned long word=0, lengths[]={0,0,0}; for (;;) { - len = read(fd, toybuf, sizeof(toybuf)); - if (len<0) { + i = 0; +again: + len = i+read(fd, toybuf+i, sizeof(toybuf)-i); + if (len < i) { perror_msg("%s",name); - toys.exitval = EXIT_FAILURE; + toys.exitval = 1; } - if (len<1) break; + if (!len) break; for (i=0; i