diff options
author | Lieuwe Rooijakkers <lieuwerooijakkers@gmail.com> | 2024-08-04 14:58:01 +0200 |
---|---|---|
committer | Lieuwe Rooijakkers <lieuwerooijakkers@gmail.com> | 2024-08-04 14:58:01 +0200 |
commit | 98e7281d39f8ce9af07d7bf1461d18982d1984eb (patch) | |
tree | 0711ff1531034dc4306b23c200ccd3ad517692ab | |
parent | 2763d17085af7f8fe4fd5f89154b9934e15707f3 (diff) |
loop_args util function
-rw-r--r-- | src/util/loop_args.c | 24 | ||||
-rw-r--r-- | src/util/loop_args.h | 5 |
2 files changed, 29 insertions, 0 deletions
diff --git a/src/util/loop_args.c b/src/util/loop_args.c new file mode 100644 index 0000000..4609c71 --- /dev/null +++ b/src/util/loop_args.c @@ -0,0 +1,24 @@ +#include <string.h> + +#include "loop_args.h" + +int loop_args(char **args, int (*callback)(char *argv, bool isstdin)) { + if (*args == NULL) { + return callback(*args, true); + } + + while (*args != NULL) { + int res = 0; + if (!strcmp(*args, "-")) { + res = callback(*args, true); + } else { + res = callback(*args, false); + } + + if (res != 0) return res; + + args++; + } + + return 0; +} diff --git a/src/util/loop_args.h b/src/util/loop_args.h new file mode 100644 index 0000000..359ed75 --- /dev/null +++ b/src/util/loop_args.h @@ -0,0 +1,5 @@ +#pragma once + +#include <stdbool.h> + +int loop_args(char **args, int (*callback)(char *arg, bool isstdin)); |