OSDN Git Service

6f93e439951d76559a80360016b91e321e455a9e
[uclinux-h8/uClibc.git] / libc / unistd / getopt-susv3.c
1 /*  Copyright (C) 2003     Manuel Novoa III
2  *
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.
7  *
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.
12  *
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.
16  */
17
18 /*  ATTENTION!   ATTENTION!   ATTENTION!   ATTENTION!   ATTENTION!
19  *
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.
25  *  Thanks.  Manuel
26  *
27  *  ATTENTION!   ATTENTION!   ATTENTION!   ATTENTION!   ATTENTION! */
28
29 /* Sep 7, 2003
30  *   Initial version of a SUSv3 compliant getopt().
31  */
32
33 #include <unistd.h>
34 #include <string.h>
35 #include <stdio.h>
36
37 #ifndef __BCC__
38 libc_hidden_proto(fprintf)
39 libc_hidden_proto(strchr)
40 libc_hidden_proto(stderr)
41 #endif
42
43 #ifdef __UCLIBC_MJN3_ONLY__
44 #warning TODO: Enable gettext awareness.
45 #endif /* __UCLIBC_MJN3_ONLY__ */
46
47 #undef _
48 #define _(X)   X
49
50 #ifdef __BCC__
51 static const char missing[] = "option requires an argument";
52 static const char illegal[] = "illegal option";
53 #else
54 static const char missing[] = "%s: option requires an argument -- %c\n";
55 static const char illegal[] = "%s: illegal option -- %c\n";
56 #endif
57
58 int opterr = 1;
59 int optind = 1;
60 int optopt = 0;
61 char *optarg = NULL;
62
63 int getopt(int argc, char * const argv[], const char *optstring)
64 {
65         static const char *o;           /* multi opt position */
66         register const char *p;
67         register const char *s;
68         int retval = -1;
69
70         optopt = 0;
71         optarg = NULL;
72
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? */
78                         ) {
79                         goto DONE;
80                 }
81                 if ((*p == '-') && (p[1] == 0)) { /* "--" case. */
82 /*                      ++optind; */
83 /*                      goto DONE; */
84                         goto NEXTOPT;           /* Less code generated... */
85                 }
86                 o = p;
87         }
88
89 #ifdef __BCC__
90         p = o;                                          /* Sigh... Help out bcc. */
91 #define o p
92 #endif
93         retval = (unsigned char) *o; /* Avoid problems for char val of -1. */
94
95         if ((*o == ':') || !(s = strchr(optstring, *o))) { /* Illegal option? */
96                 s = illegal;
97                 retval = '?';
98                 goto BAD;
99         }
100         
101         if (s[1] == ':') {                      /* Option takes an arg? */
102                 if (o[1]) {                                     /* No space between option and arg? */
103                         optarg = (char *)(o + 1);
104                         goto NEXTOPT;
105                 }
106
107                 if (optind + 1 < argc) {        /* Space between option and arg? */
108                         optarg = argv[++optind];
109                 } else {                                /* Out of args! */
110                         s = missing;
111                         retval = ':';
112                 BAD:
113                         optopt = *o;
114                         if (*optstring != ':') {
115                                 retval = '?';
116                                 if (opterr) {
117 #ifdef __BCC__
118                                         fprintf(stderr, "%s: %s -- %c\n", argv[0], s, *o);
119 #else
120                                         fprintf(stderr, _(s), argv[0], *o);
121 #endif
122                                 }
123                         }
124                 }
125         }
126
127 #ifdef __BCC__
128 #undef o
129 #endif
130
131         if (!*++o) {
132         NEXTOPT:
133                 o = NULL;
134                 ++optind;
135         }
136  DONE:
137         return retval;
138 }