|
|
1.1.1.2 ! root 1: /* ! 2: ** @(#)getopt.c 2.5 (smail) 9/15/87 ! 3: */ ! 4: ! 5: /* ! 6: * This is the AT&T public domain source for getopt(3). It is the code ! 7: * which was given out at the 1985 UNIFORUM conference in Dallas. ! 8: * ! 9: * There is no manual page. That is because the one they gave out at ! 10: * UNIFORUM was slightly different from the current System V Release 2 ! 11: * manual page. The difference apparently involved a note about the ! 12: * famous rules 5 and 6, recommending using white space between an ! 13: * option and its first argument, and not grouping options that have ! 14: * arguments. Getopt itself is currently lenient about both of these ! 15: * things. White space is allowed, but not mandatory, and the last option ! 16: * in a group can have an argument. That particular version of the man ! 17: * page evidently has no official existence. The current SVR2 man page ! 18: * reflects the actual behavor of this getopt. ! 19: */ ! 20: ! 21: #include <string.h> ! 22: ! 23: /*LINTLIBRARY*/ ! 24: #ifndef NULL ! 25: #define NULL 0 ! 26: #endif ! 27: #define EOF (-1) ! 28: #define ERR(s, c) if(opterr){\ ! 29: extern int write();\ ! 30: char errbuf[2];\ ! 31: errbuf[0] = c; errbuf[1] = '\n';\ ! 32: (void) write(2, argv[0], (unsigned)strlen(argv[0]));\ ! 33: (void) write(2, s, (unsigned)strlen(s));\ ! 34: (void) write(2, errbuf, 2);} ! 35: ! 36: int opterr = 1; ! 37: int optind = 1; ! 38: int optopt = 0; ! 39: char *optarg = 0; ! 40: ! 41: int ! 42: getopt(argc, argv, opts) ! 43: int argc; ! 44: char **argv, *opts; ! 45: { ! 46: static int sp = 1; ! 47: register int c; ! 48: register char *cp; ! 49: ! 50: if(sp == 1) { ! 51: if(optind >= argc || (argv[optind][0] != '+' && ! 52: argv[optind][0] != '-') || argv[optind][1] == '\0') ! 53: return(EOF); ! 54: else if(strcmp(argv[optind], "--") == 0) { ! 55: optind++; ! 56: return(EOF); ! 57: } ! 58: /* '+' for config options, '+' should not be in the opts list */ ! 59: if (argv[optind][0] == '+') { ! 60: optarg = argv[optind++] + 1; ! 61: return '+'; ! 62: } ! 63: } ! 64: optopt = c = argv[optind][sp]; ! 65: if(c == ':' || (cp=strchr(opts, c)) == NULL) { ! 66: ERR(": illegal option -- ", c); ! 67: if(argv[optind][++sp] == '\0') { ! 68: optind++; ! 69: sp = 1; ! 70: } ! 71: return('\0'); ! 72: } ! 73: if(*++cp == ':') { ! 74: if(argv[optind][sp+1] != '\0') ! 75: optarg = &argv[optind++][sp+1]; ! 76: else if(++optind >= argc) { ! 77: ERR(": option requires an argument -- ", c); ! 78: sp = 1; ! 79: return('\0'); ! 80: } else ! 81: optarg = argv[optind++]; ! 82: sp = 1; ! 83: } else { ! 84: if(argv[optind][++sp] == '\0') { ! 85: sp = 1; ! 86: optind++; ! 87: } ! 88: optarg = NULL; ! 89: } ! 90: return(c); ! 91: } ! 92:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.