File: getopt.c

package info (click to toggle)
lifelines 3.0.50-2etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 11,160 kB
  • ctags: 6,517
  • sloc: ansic: 57,468; xml: 8,014; sh: 4,489; makefile: 848; yacc: 601; perl: 170; sed: 16
file content (158 lines) | stat: -rw-r--r-- 3,071 bytes parent folder | download | duplicates (11)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* getopt.c -
   getopt() for those systems that don't have it.

   Derived from comp.sources.unix/volume3/att_getopt.
   Modified by James Clark (jjc@jclark.com).
*/

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

#ifdef SWITCHAR
#include <dos.h>
#endif

int	opterr = 1;
int	optind = 1;
int	optopt;
char *optarg;

#ifndef OPTION_CHAR
#define OPTION_CHAR '-'
#endif

int getopt(int argc, char **argv, char *opts)
{
#ifdef SWITCHAR
	union REGS regs;
	static char switchar = '\0';
#endif
	static int sp = 1;
	register int c;
	register char *cp;
	char *message;
#ifdef SWITCHAR
	if (switchar == '\0') {
		regs.x.ax = 0x3700;
		intdos(&regs, &regs);
		if (!regs.x.cflag)
			switchar = regs.h.dl;
		else
			switchar = '/';
	}
#endif
	if (sp == 1) {
		if (optind >= argc)
			return EOF;
		if ((
#ifdef SWITCHAR
			argv[optind][0] != switchar &&
#endif
			argv[optind][0] != OPTION_CHAR) || argv[optind][1] == '\0') {
#ifdef REORDER_ARGS
			int i;
			for (i = optind; i < argc; i++)
				if ((
#ifdef SWITCHAR
					 argv[i][0] == switchar ||
#endif
					 argv[i][0] == OPTION_CHAR) && argv[i][1] != '\0')
					break;
			if (i < argc) {
				c = argv[i][1];
#ifdef CASE_INSENSITIVE_OPTIONS
				if (isupper(c))
					c = tolower(c);
#endif
				if (c != ':' && c != OPTION_CHAR && (cp = strchr(opts, c)) != NULL
					&& cp[1] == ':' && argv[i][2] == 0 && i < argc - 1) {
					int j;
					char *temp1 = argv[i];
					char *temp2 = argv[i+1];
					for (j = i - 1; j >= optind; j--)
						argv[j+2] = argv[j];
					argv[optind] = temp1;
					argv[optind+1] = temp2;
				}
				else {
					int j;
					char *temp = argv[i];
					for (j = i - 1; j >= optind; j--)
						argv[j+1] = argv[j];
					argv[optind] = temp;
				}
			}
			else
#endif
				return EOF;
		}
		if ((argv[optind][0] == OPTION_CHAR && argv[optind][1] == OPTION_CHAR
				  && argv[optind][2] == '\0')
#ifdef SWITCHAR
			|| (argv[optind][0] == switchar && argv[optind][1] == switchar
				&& argv[optind][2] == '\0')
#endif
			) {
			optind++;
			return(EOF);
		}
	}
	optopt = c = argv[optind][sp];
#ifdef CASE_INSENSITIVE_OPTIONS
	if (
#ifdef USE_ISASCII
		isascii(c) &&
#endif /* USE_ISASCII */
		isupper((unsigned char)c))
		optopt = c = tolower((unsigned char)c);
#endif /* CASE_INSENSITIVE_OPTIONS */
	if (c == ':' || (cp = strchr(opts, c)) == NULL) {
		if (argv[optind][++sp] == '\0') {
			optind++;
			sp = 1;
		}
		message = ": illegal option -- ";
		goto bad;
	}
	if (*++cp == ':') {
		if (argv[optind][sp+1] != '\0')
			optarg = &argv[optind++][sp+1];
		else if (++optind >= argc) {
			sp = 1;
			message = ": option requires an argument -- ";
			goto bad;
		}
		else
			optarg = argv[optind++];
		sp = 1;
	} 
	else {
		if (argv[optind][++sp] == '\0') {
			sp = 1;
			optind++;
		}
		optarg = NULL;
	}
	return c;
bad:
	if (opterr) {
		fputs(argv[0], stderr);
		fputs(message, stderr);
		fputc(optopt, stderr);
		fputc('\n', stderr);
	}
	return '?';
}

/*
Local Variables:
c-indent-level: 4
c-continued-statement-offset: 4
c-brace-offset: 4
c-argdecl-indent: 4
c-label-offset: -4
tab-width: 4
End:
*/