|
|
1.1 ! root 1: ! 2: ! 3: getopt() General Function getopt() ! 4: ! 5: ! 6: ! 7: ! 8: Get option letter from argv ! 9: ! 10: iinntt ggeettoopptt(_a_r_g_c, _a_r_g_v, _o_p_t_s_t_r_i_n_g) ! 11: iinntt _a_r_g_c; ! 12: cchhaarr **_a_r_g_v; ! 13: cchhaarr *_o_p_t_s_t_r_i_n_g; ! 14: eexxtteerrnn cchhaarr *_o_p_t_a_r_g; ! 15: eexxtteerrnn iinntt _o_p_t_i_n_d; ! 16: ! 17: getopt returns the next option letter in argv that matches a let- ! 18: ter in optstring. optstring is a string of recognized option ! 19: letters. If a letter is followed by a colon, the option is ex- ! 20: pected to have an argument, which may or may not be separated ! 21: from it by white space. optarg is set to point to the start of ! 22: the option argument on return from getopt. ! 23: ! 24: getopt places into optind the argv index of the next argument to ! 25: be processed. Because optind is external, it is normally in- ! 26: itialized to zero automatically before the first call to getopt. ! 27: ! 28: When all options have been processed (i.e., up to the first non- ! 29: option argument), getopt returns EOF. The special option -- may ! 30: be used to delimit the end of the options: EOF will be returned, ! 31: and -- will be skipped. ! 32: ! 33: ***** Example ***** ! 34: ! 35: The following code fragment shows how one might process the ar- ! 36: guments for a command that can take the mutually exclusive op- ! 37: tions a and b, and the options f and o, both of which require ar- ! 38: guments: ! 39: ! 40: ! 41: main(argc, argv) ! 42: int argc; ! 43: char **argv; ! 44: { ! 45: int c; ! 46: extern int optind; ! 47: extern char *optarg; ! 48: ! 49: ! 50: ! 51: . ! 52: . ! 53: . ! 54: ! 55: ! 56: ! 57: while ((c = getopt(argc, argv, "abf:o:")) != EOF) ! 58: switch (c) { ! 59: ! 60: ! 61: ! 62: ! 63: ! 64: COHERENT Lexicon Page 1 ! 65: ! 66: ! 67: ! 68: ! 69: getopt() General Function getopt() ! 70: ! 71: ! 72: ! 73: ! 74: case 'a': ! 75: if (bflg) ! 76: errflg++; ! 77: else ! 78: aflg++; ! 79: break; ! 80: ! 81: ! 82: ! 83: case 'b': ! 84: if (aflg) ! 85: errflg++; ! 86: else ! 87: bflg++; ! 88: break; ! 89: ! 90: ! 91: ! 92: case 'f': ! 93: ifile = optarg; ! 94: break; ! 95: ! 96: ! 97: ! 98: case 'o': ! 99: ofile = optarg; ! 100: break; ! 101: ! 102: ! 103: ! 104: case '?': ! 105: default: ! 106: errflg++; ! 107: break; ! 108: } ! 109: ! 110: ! 111: ! 112: if (errflg) { ! 113: fprintf(stderr, "Usage: ..."); ! 114: exit(2); ! 115: } ! 116: ! 117: ! 118: ! 119: for (; optind < argc; optind++) { ! 120: . ! 121: . ! 122: . ! 123: } ! 124: . ! 125: . ! 126: . ! 127: } ! 128: ! 129: ! 130: COHERENT Lexicon Page 2 ! 131: ! 132: ! 133: ! 134: ! 135: getopt() General Function getopt() ! 136: ! 137: ! 138: ! 139: ! 140: ! 141: ***** See Also ***** ! 142: ! 143: general functions ! 144: ! 145: ***** Diagnostics ***** ! 146: ! 147: getopt prints an error message on stderr and returns a question ! 148: mark when it encounters an option letter not included in ! 149: optstring. ! 150: ! 151: ***** Notes ***** ! 152: ! 153: It is not obvious how `-' standing alone should be treated. This ! 154: version treats it as a non-option argument, which is not always ! 155: right. ! 156: ! 157: Option arguments are allowed to begin with `-'. This is reason- ! 158: able, but reduces the amount of error checking possible. ! 159: ! 160: ! 161: ! 162: ! 163: ! 164: ! 165: ! 166: ! 167: ! 168: ! 169: ! 170: ! 171: ! 172: ! 173: ! 174: ! 175: ! 176: ! 177: ! 178: ! 179: ! 180: ! 181: ! 182: ! 183: ! 184: ! 185: ! 186: ! 187: ! 188: ! 189: ! 190: ! 191: ! 192: ! 193: ! 194: ! 195: ! 196: COHERENT Lexicon Page 3 ! 197: ! 198:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.