File: getopt.c

package info (click to toggle)
frotz 2.40-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 468 kB
  • ctags: 891
  • sloc: ansic: 6,774; makefile: 143; sh: 63
file content (72 lines) | stat: -rw-r--r-- 1,107 bytes parent folder | download | duplicates (5)
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
/*
 * getopt.c
 *
 * Replacement for a Unix style getopt function
 *
 * Quick, clean, and portable to funky systems that don't have getopt() 
 * for whatever reason.
 *
 */

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

#ifndef MSDOS_16BIT
#define cdecl
#endif

int optind = 1;
int optopt = 0;

const char *optarg = NULL;

int cdecl getopt (int argc, char *argv[], const char *options)
{
    static int 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 -- ", stdout);
	goto error;

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

	if (optind >= argc) {

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

	} else {

	    optarg = argv[optind];

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

	    pos = 1; optind++;

	}
    }
    return optopt;

error:

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

    return '?';

}/* getopt */