OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [linux/] [uClibc/] [libc/] [unistd/] [getopt-susv3.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/*  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
#define _GNU_SOURCE
34
#include <unistd.h>
35
#include <string.h>
36
#include <stdio.h>
37
 
38
#ifdef __UCLIBC_MJN3_ONLY__
39
#warning TODO: Enable gettext awareness.
40
#endif /* __UCLIBC_MJN3_ONLY__ */
41
 
42
#undef _
43
#define _(X)   X
44
 
45
#ifdef __BCC__
46
static const char missing[] = "option requires an argument";
47
static const char illegal[] = "illegal option";
48
#else
49
static const char missing[] = "%s: option requires an argument -- %c\n";
50
static const char illegal[] = "%s: illegal option -- %c\n";
51
#endif
52
 
53
int opterr = 1;
54
int optind = 1;
55
int optopt = 0;
56
char *optarg = NULL;
57
 
58
int getopt(int argc, char * const argv[], const char *optstring)
59
{
60
        static const char *o;           /* multi opt position */
61
        register const char *p;
62
        register const char *s;
63
        int retval = -1;
64
 
65
        optopt = 0;
66
        optarg = NULL;
67
 
68
        if (!o) {                               /* Not in a multi-option arg. */
69
                if ((optind >= argc)    /* No more args? */
70
                        || ((p = argv[optind]) == NULL) /* Missing? */
71
                        || (*p != '-')          /* Not an option? */
72
                        || (!*++p)                      /* "-" case? */
73
                        ) {
74
                        goto DONE;
75
                }
76
                if ((*p == '-') && (p[1] == 0)) { /* "--" case. */
77
/*                      ++optind; */
78
/*                      goto DONE; */
79
                        goto NEXTOPT;           /* Less code generated... */
80
                }
81
                o = p;
82
        }
83
 
84
#ifdef __BCC__
85
        p = o;                                          /* Sigh... Help out bcc. */
86
#define o p
87
#endif
88
        retval = (unsigned char) *o; /* Avoid problems for char val of -1. */
89
 
90
        if ((*o == ':') || !(s = strchr(optstring, *o))) { /* Illegal option? */
91
                s = illegal;
92
                retval = '?';
93
                goto BAD;
94
        }
95
 
96
        if (s[1] == ':') {                      /* Option takes an arg? */
97
                if (o[1]) {                                     /* No space between option and arg? */
98
                        optarg = (char *)(o + 1);
99
                        goto NEXTOPT;
100
                }
101
 
102
                if (optind + 1 < argc) {        /* Space between option and arg? */
103
                        optarg = argv[++optind];
104
                } else {                                /* Out of args! */
105
                        s = missing;
106
                        retval = ':';
107
                BAD:
108
                        optopt = *o;
109
                        if (*optstring != ':') {
110
                                retval = '?';
111
                                if (opterr) {
112
#ifdef __BCC__
113
                                        fprintf(stderr, "%s: %s -- %c\n", argv[0], s, *o);
114
#else
115
                                        fprintf(stderr, _(s), argv[0], *o);
116
#endif
117
                                }
118
                        }
119
                }
120
        }
121
 
122
#ifdef __BCC__
123
#undef o
124
#endif
125
 
126
        if (!*++o) {
127
        NEXTOPT:
128
                o = NULL;
129
                ++optind;
130
        }
131
 DONE:
132
        return retval;
133
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.