Autzoo.1269
net.sources
utcsrgv!utzoo!henry
Mon Jan 11 15:42:39 1982
getopt(3)
The following is the source and manual page for a getopt() routine
written locally to match the description in a copy of the Unix 3.0
manual that I got to see once.  Behavior is believed identical to the
Bell one, but this is NOT Bell code and carries no nondisclosure
restrictions.  Use it on any system you wish.
------------
/*
* getopt - get option letter from argv
*/

#include <STDIO.H>

char    *optarg;        /* Global argument pointer. */
int     optind = 0;     /* Global argv index. */

static char     *scan = NULL;   /* Private scan pointer. */

extern char     *index();

int
getopt(argc, argv, optstring)
int argc;
char *argv[];
char *optstring;
{
       register char c;
       register char *place;

       optarg = NULL;

       if (scan == NULL || *scan == '\0') {
               if (optind == 0)
                       optind++;

               if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
                       return(EOF);
               if (strcmp(argv[optind], "--")==0) {
                       optind++;
                       return(EOF);
               }

               scan = argv[optind]+1;
               optind++;
       }

       c = *scan++;
       place = index(optstring, c);

       if (place == NULL || c == ':') {
               fprintf(stderr, "%s: unknown option -%c\n", argv[0], c);
               return('?');
       }

       place++;
       if (*place == ':') {
               if (*scan != '\0') {
                       optarg = scan;
                       scan = NULL;
               } else {
                       optarg = argv[optind];
                       optind++;
               }
       }

       return(c);
}
------------
TH GETOPT 3 local
DA 28 Oct 1981
SH NAME
getopt \- get option letter from argv
SH SYNOPSIS
ft B
int getopt(argc, argv, optstring)
br
int argc;
br
char **argv;
br
char *optstring;
sp
extern char *optarg;
br
extern int optind;
ft
SH DESCRIPTION
I Getopt
returns the next option letter in
I argv
that matches a letter in
IR optstring .
I Optstring
is a string of recognized option letters;
if a letter is followed by a colon, the option is expected to have
an argument that may or may not be separated from it by white space.
I Optarg
is set to point to the start of the option argument on return from
IR getopt .
PP
I Getopt
places in
I optind
the
I argv
index of the next argument to be processed.
Because
I optind
is external, it is normally initialized to zero automatically
before the first call to
IR getopt .
PP
When all options have been processed (i.e., up to the first
non-option argument),
I getopt
returns
BR EOF .
The special option
B \-\-
may be used to delimit the end of the options;
B EOF
will be returned, and
B \-\-
will be skipped.
SH SEE ALSO
getopt(1)
SH DIAGNOSTICS
I Getopt
prints an error message on
I stderr
and returns a question mark
RB ( ? )
when it encounters an option letter not included in
IR optstring .
SH EXAMPLE
The following code fragment shows how one might process the arguments
for a command that can take the mutually exclusive options
B a
and
BR b ,
and the options
B f
and
BR o ,
both of which require arguments:
PP
RS
nf
main(argc, argv)
int argc;
char **argv;
{
       int c;
       extern int optind;
       extern char *optarg;
       \&.
       \&.
       \&.
       while ((c = getopt(argc, argv, "abf:o:")) != EOF)
               switch (c) {
               case 'a':
                       if (bflg)
                               errflg++;
                       else
                               aflg++;
                       break;
               case 'b':
                       if (aflg)
                               errflg++;
                       else
                               bproc();
                       break;
               case 'f':
                       ifile = optarg;
                       break;
               case 'o':
                       ofile = optarg;
                       break;
               case '?':
                       errflg++;
               }
       if (errflg) {
               fprintf(stderr, "Usage: ...");
               exit(2);
       }
       for (; optind
<HR>
This Usenet Oldnews Archive
article may be copied and distributed freely, provided:
<P>
1. There is no money collected for the text(s) of the articles.
<BR>
2. The following notice remains appended to each copy:
<P>
<EM>The Usenet Oldnews Archive: Compilation Copyright&copy 1981, 1996
<BR> Bruce Jones, Henry Spencer, David Wiseman.</EM>
<P>
<HR>
Goto <A HREF="82.01.19_utzoo.1288_net.sources.html">NEXT</A> article in NET.sources Newsgroup
<BR>Return to <A HREF="NET.sources-index.html">NET.sources index</A>
<BR>Return to the
       <A HREF="../index.html">Usenet Oldnews Archive index</A>
</HTML>

-----------------------------------------------------------------
gopher://quux.org/ conversion by John Goerzen <[email protected]>
of http://communication.ucsd.edu/A-News/


This Usenet Oldnews Archive
article may be copied and distributed freely, provided:

1. There is no money collected for the text(s) of the articles.

2. The following notice remains appended to each copy:

The Usenet Oldnews Archive: Compilation Copyright (C) 1981, 1996
Bruce Jones, Henry Spencer, David Wiseman.