summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorLieuwe Rooijakkers <lieuwerooijakkers@gmail.com>2024-08-04 14:58:01 +0200
committerLieuwe Rooijakkers <lieuwerooijakkers@gmail.com>2024-08-04 14:58:01 +0200
commit98e7281d39f8ce9af07d7bf1461d18982d1984eb (patch)
tree0711ff1531034dc4306b23c200ccd3ad517692ab /src/util
parent2763d17085af7f8fe4fd5f89154b9934e15707f3 (diff)
loop_args util function
Diffstat (limited to 'src/util')
-rw-r--r--src/util/loop_args.c24
-rw-r--r--src/util/loop_args.h5
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));