Annotation of researchv10dc/man/man3/getopt.3, revision 1.1.1.1

1.1       root        1: .TH GETOPT 3
                      2: .CT 2 data_man
                      3: .SH NAME
                      4: getopt \(mi get option letter from argv
                      5: .SH SYNOPSIS
                      6: .nf
                      7: .B int getopt (argc, argv, optstring)
                      8: .B int argc;
                      9: .B char **argv;
                     10: .B char *optstring;
                     11: .PP
                     12: .B extern char *optarg;
                     13: .B extern int optind;
                     14: .B extern int opterr;
                     15: .fi
                     16: .SH DESCRIPTION
                     17: .I Getopt
                     18: returns the next option letter in
                     19: .I argv
                     20: that matches a letter in
                     21: .IR optstring .
                     22: .I Optstring
                     23: is a string of recognized option letters;
                     24: if a letter is followed by a colon, the option
                     25: is expected to have an argument, which may or
                     26: may not be separated from it by white space.
                     27: .I Optarg
                     28: is set to point to the start of the option argument, if any.
                     29: .PP
                     30: .I Optind,
                     31: initially 1, holds the index in
                     32: .I argv
                     33: of the next argument to be processed.
                     34: When
                     35: .I opterr
                     36: is nonzero (the default state), errors cause
                     37: diagnostic messages.
                     38: .PP
                     39: Option letters appear in nonempty clusters preceded by 
                     40: .BR - .
                     41: The special option
                     42: .L --
                     43: may be used to mark the end of the options.
                     44: .SH EXAMPLES
                     45: This fragment processes arguments
                     46: for a command that can take option
                     47: .B a
                     48: and option
                     49: .BR f ,
                     50: which requires an argument.
                     51: .PP
                     52: .EX
                     53: .ta \w'12345678'u +\w'12345678'u
                     54: main (argc, argv) char **argv;
                     55: {
                     56:        int c, errflg = 0;
                     57:        extern int optind;
                     58:        extern char *optarg, *ifile;
                     59:        while((c = getopt(argc, argv, "af:")) != -1)
                     60:                switch (c){
                     61:                case 'a':  aflg=1; break;
                     62:                case 'f':  ifile = optarg; break;
                     63:                case '?':  errflg=1; break;
                     64:                }
                     65:        if(errflg){
                     66:                fprintf(stderr, "usage: . . . ");
                     67:                exit(2);
                     68:        }
                     69:        for( ; optind < argc; optind++){
                     70:                if(access(argv[optind], 4)){
                     71:                        ...
                     72:                }
                     73:        }
                     74:        ...
                     75: }
                     76: .EE
                     77: .SH SEE ALSO
                     78: .IR getflags (3)
                     79: .SH DIAGNOSTICS
                     80: When all options have been processed,
                     81: \-1 is returned;
                     82: .I optind
                     83: refers to the first non-option argument.
                     84: .br
                     85: When 
                     86: .I getopt
                     87: encounters an option letter not included in
                     88: .I optstring
                     89: or finds an option argument missing, it prints a diagnostic on
                     90: .B stderr
                     91: under control of
                     92: .I opterr
                     93: and returns a question mark
                     94: .LR ? .

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.