.\" Copyright (c) 1988 Regents of the University of California.
.\" Redistribution and use in source and binary forms are permitted
.\" provided that the above copyright notice and this paragraph are
.\" duplicated in all such forms and that any documentation,
.\" advertising materials, and other materials related to such
.\" distribution and use acknowledge that the software was developed
.\" by the University of California, Berkeley. The name of the
.\" University may not be used to endorse or promote products derived
.\" from this software without specific prior written permission.
.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
.\" IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
.\" WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
.\" @(#)getopt.3 6.13 (Berkeley) %G%
getopt \- get option letter from argv
int getopt(argc, argv, optstring)
returns the next option letter in
is a string of recognized option letters; if a letter is followed by a
colon, the option is expected to have an argument that may or may not
be separated from it by white space.
optarg is set to point to the start of any option argument.
index of the next argument to be processed.
are both initialized to 1.
When all options have been processed (i.e., up to the first non-option
The special option ``\-\-'' may be used to delimit the end of the options;
EOF will be returned, and the ``\-\-'' will be skipped.
prints an error message on
and returns a question mark (``?'') when it encounters an option
or it encounters an option that requires an argument which is not
to a zero will disable these error messages.
while ((ch = getopt(argc, argv, "bf:")) != EOF)
if ((fd = open(optarg, O_RDONLY, 0)) < 0) {
"myname: unable to read file %s.\en", optarg);
Option arguments are allowed to begin with ``\-''; this is reasonable but
reduces the amount of error checking possible.
A single dash (``-'') may be specified as an character in
have an argument associated with it.
to be used with programs that expect ``-'' as an option flag.
This practice is wrong, and should not be used in any current development.
It is provided for backward compatibility
By default, a single dash causes
This is, we believe, compatible with System V.
It is also possible to handle digits as option letters.
to be used with programs that expect a number (``-3'') as an option.
This practice is wrong, and should not be used in any current development.
It is provided for backward compatibility
The following code fragment works fairly well.
while ((c = getopt(argc, argv, "0123456789")) != EOF)
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
if (p[0] == '-' && p[1] == ch && !p[2])
length = atoi(argv[optind] + 1);