1
0
mirror of https://github.com/cc65/cc65.git synced 2024-12-23 04:30:10 +00:00
cc65/libsrc/common/getopt.c
uz 3afb4e4e94 Added getopt. The implementation is based on a public domain source,
originally written by Henry Spencer and supplied by Harald Arnesen.


git-svn-id: svn://svn.cc65.org/cc65/trunk@4737 b7a2c559-68d2-44c3-8de9-860c34a00d81
2010-07-10 11:28:02 +00:00

85 lines
2.5 KiB
C

/*
* This is part of a changed public domain getopt implementation that
* had the following text on top:
*
* I got this off net.sources from Henry Spencer.
* It is a public domain getopt(3) like in System V.
* I have made the following modifications:
*
* A test main program was added, ifdeffed by GETOPT.
* This main program is a public domain implementation
* of the getopt(1) program like in System V. The getopt
* program can be used to standardize shell option handling.
* e.g. cc -DGETOPT getopt.c -o getopt
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define ARGCH ':'
#define BADCH '?'
#define EMSG ""
int opterr = 1; /* useless, never set or used */
int optind = 1; /* index into parent argv vector */
int optopt; /* character checked for validity */
char *optarg; /* argument associated with option */
#define tell(s) fputs(*argv,stderr);fputs(s,stderr); \
fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);
int __fastcall__ getopt (int argc, char* const* argv, const char* optstring)
/* Get option letter from argument vector */
{
static char *place = EMSG; /* option letter processing */
register char *oli; /* option letter list index */
if (!*place) { /* update scanning pointer */
if (optind >= argc || *(place = argv[optind]) != '-' || !*++place) {
return (EOF);
}
if (*place == '-') {
/* found "--" */
++optind;
return (EOF);
}
}
/* option letter okay? */
if ((optopt = (int) *place++) == ARGCH ||
!(oli = strchr (optstring, 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) {
/* no white space */
optarg = place;
}
else if (argc <= ++optind) { /* no arg */
place = EMSG;
tell (": option requires an argument -- ");
} else {
/* white space */
optarg = argv[optind];
}
place = EMSG;
++optind;
}
return (optopt); /* dump back option letter */
}