1 /* Copyright (C) 2003 Manuel Novoa III
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Library General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Library General Public License for more details.
13 * You should have received a copy of the GNU Library General Public
14 * License along with this library; if not, write to the Free
15 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 /* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION!
20 * Besides uClibc, I'm using this code in my libc for elks, which is
21 * a 16-bit environment with a fairly limited compiler. It would make
22 * things much easier for me if this file isn't modified unnecessarily.
23 * In particular, please put any new or replacement functions somewhere
24 * else, and modify the makefile to use your version instead.
27 * ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! */
30 * Initial version of a SUSv3 compliant getopt().
38 libc_hidden_proto(fprintf)
39 libc_hidden_proto(strchr)
40 libc_hidden_proto(stderr)
43 #ifdef __UCLIBC_MJN3_ONLY__
44 #warning TODO: Enable gettext awareness.
45 #endif /* __UCLIBC_MJN3_ONLY__ */
51 static const char missing[] = "option requires an argument";
52 static const char illegal[] = "illegal option";
54 static const char missing[] = "%s: option requires an argument -- %c\n";
55 static const char illegal[] = "%s: illegal option -- %c\n";
63 int getopt(int argc, char * const argv[], const char *optstring)
65 static const char *o; /* multi opt position */
66 register const char *p;
67 register const char *s;
73 if (!o) { /* Not in a multi-option arg. */
74 if ((optind >= argc) /* No more args? */
75 || ((p = argv[optind]) == NULL) /* Missing? */
76 || (*p != '-') /* Not an option? */
77 || (!*++p) /* "-" case? */
81 if ((*p == '-') && (p[1] == 0)) { /* "--" case. */
84 goto NEXTOPT; /* Less code generated... */
90 p = o; /* Sigh... Help out bcc. */
93 retval = (unsigned char) *o; /* Avoid problems for char val of -1. */
95 if ((*o == ':') || !(s = strchr(optstring, *o))) { /* Illegal option? */
101 if (s[1] == ':') { /* Option takes an arg? */
102 if (o[1]) { /* No space between option and arg? */
103 optarg = (char *)(o + 1);
107 if (optind + 1 < argc) { /* Space between option and arg? */
108 optarg = argv[++optind];
109 } else { /* Out of args! */
114 if (*optstring != ':') {
118 fprintf(stderr, "%s: %s -- %c\n", argv[0], s, *o);
120 fprintf(stderr, _(s), argv[0], *o);