Skip to content

Instantly share code, notes, and snippets.

@danieldk
Created November 21, 2019 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danieldk/f8cdaed4ba255fb2954ded50dd2931ed to your computer and use it in GitHub Desktop.
Save danieldk/f8cdaed4ba255fb2954ded50dd2931ed to your computer and use it in GitHub Desktop.
--- wc.c 2019-11-21 06:57:27.995567360 +0100
+++ wc3.c 2019-11-21 08:05:08.386223937 +0100
@@ -137,11 +137,10 @@
struct stat sb;
uintmax_t linect, wordct, charct;
int fd, len, warned;
- size_t clen;
short gotsp;
u_char *p;
u_char buf[MAXBSIZE];
- wchar_t wch;
+ char ch;
mbstate_t mbs;
linect = wordct = charct = 0;
@@ -210,31 +209,14 @@
(void)close(fd);
return (1);
}
- p = buf;
- while (len > 0) {
- if (!domulti || MB_CUR_MAX == 1) {
- clen = 1;
- wch = (unsigned char)*p;
- } else if ((clen = mbrtowc(&wch, p, len, &mbs)) ==
- (size_t)-1) {
- if (!warned) {
- errno = EILSEQ;
- warn("%s", file);
- warned = 1;
- }
- memset(&mbs, 0, sizeof(mbs));
- clen = 1;
- wch = (unsigned char)*p;
- } else if (clen == (size_t)-2)
- break;
- else if (clen == 0)
- clen = 1;
+ for (p = buf; p != buf + len; ++p) {
+ ch = (unsigned char) *p;
charct++;
- len -= clen;
- p += clen;
- if (wch == L'\n')
+ if (ch == '\n') {
++linect;
- if (iswspace(wch))
+ gotsp = 1;
+ }
+ else if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\v' || ch == '\f')
gotsp = 1;
else if (gotsp) {
gotsp = 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment