From: Thomas Weißschuh Date: Sat, 12 Nov 2022 19:30:59 +0000 (+0100) Subject: rev: make separator configurable X-Git-Tag: v2.39-rc1~423^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d2082bf70e5dbd812c223ffaa95c2d0c1c5d77d5;p=thirdparty%2Futil-linux.git rev: make separator configurable --- diff --git a/text-utils/rev.c b/text-utils/rev.c index 568262f94f..f69e47ef7b 100644 --- a/text-utils/rev.c +++ b/text-utils/rev.c @@ -96,7 +96,7 @@ static void reverse_str(wchar_t *str, size_t n) } } -static size_t read_line(wchar_t *str, size_t n, FILE *stream) +static size_t read_line(wchar_t sep, wchar_t *str, size_t n, FILE *stream) { size_t r = 0; while (r < n) { @@ -104,7 +104,7 @@ static size_t read_line(wchar_t *str, size_t n, FILE *stream) if (c == WEOF) break; str[r++] = c; - if (c == L'\n') + if ((wchar_t) c == sep) break; } return r; @@ -120,6 +120,7 @@ int main(int argc, char *argv[]) { char const *filename = "stdin"; wchar_t *buf; + wchar_t sep = L'\n'; size_t len, bufsiz = BUFSIZ; FILE *fp = stdin; int ch, rval = EXIT_SUCCESS; @@ -167,7 +168,7 @@ int main(int argc, char *argv[]) line = 0; while (!feof(fp)) { - len = read_line(buf, bufsiz, fp); + len = read_line(sep, buf, bufsiz, fp); if (len == 0) continue; @@ -180,9 +181,9 @@ int main(int argc, char *argv[]) buf = xrealloc(buf, bufsiz * sizeof(wchar_t)); /* And fill the rest of the buffer */ - len += read_line(&buf[len], bufsiz/2, fp); + len += read_line(sep, &buf[len], bufsiz/2, fp); } - reverse_str(buf, buf[len - 1] == L'\n' ? len - 1 : len); + reverse_str(buf, buf[len - 1] == sep ? len - 1 : len); write_line(buf, len, stdout); line++; }