b64/main.c

66 lines
1.9 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include "input.h"
#include "encode.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, url, hlp;
char *prog = *argv;
FILE *in, *out;
dec = url = hlp = 0;
while (--argc > 0 && (*++argv)[0] == '-')
while ((c = *++argv[0]))
switch (c) {
case 'd':
dec = 1;
break;
case 'u':
url = 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, url);
fwrite(obuf, sizeof(*obuf), n, (argc == 2) ? out : stdout);
}
} else {
while ((n = getocts((argc >= 1) ? in : stdin, obuf, OBUFSIZE))) {
encode(obuf, n, sbuf, url);
fprintf((argc == 2) ? out : stdout, "%s", sbuf);
}
fprintf((argc == 2) ? out : stdout, "\n");
}
if (in)
fclose(in);
if (out)
fclose(out);
}
exit(EXIT_SUCCESS);
}