File: countall.c

package info (click to toggle)
enca 1.21-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,948 kB
  • sloc: ansic: 10,297; sh: 5,858; xml: 2,132; makefile: 700; perl: 261
file content (57 lines) | stat: -rw-r--r-- 1,148 bytes parent folder | download
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
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>

#define isbinary(x) (iscntrl(x) || (x) == 127)

#define FILL_CHARACTER '.'

void print_usage(const char *progname) {
    printf("Usage: %s < [SAMPLE_TEXT]\n", progname);
    printf("Options:\n");
    printf("  -h, --help            Show this help message\n");
}

int main(int argc, char *argv[])
{
  unsigned long int count[0x100];
  int i, c;
  int opt;
  int option_index = 0;
  static struct option long_options[] =
    {
    /* These options set a flag. */
    {"help",     no_argument,       0, 'h'}
    };

  opt = getopt_long (argc, argv, "h:",
                long_options, &option_index);

  switch (opt) {
      case -1:
          break;
      case 'h':
          print_usage(argv[0]);
          return 0;
      default:
          print_usage(argv[0]);
          return 1;
  }

  for (i = 0; i < 0x100; i++)
    count[i]=0;

  while ((c = getchar()) != EOF)
    count[c]++;

  for (i = 0; i < 0x100; i++) {
    if (count[i]) {
      printf("0x%02x ", i);
      printf("%c %lu\n", isbinary(i) ? '.' : i, count[i]);
    }
  }

  return 0;
}