1*61180Sbostic.\" Copyright (c) 1988, 1991, 1993 2*61180Sbostic.\" The Regents of the University of California. All rights reserved. 320617Smckusick.\" 443571Strent.\" %sccs.include.redist.man% 520617Smckusick.\" 6*61180Sbostic.\" @(#)getopt.3 8.1 (Berkeley) 06/04/93 733992Sbostic.\" 848349Scael.Dd 948349Scael.Dt GETOPT 3 1048349Scael.Os BSD 4.3 1148349Scael.Sh NAME 1248349Scael.Nm getopt 1358389Sbostic.Nd get option character from command line argument list 1448349Scael.Sh SYNOPSIS 1548349Scael.Fd #include <stdlib.h> 1648349Scael.Vt extern char *optarg 1748349Scael.Vt extern int optind 1858389Sbostic.Vt extern int optopt 1948349Scael.Vt extern int opterr 2054707Smarc.Vt extern int optreset 2148349Scael.Ft int 2248349Scael.Fn getopt "int argc" "char * const *argv" "const char *optstring" 2348349Scael.Sh DESCRIPTION 2448349ScaelThe 2548349Scael.Fn getopt 2658389Sbosticfunction incrementally parses a command line argument list 2758389Sbostic.Fa argv 2858389Sbosticand returns the next 2948349Scael.Em known 3058389Sbosticoption character. 3148349ScaelAn option character is 3248349Scael.Em known 3348349Scaelif it has been specified in the string of accepted option characters, 3448349Scael.Fa optstring . 3548349Scael.Pp 3648349ScaelThe option string 3748349Scael.Fa optstring 3858389Sbosticmay contain the following elements: individual characters, and 3958389Sbosticcharacters followed by a colon to indicate an option argument 4054707Smarcis to follow. 4158389SbosticFor example, an option string 4258389Sbostic.Li "\&""x"" 4358389Sbosticrecognizes an option 4458389Sbostic.Dq Fl x , 4558389Sbosticand an option string 4658389Sbostic.Li "\&""x:"" 4758389Sbosticrecognizes an option and argument 4858389Sbostic.Dq Fl x Ar argument . 4958389SbosticIt does not matter to 5048349Scael.Fn getopt 5148349Scaelif a following argument has leading white space. 5248349Scael.Pp 5339478SbosticOn return from 5448349Scael.Fn getopt , 5548349Scael.Va optarg 5648349Scaelpoints to an option argument, if it is anticipated, 5748349Scaeland the variable 5848349Scael.Va optind 5948349Scaelcontains the index to the next 6048349Scael.Fa argv 6148349Scaelargument for a subsequent call 6248349Scaelto 6348349Scael.Fn getopt . 6458389SbosticThe variable 6558389Sbostic.Va optopt 6658389Sbosticsaves the last 6758389Sbostic.Em known 6858389Sbosticoption character returned by 6958389Sbostic.Fn getopt . 7048349Scael.Pp 7148349ScaelThe variable 7248349Scael.Va opterr 7339478Sbosticand 7448349Scael.Va optind 7539478Sbosticare both initialized to 1. 7654707SmarcThe 7754707Smarc.Va optind 7854707Smarcvariable may be set to another value before a set of calls to 7954707Smarc.Fn getopt 8054707Smarcin order to skip over more or less argv entries. 8154707Smarc.Pp 8242262SbosticIn order to use 8348349Scael.Fn getopt 8442262Sbosticto evaluate multiple sets of arguments, or to evaluate a single set of 8542262Sbosticarguments multiple times, 8654707Smarcthe variable 8754707Smarc.Va optreset 8854707Smarcmust be set to 1 before the second and each additional set of calls to 8954707Smarc.Fn getopt , 9054707Smarcand the variable 9148349Scael.Va optind 9254707Smarcmust be reinitialized. 9348349Scael.Pp 9448349ScaelThe 9548349Scael.Fn getopt 9648349Scaelfunction 9748349Scaelreturns an 9848349Scael.Dv EOF 9948349Scaelwhen the argument list is exhausted, or a non-recognized 10048349Scaeloption is encountered. 10148349ScaelThe interpretation of options in the argument list may be cancelled 10248349Scaelby the option 10348349Scael.Ql -- 10448349Scael(double dash) which causes 10548349Scael.Fn getopt 10648349Scaelto signal the end of argument processing and return an 10748349Scael.Dv EOF . 10839478SbosticWhen all options have been processed (i.e., up to the first non-option 10939478Sbosticargument), 11048349Scael.Fn getopt 11148349Scaelreturns 11248349Scael.Dv EOF . 11348349Scael.Sh DIAGNOSTICS 11448349ScaelIf the 11548349Scael.Fn getopt 11648349Scaelfunction encounters a character not found in the string 11748349Scael.Va optarg 11848349Scaelor detects 11954707Smarca missing option argument it writes an error message and returns 12048349Scael.Ql ? 12148349Scaelto the 12248349Scael.Em stderr . 12339478SbosticSetting 12448349Scael.Va opterr 12539478Sbosticto a zero will disable these error messages. 12654707SmarcIf 12754707Smarc.Va optstring 12854707Smarchas a leading 12954707Smarc.Ql \&: 13054707Smarcthen then a missing option argumet causes a 13154707Smarc.Ql \&: 13254707Smarcto be returned in addition to supressing any error messages. 13354707Smarcoption argument 13454707Smarc.Pp 13554707SmarcOption arguments are allowed to begin with 13654707Smarc.Dq Li \- ; 13754707Smarcthis is reasonable but 13854707Smarcreduces the amount of error checking possible. 13954707Smarc.Sh EXTENSIONS 14054707SmarcThe 14154707Smarc.Va optreset 14254707Smarcvariable was added to make it possible to call the 14354707Smarc.Fn getopt 14454707Smarcfunction multiple times. 14554707SmarcThis is an extension to the 14654707Smarc.St -p1003.2 14754707Smarcspecification. 14848349Scael.Sh EXAMPLE 14948349Scael.Bd -literal -compact 15039478Sbosticextern char *optarg; 15139478Sbosticextern int optind; 15239478Sbosticint bflag, ch, fd; 15339478Sbostic 15439478Sbosticbflag = 0; 15539478Sbosticwhile ((ch = getopt(argc, argv, "bf:")) != EOF) 15639478Sbostic switch(ch) { 15739478Sbostic case 'b': 15839478Sbostic bflag = 1; 15939478Sbostic break; 16039478Sbostic case 'f': 16139478Sbostic if ((fd = open(optarg, O_RDONLY, 0)) < 0) { 16239478Sbostic (void)fprintf(stderr, 16354707Smarc "myname: %s: %s\en", optarg, strerror(errno)); 16454707Smarc exit(1); 16520617Smckusick } 16639478Sbostic break; 16739478Sbostic case '?': 16839478Sbostic default: 16939478Sbostic usage(); 17048349Scael} 17139478Sbosticargc -= optind; 17239478Sbosticargv += optind; 17348349Scael.Ed 17448349Scael.Sh HISTORY 17548349ScaelThe 17648349Scael.Fn getopt 17748349Scaelfunction appeared 17848349Scael.Bx 4.3 . 17948349Scael.Sh BUGS 18048349ScaelA single dash 18148349Scael.Dq Li - 18248349Scaelmay be specified as an character in 18348349Scael.Fa optstring , 18439478Sbostichowever it should 18548349Scael.Em never 18639478Sbostichave an argument associated with it. 18739478SbosticThis allows 18848349Scael.Fn getopt 18948349Scaelto be used with programs that expect 19048349Scael.Dq Li - 19148349Scaelas an option flag. 19239478SbosticThis practice is wrong, and should not be used in any current development. 19339478SbosticIt is provided for backward compatibility 19448349Scael.Em only . 19539712SbosticBy default, a single dash causes 19648349Scael.Fn getopt 19748349Scaelto return 19848349Scael.Dv EOF . 19939712SbosticThis is, we believe, compatible with System V. 20048349Scael.Pp 20139478SbosticIt is also possible to handle digits as option letters. 20239478SbosticThis allows 20348349Scael.Fn getopt 20448349Scaelto be used with programs that expect a number 20548349Scael.Pq Dq Li \&-\&3 20648349Scaelas an option. 20739478SbosticThis practice is wrong, and should not be used in any current development. 20839478SbosticIt is provided for backward compatibility 20948349Scael.Em only . 21054707SmarcThe following code fragment works in most cases. 21148349Scael.Bd -literal -offset indent 21239478Sbosticint length; 21339478Sbosticchar *p; 21435982Sbostic 21539478Sbosticwhile ((c = getopt(argc, argv, "0123456789")) != EOF) 21639478Sbostic switch (c) { 21739478Sbostic case '0': case '1': case '2': case '3': case '4': 21839478Sbostic case '5': case '6': case '7': case '8': case '9': 21939478Sbostic p = argv[optind - 1]; 22039478Sbostic if (p[0] == '-' && p[1] == ch && !p[2]) 22139478Sbostic length = atoi(++p); 22239478Sbostic else 22339478Sbostic length = atoi(argv[optind] + 1); 22439478Sbostic break; 22535982Sbostic } 22639478Sbostic} 22748349Scael.Ed 228