Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site ut-sally.UUCP Path: utzoo!decvax!ucbvax!dual!mordor!ut-sally!jsq From: jsq@ut-sally.UUCP (John Quarterman) Newsgroups: mod.std.unix Subject: Re: command line arguments Message-ID: <2300@ut-sally.UUCP> Date: Mon, 8-Jul-85 15:40:08 EDT Article-I.D.: ut-sally.2300 Posted: Mon Jul 8 15:40:08 1985 Date-Received: Tue, 9-Jul-85 01:57:53 EDT References: <2210@ut-sally.UUCP> <2226@ut-sally.UUCP>, <2244@ut-sally.UUCP> <2255@ut-sally.UUCP> <2256@ut-sally.UUCP> <2280@ut-sally.UUCP> Reply-To: std-unix-request@ut-sally Organization: U. Texas CS Dept., Austin, Texas Lines: 94 Approved: jsq@ut-sally.UUCP Date: Mon, 8 Jul 85 12:51:54 EDT From: Keith BosticTo: ut-sally!std-unix Subject: Public domain version of getopt(3) John, you posted Henry Spencer's original version of getopt (message #11, I believe) unless you've posted two versions. [ I've posted only one before, which was Henry Spencer's. -mod ] The reason that I reimplemented after that version was: 1: his used the stdio library printf call, thus forcing the loading of some large routines even if your program didn't need them. 2: his didn't have the same error messages that the SysV one had. 3: his didn't use certain external variables (optarg, opterr, optind and optopt) that the SysV one had. Obviously, #3 is the real reason, the others were bug fixes if anything. In any case, the version I sent to you (it's included below if you've lost it) [ I have the one from net.sources, but I thought it best to get whatever your latest version is. If you mailed me one before, it never arrived, possibly because my sun crashed about that time. -mod ] is the version that will be released with 4.3BSD and, as far as I know, is totally compatible with SysV. [ How about sending it to mod.sources, as well? -mod ] --keith ====================================================================== #include /* * this is a public domain version of getopt(3). * bugs, fixes to: * Keith Bostic * ARPA: keith@seismo * UUCP: seismo!keith */ /* * get option letter from argument vector */ int opterr = 1, /* useless, never set or used */ optind = 1, /* index into parent argv vector */ optopt; /* character checked for validity */ char *optarg; /* argument associated with option */ #define BADCH (int)'?' #define EMSG "" #define tell(s) fputs(*nargv,stderr);fputs(s,stderr); \ fputc(optopt,stderr);fputc('\n',stderr);return(BADCH); getopt(nargc,nargv,ostr) int nargc; char **nargv, *ostr; { static char *place = EMSG; /* option letter processing */ register char *oli; /* option letter list index */ char *index(); if(!*place) { /* update scanning pointer */ if(optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) return(EOF); if (*place == '-') { /* found "--" */ ++optind; return(EOF); } } /* option letter okay? */ if ((optopt = (int)*place++) == (int)':' || !(oli = index(ostr,optopt))) { if(!*place) ++optind; tell(": illegal option -- "); } if (*++oli != ':') { /* don't need argument */ optarg = NULL; if (!*place) ++optind; } else { /* need an argument */ if (*place) optarg = place; /* no white space */ else if (nargc <= ++optind) { /* no arg */ place = EMSG; tell(": option requires an argument -- "); } else optarg = nargv[optind]; /* white space */ place = EMSG; ++optind; } return(optopt); /* dump back option letter */ } -- John Quarterman, UUCP: {ihnp4,seismo,harvard,gatech}!ut-sally!jsq ARPA Internet and CSNET: jsq@ut-sally.ARPA, soon to be jsq@sally.UTEXAS.EDU