b64/main.c
Eric 196c54cffa change newline termination behaviour when encoding
Before, encoded text would be newline terminated by default.
This was supposed to make short encoded strings easier to read when
using an interactive shell.
Now this only happens with the explicit line wrap option.
2025-05-27 02:09:21 +02:00

77 lines
2.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include "input.h"
#include "output.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, 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, 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))) {
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);
}