crm@duke.UUCP (Charlie Martin) (07/03/85)
Could someone (are you there Henry?) post or mail the sources for the public- domain getopt that has been bandied about on mod.std.unix? Thanks. Charlie Martin -- crm@duke -- {decvax,ihnp4}!mcnc!duke!crm -- Charlie Martin (...mcnc!duke!crm)
schwartz@uw-june (Michael Schwartz) (10/09/85)
There has been a fair amount of software posted on the net which uses getopt, but it took me a while to track down a copy of getopt for myself. Here it is, including the manpage. The manpage output (as well as source) is included for systems which can't process the manpage source. --------------------------------- cut here ------------------------------------ ############################################################ # # getopt.ar # # To extract the files from this shell archive file # simply create a directory for this file # and move the archive file to it # and enter the command # # sh filename.ar # # Do not use csh # The files will be extracted automatically # ############################################################ echo "Extracting getopt.3c <-- getopt.ar" cat << \===getopt.3c=== > getopt.3c .\" @(#)getopt.3c 1.1 83/08/30 SMI; from UCB 4.2 .TH GETOPT 3C "26 August 1983" .SH NAME getopt, optarg, optind \- get option letter from argv .SH SYNOPSIS .nf .ft B int getopt(argc, argv, optstring) int args; char **argv; char *optstring; .ft P .LP .ft B extern char *optarg; extern int optind; .ft R .fi .SH DESCRIPTION .ft B This routine is included for compatibility with UNIX system-III. It is of marginal value, and should not be used in new programs. .ft R .LP .I Getopt returns the next option letter in .I argv that matches a letter in .I 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 .I getopt. .LP .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 .I getopt. .LP When all options have been processed (i.e., up to the first non-option argument), .I getopt returns .B \s-2EOF\s0. The special option \-\- may be used to delimit the end of the options; .B \s-2EOF\s0 will be returned, and \-\- will be skipped. .SH DIAGNOSTICS .I Getopt prints an error message on .I stderr and returns a question mark (?) when it encounters an option letter not included in .I 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 .B b, and the options .B f and .B o, both of which require arguments: .LP .nf .DT main(argc, argv) int argc; char **argv; { int c; extern int optind; extern char *optarg; \&\fB.\fP \&\fB.\fP \&\fB.\fP 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': infile = optarg; break; case 'o': ofile = optarg; bufsiza = 512; break; case '?': errflg++; } if (errflg) { fprintf(stderr, "usage: . . . "); exit(2); } for (; optind < argc; optind++) { if (access(argv[optind], 4)) { \&\fB.\fP \&\fB.\fP \&\fB.\fP } .fi ===getopt.3c=== # ---------- echo "Extracting getopt.c <-- getopt.ar" cat << \===getopt.c=== > getopt.c /* * 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 if (optind < argc) { optarg = argv[optind]; optind++; } else { fprintf(stderr, "%s: -%c argument missing\n", argv[0], c); return('?'); } } return(c); } ===getopt.c=== # ---------- echo "Extracting getopt.manpage <-- getopt.ar" cat << \===getopt.manpage=== > getopt.manpage GETOPT(3C) UNIX Programmer's Manual GETOPT(3C) NAME getopt, optarg, optind - get option letter from argv SYNOPSIS int getopt(argc, argv, optstring) int args; char **argv; char *optstring; extern char *optarg; extern int optind; DESCRIPTION This routine is included for compatibility with UNIX system-III. It is of marginal value, and should not be used in new programs. _G_e_t_o_p_t returns the next option letter in _a_r_g_v that matches a letter in _o_p_t_s_t_r_i_n_g. _O_p_t_s_t_r_i_n_g 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. _O_p_t_a_r_g is set to point to the start of the option argument on return from _g_e_t_o_p_t. _G_e_t_o_p_t places in _o_p_t_i_n_d the _a_r_g_v index of the next argument to be processed. Because _o_p_t_i_n_d is external, it is normally initialized to zero automatically before the first call to _g_e_t_o_p_t. When all options have been processed (i.e., up to the first non-option argument), _g_e_t_o_p_t returns EOF. The special option -- may be used to delimit the end of the options; EOF will be returned, and -- will be skipped. DIAGNOSTICS _G_e_t_o_p_t prints an error message on _s_t_d_e_r_r and returns a ques- tion mark (?) when it encounters an option letter not included in _o_p_t_s_t_r_i_n_g. EXAMPLE The following code fragment shows how one might process the arguments for a command that can take the mutually exclusive options a and b, and the options f and o, both of which require arguments: main(argc, argv) int argc; char **argv; { int c; extern int optind; extern char *optarg; Printed 10/8/85 26 August 1983 1 GETOPT(3C) UNIX Programmer's Manual GETOPT(3C) . . . 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': infile = optarg; break; case 'o': ofile = optarg; bufsiza = 512; break; case '?': errflg++; } if (errflg) { fprintf(stderr, "usage: . . . "); exit(2); } for (; optind < argc; optind++) { if (access(argv[optind], 4)) { . . . } Printed 10/8/85 26 August 1983 2 ===getopt.manpage=== # ----------