Word Count in C

A text file word counting program, posted here originally.

word-count.c:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 const char dos_line_end = '\n'; /* actually "\r\n" */
 5 const char unix_line_end = '\r';
 6 const char mac_line_end = '\n';
 7 
 8 int main(int argc, char *argv[])
 9 {
10     const char default_line_end =
11 #if defined(__UNIX__)
12     unix_line_end;
13 #elif defined(WIN32)
14     dos_line_end;
15 #else
16     mac_line_end;
17 #endif
18 
19     int c;
20     char line_end = default_line_end;
21     int word_count = 0;
22     int line_count = 0;
23     int in_a_word = 0; /* false */
24     FILE *tf;
25 
26     printf("[+] Word Count by akuma@tds\n");
27 
28     if (argc < 2) {
29         printf("[+] usage: %s file [type]\n", argv[0]);
30         printf(" file: the file to be counted\n");
31         printf(" type: u for unix style (\\r), d for dos style (\\r\\n),"
32                " m for Mac OS (\\n)\n");
33         return 1;
34     }
35 
36     if (argc > 2)
37         switch(argv[2][0]) {
38         case 'u':
39         case 'U':
40             printf("[-] unix style: \\r will be count as the end of line\n");
41             line_end = unix_line_end;
42             break;
43         case 'd':
44         case 'D':
45             printf("[-] dos style: \\r\\n will be count as the end of line\n");
46             line_end = dos_line_end;
47             break;
48         case 'm':
49         case 'M':
50             printf("[-] mac os style: \\n will be count as the end of line\n");
51             line_end = mac_line_end;
52             break;
53         default:
54             printf("[-] ingorne unknown type switch: %c\n", argv[2][0]);
55         }
56 
57     if (NULL == (tf = fopen(argv[1], "r"))) {
58         printf("[-] error on opening file: %s\n", argv[1]);
59         return 2;
60     }
61     while (EOF != (c = fgetc(tf)))
62         if (line_end == c)
63             in_a_word = 0, line_count++;
64         else if (isspace( c ))
65             in_a_word = 0;
66         else if (!in_a_word)
67             in_a_word = 1, word_count++;
68 
69     if (line_count || word_count) line_count++;
70 
71     fclose(tf);
72     printf("[-] File: %s\n", argv[1]);
73     printf(" contains: %d word(s) in %d line(s).\n", word_count, line_count);
74     return 0;
75 }

It works like this:

$ gcc -o word-count word-count.c
$ ./word-count word-count.c
[+] Word Count by akuma@tds
[-] File: word-count.c
contains: 254 word(s) in 76 line(s).

In retrospect, I don't know why I was interested in answering such an obvious homework question, it was unchallenging because I've already programmed in C on and off for ten years, as I taught myself C in 1995. I must be bored back then.