summaryrefslogtreecommitdiff
path: root/src/libstddjb/subgetopt.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/libstddjb/subgetopt.c')
-rw-r--r--src/libstddjb/subgetopt.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/libstddjb/subgetopt.c b/src/libstddjb/subgetopt.c
new file mode 100644
index 0000000..69201c6
--- /dev/null
+++ b/src/libstddjb/subgetopt.c
@@ -0,0 +1,63 @@
+/* ISC license. */
+
+#undef SUBGETOPT_SHORT
+#include <skalibs/sgetopt.h>
+
+int subgetopt_r (int argc, char const *const *argv, char const *opts, subgetopt_t_ref o)
+{
+ o->arg = 0 ;
+ if ((o->ind >= argc) || !argv[o->ind]) return -1 ;
+ if (o->pos && !argv[o->ind][o->pos])
+ {
+ o->ind++ ;
+ o->pos = 0 ;
+ if ((o->ind >= argc) || !argv[o->ind]) return -1 ;
+ }
+
+ if (!o->pos)
+ {
+ if (argv[o->ind][0] != '-') return -1 ;
+ else
+ {
+ char c ;
+ o->pos++ ;
+ c = argv[o->ind][1] ;
+ if (c == '-') o->ind++ ;
+ if (!c || (c == '-'))
+ {
+ o->pos = 0 ;
+ return -1 ;
+ }
+ }
+ }
+ {
+ char c = argv[o->ind][o->pos++] ;
+ char const *s = opts ;
+ char retnoarg = (*s == ':') ? (s++, ':') : '?' ;
+ while (*s)
+ {
+ if (c == *s)
+ {
+ if (s[1] == ':')
+ {
+ o->arg = argv[o->ind++] + o->pos ;
+ o->pos = 0 ;
+ if (!*o->arg)
+ {
+ o->arg = argv[o->ind] ;
+ if ((o->ind >= argc) || !o->arg)
+ {
+ o->problem = c ;
+ return retnoarg ;
+ }
+ o->ind++ ;
+ }
+ }
+ return c ;
+ }
+ if (*++s == ':') s++ ;
+ }
+ o->problem = c ;
+ }
+ return '?' ;
+}