4 * Simple POSIX getopt(), no GNU extensions...
14 static const char *__optptr
;
16 int getopt(int argc
, char * const *argv
, const char *optstring
)
18 const char *carg
= argv
[optind
];
22 /* We don't actually need argc */
25 /* First, eliminate all non-option cases */
27 if ( !carg
|| carg
[0] != '-' || !carg
[1] ) {
31 if ( carg
[1] == '-' && !carg
[2] ) {
36 if ( (uintptr_t)(__optptr
-carg
) > (uintptr_t)strlen(carg
) )
37 __optptr
= carg
+1; /* Someone frobbed optind, change to new opt. */
41 if ( opt
!= ':' && (osptr
= strchr(optstring
, opt
)) ) {
42 if ( osptr
[1] == ':' ) {
44 /* Argument-taking option with attached argument */
45 optarg
= (char *)__optptr
;
48 /* Argument-taking option with non-attached argument */
49 if ( argv
[optind
+1] ) {
50 optarg
= (char *)argv
[optind
+1];
53 /* Missing argument */
54 return (optstring
[0] == ':') ? ':' : '?';
59 /* Non-argument-taking option */
60 /* __optptr will remember the exact position to resume at */