7 #include "locale_impl.h"
10 int optind
=1, opterr
=1, optopt
, __optpos
, __optreset
=0;
12 #define optpos __optpos
13 weak_alias(__optreset
, optreset
);
15 void __getopt_msg(const char *a
, const char *b
, const char *c
, size_t l
)
21 && fwrite(b
, strlen(b
), 1, f
)
22 && fwrite(c
, 1, l
, f
)==l
27 int getopt(int argc
, char * const argv
[], const char *optstring
)
34 if (!optind
|| __optreset
) {
40 if (optind
>= argc
|| !argv
[optind
])
43 if (argv
[optind
][0] != '-') {
44 if (optstring
[0] == '-') {
45 optarg
= argv
[optind
++];
54 if (argv
[optind
][1] == '-' && !argv
[optind
][2])
57 if (!optpos
) optpos
++;
58 if ((k
= mbtowc(&c
, argv
[optind
]+optpos
, MB_LEN_MAX
)) < 0) {
60 c
= 0xfffd; /* replacement char */
62 optchar
= argv
[optind
]+optpos
;
65 if (!argv
[optind
][optpos
]) {
70 if (optstring
[0] == '-' || optstring
[0] == '+')
76 l
= mbtowc(&d
, optstring
+i
, MB_LEN_MAX
);
77 if (l
>0) i
+=l
; else i
++;
78 } while (l
&& d
!= c
);
82 if (optstring
[0] != ':' && opterr
)
83 __getopt_msg(argv
[0], ": unrecognized option: ", optchar
, k
);
86 if (optstring
[i
] == ':') {
87 if (optstring
[i
+1] == ':') optarg
= 0;
88 else if (optind
>= argc
) {
90 if (optstring
[0] == ':') return ':';
91 if (opterr
) __getopt_msg(argv
[0],
92 ": option requires an argument: ",
96 if (optstring
[i
+1] != ':' || optpos
) {
97 optarg
= argv
[optind
++] + optpos
;
104 weak_alias(getopt
, __posix_getopt
);