commit 0278dc6c7c605e7ef87e0d1127f7bc67cee4f66a
parent 4948fa51e1f3d1f38cb2b7b83911692ca0bd4d43
Author: phoebos <ben@bvnf.space>
Date:   Sun, 11 Jul 2021 21:14:23 +0100
base64: read all of file in a loop
Diffstat:
1 file changed, 9 insertions(+), 6 deletions(-)
diff --git a/base64.c b/base64.c
@@ -121,9 +121,15 @@ int main(int argc, char **argv) {
     }
     ssize_t bytes;
     char buf[BUF_SIZE] = {'\0'};
-    if ((bytes = read(fd, buf, BUF_SIZE)) <= 0 ) {
-        perror("read");
-        return 1;
+    while (1){
+        if ((bytes = read(fd, buf, BUF_SIZE)) <= 0 ) {
+            if (bytes == 0) break; /* read returns 0 when EOF has been reached */
+            perror("read");
+            return 1;
+        }
+
+        char *output = base64(buf, bytes, dflg);
+        printf("%s\n", output);
     }
 
     if (close(fd) != 0) {
@@ -131,9 +137,6 @@ int main(int argc, char **argv) {
         return 1;
     }
 
-    char *output = base64(buf, bytes, dflg);
-    printf("%s\n", output);
-
     return 0;
 
 }