kandr

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit a34ba1fe4e9cb27ca7df689e477576181e0009d0
parent 99b3a546bd421b5dc13ee12e3b5ba5fccdee743b
Author: phoebos <ben@bvnf.space>
Date:   Sat, 10 Jul 2021 20:48:00 +0100

base64: tweaks

Diffstat:
Mbase64.c | 15++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)

diff --git 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;