From c61e14d3a8412cd50d98aab604e607692c844c8a Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 14 Apr 2024 21:33:30 +0200 Subject: Adding upstream version 2.40. Signed-off-by: Daniel Baumann --- text-utils/rev.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'text-utils/rev.c') diff --git a/text-utils/rev.c b/text-utils/rev.c index fbf04d1..4b73189 100644 --- a/text-utils/rev.c +++ b/text-utils/rev.c @@ -79,8 +79,8 @@ static void __attribute__((__noreturn__)) usage(void) fputs(_("Reverse lines characterwise.\n"), out); fputs(USAGE_OPTIONS, out); - printf(USAGE_HELP_OPTIONS(16)); - printf(USAGE_MAN_TAIL("rev(1)")); + fprintf(out, USAGE_HELP_OPTIONS(16)); + fprintf(out, USAGE_MAN_TAIL("rev(1)")); exit(EXIT_SUCCESS); } @@ -157,7 +157,7 @@ int main(int argc, char *argv[]) argc -= optind; argv += optind; - buf = xmalloc(bufsiz * sizeof(wchar_t)); + buf = xreallocarray(NULL, bufsiz, sizeof(wchar_t)); do { if (*argv) { @@ -173,8 +173,6 @@ int main(int argc, char *argv[]) line = 0; while (!feof(fp)) { len = read_line(sep, buf, bufsiz, fp); - if (len == 0) - continue; /* This is my hack from setpwnam.c -janl */ while (len == bufsiz && !feof(fp)) { @@ -182,19 +180,23 @@ int main(int argc, char *argv[]) /* So now we double the buffer size */ bufsiz *= 2; - buf = xrealloc(buf, bufsiz * sizeof(wchar_t)); + buf = xreallocarray(buf, bufsiz, sizeof(wchar_t)); /* And fill the rest of the buffer */ len += read_line(sep, &buf[len], bufsiz/2, fp); } + if (ferror(fp)) { + warn("%s: %ju", filename, line); + rval = EXIT_FAILURE; + break; + } + if (len == 0) + continue; + reverse_str(buf, buf[len - 1] == sep ? len - 1 : len); write_line(buf, len, stdout); line++; } - if (ferror(fp)) { - warn("%s: %ju", filename, line); - rval = EXIT_FAILURE; - } if (fp != stdin) fclose(fp); } while(*argv); @@ -202,4 +204,3 @@ int main(int argc, char *argv[]) free(buf); return rval; } - -- cgit v1.2.3