From 0278dc6c7c605e7ef87e0d1127f7bc67cee4f66a Mon Sep 17 00:00:00 2001 From: phoebos Date: Sun, 11 Jul 2021 21:14:23 +0100 Subject: base64: read all of file in a loop --- base64.c | 15 +++++++++------ 1 file 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; } -- cgit 1.4.1-2-gfad0