1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
/* Copyright (C) 2003 Manuel Novoa III
*
* Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
*/
/* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION!
*
* Besides uClibc, I'm using this code in my libc for elks, which is
* a 16-bit environment with a fairly limited compiler. It would make
* things much easier for me if this file isn't modified unnecessarily.
* In particular, please put any new or replacement functions somewhere
* else, and modify the makefile to use your version instead.
* Thanks. Manuel
*
* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! */
/* Sep 7, 2003
* Initial version of a SUSv3 compliant getopt().
*/
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <getopt.h>
/* libc_hidden_proto(fprintf) */
/* Experimentally off - libc_hidden_proto(strchr) */
#ifdef __UCLIBC_MJN3_ONLY__
#warning TODO: Enable gettext awareness.
#endif /* __UCLIBC_MJN3_ONLY__ */
#undef _
#define _(X) X
#ifdef __BCC__
static const char missing[] = "option requires an argument";
static const char illegal[] = "illegal option";
#else
static const char missing[] = "%s: option requires an argument -- %c\n";
static const char illegal[] = "%s: illegal option -- %c\n";
#endif
int opterr = 1;
int optind = 1;
int optopt = 0;
char *optarg = NULL;
libc_hidden_proto(getopt)
int getopt(int argc, char * const argv[], const char *optstring)
{
static const char *o; /* multi opt position */
register const char *p;
register const char *s;
int retval = -1;
optopt = 0;
optarg = NULL;
if (!o) { /* Not in a multi-option arg. */
if ((optind >= argc) /* No more args? */
|| ((p = argv[optind]) == NULL) /* Missing? */
|| (*p != '-') /* Not an option? */
|| (!*++p) /* "-" case? */
) {
goto DONE;
}
if ((*p == '-') && (p[1] == 0)) { /* "--" case. */
/* ++optind; */
/* goto DONE; */
goto NEXTOPT; /* Less code generated... */
}
o = p;
}
#ifdef __BCC__
p = o; /* Sigh... Help out bcc. */
#define o p
#endif
retval = (unsigned char) *o; /* Avoid problems for char val of -1. */
if ((*o == ':') || !(s = strchr(optstring, *o))) { /* Illegal option? */
s = illegal;
retval = '?';
goto BAD;
}
if (s[1] == ':') { /* Option takes an arg? */
if (o[1]) { /* No space between option and arg? */
optarg = (char *)(o + 1);
goto NEXTOPT;
}
if (optind + 1 < argc) { /* Space between option and arg? */
optarg = argv[++optind];
} else { /* Out of args! */
s = missing;
retval = ':';
BAD:
optopt = *o;
if (*optstring != ':') {
retval = '?';
if (opterr) {
#ifdef __BCC__
fprintf(stderr, "%s: %s -- %c\n", argv[0], s, *o);
#else
fprintf(stderr, _(s), argv[0], *o);
#endif
}
}
}
}
#ifdef __BCC__
#undef o
#endif
if (!*++o) {
NEXTOPT:
o = NULL;
++optind;
}
DONE:
return retval;
}
libc_hidden_def(getopt)
|