commit 1e59a135d33a8f3da633852add2551f8f9b2b624
parent 820f54a47ef9f5f01d1abbc196eaa09d64cb09b4
Author: phoebos <ben@bvnf.space>
Date: Tue, 6 Jul 2021 20:25:10 +0100
base64: allow no arguments for stdin
Diffstat:
M | base64.c | | | 44 | ++++++++++++++++++++++++++------------------ |
1 file changed, 26 insertions(+), 18 deletions(-)
diff --git a/base64.c b/base64.c
@@ -30,10 +30,14 @@ static void usage(const char *name) {
fprintf(stderr, "usage: %s [-d] [FILE]\n", name);
}
+char *base64(char *input, unsigned dflg){
+ return input;
+}
+
int main(int argc, char **argv) {
int c;
- unsigned dflg;
+ unsigned dflg = 0;
const char *name = argv[0];
while ((c = getopt(argc, argv, "dh")) != -1) {
switch(c) {
@@ -51,29 +55,33 @@ int main(int argc, char **argv) {
}
}
int fd;
- for (; optind < argc; optind++) {
- if (*argv[optind] == '-') {
- fd = STDIN_FILENO;
- } else {
- if ((fd = open(argv[optind], O_RDONLY)) == -1 ) {
- perror(argv[optind]);
+ if (optind == argc) {
+ base64(STDIN_FILENO, dflg);
+ } 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;
}
- }
- char buf[BUF_SIZE] = {'\0'};
- if (read(fd, buf, BUF_SIZE) == -1 ) {
- perror("read");
- return 1;
- }
- printf("%s\n", buf);
+ printf("%s\n", buf);
- if (close(fd) != 0) {
- perror("close");
- return 1;
+ if (close(fd) != 0) {
+ perror("close");
+ return 1;
+ }
}
+ }
- }
return 0;
}