diff options
author | Rob Landley <rob@landley.net> | 2012-12-01 00:42:01 -0600 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2012-12-01 00:42:01 -0600 |
commit | 6e3876a5a6800e289d34b107bddf71f17c641694 (patch) | |
tree | 8da7532c0c247bef79825af224f5541f61eb2f38 | |
parent | f6379761b605a606764bc139c5b64c75c45e0559 (diff) | |
download | toybox-6e3876a5a6800e289d34b107bddf71f17c641694.tar.gz |
Felix Janda pointed out that the r in mbrtowc() stands for "restartable" so it's already buffering the partial data we feed it, so rolling back most of the last commit to wc.
-rw-r--r-- | toys/posix/wc.c | 27 |
1 files changed, 11 insertions, 16 deletions
diff --git a/toys/posix/wc.c b/toys/posix/wc.c index d6029b6e..a9c588e8 100644 --- a/toys/posix/wc.c +++ b/toys/posix/wc.c @@ -51,30 +51,25 @@ static void do_wc(int fd, char *name) unsigned long word=0, lengths[]={0,0,0}; for (;;) { - i = 0; -again: - len = i+read(fd, toybuf+i, sizeof(toybuf)-i); - if (len < i) { + len = read(fd, toybuf, sizeof(toybuf)); + if (len<0) { perror_msg("%s",name); toys.exitval = 1; } - if (!len) break; + if (len<1) break; for (i=0; i<len; i+=clen) { - if (CFG_TOYBOX_I18N && (toys.optflags&FLAG_m)) { - wchar_t wchar = 0; + wchar_t wchar; + if (CFG_TOYBOX_I18N && (toys.optflags&FLAG_m)) { clen = mbrtowc(&wchar, toybuf+i, len-i, 0); - if (clen < 1) { - // If the problem might be buffer wrap, move and read more data - if (i) { - memmove(toybuf, toybuf+i, sizeof(toybuf)-i); - i = len - i; - goto again; - } else { - clen=1; + if (clen == -1) { + if (i != len-1) { + clen = 1; continue; - } + } else break; } + if (clen == -2) break; + if (clen == 0) clen=1; space = iswspace(wchar); } else space = isspace(toybuf[i]); |