*** UNIX MANUAL PAGE BROWSER ***

A Nergahak database for man pages research.

Navigation

Directory Browser

1Browse 4.4BSD4.4BSD
1Browse Digital UNIXDigital UNIX 4.0e
1Browse FreeBSDFreeBSD 14.3
1Browse MINIXMINIX 3.4.0rc6-d5e4fc0
1Browse NetBSDNetBSD 10.1
1Browse OpenBSDOpenBSD 7.7
1Browse UNIX v7Version 7 UNIX
1Browse UNIX v10Version 10 UNIX

Manual Page Search

Manual Page Result

0 Command: getopt | Section: 3 | Source: UNIX v10 | File: getopt.3
GETOPT(3) Library Functions Manual GETOPT(3) NAME getopt - get option letter from argv SYNOPSIS int getopt (argc, argv, optstring) int argc; char **argv; char *optstring; extern char *optarg; extern int optind; extern int opterr; DESCRIPTION Getopt returns the next option letter in argv that matches a letter in optstring. 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, which may or may not be separated from it by white space. Optarg is set to point to the start of the option argument, if any. Optind, initially 1, holds the index in argv of the next argument to be processed. When opterr is nonzero (the default state), errors cause diagnostic messages. Option letters appear in nonempty clusters preceded by -. The special option may be used to mark the end of the options. EXAMPLES This fragment processes arguments for a command that can take option a and option f, which requires an argument. main (argc, argv) char **argv; { int c, errflg = 0; extern int optind; extern char *optarg, *ifile; while((c = getopt(argc, argv, "af:")) != -1) switch (c){ case 'a': aflg=1; break; case 'f': ifile = optarg; break; case '?': errflg=1; break; } if(errflg){ fprintf(stderr, "usage: . . . "); exit(2); } for( ; optind < argc; optind++){ if(access(argv[optind], 4)){ ... } } ... } SEE ALSO getflags(3) DIAGNOSTICS When all options have been processed, -1 is returned; optind refers to the first non-option argument. When getopt encounters an option letter not included in optstring or finds an option argument missing, it prints a diagnostic on stderr un- der control of opterr and returns a question mark GETOPT(3)

Navigation Options