Category : C Source Code
Archive   : OPTIONS.ZIP
Filename : OPTIONS.C

 
Output of file : OPTIONS.C contained in archive : OPTIONS.ZIP
/* standard handler for command line options/arguments */

/*
* provides unix-style option handling
* with table-driven specification
* and built-in "usage" message
*
* limitations:
*
* 1. lattice C doesn't pass program name thru
* 2. this version allows single char options only
*
*/

/*
* Ian Wilson 11/12/85
* tested with Lattice C v1.2
*/

#include
#include

/*
* this does all the work.
* if the first argument begins with a '-',
* scan the option table (optab) for a match
* if found, get a command line arg if specified
* then call the func with command line arg as parameter
*/


options(argcp, argvp, nopts, optab, usage, progname)
int *argcp; /* & number of args */
char **argvp[]; /* & array of strings */
int nopts; /* # table entries */
OPT optab[]; /* array of OPT specifiers */
int (*usage)(); /* function to call if no args */
char *progname; /* should be argv[0] but isn't */
{
char *optstr;
OPT *t;
int i;
char *arg;

/* do we have args? */
if(*argcp == 1) { /* no: maybe a usage message? */
if(usage) {
(*usage)(nopts, optab, progname); /* pass thru to usage */
return -1;
}
else return 0;
}

/* argc is at least 1. Process line till no more to do. */
while(*argcp > 1) {
optstr = *(*argvp + 1); /* get an argument */
if(*optstr == '-') { /* option specifier? */
while(*++optstr) { /* process option specs */

/* look in table for match */
for(i=0, t=optab; i if(t->optchar == *optstr) {
if(t->takesarg) {
if(*argcp > 2) {
arg = *(*argvp + 2);
(*argvp)++;
(*argcp)--;
}
else {
return -1;
}
}
else arg = "";

break;
}
}

if(i >= nopts) {
return -1;
}

/* got option + arg (if any), do func */
(*t->func)(arg);
}

(*argvp)++; /* move to next argument */
(*argcp)--; /* decrement arg count */
}
else break; /* not an option string */
}
return 0;
}




  3 Responses to “Category : C Source Code
Archive   : OPTIONS.ZIP
Filename : OPTIONS.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/