-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
172 lines (141 loc) · 3.75 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
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <getopt.h>
#include <error.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include "config.h"
struct option long_options[] = {
{"decode", no_argument, NULL, 'd'},
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{0, 0, 0, 0}
};
const char *program_name;
const char *charset = "mpf";
void usage(int status) {
if (status != EXIT_SUCCESS) {
fprintf(stderr, "Try '%s --help' for more information.\n",
program_name);
} else {
printf(
"Usage: %s [OPTION]... [FILE]\n"
"Kenny encode or decode FILE, or standard input, to standard output.\n"
"\n"
"With no FILE, or when FILE is -, read standard input.\n"
"\n",
program_name
);
fputs(
" -d, --decode decode data\n",
stdout
);
}
exit(status);
}
void encode(FILE *in, FILE *out) {
int c;
unsigned short isUpper;
char buff[4];
buff[3] = '\0';
while ((c = fgetc(in)) != EOF && !ferror(in)) {
if (!isalpha(c)) {
fputc(c, out);
continue;
}
isUpper = isupper(c);
c = tolower(c) - 'a';
buff[2] = charset[c % 3];
c /= 3;
buff[1] = charset[c % 3];
c /= 3;
buff[0] = charset[c % 3];
if (isUpper) {
buff[0] = (char) toupper(buff[0]);
}
fputs(buff, out);
}
}
void decode(FILE *in, FILE *out) {
int c, i = 9, ord = 0;
unsigned short isUpper = 0;
char *ptr = NULL;
while ((c = fgetc(in)) != EOF && !ferror(in)) {
if (!isalpha(c)) {
fputc(c, out);
continue;
}
if (i == 9) {
isUpper = isupper(c);
}
c = tolower(c);
ptr = strchr(charset, c);
if (ptr == NULL) {
fputc(isUpper ? toupper(c) : c, out);
continue;
}
ord += i * (ptr - charset);
if (i == 1) {
fputc(ord + (isUpper ? 'A' : 'a'), out);
i = 9;
ord = isUpper = 0;
} else {
i /= 3;
}
}
}
int main(int argc, char **argv) {
int opt;
bool doDecode = false;
const char *inputFile;
FILE *inputHandle;
program_name = argv[0];
while ((opt = getopt_long(argc, argv, "dhv", long_options, NULL)) != -1) {
switch (opt) {
case 'd':
doDecode = true;
break;
case 'h':
usage(EXIT_SUCCESS);
case 'v':
printf(
"kenny %d.%d.%d\n",
VERSION_MAJOR,
VERSION_MINOR,
VERSION_PATCH
);
exit(EXIT_SUCCESS);
default:
usage(EXIT_FAILURE);
}
}
if (argc - optind > 1) {
error(0, 0, "extra operand '%s'", argv[optind]);
usage(EXIT_FAILURE);
}
if (optind < argc)
inputFile = argv[optind];
else
inputFile = "-";
if (strcmp(inputFile, "-") == 0) {
inputHandle = stdin;
} else {
inputHandle = fopen(inputFile, "r");
if (inputHandle == NULL)
error(EXIT_FAILURE, errno, "%s", inputFile);
}
if (doDecode)
decode(inputHandle, stdout);
else
encode(inputHandle, stdout);
if (fclose(inputHandle) == EOF) {
if (strcmp(inputFile, "-") == 0)
error(EXIT_FAILURE, errno, "closing standard input");
else
error(EXIT_FAILURE, errno, "%s", inputFile);
}
return EXIT_SUCCESS;
}