diff options
author | Jakob Kaivo <jkk@ung.org> | 2020-02-26 17:04:44 -0500 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2020-02-26 17:04:44 -0500 |
commit | e9813f9ebdfe80b2577562262843acbadfe4139a (patch) | |
tree | e1ccf38c6525399135ce6816a1609149788e54b3 /src | |
parent | 1321fdca3ab3cf603e765a4cd29364c87a5044af (diff) |
properly implement getopt()
Diffstat (limited to 'src')
-rw-r--r-- | src/unistd/getopt.c | 80 |
1 files changed, 53 insertions, 27 deletions
diff --git a/src/unistd/getopt.c b/src/unistd/getopt.c index a2dbea7b..0af466aa 100644 --- a/src/unistd/getopt.c +++ b/src/unistd/getopt.c @@ -3,18 +3,31 @@ #include "stdio.h" #include <unistd.h> -int getopt(int argc, char * const argv[], const char *optstring) +int ung_getopt(int argc, char * const argv[], const char *optstring) { static int optchar = 0; - char *cursor = NULL; + char *option = NULL; - if (optind == 0 || argv[optind][optchar] == '\0') { - optind++; + if (optind == 0) { + optind = 1; optchar = 0; } + if (argv[optind] == NULL) { + return -1; + } + + if (argv[optind][0] != '-') { + return -1; + } + + if (!strcmp(argv[optind], "-")) { + return -1; + } + if (!strcmp(argv[optind], "--")) { optind++; + optchar = 0; return -1; } @@ -24,34 +37,47 @@ int getopt(int argc, char * const argv[], const char *optstring) optchar++; - printf("Checking %c\n", argv[optind][optchar]); - - cursor = strchr(optstring, argv[optind][optchar]); - if (cursor) { - if (cursor[1] == ':') { - /* An option-argument is required */ - /* if (no arg) { */ - /* optopt = *cursor; */ - /* if (opterr) { */ - /* fprintf(stderr, "%s: Missing argument to -%c\n", argv[0], *cursor); */ - /* } */ - /* return optstring[0] == ':' ? ':' : '?'; */ - /* } */ - /* optarg = argv[++optind]; */ - /* optchar = 0; */ - /* optind++; */ + if (argv[optind][optchar] == '\0') { + optind++; + optchar = 0; + return ung_getopt(argc, argv, optstring); + } + + option = strchr(optstring, argv[optind][optchar]); + + if (!option) { + optopt = argv[optind][optchar]; + if (opterr != 0 && optstring[0] != ':') { + fprintf(stderr, "%s: invalid option -%c\n", argv[0], + optopt); } - return *cursor; + return '?'; + } + + if (option[1] != ':') { + return *option; } - - optopt = argv[optind][optchar]; - if (opterr) { - fprintf(stderr, "%s: Invalid option -%c\n", *cursor); + + if (argv[optind][optchar + 1] == '\0') { + optarg = argv[++optind]; + } else { + optarg = argv[optind] + optchar + 1; + } + + optind++; + if (optarg == NULL) { + optopt = *option; + if (opterr != 0 && optstring[0] != ':') { + fprintf(stderr, "%s: missing argument to option -%c\n", + argv[0], optopt); + } + return optstring[0] == ':' ? ':' : '?'; } - return '?'; + + optchar = 0; + return *option; } /* POSIX(2) */ - |