Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

getopt(1)

getopt(3)  —  Subroutines

OSF

NAME

getopt − Gets flag letters from the argument vector

LIBRARY

Standard C Library (libc.a)

SYNOPSIS

#include <stdio.h>

#include <stdlib.h>

int getopt (
int argc,
char ∗argv[],
char ∗optstring );

extern char ∗optarg;

extern int optind;

extern int opterr;

extern char optopt;

PARAMETERS

argcSpecifies the number of parameters passed to the routine. 

argvPoints to an array of argc pointers to argument strings. 

optstringSpecifies a string of recognized option characters.  If a character is followed by a colon, the flag is expected to take a parameter that may or may not be separated from it by white space. 

DESCRIPTION

The getopt() function returns the next flag character in the argv parameter list that matches a character in the optstring parameter.  The getopt() function is used to help programs interpret shell command-line flags that are passed to them. 

The optarg external variable is set to point to the start of the flag’s parameter on return from the getopt() function. 

The getopt() function places the argv index of the next argument to be processed in optind.  The optind variable is externally initialized to 1 before the first call to getopt() so that argv[0] is not processed. 

NOTES

The external int optopt variable is set to the real option found in the argv parameter.  This is true whether the flag is in the optstring parameter or not. 

AES Support Level:
Trial use

RETURN VALUES

Upon successful completion, the getopt() function returns the flag character that was detected.  If it encounters a flag that is not included in the optstring parameter, or if the : (colon) character is used incorrectly, the getopt() function prints an error message on stderr and returns a ? (question mark).  The error message can be suppressed by setting the int variable opterr to 0 (zero). 

When all flags have been processed (that is, up to the first nonflag argument), the getopt() function returns EOF.  The special flag -- (dash dash) can be used to delimit the end of the flags; EOF is returned, and the -- string is skipped. 

RELATED INFORMATION

Commands: getopt(1)

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026