1 /* Copyright (C) 2003 Manuel Novoa III
3 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
6 /* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION!
8 * Besides uClibc, I'm using this code in my libc for elks, which is
9 * a 16-bit environment with a fairly limited compiler. It would make
10 * things much easier for me if this file isn't modified unnecessarily.
11 * In particular, please put any new or replacement functions somewhere
12 * else, and modify the makefile to use your version instead.
15 * ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! */
18 * Initial version of a SUSv3 compliant getopt().
27 static const char missing
[] = "option requires an argument";
28 static const char illegal
[] = "illegal option";
30 static const char missing
[] = "%s: option requires an argument -- %c\n";
31 static const char illegal
[] = "%s: illegal option -- %c\n";
39 int getopt(int argc
, char * const argv
[], const char *optstring
)
41 static const char *o
; /* multi opt position */
42 register const char *p
;
43 register const char *s
;
49 if (!o
) { /* Not in a multi-option arg. */
50 if ((optind
>= argc
) /* No more args? */
51 || ((p
= argv
[optind
]) == NULL
) /* Missing? */
52 || (*p
!= '-') /* Not an option? */
53 || (!*++p
) /* "-" case? */
57 if ((*p
== '-') && (p
[1] == 0)) { /* "--" case. */
60 goto NEXTOPT
; /* Less code generated... */
66 p
= o
; /* Sigh... Help out bcc. */
69 retval
= (unsigned char) *o
; /* Avoid problems for char val of -1. */
71 if ((*o
== ':') || !(s
= strchr(optstring
, *o
))) { /* Illegal option? */
77 if (s
[1] == ':') { /* Option takes an arg? */
78 if (o
[1]) { /* No space between option and arg? */
79 optarg
= (char *)(o
+ 1);
83 if (optind
+ 1 < argc
) { /* Space between option and arg? */
84 optarg
= argv
[++optind
];
85 } else { /* Out of args! */
90 if (*optstring
!= ':') {
94 fprintf(stderr
, "%s: %s -- %c\n", argv
[0], s
, *o
);
96 fprintf(stderr
, _(s
), argv
[0], *o
);
115 libc_hidden_def(getopt
)