getopt(3C) (MLX Addendum) (MLX) getopt(3C) (MLX Addendum)
NAME
getopt - get option letter from argument vector (MLX addendum)
SYNOPSIS
#include <stdio.h>
int getopt(int argc, char *const *argv, const char *optstring);
extern char *optarg;
extern int optind, opterr;
DESCRIPTION
The function getopt() is a command-line parser. It returns the next
option letter in argv that matches a letter in optstring.
The function getopt() places in optind the argv index of the next
argument to be processed. The external variable optind is initialized
to 1 before the first call to the function getopt().
The argument optstring is a string of recognized option letters; if a
letter is followed by a colon, the option is expected to have an argu-
ment that may be separated from it by white space.
The variable optarg is set to point to the start of the option argu-
ment on return from getopt().
When all options have been processed (i.e., up to the first non-option
argument), the function getopt() returns EOF. The special option --
may be used to delimit the end of the options; EOF will be returned
and -- will be skipped.
The following rules comprise the Reliant UNIX standard for command-
line syntax:
RULE 1: Command names must be between two and nine characters.
RULE 2: Command names must include lower-case letters and digits
only.
RULE 3: Option names must be a single character in length.
RULE 4: All options must be delimited by the - character.
RULE 5: Options with no arguments may be grouped behind one delim-
iter.
RULE 6: The first option-argument following an option may be pre-
ceded by white space.
RULE 7: Option arguments cannot be optional.
RULE 8: Groups of option arguments following an option must be
separated by commas or separated by white space and quoted.
Page 1 Reliant UNIX 5.44 6, 198
getopt(3C) (MLX Addendum) (MLX) getopt(3C) (MLX Addendum)
RULE 9: All options must precede operands on the command line.
RULE 10: The characters -- may be used to delimit the end of the
options.
RULE 11: The order of options relative to one another should not
matter.
RULE 12: The order of operands may matter and position-related
interpretations should be determined on a command-specific
basis.
RULE 13: The - character preceded and followed by white space should
be used only to mean standard input.
RETURN VALUE
The function getopt() returns a question mark (?) when it encounters
an option letter not included in optstring; it also prints an error
message on stderr if opterr is non-zero (opterr is initialized to 1).
The message is printed in the standard error format. getopt() supports
localized output messages. If the appropriate translated system mes-
sages are installed on the system, they are selected by the most
recent call to setlocale() (using the LC_ALL or LC_MESSAGES
categories).
The label defined by a call to setlabel() will be used if available,
otherwise the name of the utility (argv[0]) will be used.
EXAMPLE
The following code fragment shows how one might process the options
and arguments for a command that takes: mutually exclusive options a
and b, exactly one of which is required; an optional option i which
takes an option-argument; and at least two arguments.
main(int argc, char *argv[] /*, char envp[]*/)
/* envp is unused in this example */
{
int opt, aflg=0, bflg=0, iflg=0, errflg=0, retval ;
char *cmdname, *ifile, *ofile ;
FILE *infile, *outfile ;
extern int optind, opterr, errno ;
extern char *optarg ;
setlabel("UX:example");
cmdname = argv[0] ;
opterr = 0 ; /* inhibit getopt err msg */
while ( (opt=getopt(argc,argv,"abi:")) != EOF ) {
switch ( opt ) {
case 'a' :
aflg += 1 ; break ;
case 'b' :
bflg += 1 ; break ;
Page 2 Reliant UNIX 5.44 6, 198
getopt(3C) (MLX Addendum) (MLX) getopt(3C) (MLX Addendum)
case 'i' :
iflg += 1 ; ifile = optarg ; break ;
default : /* includes '?' case */
errflg += 1 ; break ;
}
}
if ( errflg>0 || aflg+bflg!=1 || iflg>1 || argc-optind<2 ) {
usage_err_exit(cmdname) ;
}
(continues)
Page 3 Reliant UNIX 5.44 6, 198
getopt(3C) (MLX Addendum) (MLX) getopt(3C) (MLX Addendum)
if ( iflg == 0 ) {
infile = stdin ;
} else if ( (infile=fopen(ifile,"r")) == NULL ) {
open_err_exit(cmdname,ifile,errno) ;
}
for ( ; optind<argc ; optind+=1 ) {
if ( (outfile=fopen(ofile=argv[optind],"r+")) == NULL ) {
open_err_exit(cmdname,ofile,errno) ;
}
if ( (retval=do_work(aflg,bflg,infile,outfile)) != 0 ) {
work_err_exit(cmdname,ofile,retval) ;
}
if ( fclose(outfile) != 0 ) {
close_err_exit(cmdname,ofile,errno) ;
}
}
exit(0) ;
}
SEE ALSO
pfmt(3C), setlabel(3C).
Page 4 Reliant UNIX 5.44 6, 198