kandr

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

commit bc4022292b3366ef3f4dfbdd1347aa15c8297e83
parent 1e59a135d33a8f3da633852add2551f8f9b2b624
Author: phoebos <ben@bvnf.space>
Date:   Tue,  6 Jul 2021 20:26:21 +0100

base64: only work on one file/stdin

Diffstat:
Mbase64.c | 46++++++++++++++++++++++++----------------------
1 file changed, 24 insertions(+), 22 deletions(-)

diff --git a/base64.c b/base64.c @@ -54,33 +54,35 @@ int main(int argc, char **argv) { break; } } + /* only work on one file */ + if (optind + 1 < argc) { + fprintf(stderr, "too many arguments\n"); + usage(name); + return 2; + } int fd; - if (optind == argc) { - base64(STDIN_FILENO, dflg); + if (optind == argc || *argv[optind] == '-') { + fd = STDIN_FILENO; } else { - for (; optind < argc; optind++) { - if (*argv[optind] == '-') { - fd = STDIN_FILENO; - } else { - if ((fd = open(argv[optind], O_RDONLY)) == -1 ) { - perror(argv[optind]); - return 1; - } - } - char buf[BUF_SIZE] = {'\0'}; - if (read(fd, buf, BUF_SIZE) == -1 ) { - perror("read"); - return 1; - } - printf("%s\n", buf); - - if (close(fd) != 0) { - perror("close"); - return 1; - } + if ((fd = open(argv[optind], O_RDONLY)) == -1 ) { + perror(argv[optind]); + return 1; } } + char buf[BUF_SIZE] = {'\0'}; + if (read(fd, buf, BUF_SIZE) == -1 ) { + perror("read"); + return 1; + } + + if (close(fd) != 0) { + perror("close"); + return 1; + } + char *output = base64(buf, dflg); + printf("%s\n", buf); + printf("%s\n", output); return 0;