|
|
1.1 ! root 1: #ifndef lint ! 2: static char NFSTestID[] = "@(#)getopt.c 1.1 Lachman ONC Test Suite source"; ! 3: #endif /* lint */ ! 4: /* ! 5: * Here's something you've all been waiting for: the AT&T public domain ! 6: * source for getopt(3). It is the code which was given out at the 1985 ! 7: * UNIFORUM conference in Dallas. I obtained it by electronic mail ! 8: * directly from AT&T. The people there assure me that it is indeed ! 9: * in the public domain. ! 10: * ! 11: * There is no manual page. That is because the one they gave out at ! 12: * UNIFORUM was slightly different from the current System V Release 2 ! 13: * manual page. The difference apparently involved a note about the ! 14: * famous rules 5 and 6, recommending using white space between an option ! 15: * and its first argument, and not grouping options that have arguments. ! 16: * Getopt itself is currently lenient about both of these things White ! 17: * space is allowed, but not mandatory, and the last option in a group can ! 18: * have an argument. That particular version of the man page evidently ! 19: * has no official existence, and my source at AT&T did not send a copy. ! 20: * The current SVR2 man page reflects the actual behavor of this getopt. ! 21: * However, I am not about to post a copy of anything licensed by AT&T. ! 22: */ ! 23: ! 24: ! 25: /*LINTLIBRARY*/ ! 26: #define NULL 0 ! 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: #ifdef SVR3 ! 37: extern char *strchr(); ! 38: #else ! 39: extern char *index(); ! 40: #endif ! 41: ! 42: int opterr = 1; ! 43: int optind = 1; ! 44: int optopt; ! 45: char *optarg; ! 46: ! 47: int ! 48: getopt(argc, argv, opts) ! 49: int argc; ! 50: char **argv, *opts; ! 51: { ! 52: static int sp = 1; ! 53: register int c; ! 54: register char *cp; ! 55: ! 56: if(sp == 1) ! 57: if(optind >= argc || ! 58: argv[optind][0] != '-' || argv[optind][1] == '\0') ! 59: return(EOF); ! 60: else if(strcmp(argv[optind], "--") == NULL) { ! 61: optind++; ! 62: return(EOF); ! 63: } ! 64: optopt = c = argv[optind][sp]; ! 65: #ifdef SVR3 ! 66: if(c == ':' || (cp=strchr(opts, c)) == NULL) { ! 67: #else ! 68: if(c == ':' || (cp=index(opts, c)) == NULL) { ! 69: #endif ! 70: ERR(": illegal option -- ", c); ! 71: if(argv[optind][++sp] == '\0') { ! 72: optind++; ! 73: sp = 1; ! 74: } ! 75: return('?'); ! 76: } ! 77: if(*++cp == ':') { ! 78: if(argv[optind][sp+1] != '\0') ! 79: optarg = &argv[optind++][sp+1]; ! 80: else if(++optind >= argc) { ! 81: ERR(": option requires an argument -- ", c); ! 82: sp = 1; ! 83: return('?'); ! 84: } else ! 85: optarg = argv[optind++]; ! 86: sp = 1; ! 87: } else { ! 88: if(argv[optind][++sp] == '\0') { ! 89: sp = 1; ! 90: optind++; ! 91: } ! 92: optarg = NULL; ! 93: } ! 94: return(c); ! 95: } ! 96: ! 97: #include <stdio.h> ! 98: ! 99: main(ac, av) ! 100: char **av; ! 101: { ! 102: register int i; ! 103: int first = 1; ! 104: int error = 0; ! 105: char buf[BUFSIZ]; ! 106: char line[BUFSIZ]; ! 107: extern char *optarg; ! 108: extern int optind; ! 109: ! 110: if (ac == 1) { ! 111: fprintf(stderr, "usage: getopt legal-args $*\n"); ! 112: exit(2); ! 113: } ! 114: ! 115: line[0] = '\0'; ! 116: while ((i = getopt(ac - 1, &av[1], av[1])) != EOF) { ! 117: if (i == '?') ! 118: exit(2); ! 119: ! 120: if (first) { ! 121: first = 0; ! 122: sprintf(buf, "-%c", i & 0xff); ! 123: } ! 124: else ! 125: sprintf(buf, " -%c", i & 0xff); ! 126: strcat(line, buf); ! 127: if (optarg) { ! 128: sprintf(buf, " %s", optarg); ! 129: strcat(line, buf); ! 130: } ! 131: } ! 132: ! 133: if (first) ! 134: strcat(line, "--"); ! 135: else ! 136: strcat(line, " --"); ! 137: ! 138: optind++; ! 139: for (; optind < ac; optind++) { ! 140: sprintf(buf, " %s", av[optind]); ! 141: strcat(line, buf); ! 142: } ! 143: printf("%s\n", line); ! 144: exit(0); ! 145: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.