[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference decwet::visual

Title:Microsoft Visual C++ bug reports and kits
Notice:Register in Topic 2. 5.Last for latest Kit
Moderator:DECWET::THOMASN
Created:Tue May 17 1994
Last Modified:Fri Jun 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:521
Total number of notes:2938

466.0. "Porting UNIX function getopt" by SPANIX::ALVARO () Thu Feb 20 1997 08:39

    
    
    
    Hi,
    
    
    I'm moving a UNIX targeted sourced (fly utility) with VisualC and the
    linker can't reference function called getopt and optarg which are
    availables on UNIX. Does anyone know how to port to Visualc?
    
    
    	Thanks and best regards,
    
    	Alvaro
T.RTitleUserPersonal
Name
DateLines
466.1getopt.cDECWET::KOWALSKITime's not for savingThu Feb 20 1997 09:4391
#include <stdio.h>
#include <string.h>

#define tell(S) \
if (opterr) { \
	(void) fprintf(stderr, "%s%s%c\n", *nargv, S, optopt); \
} \
return ('?'); 

/*
 * statics
 */
static char *place = "";	/* option letter processing */
static int opterr, optopt;
int optind;
char *optarg;

void getopt_init()
{
	opterr = 1;
	optind = 0;
	optopt = 0;
	optarg = NULL;
	place = "";
}


/*
 * get command line arguments. 
 */
char *getarg(nargc, nargv)
	int nargc;
	char *nargv[];
{
	if (optind >= nargc)
		return (NULL);
	else
		return (nargv[optind++]);
}

int getopt(nargc, nargv, ostr)
	int nargc;
	char *nargv[];
	char *ostr;
{
	register char *oli;		/* option letter list index */
	if (!*place) {			/* update scanning pointer */
		if (optind >= nargc) {
			place = "";	/* if there's a next time */
			return (EOF);
		}
		if (*(place = nargv[optind]) != '-') {
			place = "";	/* if there's a next time */
			return (EOF);
		}
		if (!*++place) {
			place = "";	/* if there's a next time */
			return (EOF);
		}
		if (*place == '-') {	/* found "--" */
			++optind;
			place = "";	/* if there's a next time */
			return (EOF);
		}
	}				/* option letter okay? */
	optopt = (int)*place++;
	oli = strchr(ostr, optopt);
	if ((optopt == (int)':') || !oli) {
		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 = "";
			tell(": option requires an argument -- ");
		} else {
			optarg = nargv[optind];	/* white space */
		}
		place = "";
		++optind;
	}
	return (optopt);			/* dump back option letter */
}