Lely core libraries 1.9.2
getopt.c
Go to the documentation of this file.
1
23#include "libc.h"
24#include <lely/libc/unistd.h>
25
26#if !LELY_HAVE_UNISTD_H
27
28#include <assert.h>
29#include <stddef.h>
30#include <stdio.h>
31
32char *optarg;
33int optind = 1;
34int opterr = 1;
35int optopt;
36
41static int optoff;
42
43int
44getopt(int argc, char *const argv[], const char *optstring)
45{
46 assert(argv);
47 assert(optstring);
48
49 optarg = NULL;
50
51 // Check whether any arguments remain.
52 if (optind >= argc || !argv[optind])
53 return -1;
54 // Continue with the next option in the current argument.
55 char *cp = argv[optind] + optoff;
56 // If this is a new argument, check if it is an option argument.
57 if (!optoff) {
58 // An option argument begins with '-'.
59 if (*cp++ != '-' || !*cp)
60 return -1;
61 // A double dash ("--") denotes the end of option arguments.
62 if (*cp == '-' && !cp[1]) {
63 optind++;
64 return -1;
65 }
66 optoff = 1;
67 }
68 int c = *cp++;
69 // Update the index and offset of the next option.
70 if (*cp) {
71 optoff++;
72 } else {
73 optind++;
74 optoff = 0;
75 }
76
77 // Check if the option character occurs in `optstring`.
78 const char *op = optstring;
79 while (*op && (*op == ':' || *op == '?' || *op != c))
80 op++;
81 if (!*op) {
82 optopt = c;
83 if (opterr && *optstring != ':')
84 fprintf(stderr, "%s: illegal option -- %c\n", argv[0],
85 optopt);
86 return '?';
87 }
88
89 // If the option does not take an argument, we are done.
90 if (*++op != ':')
91 return c;
92
93 optind++;
94 optoff = 0;
95 if (*cp) {
96 // If any characters remain in the current argument, they form
97 // the argument for the option...
98 optarg = cp;
99 } else {
100 // ... otherwise, the next argument is used, if it exists.
101 if (optind > argc) {
102 optopt = c;
103 if (opterr && *optstring != ':')
104 fprintf(stderr,
105 "%s: option requires an "
106 "argument -- %c\n",
107 argv[0], optopt);
108 return *optstring == ':' ? ':' : '?';
109 }
110 optarg = argv[optind - 1];
111 }
112 return c;
113}
114
115#endif // !LELY_HAVE_UNISTD_H
int getopt(int argc, char *const argv[], const char *optstring)
Parses options passed as arguments to main().
Definition: getopt.c:44
static int optoff
The offset (in characters) of the next option with respect to the beginning of the current option arg...
Definition: getopt.c:41
This is the internal header file of the C11 and POSIX compatibility library.
This header file is part of the C11 and POSIX compatibility library; it includes <stddef....
This header file is part of the C11 and POSIX compatibility library; it includes <stdio....
This header file is part of the C11 and POSIX compatibility library; it includes <unistd....
int optopt
The last option character to cause an error.
Definition: unistd.h:55
int optind
The index of the next argument to be parsed by getopt().
Definition: unistd.h:45
char * optarg
A pointer to the argument of the current option.
Definition: unistd.h:42
int opterr
A flag indicating whether a diagnostic message should be printed if an unknown option character or mi...
Definition: unistd.h:52