From 9d6db6fc47dc6fad850e57f9808aa652462408a2 Mon Sep 17 00:00:00 2001 From: qorg11 Date: Tue, 16 Jun 2020 21:28:56 +0200 Subject: [PATCH] Improved wc --- src/wc.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/wc.c b/src/wc.c index a262d9a..4558464 100644 --- a/src/wc.c +++ b/src/wc.c @@ -1,12 +1,12 @@ #include -int -main(void) +void +wc(FILE *file) { char c; - int newlines = 0, spaces = 0, bytes = 0; - - while((c = getchar()) != EOF) + int newlines, spaces, bytes = 0; + newlines = spaces = bytes = 0; + while((c = fgetc(file)) > 0) { bytes++; if(c == '\n') @@ -14,7 +14,22 @@ main(void) if(c == ' ') spaces++; } - printf("%i %i %i\n",newlines,spaces,bytes); + fclose(file); +} + +int +main(int argc, char *argv[]) +{ + if (argc == 1) + wc(stdin); + else + { + for(int i = 1; i