home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Zodiac Super OZ
/
MEDIADEPOT.ISO
/
FILES
/
16
/
FREEDOS.ZIP
/
FD_A4PRE.ZIP
/
SOURCE
/
MICROC.ZIP
/
GETOPT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1995-05-21
|
5KB
|
169 lines
/*
Free-DOS Function Library GETOPT.C - Returns command line options
Written by: Morgan "Hannibal" Toal, based upon original source by James Hall
Compiler: MICRO-C 3.13/ARROWASM/VALLINK
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
int optind = 1, optchar = 0;
char *optarg;
void usage (void);
int getopt (int argc, char *argv[], char *options, char *argoptions)
{
int c;
while (optind < argc)
{
optarg = NULL;
/* If the current argv is a switch (i.e. starts with SWCHAR)... */
if (argv[optind][0] == SWCHAR)
switch (c = toupper(argv[optind][optchar]))
{
/* ...advance to next argument if optchar is NULL or ARGCHAR */
case NULL:
optchar = 0;
optind++;
break;
/* ...skip SWCHAR, but return ? if next optchar is SWCHAR or NULL */
case SWCHAR:
c = toupper(argv[optind][++optchar]);
if ((c == SWCHAR) || (c == NULL))
return ('?');
break;
/* ...return the optchar if in options or argoptions, or return ? */
default:
optchar++;
/* ...call usage if we have a "?" */
if (c == '?')
usage();
/* ...check to see if it is an argument-required option */
if (strchr(argoptions, c) != NULL)
if ((argv[optind][optchar] == ARGCHAR)
&& (argv[optind][optchar+1] != NULL))
{
optarg = &argv[optind++][optchar+1];
optchar = 0;
return (c);
}
else
{
fprintf (stderr, "Argument not specified for: %c%c\n", SWCHAR, c);
usage ();
}
/* ...check to see if it is a non-argument option */
if (strchr(options, c) != NULL)
if (argv[optind][optchar] != ARGCHAR)
return (c);
else
{
fprintf (stderr, "Argument not allowed for: %c%c\n", SWCHAR, c);
usage ();
}
/* ...what the hell is it? */
else
fprintf (stderr, "Option not recognized: %c%c\n", SWCHAR, c);
usage ();
}
/* return EOF because we have discovered a non-option argument */
else
return (EOF);
}
/* return EOF because we have run out of arguments */
return (EOF);
}
#ifdef TEST
main (int argc, char **argv)
{
int i;
while ((i = getopt (argc, argv, "ABC", "XYZ")) != EOF)
{
switch (i)
{
case 'A':
case 'B':
case 'C':
printf ("Option %c selected.\n", i);
break;
case 'X':
case 'Y':
case 'Z':
printf ("Option %c selected, with argument %s.\n", i, optarg);
break;
default:
printf ("Unknown Error.\n");
}
}
if (optind < argc)
{
printf ("The rest of the arguments are...\n");
for (i = optind ; i < argc; i++)
printf ("%d. %s\n", 1+i-optind, argv[i]);
}
printf ("This program would now be doing something useful.\n");
}
void usage(void)
{
printp ("GETOPT", "Test program for GETOPT.C function");
printu ("GETOPT", "[\A] [\B] [\C] [\X=argument] [\Y=argument] [\Z=argument] [argument..]");
printo ("A", "Bogus option");
printo ("B", "Bogus option");
printo ("C", "Bogus option");
printo ("X", "Bogus option that requires an argument");
printo ("Y", "Bogus option that requires an argument");
printo ("Z", "Bogus option that requires an argument");
abort (1);
}
#endif