Skip to content

Commit

Permalink
cat(1): fix printing of non-printable chars with -v
Browse files Browse the repository at this point in the history
Fixes #14
  • Loading branch information
q66 committed Nov 17, 2023
1 parent 4db160b commit 5ddce8c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
16 changes: 8 additions & 8 deletions patches/src.freebsd.patch
Original file line number Diff line number Diff line change
Expand Up @@ -1843,10 +1843,11 @@

/* Reset EOF condition on stdin. */
if (fp == stdin && feof(stdin))
@@ -342,23 +349,33 @@
@@ -341,24 +348,32 @@
break;
continue;
}
} else if (vflag) {
- } else if (vflag) {
- (void)ungetc(ch, fp);
- /*
- * Our getwc(3) doesn't change file position
Expand All @@ -1859,11 +1860,10 @@
- memset(&fp->_mbstate, 0, sizeof(mbstate_t));
- if ((ch = getc(fp)) == EOF)
- break;
+ } else if (vflag && !isprint(ch)) {
+ mbstate_t st = {0};
+ unsigned char b;
+ size_t l = (size_t)-2;
+ if (ch == EOF)
+ break;
+ size_t l;
+ b = ch;
+ l = mbrtowc(&wch, (void *)&b, 1, &st);
+ if (l == (size_t)-1) {
Expand All @@ -1882,7 +1882,7 @@
+ l = mbrtowc(&wch, (void *)&b, 1, &st);
+ if (l == (size_t)-1) {
+ /* go back by the failed char */
+ ungetc(ch, fp);
+ ungetc(nch, fp);
+ wch = ch;
+ goto ilseq;
+ }
Expand All @@ -1891,7 +1891,7 @@
if (!iswascii(wch) && !iswprint(wch)) {
ilseq:
if (putchar('M') == EOF || putchar('-') == EOF)
@@ -390,6 +407,18 @@
@@ -390,6 +405,18 @@
}

static ssize_t
Expand All @@ -1910,7 +1910,7 @@
in_kernel_copy(int rfd)
{
int wfd;
@@ -400,6 +429,9 @@
@@ -400,6 +427,9 @@

while (ret > 0)
ret = copy_file_range(rfd, NULL, wfd, NULL, SSIZE_MAX, 0);
Expand Down
8 changes: 3 additions & 5 deletions src.freebsd/coreutils/cat/cat.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,10 @@ cook_cat(FILE *fp)
break;
continue;
}
} else if (vflag) {
} else if (vflag && !isprint(ch)) {
mbstate_t st = {0};
unsigned char b;
size_t l = (size_t)-2;
if (ch == EOF)
break;
size_t l;
b = ch;
l = mbrtowc(&wch, (void *)&b, 1, &st);
if (l == (size_t)-1) {
Expand All @@ -370,7 +368,7 @@ cook_cat(FILE *fp)
l = mbrtowc(&wch, (void *)&b, 1, &st);
if (l == (size_t)-1) {
/* go back by the failed char */
ungetc(ch, fp);
ungetc(nch, fp);
wch = ch;
goto ilseq;
}
Expand Down

0 comments on commit 5ddce8c

Please sign in to comment.