commit aef8269a68cf28666286eed0efb73bf5524434bb
parent c7520331211f54c090569f88279d811e367b1acb
Author: NunoSempere <nuno.sempere@protonmail.com>
Date: Fri, 8 Sep 2023 23:22:57 +0200
add a few more tests, mini-refactor.
Diffstat:
3 files changed, 16 insertions(+), 5 deletions(-)
diff --git 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
Binary files differ.
diff --git a/wc.c b/wc.c
@@ -2,11 +2,11 @@
#include <unistd.h> // 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;
}