about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorphoebos <ben@bvnf.space>2021-07-10 20:48:00 +0100
committerphoebos <ben@bvnf.space>2021-07-10 20:48:00 +0100
commita34ba1fe4e9cb27ca7df689e477576181e0009d0 (patch)
tree7e57c64f9304a0bb4973460fbff27d6e95b7c483
parent99b3a546bd421b5dc13ee12e3b5ba5fccdee743b (diff)
downloadkandr-a34ba1fe4e9cb27ca7df689e477576181e0009d0.tar.gz
base64: tweaks
-rw-r--r--base64.c15
1 files changed, 8 insertions, 7 deletions
diff --git a/base64.c b/base64.c
index d59be24..e649728 100644
--- a/base64.c
+++ b/base64.c
@@ -35,7 +35,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 
-#define BUF_SIZE 1000
+#define BUF_SIZE 999
 
 const char tbl_base64[] = {
         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
@@ -53,12 +53,11 @@ static void usage(const char *name) {
     fprintf(stderr, "usage: %s [-d] [FILE]\n", name);
 }
 
-char *base64(char *input, unsigned dflg){
-    return input;
+char *base64(char *i, ssize_t length, unsigned dflg){
+    return i;
 }
 
 int main(int argc, char **argv) {
-
     int c;
     unsigned dflg = 0;
     const char *name = argv[0];
@@ -85,15 +84,18 @@ int main(int argc, char **argv) {
     }
     int fd;
     if (optind == argc || *argv[optind] == '-') {
+        /* read from stdin */
         fd = STDIN_FILENO;
     } else {
+        /* open the named file */
         if ((fd = open(argv[optind], O_RDONLY)) == -1 ) {
             perror(argv[optind]);
             return 1;
         }
     }
+    ssize_t bytes;
     char buf[BUF_SIZE] = {'\0'};
-    if (read(fd, buf, BUF_SIZE) == -1 ) {
+    if ((bytes = read(fd, buf, BUF_SIZE)) <= 0 ) {
         perror("read");
         return 1;
     }
@@ -103,8 +105,7 @@ int main(int argc, char **argv) {
         return 1;
     }
 
-    char *output = base64(buf, dflg);
-    printf("%s\n", buf);
+    char *output = base64(buf, bytes, dflg);
     printf("%s\n", output);
 
     return 0;