summaryrefslogtreecommitdiff
path: root/wc.c
blob: 00b26ae5cf80824dedf9401b66a2a593d1f81473 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
#include <wctype.h>

int main(int argc, const char *argv[])
{
  int character;
  int lines = 0, words = 0, bytes = 0, got_space = 0;
  const char *filename = argv[1];
  FILE *file = fopen(filename, "r");

  if (file) {
    while ( (character = getc(file)) != EOF ) {
      if (character == '\n') {
        ++lines;
      }
      if (iswspace(character) != 0) {
        if (got_space == 0) {
          ++words;
        }
        got_space = 1;
      }
      else {
        got_space = 0;
      }

      ++bytes;
    }
    printf(" %7d %7d %7d %s\n", lines, words, bytes, filename);

    fclose(file);
    return 0;
  }

  return 1;
}