fold: fix a corner case. By Tomas Heinrich (heinrich.tomas AT gmail.com)

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2009-11-04 15:31:19 +01:00
parent 692bcff577
commit ded688c6f6
2 changed files with 21 additions and 8 deletions

View File

@ -30,7 +30,7 @@ static int adjust_column(int column, char c)
column = 0; column = 0;
else if (c == '\t') else if (c == '\t')
column = column + 8 - column % 8; column = column + 8 - column % 8;
else /* if (isprint (c)) */ else /* if (isprint(c)) */
column++; column++;
} else } else
column++; column++;
@ -38,7 +38,7 @@ static int adjust_column(int column, char c)
} }
int fold_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int fold_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int fold_main(int argc, char **argv) int fold_main(int argc UNUSED_PARAM, char **argv)
{ {
char *line_out = NULL; char *line_out = NULL;
int allocated_out = 0; int allocated_out = 0;
@ -49,7 +49,7 @@ int fold_main(int argc, char **argv)
if (ENABLE_INCLUDE_SUSv2) { if (ENABLE_INCLUDE_SUSv2) {
/* Turn any numeric options into -w options. */ /* Turn any numeric options into -w options. */
for (i = 1; i < argc; i++) { for (i = 1; argv[i]; i++) {
char const *a = argv[i]; char const *a = argv[i];
if (*a++ == '-') { if (*a++ == '-') {
@ -122,12 +122,11 @@ int fold_main(int argc, char **argv)
} }
goto rescan; goto rescan;
} }
} else { }
if (offset_out == 0) { if (offset_out == 0) {
line_out[offset_out++] = c; line_out[offset_out++] = c;
continue; continue;
} }
}
line_out[offset_out++] = '\n'; line_out[offset_out++] = '\n';
fwrite(line_out, sizeof(char), (size_t) offset_out, stdout); fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
column = offset_out = 0; column = offset_out = 0;

14
testsuite/fold.tests Executable file
View File

@ -0,0 +1,14 @@
#!/bin/sh
# Copyright 2009 by Denys Vlasenko
# Licensed under GPL v2, see file LICENSE for details.
. testing.sh
# testing "test name" "options" "expected result" "file input" "stdin"
testing "fold -s" "fold -w 7 -s" \
"123456\n\t\nasdf" \
"" \
"123456\tasdf" \
exit $FAILCOUNT