62 lines
1.7 KiB
C
62 lines
1.7 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "input.h"
|
|
#include "trans.h"
|
|
|
|
#define OBUFSIZE 4
|
|
#define SBUFSIZE 5
|
|
|
|
unsigned char obuf[OBUFSIZE];
|
|
unsigned char sbuf[SBUFSIZE];
|
|
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
int c, n, dec, hlp;
|
|
char *prog = *argv;
|
|
FILE *in, *out;
|
|
|
|
dec = hlp = 0;
|
|
while (--argc > 0 && (*++argv)[0] == '-')
|
|
while ((c = *++argv[0]))
|
|
switch (c) {
|
|
case 'd':
|
|
dec = 1;
|
|
break;
|
|
case 'h':
|
|
hlp = 1;
|
|
break;
|
|
default:
|
|
fprintf(stderr, "%s: illegal option %c\n", prog, c);
|
|
exit(EXIT_FAILURE);
|
|
break;
|
|
}
|
|
if (hlp) {
|
|
fprintf(stdout, "Usage: %s -d -h infile outfile\n", prog);
|
|
} else {
|
|
if (argc >= 1 && (in = fopen(*argv, "r")) == NULL) {
|
|
fprintf(stderr, "%s: can't open %s\n", prog, *argv);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (argc == 2 && (out = fopen(*(argv+1), "w")) == NULL) {
|
|
fprintf(stderr, "%s: can't open %s\n", prog, *(argv+1));
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (dec) {
|
|
while ((n = getsxts((argc >= 1) ? in : stdin, sbuf, SBUFSIZE))) {
|
|
n = decode(sbuf, n, obuf);
|
|
fwrite(obuf, sizeof(*obuf), n, (argc == 2) ? out : stdout);
|
|
}
|
|
} else {
|
|
while ((n = getocts((argc >= 1) ? in : stdin, obuf, OBUFSIZE))) {
|
|
encode(obuf, n, sbuf);
|
|
fprintf((argc == 2) ? out : stdout, "%s", sbuf);
|
|
}
|
|
}
|
|
if (in)
|
|
fclose(in);
|
|
if (out)
|
|
fclose(out);
|
|
}
|
|
exit(EXIT_SUCCESS);
|
|
}
|