wc

Count words in <50 lines of C
Log | Files | Refs | README

commit 4a9ab4f9f31ff84bfcff7f70149264d82e2b2fbf
parent 7108aef1b31576bbeed414ca64a2ef6ad49e509c
Author: NunoSempere <nuno.sempere@protonmail.com>
Date:   Fri,  8 Sep 2023 23:02:54 +0200

differentiate between file and pipe.

Diffstat:
Mmakefile | 3+++
Mwc | 0
Mwc.c | 27++++++++++++++++++++-------
3 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/makefile b/makefile @@ -28,3 +28,6 @@ build: $(SRC) format: $(SRC) $(FORMATTER) $(SRC) + +test: $(OUTPUT) + /bin/echo -e "123\n45 67" | ./$(OUTPUT) diff --git a/wc b/wc Binary files differ. diff --git a/wc.c b/wc.c @@ -1,10 +1,23 @@ -#include <unistd.h> #include <stdio.h> +#include <unistd.h> // read, isatty -int main(){ - char buffer[1]; - while(read(0,buffer,sizeof(buffer)) > 0){ - printf("%c", buffer[0]); - } - return 0; +// STDIN_FILENO +int process_fp(FILE* fp) +{ + char buffer[1]; + while (read(0, buffer, sizeof(buffer)) > 0) { + printf("%c", buffer[0]); + } + return 0; +} + +int main(int argc, char** argv) +{ + // + if (!isatty(STDIN_FILENO)) { // check if stdin is coming from terminal or pipeline + return process_fp(STDIN_FILENO); + } else if(argc > 1){ + printf("To do"); + } + return 0; }