summaryrefslogtreecommitdiff
path: root/src/toilet.c
blob: ce42aca163a3f009f697657f317518c9eca87edc (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <assert.h>
#include <ctype.h>
#include <getopt.h>
#include <limits.h>
#include <pwd.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "util/versie.h"
#include "util/error.h"
#include "util/debug.h"
#include "util/map.h"

static void usage(FILE *f) {
  fprintf(f,
      "Gebruik: toilet [-nchV] [BESTAND]...\n"
      "\n"
      "Toon de hoeveelheid regels, woorden en beten for elk BESTAND\n"
      "\n"
      "  -c          Geef het aantal beten weer\n"
      "  -w          Geef het aantal woorden weer\n"
      "  -l          Geef het aantal regels weer\n"
      "  -h          Toon deze hulptekst\n"
      "  -V          Toon versienummer\n");
}

enum MODE {
  M_LINES = 1 << 0,
  M_WORDS = 1 << 1,
  M_BYTES = 1 << 2,
};

// Returns pointer to argument array containing the file names
static char** parse_options(int argc, char **argv, int *modeMap) {
  int opt;
  while ((opt = getopt(argc, argv, "cwlhV")) != -1) {
    switch (opt) {
      case 'c':
        *modeMap |= M_BYTES;
        break;

      case 'w':
        *modeMap |= M_WORDS;
        break;

      case 'l':
        *modeMap |= M_LINES;
        break;

      case 'h':
        usage(stdout);
        exit(0);

      case 'V':
        drukkedoos_print_versie(stdout, "toilet");
        exit(0);

      case '?':
        usage(stderr);
        exit(1);
    }
  }

  return argv + optind;
}

size_t get_count(enum MODE mode, struct map *map) {
  switch (mode) {
    case M_BYTES:
      return map->sb.st_size;

    case M_WORDS: {
      size_t words = 0;

      assert(map->sb.st_size >= 0);
      // (c) Tom Forging
      for (size_t i = 0; i < (size_t)map->sb.st_size;) {
        size_t previ = i;
        while (!isspace(map->addr[i])) i++;
        words += i != previ;
        while (isspace(map->addr[i])) i++;
      }

      return words;
    }

    case M_LINES: {
      size_t lines = 0;
      char *ptr = map->addr;

      while (ptr != map->end) {
        if (*ptr == '\n') lines++;
        ptr++;
      }

      // handle case if file does not have trailing newline
      if (*(ptr - 1) != '\n') {
        lines++;
      }

      return lines;
    }

    default:
      assert(false);
  }
}

int entry_toilet(int argc, char **argv) {
  int modeMap = 0;
  char **args = parse_options(argc, argv, &modeMap);
  if (modeMap == 0) {
    modeMap = INT_MAX;
  }

  while (*args != NULL) {
    bool isdir;
    struct map *map = open_map(*args, &isdir);
    if (map == NULL && !isdir) {
      fprintf(stderr, "toilet: fout bij lezen bestand '%s'\n", *args);
      return 1;
    } else if (isdir) {
      fprintf(stderr, "toilet: %s: is een mapje\n", *args);
      goto next;
    }

    for (enum MODE mode = 1; mode <= M_BYTES; mode <<= 1) {
      if (mode & modeMap) {
        const size_t count = get_count(mode, map);
        printf("%li ", count);
      }
    }
    printf("%s\n", *args);

    close_map(map);

next:
    args++;
  }

  return 0;
}