1 /* Getopt for Microsoft C
2 This code is a modification of the Free Software Foundation, Inc.
3 Getopt library for parsing command line argument the purpose was
4 to provide a Microsoft Visual C friendly derivative. This code
5 provides functionality for both Unicode and Multibyte builds.
7 Date: 02/03/2011 - Ludvik Jerabek - Initial Release
9 Comment: Supports getopt, getopt_long, and getopt_long_only
10 and POSIXLY_CORRECT environment flag
15 02/03/2011 - Ludvik Jerabek - Initial Release
16 02/20/2011 - Ludvik Jerabek - Fixed compiler warnings at Level 4
17 07/05/2011 - Ludvik Jerabek - Added no_argument, required_argument, optional_argument defs
18 08/03/2011 - Ludvik Jerabek - Fixed non-argument runtime bug which caused runtime exception
19 08/09/2011 - Ludvik Jerabek - Added code to export functions for DLL and LIB
20 02/15/2012 - Ludvik Jerabek - Fixed _GETOPT_THROW definition missing in implementation file
21 08/01/2012 - Ludvik Jerabek - Created separate functions for char and wchar_t characters so single dll can do both unicode and ansi
22 10/15/2012 - Ludvik Jerabek - Modified to match latest GNU features
23 06/19/2015 - Ludvik Jerabek - Fixed maximum option limitation caused by option_a (255) and option_w (65535) structure val variable
26 THIS MATERIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
27 EITHER EXPRESS OR IMPLIED, INCLUDING, BUT Not LIMITED TO, THE
28 IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
29 PURPOSE, OR NON-INFRINGEMENT. SOME JURISDICTIONS DO NOT ALLOW THE
30 EXCLUSION OF IMPLIED WARRANTIES, SO THE ABOVE EXCLUSION MAY NOT
31 APPLY TO YOU. IN NO EVENT WILL I BE LIABLE TO ANY PARTY FOR ANY
32 DIRECT, INDIRECT, SPECIAL OR OTHER CONSEQUENTIAL DAMAGES FOR ANY
33 USE OF THIS MATERIAL INCLUDING, WITHOUT LIMITATION, ANY LOST
34 PROFITS, BUSINESS INTERRUPTION, LOSS OF PROGRAMS OR OTHER DATA ON
35 YOUR INFORMATION HANDLING SYSTEM OR OTHERWISE, EVEN If WE ARE
36 EXPRESSLY ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
43 #define alloca _alloca
46 #define _GETOPT_THROW throw()
54 enum ENUM_ORDERING
{ REQUIRE_ORDER
, PERMUTE
, RETURN_IN_ORDER
};
58 // Ansi structures and functions follow
62 static struct _getopt_data_a
70 enum ENUM_ORDERING __ordering
;
71 int __posixly_correct
;
77 static void exchange_a(char **argv
, struct _getopt_data_a
*d
)
79 int bottom
= d
->__first_nonopt
;
80 int middle
= d
->__last_nonopt
;
83 while (top
> middle
&& middle
> bottom
)
85 if (top
- middle
> middle
- bottom
)
87 int len
= middle
- bottom
;
89 for (i
= 0; i
< len
; i
++)
91 tem
= argv
[bottom
+ i
];
92 argv
[bottom
+ i
] = argv
[top
- (middle
- bottom
) + i
];
93 argv
[top
- (middle
- bottom
) + i
] = tem
;
99 int len
= top
- middle
;
101 for (i
= 0; i
< len
; i
++)
103 tem
= argv
[bottom
+ i
];
104 argv
[bottom
+ i
] = argv
[middle
+ i
];
105 argv
[middle
+ i
] = tem
;
110 d
->__first_nonopt
+= (d
->optind
- d
->__last_nonopt
);
111 d
->__last_nonopt
= d
->optind
;
113 static const char *_getopt_initialize_a (const char *optstring
, struct _getopt_data_a
*d
, int posixly_correct
)
115 d
->__first_nonopt
= d
->__last_nonopt
= d
->optind
;
116 d
->__nextchar
= NULL
;
117 d
->__posixly_correct
= posixly_correct
| !!getenv("POSIXLY_CORRECT");
118 if (optstring
[0] == '-')
120 d
->__ordering
= RETURN_IN_ORDER
;
123 else if (optstring
[0] == '+')
125 d
->__ordering
= REQUIRE_ORDER
;
128 else if (d
->__posixly_correct
)
129 d
->__ordering
= REQUIRE_ORDER
;
131 d
->__ordering
= PERMUTE
;
134 int _getopt_internal_r_a (int argc
, char *const *argv
, const char *optstring
, const struct option_a
*longopts
, int *longind
, int long_only
, struct _getopt_data_a
*d
, int posixly_correct
)
136 int print_errors
= d
->opterr
;
140 if (d
->optind
== 0 || !d
->__initialized
)
144 optstring
= _getopt_initialize_a (optstring
, d
, posixly_correct
);
145 d
->__initialized
= 1;
147 else if (optstring
[0] == '-' || optstring
[0] == '+')
149 if (optstring
[0] == ':')
151 if (d
->__nextchar
== NULL
|| *d
->__nextchar
== '\0')
153 if (d
->__last_nonopt
> d
->optind
)
154 d
->__last_nonopt
= d
->optind
;
155 if (d
->__first_nonopt
> d
->optind
)
156 d
->__first_nonopt
= d
->optind
;
157 if (d
->__ordering
== PERMUTE
)
159 if (d
->__first_nonopt
!= d
->__last_nonopt
&& d
->__last_nonopt
!= d
->optind
)
160 exchange_a ((char **) argv
, d
);
161 else if (d
->__last_nonopt
!= d
->optind
)
162 d
->__first_nonopt
= d
->optind
;
163 while (d
->optind
< argc
&& (argv
[d
->optind
][0] != '-' || argv
[d
->optind
][1] == '\0'))
165 d
->__last_nonopt
= d
->optind
;
167 if (d
->optind
!= argc
&& !strcmp(argv
[d
->optind
], "--"))
170 if (d
->__first_nonopt
!= d
->__last_nonopt
&& d
->__last_nonopt
!= d
->optind
)
171 exchange_a((char **) argv
, d
);
172 else if (d
->__first_nonopt
== d
->__last_nonopt
)
173 d
->__first_nonopt
= d
->optind
;
174 d
->__last_nonopt
= argc
;
177 if (d
->optind
== argc
)
179 if (d
->__first_nonopt
!= d
->__last_nonopt
)
180 d
->optind
= d
->__first_nonopt
;
183 if ((argv
[d
->optind
][0] != '-' || argv
[d
->optind
][1] == '\0'))
185 if (d
->__ordering
== REQUIRE_ORDER
)
187 d
->optarg
= argv
[d
->optind
++];
190 d
->__nextchar
= (argv
[d
->optind
] + 1 + (longopts
!= NULL
&& argv
[d
->optind
][1] == '-'));
192 if (longopts
!= NULL
&& (argv
[d
->optind
][1] == '-' || (long_only
&& (argv
[d
->optind
][2] || !strchr(optstring
, argv
[d
->optind
][1])))))
195 unsigned int namelen
;
196 const struct option_a
*p
;
197 const struct option_a
*pfound
= NULL
;
200 const struct option_a
*p
;
201 struct option_list
*next
;
202 } *ambig_list
= NULL
;
206 for (nameend
= d
->__nextchar
; *nameend
&& *nameend
!= '='; nameend
++);
207 namelen
= (unsigned int)(nameend
- d
->__nextchar
);
208 for (p
= longopts
, option_index
= 0; p
->name
; p
++, option_index
++)
209 if (!strncmp(p
->name
, d
->__nextchar
, namelen
))
211 if (namelen
== (unsigned int)strlen(p
->name
))
214 indfound
= option_index
;
218 else if (pfound
== NULL
)
221 indfound
= option_index
;
223 else if (long_only
|| pfound
->has_arg
!= p
->has_arg
|| pfound
->flag
!= p
->flag
|| pfound
->val
!= p
->val
)
225 struct option_list
*newp
= (struct option_list
*)alloca(sizeof(*newp
));
227 newp
->next
= ambig_list
;
231 if (ambig_list
!= NULL
&& !exact
)
235 struct option_list first
;
237 first
.next
= ambig_list
;
239 fprintf (stderr
, "%s: option '%s' is ambiguous; possibilities:", argv
[0], argv
[d
->optind
]);
242 fprintf (stderr
, " '--%s'", ambig_list
->p
->name
);
243 ambig_list
= ambig_list
->next
;
245 while (ambig_list
!= NULL
);
246 fputc ('\n', stderr
);
248 d
->__nextchar
+= strlen(d
->__nextchar
);
255 option_index
= indfound
;
260 d
->optarg
= nameend
+ 1;
265 if (argv
[d
->optind
- 1][1] == '-')
267 fprintf(stderr
, "%s: option '--%s' doesn't allow an argument\n",argv
[0], pfound
->name
);
271 fprintf(stderr
, "%s: option '%c%s' doesn't allow an argument\n",argv
[0], argv
[d
->optind
- 1][0],pfound
->name
);
274 d
->__nextchar
+= strlen(d
->__nextchar
);
275 d
->optopt
= pfound
->val
;
279 else if (pfound
->has_arg
== 1)
281 if (d
->optind
< argc
)
282 d
->optarg
= argv
[d
->optind
++];
287 fprintf(stderr
,"%s: option '--%s' requires an argument\n",argv
[0], pfound
->name
);
289 d
->__nextchar
+= strlen(d
->__nextchar
);
290 d
->optopt
= pfound
->val
;
291 return optstring
[0] == ':' ? ':' : '?';
294 d
->__nextchar
+= strlen(d
->__nextchar
);
296 *longind
= option_index
;
299 *(pfound
->flag
) = pfound
->val
;
304 if (!long_only
|| argv
[d
->optind
][1] == '-' || strchr(optstring
, *d
->__nextchar
) == NULL
)
308 if (argv
[d
->optind
][1] == '-')
310 fprintf(stderr
, "%s: unrecognized option '--%s'\n",argv
[0], d
->__nextchar
);
314 fprintf(stderr
, "%s: unrecognized option '%c%s'\n",argv
[0], argv
[d
->optind
][0], d
->__nextchar
);
317 d
->__nextchar
= (char *)"";
324 char c
= *d
->__nextchar
++;
325 char *temp
= (char*)strchr(optstring
, c
);
326 if (*d
->__nextchar
== '\0')
328 if (temp
== NULL
|| c
== ':' || c
== ';')
332 fprintf(stderr
, "%s: invalid option -- '%c'\n", argv
[0], c
);
337 if (temp
[0] == 'W' && temp
[1] == ';')
340 const struct option_a
*p
;
341 const struct option_a
*pfound
= NULL
;
346 if (longopts
== NULL
)
348 if (*d
->__nextchar
!= '\0')
350 d
->optarg
= d
->__nextchar
;
353 else if (d
->optind
== argc
)
357 fprintf(stderr
,"%s: option requires an argument -- '%c'\n",argv
[0], c
);
360 if (optstring
[0] == ':')
367 d
->optarg
= argv
[d
->optind
++];
368 for (d
->__nextchar
= nameend
= d
->optarg
; *nameend
&& *nameend
!= '='; nameend
++);
369 for (p
= longopts
, option_index
= 0; p
->name
; p
++, option_index
++)
370 if (!strncmp(p
->name
, d
->__nextchar
, nameend
- d
->__nextchar
))
372 if ((unsigned int) (nameend
- d
->__nextchar
) == strlen(p
->name
))
375 indfound
= option_index
;
379 else if (pfound
== NULL
)
382 indfound
= option_index
;
384 else if (long_only
|| pfound
->has_arg
!= p
->has_arg
|| pfound
->flag
!= p
->flag
|| pfound
->val
!= p
->val
)
391 fprintf(stderr
, "%s: option '-W %s' is ambiguous\n",argv
[0], d
->optarg
);
393 d
->__nextchar
+= strlen(d
->__nextchar
);
399 option_index
= indfound
;
403 d
->optarg
= nameend
+ 1;
408 fprintf(stderr
, "%s: option '-W %s' doesn't allow an argument\n",argv
[0], pfound
->name
);
410 d
->__nextchar
+= strlen(d
->__nextchar
);
414 else if (pfound
->has_arg
== 1)
416 if (d
->optind
< argc
)
417 d
->optarg
= argv
[d
->optind
++];
422 fprintf(stderr
, "%s: option '-W %s' requires an argument\n",argv
[0], pfound
->name
);
424 d
->__nextchar
+= strlen(d
->__nextchar
);
425 return optstring
[0] == ':' ? ':' : '?';
430 d
->__nextchar
+= strlen(d
->__nextchar
);
432 *longind
= option_index
;
435 *(pfound
->flag
) = pfound
->val
;
441 d
->__nextchar
= NULL
;
448 if (*d
->__nextchar
!= '\0')
450 d
->optarg
= d
->__nextchar
;
455 d
->__nextchar
= NULL
;
459 if (*d
->__nextchar
!= '\0')
461 d
->optarg
= d
->__nextchar
;
464 else if (d
->optind
== argc
)
468 fprintf(stderr
,"%s: option requires an argument -- '%c'\n",argv
[0], c
);
471 if (optstring
[0] == ':')
477 d
->optarg
= argv
[d
->optind
++];
478 d
->__nextchar
= NULL
;
484 int _getopt_internal_a (int argc
, char *const *argv
, const char *optstring
, const struct option_a
*longopts
, int *longind
, int long_only
, int posixly_correct
)
487 getopt_data_a
.optind
= optind
;
488 getopt_data_a
.opterr
= opterr
;
489 result
= _getopt_internal_r_a (argc
, argv
, optstring
, longopts
,longind
, long_only
, &getopt_data_a
,posixly_correct
);
490 optind
= getopt_data_a
.optind
;
491 optarg_a
= getopt_data_a
.optarg
;
492 optopt
= getopt_data_a
.optopt
;
495 int getopt_a (int argc
, char *const *argv
, const char *optstring
) _GETOPT_THROW
497 return _getopt_internal_a (argc
, argv
, optstring
, (const struct option_a
*) 0, (int *) 0, 0, 0);
499 int getopt_long_a (int argc
, char *const *argv
, const char *options
, const struct option_a
*long_options
, int *opt_index
) _GETOPT_THROW
501 return _getopt_internal_a (argc
, argv
, options
, long_options
, opt_index
, 0, 0);
503 int getopt_long_only_a (int argc
, char *const *argv
, const char *options
, const struct option_a
*long_options
, int *opt_index
) _GETOPT_THROW
505 return _getopt_internal_a (argc
, argv
, options
, long_options
, opt_index
, 1, 0);
507 int _getopt_long_r_a (int argc
, char *const *argv
, const char *options
, const struct option_a
*long_options
, int *opt_index
, struct _getopt_data_a
*d
)
509 return _getopt_internal_r_a (argc
, argv
, options
, long_options
, opt_index
,0, d
, 0);
511 int _getopt_long_only_r_a (int argc
, char *const *argv
, const char *options
, const struct option_a
*long_options
, int *opt_index
, struct _getopt_data_a
*d
)
513 return _getopt_internal_r_a (argc
, argv
, options
, long_options
, opt_index
, 1, d
, 0);
518 // Unicode Structures and Functions
522 static struct _getopt_data_w
530 enum ENUM_ORDERING __ordering
;
531 int __posixly_correct
;
537 static void exchange_w(wchar_t **argv
, struct _getopt_data_w
*d
)
539 int bottom
= d
->__first_nonopt
;
540 int middle
= d
->__last_nonopt
;
543 while (top
> middle
&& middle
> bottom
)
545 if (top
- middle
> middle
- bottom
)
547 int len
= middle
- bottom
;
549 for (i
= 0; i
< len
; i
++)
551 tem
= argv
[bottom
+ i
];
552 argv
[bottom
+ i
] = argv
[top
- (middle
- bottom
) + i
];
553 argv
[top
- (middle
- bottom
) + i
] = tem
;
559 int len
= top
- middle
;
561 for (i
= 0; i
< len
; i
++)
563 tem
= argv
[bottom
+ i
];
564 argv
[bottom
+ i
] = argv
[middle
+ i
];
565 argv
[middle
+ i
] = tem
;
570 d
->__first_nonopt
+= (d
->optind
- d
->__last_nonopt
);
571 d
->__last_nonopt
= d
->optind
;
573 static const wchar_t *_getopt_initialize_w (const wchar_t *optstring
, struct _getopt_data_w
*d
, int posixly_correct
)
575 d
->__first_nonopt
= d
->__last_nonopt
= d
->optind
;
576 d
->__nextchar
= NULL
;
577 d
->__posixly_correct
= posixly_correct
| !!_wgetenv(L
"POSIXLY_CORRECT");
578 if (optstring
[0] == L
'-')
580 d
->__ordering
= RETURN_IN_ORDER
;
583 else if (optstring
[0] == L
'+')
585 d
->__ordering
= REQUIRE_ORDER
;
588 else if (d
->__posixly_correct
)
589 d
->__ordering
= REQUIRE_ORDER
;
591 d
->__ordering
= PERMUTE
;
594 int _getopt_internal_r_w (int argc
, wchar_t *const *argv
, const wchar_t *optstring
, const struct option_w
*longopts
, int *longind
, int long_only
, struct _getopt_data_w
*d
, int posixly_correct
)
596 int print_errors
= d
->opterr
;
600 if (d
->optind
== 0 || !d
->__initialized
)
604 optstring
= _getopt_initialize_w (optstring
, d
, posixly_correct
);
605 d
->__initialized
= 1;
607 else if (optstring
[0] == L
'-' || optstring
[0] == L
'+')
609 if (optstring
[0] == L
':')
611 if (d
->__nextchar
== NULL
|| *d
->__nextchar
== L
'\0')
613 if (d
->__last_nonopt
> d
->optind
)
614 d
->__last_nonopt
= d
->optind
;
615 if (d
->__first_nonopt
> d
->optind
)
616 d
->__first_nonopt
= d
->optind
;
617 if (d
->__ordering
== PERMUTE
)
619 if (d
->__first_nonopt
!= d
->__last_nonopt
&& d
->__last_nonopt
!= d
->optind
)
620 exchange_w((wchar_t **) argv
, d
);
621 else if (d
->__last_nonopt
!= d
->optind
)
622 d
->__first_nonopt
= d
->optind
;
623 while (d
->optind
< argc
&& (argv
[d
->optind
][0] != L
'-' || argv
[d
->optind
][1] == L
'\0'))
625 d
->__last_nonopt
= d
->optind
;
627 if (d
->optind
!= argc
&& !wcscmp(argv
[d
->optind
], L
"--"))
630 if (d
->__first_nonopt
!= d
->__last_nonopt
&& d
->__last_nonopt
!= d
->optind
)
631 exchange_w((wchar_t **) argv
, d
);
632 else if (d
->__first_nonopt
== d
->__last_nonopt
)
633 d
->__first_nonopt
= d
->optind
;
634 d
->__last_nonopt
= argc
;
637 if (d
->optind
== argc
)
639 if (d
->__first_nonopt
!= d
->__last_nonopt
)
640 d
->optind
= d
->__first_nonopt
;
643 if ((argv
[d
->optind
][0] != L
'-' || argv
[d
->optind
][1] == L
'\0'))
645 if (d
->__ordering
== REQUIRE_ORDER
)
647 d
->optarg
= argv
[d
->optind
++];
650 d
->__nextchar
= (argv
[d
->optind
] + 1 + (longopts
!= NULL
&& argv
[d
->optind
][1] == L
'-'));
652 if (longopts
!= NULL
&& (argv
[d
->optind
][1] == L
'-' || (long_only
&& (argv
[d
->optind
][2] || !wcschr(optstring
, argv
[d
->optind
][1])))))
655 unsigned int namelen
;
656 const struct option_w
*p
;
657 const struct option_w
*pfound
= NULL
;
660 const struct option_w
*p
;
661 struct option_list
*next
;
662 } *ambig_list
= NULL
;
666 for (nameend
= d
->__nextchar
; *nameend
&& *nameend
!= L
'='; nameend
++);
667 namelen
= (unsigned int)(nameend
- d
->__nextchar
);
668 for (p
= longopts
, option_index
= 0; p
->name
; p
++, option_index
++)
669 if (!wcsncmp(p
->name
, d
->__nextchar
, namelen
))
671 if (namelen
== (unsigned int)wcslen(p
->name
))
674 indfound
= option_index
;
678 else if (pfound
== NULL
)
681 indfound
= option_index
;
683 else if (long_only
|| pfound
->has_arg
!= p
->has_arg
|| pfound
->flag
!= p
->flag
|| pfound
->val
!= p
->val
)
685 struct option_list
*newp
= (struct option_list
*)alloca(sizeof(*newp
));
687 newp
->next
= ambig_list
;
691 if (ambig_list
!= NULL
&& !exact
)
695 struct option_list first
;
697 first
.next
= ambig_list
;
699 fwprintf(stderr
, L
"%s: option '%s' is ambiguous; possibilities:", argv
[0], argv
[d
->optind
]);
702 fwprintf (stderr
, L
" '--%s'", ambig_list
->p
->name
);
703 ambig_list
= ambig_list
->next
;
705 while (ambig_list
!= NULL
);
706 fputwc (L
'\n', stderr
);
708 d
->__nextchar
+= wcslen(d
->__nextchar
);
715 option_index
= indfound
;
720 d
->optarg
= nameend
+ 1;
725 if (argv
[d
->optind
- 1][1] == L
'-')
727 fwprintf(stderr
, L
"%s: option '--%s' doesn't allow an argument\n",argv
[0], pfound
->name
);
731 fwprintf(stderr
, L
"%s: option '%c%s' doesn't allow an argument\n",argv
[0], argv
[d
->optind
- 1][0],pfound
->name
);
734 d
->__nextchar
+= wcslen(d
->__nextchar
);
735 d
->optopt
= pfound
->val
;
739 else if (pfound
->has_arg
== 1)
741 if (d
->optind
< argc
)
742 d
->optarg
= argv
[d
->optind
++];
747 fwprintf(stderr
,L
"%s: option '--%s' requires an argument\n",argv
[0], pfound
->name
);
749 d
->__nextchar
+= wcslen(d
->__nextchar
);
750 d
->optopt
= pfound
->val
;
751 return optstring
[0] == L
':' ? L
':' : L
'?';
754 d
->__nextchar
+= wcslen(d
->__nextchar
);
756 *longind
= option_index
;
759 *(pfound
->flag
) = pfound
->val
;
764 if (!long_only
|| argv
[d
->optind
][1] == L
'-' || wcschr(optstring
, *d
->__nextchar
) == NULL
)
768 if (argv
[d
->optind
][1] == L
'-')
770 fwprintf(stderr
, L
"%s: unrecognized option '--%s'\n",argv
[0], d
->__nextchar
);
774 fwprintf(stderr
, L
"%s: unrecognized option '%c%s'\n",argv
[0], argv
[d
->optind
][0], d
->__nextchar
);
777 d
->__nextchar
= (wchar_t *)L
"";
784 wchar_t c
= *d
->__nextchar
++;
785 wchar_t *temp
= (wchar_t*)wcschr(optstring
, c
);
786 if (*d
->__nextchar
== L
'\0')
788 if (temp
== NULL
|| c
== L
':' || c
== L
';')
792 fwprintf(stderr
, L
"%s: invalid option -- '%c'\n", argv
[0], c
);
797 if (temp
[0] == L
'W' && temp
[1] == L
';')
800 const struct option_w
*p
;
801 const struct option_w
*pfound
= NULL
;
806 if (longopts
== NULL
)
808 if (*d
->__nextchar
!= L
'\0')
810 d
->optarg
= d
->__nextchar
;
813 else if (d
->optind
== argc
)
817 fwprintf(stderr
,L
"%s: option requires an argument -- '%c'\n",argv
[0], c
);
820 if (optstring
[0] == L
':')
827 d
->optarg
= argv
[d
->optind
++];
828 for (d
->__nextchar
= nameend
= d
->optarg
; *nameend
&& *nameend
!= L
'='; nameend
++);
829 for (p
= longopts
, option_index
= 0; p
->name
; p
++, option_index
++)
830 if (!wcsncmp(p
->name
, d
->__nextchar
, nameend
- d
->__nextchar
))
832 if ((unsigned int) (nameend
- d
->__nextchar
) == wcslen(p
->name
))
835 indfound
= option_index
;
839 else if (pfound
== NULL
)
842 indfound
= option_index
;
844 else if (long_only
|| pfound
->has_arg
!= p
->has_arg
|| pfound
->flag
!= p
->flag
|| pfound
->val
!= p
->val
)
851 fwprintf(stderr
, L
"%s: option '-W %s' is ambiguous\n",argv
[0], d
->optarg
);
853 d
->__nextchar
+= wcslen(d
->__nextchar
);
859 option_index
= indfound
;
863 d
->optarg
= nameend
+ 1;
868 fwprintf(stderr
, L
"%s: option '-W %s' doesn't allow an argument\n",argv
[0], pfound
->name
);
870 d
->__nextchar
+= wcslen(d
->__nextchar
);
874 else if (pfound
->has_arg
== 1)
876 if (d
->optind
< argc
)
877 d
->optarg
= argv
[d
->optind
++];
882 fwprintf(stderr
, L
"%s: option '-W %s' requires an argument\n",argv
[0], pfound
->name
);
884 d
->__nextchar
+= wcslen(d
->__nextchar
);
885 return optstring
[0] == L
':' ? L
':' : L
'?';
890 d
->__nextchar
+= wcslen(d
->__nextchar
);
892 *longind
= option_index
;
895 *(pfound
->flag
) = pfound
->val
;
901 d
->__nextchar
= NULL
;
908 if (*d
->__nextchar
!= L
'\0')
910 d
->optarg
= d
->__nextchar
;
915 d
->__nextchar
= NULL
;
919 if (*d
->__nextchar
!= L
'\0')
921 d
->optarg
= d
->__nextchar
;
924 else if (d
->optind
== argc
)
928 fwprintf(stderr
,L
"%s: option requires an argument -- '%c'\n",argv
[0], c
);
931 if (optstring
[0] == L
':')
937 d
->optarg
= argv
[d
->optind
++];
938 d
->__nextchar
= NULL
;
944 int _getopt_internal_w (int argc
, wchar_t *const *argv
, const wchar_t *optstring
, const struct option_w
*longopts
, int *longind
, int long_only
, int posixly_correct
)
947 getopt_data_w
.optind
= optind
;
948 getopt_data_w
.opterr
= opterr
;
949 result
= _getopt_internal_r_w (argc
, argv
, optstring
, longopts
,longind
, long_only
, &getopt_data_w
,posixly_correct
);
950 optind
= getopt_data_w
.optind
;
951 optarg_w
= getopt_data_w
.optarg
;
952 optopt
= getopt_data_w
.optopt
;
955 int getopt_w (int argc
, wchar_t *const *argv
, const wchar_t *optstring
) _GETOPT_THROW
957 return _getopt_internal_w (argc
, argv
, optstring
, (const struct option_w
*) 0, (int *) 0, 0, 0);
959 int getopt_long_w (int argc
, wchar_t *const *argv
, const wchar_t *options
, const struct option_w
*long_options
, int *opt_index
) _GETOPT_THROW
961 return _getopt_internal_w (argc
, argv
, options
, long_options
, opt_index
, 0, 0);
963 int getopt_long_only_w (int argc
, wchar_t *const *argv
, const wchar_t *options
, const struct option_w
*long_options
, int *opt_index
) _GETOPT_THROW
965 return _getopt_internal_w (argc
, argv
, options
, long_options
, opt_index
, 1, 0);
967 int _getopt_long_r_w (int argc
, wchar_t *const *argv
, const wchar_t *options
, const struct option_w
*long_options
, int *opt_index
, struct _getopt_data_w
*d
)
969 return _getopt_internal_r_w (argc
, argv
, options
, long_options
, opt_index
,0, d
, 0);
971 int _getopt_long_only_r_w (int argc
, wchar_t *const *argv
, const wchar_t *options
, const struct option_w
*long_options
, int *opt_index
, struct _getopt_data_w
*d
)
973 return _getopt_internal_r_w (argc
, argv
, options
, long_options
, opt_index
, 1, d
, 0);