b64/main.c
Eric fa5095133a remove buffer size dependency from input functions
This should've been part of 848c37, when buffers were unwired from input
functions. Now buffer size is also no longer supplied in function calls
but assumed. It is up to the caller to provide appropriately sized
buffers.
2025-05-27 18:37:48 +02:00

77 lines
2.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include "input.h"
#include "output.h"
#include "encode.h"
#define OCTETBUF 4
#define SXTETBUF 5
unsigned char obuf[OCTETBUF];
unsigned char sbuf[SXTETBUF];
int
main(int argc, char *argv[])
{
int c, n, l;
int dec, url, hlp, wrp;
char *prog = *argv;
FILE *in, *out;
dec = url = hlp = wrp = 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;
case 'w':
wrp = 1;
break;
default:
fprintf(stderr, "%s: illegal option %c\n", prog, c);
exit(EXIT_FAILURE);
break;
}
if (hlp) {
fprintf(stdout, "Usage: %s -duhw 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))) {
n = decode(sbuf, n, obuf, url);
fwrite(obuf, sizeof(*obuf), n, (argc == 2) ? out : stdout);
}
} else {
while ((n = getocts((argc >= 1) ? in : stdin, obuf))) {
if (wrp) {
n = encode(obuf, n, sbuf, url);
l = printw((argc == 2) ? out : stdout, sbuf, n);
} else {
encode(obuf, n, sbuf, url);
fprintf((argc == 2) ? out : stdout, "%s", sbuf);
}
}
if (wrp && l != '\n')
fprintf((argc == 2) ? out : stdout, "\n");
}
if (in)
fclose(in);
if (out)
fclose(out);
}
exit(EXIT_SUCCESS);
}