remove libintl stub and libintl.h header
[uclibc-ng.git] / libc / unistd / getopt-susv3.c
blob291f106f2d2c63d68c1cfe83b5daab38c74f97e9
1 /* Copyright (C) 2003 Manuel Novoa III
3 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
4 */
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.
13 * Thanks. Manuel
15 * ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! */
17 /* Sep 7, 2003
18 * Initial version of a SUSv3 compliant getopt().
21 #include <unistd.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <getopt.h>
26 #ifdef __BCC__
27 static const char missing[] = "option requires an argument";
28 static const char illegal[] = "illegal option";
29 #else
30 static const char missing[] = "%s: option requires an argument -- %c\n";
31 static const char illegal[] = "%s: illegal option -- %c\n";
32 #endif
34 int opterr = 1;
35 int optind = 1;
36 int optopt = 0;
37 char *optarg = NULL;
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;
44 int retval = -1;
46 optopt = 0;
47 optarg = NULL;
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? */
54 ) {
55 goto DONE;
57 if ((*p == '-') && (p[1] == 0)) { /* "--" case. */
58 /* ++optind; */
59 /* goto DONE; */
60 goto NEXTOPT; /* Less code generated... */
62 o = p;
65 #ifdef __BCC__
66 p = o; /* Sigh... Help out bcc. */
67 #define o p
68 #endif
69 retval = (unsigned char) *o; /* Avoid problems for char val of -1. */
71 if ((*o == ':') || !(s = strchr(optstring, *o))) { /* Illegal option? */
72 s = illegal;
73 retval = '?';
74 goto BAD;
77 if (s[1] == ':') { /* Option takes an arg? */
78 if (o[1]) { /* No space between option and arg? */
79 optarg = (char *)(o + 1);
80 goto NEXTOPT;
83 if (optind + 1 < argc) { /* Space between option and arg? */
84 optarg = argv[++optind];
85 } else { /* Out of args! */
86 s = missing;
87 retval = ':';
88 BAD:
89 optopt = *o;
90 if (*optstring != ':') {
91 retval = '?';
92 if (opterr) {
93 #ifdef __BCC__
94 fprintf(stderr, "%s: %s -- %c\n", argv[0], s, *o);
95 #else
96 fprintf(stderr, _(s), argv[0], *o);
97 #endif
103 #ifdef __BCC__
104 #undef o
105 #endif
107 if (!*++o) {
108 NEXTOPT:
109 o = NULL;
110 ++optind;
112 DONE:
113 return retval;
115 libc_hidden_def(getopt)