differentiate between file and pipe.

This commit is contained in:
NunoSempere 2023-09-08 23:02:54 +02:00
parent 7108aef1b3
commit 4a9ab4f9f3
3 changed files with 23 additions and 7 deletions

View File

@ -28,3 +28,6 @@ build: $(SRC)
format: $(SRC)
$(FORMATTER) $(SRC)
test: $(OUTPUT)
/bin/echo -e "123\n45 67" | ./$(OUTPUT)

BIN
wc

Binary file not shown.

19
wc.c
View File

@ -1,10 +1,23 @@
#include <unistd.h>
#include <stdio.h>
#include <unistd.h> // read, isatty
int main(){
// STDIN_FILENO
int process_fp(FILE* fp)
{
char buffer[1];
while(read(0,buffer,sizeof(buffer)) > 0){
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;
}