summaryrefslogtreecommitdiff
path: root/userland/minibox/utilities/wc.c
blob: 27a214bb722986440efe2e9cbb2e250ecbb4893c (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "include.h"
#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int cmode, wmode, lmode;

void output(const char *str, size_t lines, size_t words, size_t chars) {
  if (lmode) {
    printf("%d", lines);
  }

  if (wmode) {
    if (lmode) {
      putchar(' ');
    }

    printf("%d", words);
  }

  if (cmode) {
    if (lmode) {
      putchar(' ');
    }

    printf("%d", chars);
  }

  if (str) {
    printf(" %s", str);
  }

  putchar('\n');
}

void wc(int argc, char **argv) {
  int fd = fd_stdin;
  size_t total_chars, total_words, total_lines;
  total_chars = total_words = total_lines = 0;

  if (NULL == *argv) {
    goto read_stdin;
  }

  for (; *argv; argv++) {
    int new_word = 1;
    size_t chars = 0, words = chars, lines = chars;

    fd = open(*argv, O_RDONLY, 0);
  read_stdin:
    for (char c; read(fd, &c, 1);) {
      if (cmode) {
        chars++;
      }

      if (wmode) {
        if (isspace(c)) {
          new_word = 1;
        } else if (new_word) {
          words++;
          new_word = 0;
        }
      }

      if (lmode) {
        if ('\n' == c) {
          lines++;
        }
      }
    }
    close(fd);
    if (lmode) {
      total_lines += lines;
    }
    if (wmode) {
      total_words += words;
    }
    if (cmode) {
      total_chars += chars;
    }

    output(*argv, lines, words, chars);
    if (NULL == *argv) {
      return; // We read from stdin
    }
  }

  if (argc > 1) {
    output("total", total_lines, total_words, total_chars);
  }
}

int wc_main(int argc, char **argv) {
  cmode = wmode = lmode = 0;
  for (argc--; *++argv && '-' == **argv; argc--) {
    switch (*(*argv + 1)) {
    case 'l':
      lmode = 1;
      break;
    case 'w':
      wmode = 1;
      break;
    case 'c':
      cmode = 1;
      break;
    }
  }

  if (!(lmode || cmode || wmode)) {
    cmode = wmode = lmode = 1;
  }

  wc(argc, argv);
  return 0;
}