File: getopt.c

package info (click to toggle)
cc65 2.19-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,268 kB
  • sloc: ansic: 117,151; asm: 66,339; pascal: 4,248; makefile: 1,009; perl: 607
file content (84 lines) | stat: -rw-r--r-- 2,510 bytes parent folder | download | duplicates (3)
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
/*
** This is part of a changed public domain getopt implementation that
** had the following text on top:
**
**      I got this off net.sources from Henry Spencer.
**      It is a public domain getopt(3) like in System V.
**      I have made the following modifications:
**
**      A test main program was added, ifdeffed by GETOPT.
**      This main program is a public domain implementation
**      of the getopt(1) program like in System V.  The getopt
**      program can be used to standardize shell option handling.
**              e.g.  cc -DGETOPT getopt.c -o getopt
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define ARGCH    ':'
#define BADCH    '?'
#define EMSG     ""

int opterr = 1;                 /* useless, never set or used */
int optind = 1;                 /* index into parent argv vector */
int optopt;                     /* character checked for validity */

char *optarg;                   /* argument associated with option */

#define tell(s) fputs(*argv,stderr);fputs(s,stderr); \
                fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);

int __fastcall__ getopt (int argc, char* const* argv, const char* optstring)
/* Get option letter from argument vector */
{
    static char *place = EMSG;  /* option letter processing */

    register char *oli;         /* option letter list index */

    if (!*place) {              /* update scanning pointer */
        if (optind >= argc || *(place = argv[optind]) != '-' || !*++place) {
            return (EOF);
        }
        if (*place == '-') {
            /* found "--" */
            ++optind;
            return (EOF);
        }
    }

    /* option letter okay? */
    if ((optopt = (int) *place++) == ARGCH ||
        !(oli = strchr (optstring, optopt))) {
        if (!*place) {
            ++optind;
        }
        tell (": illegal option -- ");
    }
    if (*++oli != ARGCH) {
        /* don't need argument */
        optarg = NULL;
        if (!*place) {
            ++optind;
        }
    } else {
        /* need an argument */
        if (*place) {
            /* no white space */
            optarg = place;
        }
        else if (argc <= ++optind) {   /* no arg */
            place = EMSG;
            tell (": option requires an argument -- ");
        } else {
            /* white space */
            optarg = argv[optind];
        }
        place = EMSG;
        ++optind;
    }
    return (optopt);            /* dump back option letter */
}