fluffy

Network Archaeology tools for Unix
git clone https://git.woozle.org/neale/fluffy.git

Neale Pickett  ·  2020-12-22

histogram.c

 1#include <getopt.h>
 2#include <stdbool.h>
 3#include <stdio.h>
 4#include <stdlib.h>
 5
 6int main(int argc, char* argv[]) {
 7  int lineno = 0;
 8  int divisor = 1;
 9  int c;
10
11  while ((c = getopt(argc, argv, "d:")) != -1) {
12    switch (c) {
13      case 'd':
14        divisor = (atoi(optarg));
15        if (divisor > 0) {
16          break;
17        }
18        // fallthrough
19      default:
20        fprintf(stderr, "Usage: %s [-s] [-d DIVISOR]\n", argv[0]);
21        fprintf(stderr, "\n");
22        fprintf(stderr, "-d DIVISOR   Divide bar width by DIVISOR\n");
23        return 1;
24    }
25  }
26
27  for (;;) {
28    char line[128];
29    int count;
30    int ret;
31
32    ++lineno;
33    ret = scanf("%d %127[^\n]\n", &count, line);
34    if (EOF == ret) {
35      break;
36    } else if (ret < 2) {
37      fprintf(stderr, "Unparseable input on line %d\n", lineno);
38      scanf("%*[^\n]\n");  // Read in and discard one line
39      continue;
40    }
41    printf("%s ", line);
42    for (int i = 0; i < count / divisor; ++i) {
43      putchar('#');
44    }
45    printf(" %d\n", count);
46  }
47  return 0;
48}