HPlogo MPE/iX Developer's Kit Reference Manual Volume I: HP 3000 MPE/iX Computer Systems > Chapter 4  POSIX/iX Library Function Descriptions

getopt

» 

Technical documentation

Complete book in PDF
» Feedback

 » Table of Contents

 » Index

Command option parsing.

Syntax

   #include <unistd.h>

   extern char *optarg;

   extern int optind, opterr, optopt;

   int getopt(int argc, const char *argv[],

              const char *optstring);

Parameters

argc

is the argument count as passed to main().

argv[]

is the argument vector as passed to main().

optstring

is a string containing letters and/or digits which should be recognized as command line arguments. For example, if a program takes the arguments -a, -A, and -b, optstring could be "aAb". The characters in optstring may be in any order. If an option may take an argument, the option character in optstring should be followed by a colon. For example, if the example command also takes an option

    -c value
optstring could be "aAbc:". A colon as the first character of optstring returns a : (instead of a ?) if getopt encounters a missing argument.

Return Values

-1

When it reaches the end of the options. This can be when argv[optind] is NULL or the strings - or --, or when a command line argument does not begin with -. If argv[optind] is --, getopt() increments optind by 1; otherwise, it does not increment optind.

?

If getopt() encounters an invalid option (one whose character does not appear in optstring) or an option that was supposed to be followed by an argument value but was not, getopt() returns a question mark (?). If the first character is a :, and the error is a missing argument, then : is returned instead of ?. The character that caused the error is assigned to the variable optopt, and optind is not updated.

1

Normally, getopt() writes an error message to the standard error stream if it encounters an error; to disable this error message, assign the value zero to the variable opterr or start the optstring with :. By default, opterr is initialized to 1.

Description

getopt() helps parse a command line that corresponds to the standard POSIX.2 syntax: options are single letters or digits marked with a minus (-) and possibly followed by a value. getopt() recognizes that options may be concatenated; for example,

    -a -b

can be combined into

    -ab

getopt() returns the character that represents the option. For example, if getopt() identifies the -a option, it returns 'a'.

Successive calls to getopt() obtain successive options from the command line. getopt() uses the variable optind to keep track of which argv element it examines . optind is initialized to 1, and every invocation of getopt() sets optind to the command line argument to be scanned. When a single argument contains several options (as in -abc), optind indicates the same argv element until all the options have been returned.

If an option takes an argument, getopt() sets optarg to point to the associated argument, according to these rules:

*If the option character was at the end of an argv element, the associated argument is assumed to be the element of argv. In this case, optind is incremented by 2; otherwise, the argument value is assumed to come immediately after the argument letter. It is the rest of the argv element. In this case, optind is incremented by 1.

Example

The following code fragment shows how one might process the arguments for a utility that can take the mutually exclusive options a and b and the options f and o, both of which require arguments.

    #include <unistd.h>



    int main (int argc, char *argv[])

    {

    int c, bflg, aflg, errflg = 0;

    char *ifile, *ofile;

    extern char *optarg;

    extern int optind, optopt;

    . . .

    while ((c = getopt(argc, argv, ":abf:o:")) != -1) {

    switch (c) {

    case 'a':

    if (bflg)

    errflg = 1;

    else

    aflg = 1;

    break;

    case 'b':

    if (aflg)

    errflg = 1;

    else

    bflg = 1;

    bproc( );

    break;

    case 'f':

    ifile = optarg;

    break;

    case 'o':

    ofile = optarg;

    break;

    case ':': /* -f or -o without option-arg */

    fprintf (stderr,

    "Option -%c requires an option-argument0,

    optopt);

    errflg = 1;

    break;

    case '?':

    fprintf (stderr,

    "Unrecognized option: -%c0, optopt);

    errflg = 1;

    break;

    }

    }

    if (errflg) {

    fprintf(stderr, "usage: . . . ");

    exit(2);

    }

    for ( ; optind < argc; optind++) {



    if (access(argv[optind], R_OK)) {

    . . .

    }

Errors

If an error occurs, errno is set to one of the following values:

Option -option argument missingCAUSEWhen invoking a program that calls getopt(), you specified -option but did not provide the argument that optstring indicated.
 ACTIONProvide the missing argument.
Unknown option "-option"CAUSEWhen invoking a program that calls getopt(), you specified an option that was not in optstring.
 ACTIONSpecify an option included in optstring.

Implementation Considerations

The current implementation of MPE/iX uses the INFO string to pass arguments to programs. If the size of this string plus the size of the current environment (determined by the number and size of the environment variables in the current process) is greater than 8192 bytes, the string is too long to pass to a subprocess and the process creation fails.

See Also

getopt(1), getopts(1)

Feedback to webmaster