BSD 4_4_Lite1 development
[unix-history] / usr / src / contrib / rc-1.4 / getopt.c
CommitLineData
9d25673b
C
1#include "rc.h"
2
3int rc_opterr = 1;
4int rc_optind = 1;
5int rc_optopt;
6char *rc_optarg;
7
8/* getopt routine courtesy of David Sanderson */
9
10extern int rc_getopt(int argc, char **argv, char *opts) {
11 static int sp = 1;
12 int c;
13 char *cp;
14 if (rc_optind == 0) /* reset rc_getopt() */
15 rc_optind = sp = 1;
16 if (sp == 1)
17 if (rc_optind >= argc || argv[rc_optind][0] != '-' || argv[rc_optind][1] == '\0') {
18 return -1;
19 } else if (strcmp(argv[rc_optind], "--") == 0) {
20 rc_optind++;
21 return -1;
22 }
23 rc_optopt = c = argv[rc_optind][sp];
24 if (c == ':' || (cp=strchr(opts, c)) == 0) {
25 fprint(2, "%s: bad option: -%c\n", argv[0], c);
26 if (argv[rc_optind][++sp] == '\0') {
27 rc_optind++;
28 sp = 1;
29 }
30 return '?';
31 }
32 if (*++cp == ':') {
33 if (argv[rc_optind][sp+1] != '\0') {
34 rc_optarg = &argv[rc_optind++][sp+1];
35 } else if (++rc_optind >= argc) {
36 fprint(2, "%s: option requires an argument -- %c\n", argv[0], c);
37 sp = 1;
38 return '?';
39 } else
40 rc_optarg = argv[rc_optind++];
41 sp = 1;
42 } else {
43 if (argv[rc_optind][++sp] == '\0') {
44 sp = 1;
45 rc_optind++;
46 }
47 rc_optarg = NULL;
48 }
49 return c;
50}