|
|
1.1 ! root 1: .\" Copyright (c) 1988 Regents of the University of California. ! 2: .\" All rights reserved. ! 3: .\" ! 4: .\" Redistribution and use in source and binary forms are permitted provided ! 5: .\" that: (1) source distributions retain this entire copyright notice and ! 6: .\" comment, and (2) distributions including binaries display the following ! 7: .\" acknowledgement: ``This product includes software developed by the ! 8: .\" University of California, Berkeley and its contributors'' in the ! 9: .\" documentation or other materials provided with the distribution and in ! 10: .\" all advertising materials mentioning features or use of this software. ! 11: .\" Neither the name of the University nor the names of its contributors may ! 12: .\" be used to endorse or promote products derived from this software without ! 13: .\" specific prior written permission. ! 14: .\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED ! 15: .\" WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF ! 16: .\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ! 17: .\" ! 18: .\" @(#)getopt.3 6.15 (Berkeley) 6/23/90 ! 19: .\" ! 20: .TH GETOPT 3 "June 23, 1990" ! 21: .UC 6 ! 22: .SH NAME ! 23: getopt \- get option letter from argv ! 24: .SH SYNOPSIS ! 25: .ft B ! 26: .nf ! 27: int getopt(argc, argv, optstring) ! 28: int argc; ! 29: char **argv; ! 30: char *optstring; ! 31: .sp ! 32: extern char *optarg; ! 33: extern int optind; ! 34: extern int opterr; ! 35: .ft ! 36: .SH DESCRIPTION ! 37: .I Getopt ! 38: returns the next option letter in ! 39: .I argv ! 40: that matches a letter in ! 41: .IR optstring . ! 42: .I Optstring ! 43: is a string of recognized option letters; if a letter is followed by a ! 44: colon, the option is expected to have an argument that may or may not ! 45: be separated from it by white space. ! 46: .PP ! 47: On return from ! 48: .IR getopt , ! 49: optarg is set to point to the start of any option argument. ! 50: .I Optind ! 51: contains the ! 52: .I argv ! 53: index of the next argument to be processed. ! 54: .PP ! 55: .I Opterr ! 56: and ! 57: .I optind ! 58: are both initialized to 1. ! 59: In order to use ! 60: .I getopt ! 61: to evaluate multiple sets of arguments, or to evaluate a single set of ! 62: arguments multiple times, ! 63: .I optind ! 64: must be initialized to the number of argv entries to be skipped in each ! 65: evaluation. ! 66: .PP ! 67: When all options have been processed (i.e., up to the first non-option ! 68: argument), ! 69: .I getopt ! 70: returns EOF. ! 71: The special option ``\-\-'' may be used to delimit the end of the options; ! 72: EOF will be returned, and the ``\-\-'' will be skipped. ! 73: .SH DIAGNOSTICS ! 74: .I Getopt ! 75: prints an error message on ! 76: .I stderr ! 77: and returns a question mark (``?'') when it encounters an option ! 78: letter not included in ! 79: .IR optstring , ! 80: or it encounters an option that requires an argument which is not ! 81: supplied. ! 82: Setting ! 83: .I opterr ! 84: to a zero will disable these error messages. ! 85: .SH EXAMPLE ! 86: .nf ! 87: .in +5 ! 88: extern char *optarg; ! 89: extern int optind; ! 90: int bflag, ch, fd; ! 91: ! 92: bflag = 0; ! 93: while ((ch = getopt(argc, argv, "bf:")) != EOF) ! 94: switch(ch) { ! 95: case 'b': ! 96: bflag = 1; ! 97: break; ! 98: case 'f': ! 99: if ((fd = open(optarg, O_RDONLY, 0)) < 0) { ! 100: (void)fprintf(stderr, ! 101: "myname: unable to read file %s.\en", optarg); ! 102: exit(1); ! 103: } ! 104: break; ! 105: case '?': ! 106: default: ! 107: usage(); ! 108: } ! 109: argc -= optind; ! 110: argv += optind; ! 111: .fi ! 112: .SH BUGS ! 113: Option arguments are allowed to begin with ``\-''; this is reasonable but ! 114: reduces the amount of error checking possible. ! 115: .PP ! 116: A single dash (``-'') may be specified as an character in ! 117: .IR optstring , ! 118: however it should ! 119: .B never ! 120: have an argument associated with it. ! 121: This allows ! 122: .I getopt ! 123: to be used with programs that expect ``-'' as an option flag. ! 124: This practice is wrong, and should not be used in any current development. ! 125: It is provided for backward compatibility ! 126: .BR only . ! 127: By default, a single dash causes ! 128: .I getopt ! 129: to return EOF. ! 130: This is, we believe, compatible with System V. ! 131: .PP ! 132: It is also possible to handle digits as option letters. ! 133: This allows ! 134: .I getopt ! 135: to be used with programs that expect a number (``-3'') as an option. ! 136: This practice is wrong, and should not be used in any current development. ! 137: It is provided for backward compatibility ! 138: .BR only . ! 139: The following code fragment works fairly well. ! 140: .sp ! 141: .nf ! 142: .in +5 ! 143: int length; ! 144: char *p; ! 145: ! 146: while ((c = getopt(argc, argv, "0123456789")) != EOF) ! 147: switch (c) { ! 148: case '0': case '1': case '2': case '3': case '4': ! 149: case '5': case '6': case '7': case '8': case '9': ! 150: p = argv[optind - 1]; ! 151: if (p[0] == '-' && p[1] == ch && !p[2]) ! 152: length = atoi(++p); ! 153: else ! 154: length = atoi(argv[optind] + 1); ! 155: break; ! 156: } ! 157: } ! 158: .fi
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.