From 0dba01d98e239aa3dab29bd7da2c7da1c5de23ed Mon Sep 17 00:00:00 2001
From: Eric <eric@wolf42.net>
Date: Tue, 27 May 2025 01:32:09 +0200
Subject: [PATCH] add linewrap option when encoding (76 columns)

---
 Makefile |  3 ++-
 main.c   | 21 ++++++++++++++++-----
 output.c | 19 +++++++++++++++++++
 output.h |  9 +++++++++
 4 files changed, 46 insertions(+), 6 deletions(-)
 create mode 100644 output.c
 create mode 100644 output.h

diff --git a/Makefile b/Makefile
index d71ad1c..ff2a3f4 100644
--- a/Makefile
+++ b/Makefile
@@ -1,11 +1,12 @@
 CFLAGS = -g -Wall -Wextra -Werror
 
-objects = main.o input.o encode.o
+objects = main.o input.o output.o encode.o
 
 b64 : $(objects)
 	cc -o b64 $(objects)
 
 main.o input.o : input.h
+main.o output.o : output.h
 main.o encode.o : encode.h
 
 .PHONY : clean
diff --git a/main.c b/main.c
index 9cb544c..ca96728 100644
--- a/main.c
+++ b/main.c
@@ -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);
diff --git a/output.c b/output.c
new file mode 100644
index 0000000..d5c1a3a
--- /dev/null
+++ b/output.c
@@ -0,0 +1,19 @@
+#include "output.h"
+
+#define WRAPCOL 76
+
+char
+printw(FILE *fp, unsigned char *s, int slen)
+{
+    static int col;
+    int c;
+
+    while (slen--) {
+        fputc((c = *s++), fp);
+        ++col;
+        if (!(col %= WRAPCOL))
+            fputc((c = '\n'), fp);
+    }
+
+    return c;
+}
diff --git a/output.h b/output.h
new file mode 100644
index 0000000..8135e20
--- /dev/null
+++ b/output.h
@@ -0,0 +1,9 @@
+#ifndef HEADER_OUTPUT
+#define HEADER_OUTPUT
+
+#include <stdio.h>
+
+char
+printw(FILE *fp, unsigned char *s, int slen);
+
+#endif