wc/wc.c

31 lines
519 B
C
Raw Normal View History

#include <stdio.h>
2023-09-08 21:02:54 +00:00
#include <unistd.h> // read, isatty
2023-09-08 21:02:54 +00:00
// STDIN_FILENO
2023-09-08 21:22:57 +00:00
int process_fn(int fn)
2023-09-08 21:02:54 +00:00
{
2023-09-08 21:22:57 +00:00
char c[1];
while (read(fn, c, sizeof(c)) > 0) {
printf("%c", c[0]);
2023-09-08 21:02:54 +00:00
}
return 0;
}
int main(int argc, char** argv)
{
if (!isatty(STDIN_FILENO)) {
2023-09-08 21:22:57 +00:00
return process_fn(STDIN_FILENO);
2023-09-08 21:02:54 +00:00
} else if(argc > 1){
2023-09-08 21:22:57 +00:00
FILE* fp = fopen(argv[1], "r");
if(!fp){
perror("Could not open file");
return 1;
}
return process_fn(fileno(fp));
} else{
2023-09-08 21:02:54 +00:00
printf("To do");
2023-09-08 21:22:57 +00:00
2023-09-08 21:02:54 +00:00
}
return 0;
}