command line arguments

John Quarterman jsq at ut-sally.UUCP
Tue Jul 9 05:40:08 AEST 1985


Date: Mon, 8 Jul 85 12:51:54 EDT
From: Keith Bostic <seismo!keith>
To: 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 <stdio.h>

/*
 * this is a public domain version of getopt(3).
 * bugs, fixes to:
 *		Keith Bostic
 *			ARPA: keith at 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 at ut-sally.ARPA, soon to be jsq at sally.UTEXAS.EDU



More information about the Mod.std.unix mailing list