diff options
Diffstat (limited to 'ex1-9.c')
-rw-r--r-- | ex1-9.c | 38 |
1 files changed, 20 insertions, 18 deletions
@@ -2,27 +2,29 @@ #include <stdbool.h> #include <string.h> -/* Copy stdin to stdout, replacing consecutive spaces with a single space - Edge case: a leading space is retained if there are leading spaces. - Edge case: an empty file just doesn't echo anything, but emits a warning on stderr */ -int remspace(FILE* fin, FILE* fout, FILE* ferr) { - int c = fgetc(fin); - if (c == EOF) { - fprintf(ferr, "warning: no input\n"); - goto exit; - } - fputc(c, fout); - bool bl = (c == ' '); - while ((c = fgetc(fin)) != EOF) { - if ((!bl) || (c != ' ')) { +static int _remspace_internal(FILE* fin, FILE* fout, FILE* ferr, int pc) { + int c = fgetc(fin); + if (c == EOF) { + // Should I still put EOF here? does it work with pipes or not? + //fputc(c, fout); + return 0; + } + if (c == ' ' && pc == ' ') { + // skip it + } else { fputc(c, fout); } - bl = (c == ' '); - } -exit: + __attribute__((musttail)) + return _remspace_internal(fin, fout, ferr, c); +} + +/* Copy stdin to stdout, replacing consecutive spaces with a single space + Edge case: a leading space is retained if there are leading spaces. */ +int remspace(FILE* fin, FILE* fout, FILE* ferr) { + int res = _remspace_internal(fin, fout, ferr, -1); fflush(fout); fflush(ferr); - return 0; + return res; } #ifndef test @@ -47,7 +49,7 @@ void do_test(CuTest* tc, char* input, const char* expected, const char* err_expe CuAssertStrEquals(tc, err_expected, err_buf); } void test_remspace_empty(CuTest* tc) { - do_test(tc, "", "", "warning: no input\n"); + do_test(tc, "", "", ""); } void test_remspace_oneword(CuTest* tc) { do_test(tc, "foobar", "foobar", ""); |