diff options
-rw-r--r-- | base64.c | 46 |
1 files changed, 24 insertions, 22 deletions
diff --git a/base64.c b/base64.c index 87843d6..ce4f4c7 100644 --- 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; |