summaryrefslogtreecommitdiffstats
path: root/text-utils/rev.c
diff options
context:
space:
mode:
Diffstat (limited to 'text-utils/rev.c')
-rw-r--r--text-utils/rev.c23
1 files changed, 12 insertions, 11 deletions
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;
}
-