2023-09-08 20:42:06 +00:00
|
|
|
#include <stdio.h>
|
2023-09-08 21:02:54 +00:00
|
|
|
#include <unistd.h> // read, isatty
|
2023-09-08 20:42:06 +00:00
|
|
|
|
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];
|
2023-09-08 21:38:51 +00:00
|
|
|
int seen_word=0;
|
2023-09-08 22:02:31 +00:00
|
|
|
int seen_sep_after_word=0;
|
2023-09-08 21:38:51 +00:00
|
|
|
int num_words = 0;
|
2023-09-08 21:22:57 +00:00
|
|
|
while (read(fn, c, sizeof(c)) > 0) {
|
2023-09-08 22:02:31 +00:00
|
|
|
if(*c != '\n' && *c != ' ' && *c != '\t'){
|
2023-09-08 21:38:51 +00:00
|
|
|
seen_word = 1;
|
2023-09-08 22:02:31 +00:00
|
|
|
} else if(seen_word) {
|
|
|
|
seen_sep_after_word = 1;
|
|
|
|
} else {
|
|
|
|
// see a separator, but haven't seen a word: do nothing
|
|
|
|
// exercise: what happens if you only track seen_sep,
|
|
|
|
// instead of seen_sep_after_word?
|
|
|
|
// test with: $ echo " x x" | ./wc
|
2023-09-08 21:38:51 +00:00
|
|
|
}
|
2023-09-08 22:02:31 +00:00
|
|
|
if(seen_word && seen_sep_after_word){
|
2023-09-08 21:38:51 +00:00
|
|
|
num_words++;
|
2023-09-08 22:02:31 +00:00
|
|
|
seen_sep_after_word = seen_word = 0;
|
2023-09-08 21:38:51 +00:00
|
|
|
}
|
2023-09-08 21:02:54 +00:00
|
|
|
}
|
2023-09-08 21:38:51 +00:00
|
|
|
if(seen_word){
|
|
|
|
num_words++;
|
|
|
|
}
|
|
|
|
printf("%i\n",num_words);
|
2023-09-08 21:23:18 +00:00
|
|
|
return 0;
|
2023-09-08 21:02:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2023-09-08 21:07:49 +00:00
|
|
|
if (!isatty(STDIN_FILENO)) {
|
2023-09-08 21:23:18 +00:00
|
|
|
return process_fn(STDIN_FILENO);
|
|
|
|
} 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 {
|
2023-09-08 22:02:31 +00:00
|
|
|
printf("Usage: ww file.txt\n");
|
|
|
|
printf(" or: cat file.txt | ww\n");
|
2023-09-08 21:23:18 +00:00
|
|
|
}
|
2023-09-08 21:02:54 +00:00
|
|
|
return 0;
|
2023-09-08 20:42:06 +00:00
|
|
|
}
|