|
|
1.1 root 1: #include <stdio.h>
2: #define ERR(str, chr) if(opterr){fprintf(stderr, "%s%s%c\n", argv[0], str, chr);}
3: int opterr = 1;
4: int optind = 1;
5: int optopt;
6: char *optarg;
7: char *strchr();
8:
9: int
10: getopt (argc, argv, opts)
11: char **argv, *opts;
12: {
13: static int sp = 1;
14: register c;
15: register char *cp;
16:
17: if (sp == 1)
18: if (optind >= argc ||
19: argv[optind][0] != '-' || argv[optind][1] == '\0')
20: return EOF;
21: else if (strcmp(argv[optind], "--") == NULL) {
22: optind++;
23: return EOF;
24: }
25: optopt = c = argv[optind][sp];
26: if (c == ':' || (cp=strchr(opts, c)) == NULL) {
27: ERR (": illegal option -- ", c);
28: if (argv[optind][++sp] == '\0') {
29: optind++;
30: sp = 1;
31: }
32: return '?';
33: }
34: if (*++cp == ':') {
35: if (argv[optind][sp+1] != '\0')
36: optarg = &argv[optind++][sp+1];
37: else if (++optind >= argc) {
38: ERR (": option requires an argument -- ", c);
39: sp = 1;
40: return '?';
41: } else
42: optarg = argv[optind++];
43: sp = 1;
44: } else {
45: if (argv[optind][++sp] == '\0') {
46: sp = 1;
47: optind++;
48: }
49: optarg = NULL;
50: }
51: return c;
52: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.