summaryrefslogtreecommitdiff
path: root/src/libstddjb/subgetopt.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2014-09-18 18:55:44 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2014-09-18 18:55:44 +0000
commit3534b428629be185e096be99e3bd5fdfe32d5544 (patch)
tree210ef3198ed66bc7f7b7bf6a85e4579f455e5a36 /src/libstddjb/subgetopt.c
downloadskalibs-3534b428629be185e096be99e3bd5fdfe32d5544.tar.xz
initial commit with rc for skalibs-2.0.0.0
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 '?' ;
+}