~ubuntu-branches/ubuntu/saucy/di/saucy

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
/*
$Id: getopt.c,v 1.14 2010-07-14 01:54:51-07 bll Exp $
$Source: /home/bll/DI/di/RCS/getopt.c,v $
*/

#include "config.h"

#if ! _lib_getopt

/*
 *
 * $Id: getopt.c,v 1.14 2010-07-14 01:54:51-07 bll Exp $
 *
 * getopt - get option letter from argv
 *
 * from Cnews by Henry Spencer
 *
 */

#if _hdr_stdio
# include <stdio.h>
#endif
#if _hdr_stdlib
# include <stdlib.h>
#endif
#if _hdr_string
# include <string.h>
#endif
#if _hdr_strings
# include <strings.h>
#endif

char    *optarg;    /* Global argument pointer. */
int     optind = 0; /* Global argv index. */

static char *scan = NULL;   /* Private scan pointer. */

int
#if _proto_stdc
getopt (int argc, char * const argv [], const char *optstring)
#else
getopt (argc, argv, optstring)
    int argc;
    char * const argv[];
    const char *optstring;
#endif
{
    char c;
    char *place;


    optarg = NULL;

    if (scan == NULL || *scan == '\0')
    {
        if (optind == 0)
        {
            optind++;
        }

        if (optind >= argc || argv[optind][0] != '-' ||
                argv[optind][1] == '\0')
        {
            return(EOF);
        }

        if (strcmp(argv[optind], "--")==0)
        {
            optind++;
            return(EOF);
        }

        scan = argv[optind]+1;
        optind++;
    }

    c = *scan++;
    place = strchr(optstring, c);

    if (place == NULL || c == ':')
    {
        fprintf(stderr, "%s: unknown option -%c\n", argv[0], c);
        return('?');
    }

    place++;
    if (*place == ':')
    {
        if (*scan != '\0')
        {
            optarg = scan;
            scan = NULL;
        }
        else if (optind < argc)
        {
            optarg = argv[optind];
            optind++;
        }
        else
        {
            fprintf(stderr, "%s: -%c argument missing\n", argv[0], c);
            return('?');
        }
    }

    return(c);
}

#else

extern int debug;

#endif