File: getopt.c

package info (click to toggle)
frotz 2.32r2-10
  • links: PTS
  • area: non-free
  • in suites: hamm
  • size: 324 kB
  • ctags: 680
  • sloc: ansic: 5,206; makefile: 93; sh: 16
file content (69 lines) | stat: -rw-r--r-- 994 bytes parent folder | download | duplicates (2)
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
/*
 * getopt.c
 *
 * Replacement for a Unix style getopt function
 *
 */

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

#ifndef __MSDOS__
#define cdecl
#endif

int optind = 1;
int optopt = 0;

const char *optarg = NULL;

int cdecl getopt (int argc, char *argv[], const char *options)
{
    static pos = 1;

    const char *p;

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

    optopt = argv[optind][pos++];
    optarg = NULL;

    if (argv[optind][pos] == 0)
	{ pos = 1; optind++; }

    p = strchr (options, optopt);

    if (optopt == ':' || p == NULL) {

	fputs ("illegal option -- ", stderr);
	goto error;

    } else if (p[1] == ':')

	if (optind >= argc) {

	    fputs ("option requires an argument -- ", stderr);
	    goto error;

	} else {

	    optarg = argv[optind];

	    if (pos != 1)
		optarg += pos;

	    pos = 1; optind++;

	}

    return optopt;

error:

    fputc (optopt, stderr);
    fputc ('\n', stderr);

    return '?';

}/* getopt */