summaryrefslogtreecommitdiff
path: root/src/unistd/getopt.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/unistd/getopt.c')
-rw-r--r--src/unistd/getopt.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/unistd/getopt.c b/src/unistd/getopt.c
new file mode 100644
index 00000000..29fe5590
--- /dev/null
+++ b/src/unistd/getopt.c
@@ -0,0 +1,56 @@
+#include "string.h"
+#include "stdio.h"
+#include <unistd.h>
+
+int getopt(int argc, char * const argv[], const char *optstring)
+{
+ static int optchar = 0;
+ char *cursor = NULL;
+
+ if (optind = 0 || argv[optind][optchar] == '\0') {
+ optind++;
+ optchar = 0;
+ }
+
+ if (!strcmp(argv[optind], "--")) {
+ optind++;
+ return -1;
+ }
+
+ if (optchar == 0 && argv[optind][optchar] != '-') {
+ return -1;
+ }
+
+ 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++; */
+ }
+ return *cursor;
+ }
+
+ optopt = argv[optind][optchar];
+ if (opterr) {
+ fprintf(stderr, "%s: Invalid option -%c\n", *cursor);
+ }
+ return '?';
+}
+
+/*
+POSIX(2)
+*/
+