aboutsummaryrefslogtreecommitdiff
path: root/src/args.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/args.c')
-rw-r--r--src/args.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/args.c b/src/args.c
new file mode 100644
index 0000000..6a516da
--- /dev/null
+++ b/src/args.c
@@ -0,0 +1,34 @@
+#include "args.h"
+int args_optind = 1;
+
+int
+args_getopt(int argc, char *argv[], const char *optstring, char **args_optarg)
+{
+ char option;
+ if (args_optind >= argc) return -1;
+
+ if (argv[args_optind][0] != '-'
+ || strlen(argv[args_optind]) > 2
+ || !strcmp(argv[args_optind], "-")) {
+
+ *args_optarg = argv[args_optind];
+ args_optind++;
+ return 0;
+ }
+
+ int i;
+ option = argv[args_optind][1];
+ for (i = 0; i < strlen(optstring); i++) {
+ if (optstring[i + 1] == ':' && optstring[i] == option) {
+ *args_optarg = argv[++args_optind];
+ args_optind++;
+ return option;
+ } else if (optstring[i + 1] != ':' && optstring[i] == option) {
+ args_optind++;
+ return option;
+ }
+ }
+ args_optarg = 0;
+ args_optind++;
+ return -2;
+}
Feel free to download, copy and edit any repo