home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
misc
/
volume6
/
tif2ps
/
part02
/
getopt.c
next >
Wrap
C/C++ Source or Header
|
1989-02-03
|
2KB
|
62 lines
/*
* I got this off net.sources from Henry Spencer.
* It is a public domain getopt(3) like in System V.
*
* I made some minor modifications while porting it to MS-DOS.
* andy@coma
*/
#include <stdio.h>
#include <string.h>
#define ARGCH (int)':'
#define BADCH (int)'?'
#define EMSG ""
#define ENDARGS "--"
/*
* get option letter from argument vector
*/
int optind = 1, /* index into parent argv vector */
optopt; /* character checked for validity */
char *optarg; /* argument associated with option */
#define tell(s) fputs(*nargv,stderr);fputs(s,stderr); \
(void)fputc(optopt,stderr);(void)fputc('\n',stderr); \
return(BADCH);
int getopt(nargc,nargv,ostr)
int nargc;
char **nargv,
*ostr;
{
static char *place = EMSG; /* option letter processing */
register char *oli; /* option letter list 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++) == ARGCH || !(oli = strchr (ostr,optopt))) {
if(!*place) ++optind;
tell(": illegal option -- ");
}
if (*++oli != ARGCH) { /* 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 */
}