-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
180 lines (153 loc) · 3.91 KB
/
main.c
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
Author: Ilan Schnell, 2008-2023
License: BSD
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#define VERSION "1.0.3"
/* command line options */
int table, names, full;
/* byte counts */
long long bin[256];
#define USAGE printf("usage: %s [-fnthv] [file] ...\n", argv[0])
void help(void)
{
printf("\n\
Calculates the frequency of bytes of file(s) or stdin.\n\
Options:\n\
-f display full table (including zero counts, implies -t)\n\
-n show names when displaying table (implies -t)\n\
-t show table\n\
-h display this help and exit\n\
-v display version number and exit\n");
}
/* changes the global variables: table, names, full */
void process_options(int argc, char *argv[])
{
int op;
table = names = full = 0;
while ((op = getopt(argc, argv, "nfthv")) != -1)
switch(op) {
case 'h':
USAGE;
help();
exit(0);
case 'f':
full = table = 1;
break;
case 'n':
names = table = 1;
break;
case 't':
table = 1;
break;
case 'v':
printf("filebin " VERSION "\n");
exit(0);
default:
USAGE;
exit(1);
}
}
void process_fp(FILE *fp)
{
int c;
while ((c = fgetc(fp)) != EOF)
bin[c]++;
}
void process_file(char *fname)
{
FILE *fp;
fp = fopen(fname, "rb");
if (fp == NULL) {
printf("Error: Can't open `%s' for reading.\n", fname);
exit(1);
}
process_fp(fp);
fclose(fp);
}
/* write the name of character c into buff, e.g. "A", "<Space>", "<LF>" */
void char_name(char *buff, int c)
{
#define CASE(_c, _name) case _c: strcpy(buff, "<" _name ">"); break
switch (c) {
CASE('\0', "NUL");
CASE('\t', "Tab");
CASE('\n', "LF");
CASE('\v', "VTab");
CASE('\r', "CR");
CASE(' ', "Space");
CASE(127, "Del");
default:
if (c >= 33 && c <= 126) {
buff[0] = (char) c;
buff[1] = '\0';
}
else
strcpy(buff, ".");
}
#undef CASE
}
void show_table(void)
{
char buff[16];
int c;
if (names)
printf("Char Count\n"
"------------------------\n");
for (c = 0; c < 256; c++)
if (full || bin[c]) {
if (names) {
char_name(buff, c);
printf("%4d %8s %10lld\n", c, buff, bin[c]);
}
else
printf("%4d %10lld\n", c, bin[c]);
}
}
void show_sum(void)
{
long long a, b1, b2, n;
int c;
a = 0;
for (c = 32; c <= 126; c++)
a += bin[c];
printf("ASCII (32..126)........... :%12lld\n", a);
printf(" 0 (NUL)................ :%12lld\n", bin[0]);
printf(" 9 (Tab)................ :%12lld\n", bin[9]);
printf(" 10 (LF)................. :%12lld\n", bin[10]);
printf(" 11 (VTab)............... :%12lld\n", bin[11]);
printf(" 13 (CR)................. :%12lld\n", bin[13]);
printf(" 127 (Del) ............... :%12lld\n", bin[127]);
b1 = bin[12];
for (c = 1; c <= 8; c++) b1 += bin[c];
for (c = 14; c <= 31; c++) b1 += bin[c];
printf("Others (1..8,12,14..31) .. :%12lld\n", b1);
b2 = 0;
for (c = 128; c <= 255; c++) b2 += bin[c];
printf("Others (128..255) ........ :%12lld\n", b2);
n = 0;
for (c = 0; c <= 255; c++) n += bin[c];
printf(" -----------\n");
printf("Total .................... :%12lld\n", n);
}
int main(int argc, char *argv[])
{
int c;
process_options(argc, argv);
/* Initialize array */
for (c = 0; c < 256; c++)
bin[c] = 0;
if (argc == optind)
process_fp(stdin);
else
while (optind < argc)
process_file(argv[optind++]);
if (table)
show_table();
else
show_sum();
return 0;
}