add a few more tests, mini-refactor.

This commit is contained in:
NunoSempere 2023-09-08 23:22:57 +02:00
parent c752033121
commit aef8269a68
3 changed files with 16 additions and 5 deletions

View File

@ -31,3 +31,6 @@ format: $(SRC)
test: $(OUTPUT) test: $(OUTPUT)
/bin/echo -e "123\n45 67" | ./$(OUTPUT) /bin/echo -e "123\n45 67" | ./$(OUTPUT)
/bin/echo -n "" | ./wc
./$(OUTPUT) $(SRC)
./$(OUTPUT) nonexistent_file || true

BIN
wc

Binary file not shown.

18
wc.c
View File

@ -2,11 +2,11 @@
#include <unistd.h> // read, isatty #include <unistd.h> // read, isatty
// STDIN_FILENO // STDIN_FILENO
int process_fp(int filenum) int process_fn(int fn)
{ {
char buffer[1]; char c[1];
while (read(filenum, buffer, sizeof(buffer)) > 0) { while (read(fn, c, sizeof(c)) > 0) {
printf("%c", buffer[0]); printf("%c", c[0]);
} }
return 0; return 0;
} }
@ -14,9 +14,17 @@ int process_fp(int filenum)
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
if (!isatty(STDIN_FILENO)) { if (!isatty(STDIN_FILENO)) {
return process_fp(STDIN_FILENO); return process_fn(STDIN_FILENO);
} else if(argc > 1){ } 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"); printf("To do");
} }
return 0; return 0;
} }