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

Subversion Repositories diogenes

[/] [diogenes/] [trunk/] [compiler/] [lcc-4.2/] [cpp/] [getopt.c] - Blame information for rev 236

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 26 fellnhofer
#include        <stdio.h>
2
#include        <string.h>
3
#define EPR                 fprintf(stderr,
4
#define ERR(str, chr)       if(opterr){EPR "%s%c\n", str, chr);}
5
int     opterr = 1;
6
int     optind = 1;
7
int     optopt;
8
char    *optarg;
9
 
10
int
11
getopt (int argc, char *const argv[], const char *opts)
12
{
13
        static int sp = 1;
14
        int c;
15
        char *cp;
16
 
17
        if (sp == 1)
18
                if (optind >= argc ||
19
                   argv[optind][0] != '-' || argv[optind][1] == '\0')
20
                        return -1;
21
                else if (strcmp(argv[optind], "--") == 0) {
22
                        optind++;
23
                        return -1;
24
                }
25
        optopt = c = argv[optind][sp];
26
        if (c == ':' || (cp=strchr(opts, c)) == 0) {
27
                ERR (": illegal option -- ", c);
28
                if (argv[optind][++sp] == '\0') {
29
                        optind++;
30
                        sp = 1;
31
                }
32
                return '?';
33
        }
34
        if (*++cp == ':') {
35
                if (argv[optind][sp+1] != '\0')
36
                        optarg = &argv[optind++][sp+1];
37
                else if (++optind >= argc) {
38
                        ERR (": option requires an argument -- ", c);
39
                        sp = 1;
40
                        return '?';
41
                } else
42
                        optarg = argv[optind++];
43
                sp = 1;
44
        } else {
45
                if (argv[optind][++sp] == '\0') {
46
                        sp = 1;
47
                        optind++;
48
                }
49
                optarg = 0;
50
        }
51
        return c;
52
}

powered by: WebSVN 2.1.0

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