diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2020-04-02 20:03:32 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2020-04-02 23:01:48 +0200 |
commit | d76eb9dd46081279642d3353f51ebc09fe4bbe9f (patch) | |
tree | 1841a5e9016b760a6e0c6592dc85b0e4879d11c0 /lib/compat/getopt.c | |
parent | a772248e1a2f0886d8479b2f14392f020ea8c6e0 (diff) |
Add a libcompat implementation of getopt
Limited to the subset actually needed. Vaguely inspired by the NetBSD
libc implementation.
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/compat/getopt.c')
-rw-r--r-- | lib/compat/getopt.c | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/lib/compat/getopt.c b/lib/compat/getopt.c new file mode 100644 index 0000000..23ede4c --- /dev/null +++ b/lib/compat/getopt.c @@ -0,0 +1,70 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * getopt.c + * + * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at> + */ +#include "config.h" +#include "compat.h" + +#include <stdio.h> +#include <string.h> +#include <assert.h> + +#ifndef HAVE_GETOPT +static char *current = ""; +int optind = 1; +char *optarg = NULL; + +int getopt(int argc, char *const argv[], const char *optstr) +{ + char optchr, *ptr; + + if (*current == '\0') { + if (optind >= argc || argv[optind][0] != '-') + return -1; + + if (argv[optind][1] == '-' && argv[optind][2] == '\0') { + ++optind; + return -1; + } + + if (argv[optind][1] == '\0') + return -1; + + current = argv[optind] + 1; + } + + optchr = *(current++); + if (optchr == ':' || (ptr = strchr(optstr, optchr)) == NULL) + goto fail_unknown; + + if (ptr[1] == ':') { + if (*current != '\0') { + optarg = current; + } else { + if (++optind >= argc) + goto fail_arg; + + optarg = argv[optind]; + } + + current = ""; + ++optind; + } else { + optarg = NULL; + + if (*current == '\0') + ++optind; + } + + return optchr; +fail_unknown: + fprintf(stderr, "%s: unknown option `-%c`\n", argv[0], optchr); + return '?'; +fail_arg: + fprintf(stderr, "%s: missing argument for option `-%c`\n", + argv[0], optchr); + return '?'; +} +#endif |