take a filenum instead of a file pointer.

This commit is contained in:
NunoSempere 2023-09-08 23:07:49 +02:00
parent 4a9ab4f9f3
commit c752033121
2 changed files with 3 additions and 4 deletions

BIN
wc

Binary file not shown.

7
wc.c
View File

@ -2,10 +2,10 @@
#include <unistd.h> // read, isatty #include <unistd.h> // read, isatty
// STDIN_FILENO // STDIN_FILENO
int process_fp(FILE* fp) int process_fp(int filenum)
{ {
char buffer[1]; char buffer[1];
while (read(0, buffer, sizeof(buffer)) > 0) { while (read(filenum, buffer, sizeof(buffer)) > 0) {
printf("%c", buffer[0]); printf("%c", buffer[0]);
} }
return 0; return 0;
@ -13,8 +13,7 @@ int process_fp(FILE* fp)
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
// if (!isatty(STDIN_FILENO)) {
if (!isatty(STDIN_FILENO)) { // check if stdin is coming from terminal or pipeline
return process_fp(STDIN_FILENO); return process_fp(STDIN_FILENO);
} else if(argc > 1){ } else if(argc > 1){
printf("To do"); printf("To do");