Make all bucket and overflow addresses unsigned
[unix-history] / usr / src / lib / libc / stdlib / getopt.3
CommitLineData
006fcfe1
KB
1.\" Copyright (c) 1988 Regents of the University of California.
2.\" All rights reserved.
53e9b2ee 3.\"
91cff1e1 4.\" %sccs.include.redist.man%
006fcfe1 5.\"
91cff1e1 6.\" @(#)getopt.3 6.15 (Berkeley) %G%
53e9b2ee
KM
7.\"
8.TH GETOPT 3 ""
9.UC 6
10.SH NAME
11getopt \- get option letter from argv
12.SH SYNOPSIS
13.ft B
09885840 14.nf
53e9b2ee 15int getopt(argc, argv, optstring)
53e9b2ee 16int argc;
53e9b2ee 17char **argv;
53e9b2ee
KM
18char *optstring;
19.sp
20extern char *optarg;
53e9b2ee 21extern int optind;
0b64d222 22extern int opterr;
53e9b2ee
KM
23.ft
24.SH DESCRIPTION
25.I Getopt
26returns the next option letter in
27.I argv
28that matches a letter in
29.IR optstring .
30.I Optstring
09885840
KB
31is a string of recognized option letters; if a letter is followed by a
32colon, the option is expected to have an argument that may or may not
33be separated from it by white space.
53e9b2ee 34.PP
09885840
KB
35On return from
36.IR getopt ,
37optarg is set to point to the start of any option argument.
38.I Optind
39contains the
53e9b2ee
KM
40.I argv
41index of the next argument to be processed.
09885840
KB
42.PP
43.I Opterr
44and
53e9b2ee 45.I optind
09885840 46are both initialized to 1.
8fa9b88f
KB
47In order to use
48.I getopt
49to evaluate multiple sets of arguments, or to evaluate a single set of
50arguments multiple times,
51.I optind
52must be initialized to the number of argv entries to be skipped in each
53evaluation.
53e9b2ee 54.PP
09885840
KB
55When all options have been processed (i.e., up to the first non-option
56argument),
53e9b2ee 57.I getopt
09885840
KB
58returns EOF.
59The special option ``\-\-'' may be used to delimit the end of the options;
60EOF will be returned, and the ``\-\-'' will be skipped.
53e9b2ee
KM
61.SH DIAGNOSTICS
62.I Getopt
63prints an error message on
64.I stderr
09885840
KB
65and returns a question mark (``?'') when it encounters an option
66letter not included in
67.IR optstring ,
68or it encounters an option that requires an argument which is not
69supplied.
70Setting
71.I opterr
72to a zero will disable these error messages.
53e9b2ee 73.SH EXAMPLE
53e9b2ee 74.nf
09885840
KB
75.in +5
76extern char *optarg;
77extern int optind;
78int bflag, ch, fd;
79
80bflag = 0;
81while ((ch = getopt(argc, argv, "bf:")) != EOF)
82 switch(ch) {
83 case 'b':
84 bflag = 1;
85 break;
86 case 'f':
87 if ((fd = open(optarg, O_RDONLY, 0)) < 0) {
88 (void)fprintf(stderr,
89 "myname: unable to read file %s.\en", optarg);
90 exit(1);
53e9b2ee 91 }
09885840
KB
92 break;
93 case '?':
94 default:
95 usage();
53e9b2ee 96 }
09885840
KB
97argc -= optind;
98argv += optind;
99.fi
53e9b2ee 100.SH BUGS
09885840
KB
101Option arguments are allowed to begin with ``\-''; this is reasonable but
102reduces the amount of error checking possible.
53e9b2ee 103.PP
09885840
KB
104A single dash (``-'') may be specified as an character in
105.IR optstring ,
106however it should
107.B never
108have an argument associated with it.
109This allows
110.I getopt
111to be used with programs that expect ``-'' as an option flag.
112This practice is wrong, and should not be used in any current development.
113It is provided for backward compatibility
114.BR only .
dd71e7d8
KB
115By default, a single dash causes
116.I getopt
117to return EOF.
118This is, we believe, compatible with System V.
09885840
KB
119.PP
120It is also possible to handle digits as option letters.
121This allows
122.I getopt
123to be used with programs that expect a number (``-3'') as an option.
124This practice is wrong, and should not be used in any current development.
125It is provided for backward compatibility
126.BR only .
127The following code fragment works fairly well.
128.sp
7edf3dad 129.nf
09885840
KB
130.in +5
131int length;
132char *p;
7edf3dad 133
09885840
KB
134while ((c = getopt(argc, argv, "0123456789")) != EOF)
135 switch (c) {
136 case '0': case '1': case '2': case '3': case '4':
137 case '5': case '6': case '7': case '8': case '9':
138 p = argv[optind - 1];
139 if (p[0] == '-' && p[1] == ch && !p[2])
140 length = atoi(++p);
141 else
142 length = atoi(argv[optind] + 1);
143 break;
7edf3dad 144 }
09885840 145}
7edf3dad 146.fi