UPSYF173@DBIUNI11.BITNET (Wolfgang Thiel) (03/05/90)
/*
This is an example for simple ARGV parsing without complicated
(and complicating) getarg/getopt stuff:
It can handle :
- no options
- single and/or multiple options: -xyz or -x -y -z
- options with directly following specification: -b100
- options with specification after a blank: -f name
- all mixed together.
and it prints use() after -?, -h, or ?.
Wolfgang Thiel (UPSYF173@DBIUNI11.BITNET)
*/
#define _V 0x0001
#define _W 0x0002
#define NULL ((char *)0)
extern long strtol();
int flag;
long blksiz;
char file[80];
use()
{
printf("use: -vw] [-b#[k]] [-f name] [arg ...]\r\n");
exit(0);
}
int main(argc, argv)
int argc;
char **argv;
{
char *cp;
int c;
char *next;
flag = 0;
blksiz = 0l;
file[0] = 0;
while((cp = *++argv) != NULL && *cp++ == '-')
{
while((c = (int) *cp++) != 0)
{
switch(c)
{
case 'v':
flag |= _V;
break;
case 'w':
flag |= _W;
break;
case 'b':
blksiz = strtol(cp, &next, 10);
if (next == cp)
{
printf("no number found after -b\r\n");
use();
}
if (*next == 'k')
{
blksiz *= 1024l;
++next;
}
cp = next;
break;
case 'f':
if (*cp)
{
printf("blank expected after -f\r\n");
use();
}
if ( *++argv == NULL)
{
--argv;
printf("file name expected after -f\r\n");
use();
}
if (**argv == '-')
{
printf("file name expected after -f\r\n");
use();
}
strcpy(file, *argv);
break;
default:
printf("unknown option: -%c\r\n", c); /* thru */
case '?':
case 'h':
use();
break;
}
}
}
printf("Flag:x%04x Blk:%ld File:'%s'\r\n", flag, blksiz, file);
if (*argv != NULL && **argv == '?')
use();
while((cp = *argv++) != NULL)
printf("Arg: %s\r\n", cp);
return 0;
}