File: getopt.c

package info (click to toggle)
rc 1.7.1-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 856 kB
  • ctags: 814
  • sloc: ansic: 7,121; sh: 714; yacc: 124; makefile: 94
file content (51 lines) | stat: -rw-r--r-- 1,151 bytes parent folder | download | duplicates (7)
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
#include "rc.h"

int     rc_opterr = 1;
int     rc_optind = 1;
int     rc_optopt;
char    *rc_optarg;

/* getopt routine courtesy of David Sanderson */
 
extern int rc_getopt(int argc, char **argv, char *opts) {
        static int sp = 1;
	int c;
	char *cp;
	if (rc_optind == 0) /* reset rc_getopt() */
		rc_optind = sp = 1;
	if (sp == 1) {
		if (rc_optind >= argc || argv[rc_optind][0] != '-' || argv[rc_optind][1] == '\0') {
			return -1;
		} else if (strcmp(argv[rc_optind], "--") == 0) {
			rc_optind++;
			return -1;
		}
	}
	rc_optopt = c = argv[rc_optind][sp];
	if (c == ':' || (cp=strchr(opts, c)) == 0) {
		fprint(2, "%s: bad option: -%c\n", argv[0], c);
		if (argv[rc_optind][++sp] == '\0') {
			rc_optind++;
			sp = 1;
		}
		return '?';
	}
	if (*++cp == ':') {
		if (argv[rc_optind][sp+1] != '\0') {
			rc_optarg = &argv[rc_optind++][sp+1];
		} else if (++rc_optind >= argc) {
			fprint(2, "%s: option requires an argument -- %c\n", argv[0], c);
			sp = 1;
			return '?';
		} else
			rc_optarg = argv[rc_optind++];
		sp = 1;
	} else {
		if (argv[rc_optind][++sp] == '\0') {
			sp = 1;
			rc_optind++;
		}
		rc_optarg = NULL;
	}
	return c;
}