about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorphoebos <ben@bvnf.space>2021-07-11 21:14:23 +0100
committerphoebos <ben@bvnf.space>2021-07-11 21:14:23 +0100
commit0278dc6c7c605e7ef87e0d1127f7bc67cee4f66a (patch)
tree0467e45f454f97f368f8e5174e0474bd82bcea65
parent4948fa51e1f3d1f38cb2b7b83911692ca0bd4d43 (diff)
downloadkandr-0278dc6c7c605e7ef87e0d1127f7bc67cee4f66a.tar.gz
base64: read all of file in a loop
-rw-r--r--base64.c15
1 files changed, 9 insertions, 6 deletions
diff --git a/base64.c b/base64.c
index 1a73eb6..d63c301 100644
--- 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;
 
 }