7 #include "locale_impl.h"
8 #include "stdio_impl.h"
11 int optind
=1, opterr
=1, optopt
, __optpos
, __optreset
=0;
13 #define optpos __optpos
14 weak_alias(__optreset
, optreset
);
16 void __getopt_msg(const char *a
, const char *b
, const char *c
, size_t l
)
22 && fwrite(b
, strlen(b
), 1, f
)
23 && fwrite(c
, 1, l
, f
)==l
28 int getopt(int argc
, char * const argv
[], const char *optstring
)
35 if (!optind
|| __optreset
) {
41 if (optind
>= argc
|| !argv
[optind
])
44 if (argv
[optind
][0] != '-') {
45 if (optstring
[0] == '-') {
46 optarg
= argv
[optind
++];
55 if (argv
[optind
][1] == '-' && !argv
[optind
][2])
58 if (!optpos
) optpos
++;
59 if ((k
= mbtowc(&c
, argv
[optind
]+optpos
, MB_LEN_MAX
)) < 0) {
61 c
= 0xfffd; /* replacement char */
63 optchar
= argv
[optind
]+optpos
;
66 if (!argv
[optind
][optpos
]) {
71 if (optstring
[0] == '-' || optstring
[0] == '+')
77 l
= mbtowc(&d
, optstring
+i
, MB_LEN_MAX
);
78 if (l
>0) i
+=l
; else i
++;
79 } while (l
&& d
!= c
);
81 if (d
!= c
|| c
== ':') {
83 if (optstring
[0] != ':' && opterr
)
84 __getopt_msg(argv
[0], ": unrecognized option: ", optchar
, k
);
87 if (optstring
[i
] == ':') {
89 if (optstring
[i
+1] != ':' || optpos
) {
90 optarg
= argv
[optind
++] + optpos
;
95 if (optstring
[0] == ':') return ':';
96 if (opterr
) __getopt_msg(argv
[0],
97 ": option requires an argument: ",
105 weak_alias(getopt
, __posix_getopt
);