diff --git a/makefile b/makefile index 4648c2c..616fa4c 100644 --- a/makefile +++ b/makefile @@ -31,3 +31,6 @@ format: $(SRC) test: $(OUTPUT) /bin/echo -e "123\n45 67" | ./$(OUTPUT) + /bin/echo -n "" | ./wc + ./$(OUTPUT) $(SRC) + ./$(OUTPUT) nonexistent_file || true diff --git a/wc b/wc index 2b3fcac..39b027f 100755 Binary files a/wc and b/wc differ diff --git a/wc.c b/wc.c index b2047e6..3c0c547 100644 --- a/wc.c +++ b/wc.c @@ -2,11 +2,11 @@ #include // read, isatty // STDIN_FILENO -int process_fp(int filenum) +int process_fn(int fn) { - char buffer[1]; - while (read(filenum, buffer, sizeof(buffer)) > 0) { - printf("%c", buffer[0]); + char c[1]; + while (read(fn, c, sizeof(c)) > 0) { + printf("%c", c[0]); } return 0; } @@ -14,9 +14,17 @@ int process_fp(int filenum) int main(int argc, char** argv) { if (!isatty(STDIN_FILENO)) { - return process_fp(STDIN_FILENO); + return process_fn(STDIN_FILENO); } else if(argc > 1){ + FILE* fp = fopen(argv[1], "r"); + if(!fp){ + perror("Could not open file"); + return 1; + } + return process_fn(fileno(fp)); + } else{ printf("To do"); + } return 0; }