add linewrap option when encoding (76 columns)

This commit is contained in:
Eric 2025-05-27 01:32:09 +02:00
parent 6c7d699f1a
commit 0dba01d98e
4 changed files with 46 additions and 6 deletions

21
main.c
View file

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include "input.h"
#include "output.h"
#include "encode.h"
#define OBUFSIZE 4
@ -12,11 +13,12 @@ unsigned char sbuf[SBUFSIZE];
int
main(int argc, char *argv[])
{
int c, n, dec, url, hlp;
int c, n, l;
int dec, url, hlp, wrp;
char *prog = *argv;
FILE *in, *out;
dec = url = hlp = 0;
dec = url = hlp = wrp = 0;
while (--argc > 0 && (*++argv)[0] == '-')
while ((c = *++argv[0]))
switch (c) {
@ -29,6 +31,9 @@ main(int argc, char *argv[])
case 'h':
hlp = 1;
break;
case 'w':
wrp = 1;
break;
default:
fprintf(stderr, "%s: illegal option %c\n", prog, c);
exit(EXIT_FAILURE);
@ -52,10 +57,16 @@ main(int argc, char *argv[])
}
} else {
while ((n = getocts((argc >= 1) ? in : stdin, obuf, OBUFSIZE))) {
encode(obuf, n, sbuf, url);
fprintf((argc == 2) ? out : stdout, "%s", sbuf);
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);
}
}
fprintf((argc == 2) ? out : stdout, "\n");
if (!wrp || l != '\n')
fprintf((argc == 2) ? out : stdout, "\n");
}
if (in)
fclose(in);