Error messages are now directed to stderr rather than stdout.
Minor cleanup. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@8943 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -31,11 +31,14 @@
|
|||||||
* SUCH DAMAGE.
|
* SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
|
||||||
|
/* required global variables */
|
||||||
int opterr = 1; /* if error message should be printed */
|
int opterr = 1; /* if error message should be printed */
|
||||||
int optind = 1; /* index into parent argv vector */
|
int optind = 1; /* index into parent argv vector */
|
||||||
int optopt = 0; /* character checked for validity */
|
int optopt = 0; /* character checked for validity */
|
||||||
@@ -46,34 +49,35 @@ char *optarg = NULL; /* argument associated with option */
|
|||||||
#define BADARG (int)':'
|
#define BADARG (int)':'
|
||||||
#define EMSG ""
|
#define EMSG ""
|
||||||
|
|
||||||
/*
|
|
||||||
* getopt --
|
/** getopt -- Parse argc/argv argument vector.
|
||||||
* Parse argc/argv argument vector.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int
|
int
|
||||||
getopt(nargc, nargv, ostr)
|
getopt(int argc, char * const *argv, const char *optionString)
|
||||||
int nargc;
|
|
||||||
char * const *nargv;
|
|
||||||
const char *ostr;
|
|
||||||
{
|
{
|
||||||
extern char *__progname;
|
extern char *__progname;
|
||||||
static char *place = EMSG; /* option letter processing */
|
static char *place = EMSG; /* option letter processing */
|
||||||
char *oli; /* option letter list index */
|
char *oli; /* option letter list index */
|
||||||
|
|
||||||
if (optreset || !*place) { /* update scanning pointer */
|
if (optreset || !*place) {
|
||||||
|
/* update scanning pointer */
|
||||||
optreset = 0;
|
optreset = 0;
|
||||||
if (optind >= nargc || *(place = nargv[optind]) != '-') {
|
if (optind >= argc || *(place = argv[optind]) != '-') {
|
||||||
place = EMSG;
|
place = EMSG;
|
||||||
return (-1);
|
return -1;
|
||||||
}
|
}
|
||||||
if (place[1] && *++place == '-') { /* found "--" */
|
if (place[1] && *++place == '-') {
|
||||||
|
/* found "--" */
|
||||||
++optind;
|
++optind;
|
||||||
place = EMSG;
|
place = EMSG;
|
||||||
return (-1);
|
return -1;
|
||||||
}
|
}
|
||||||
} /* option letter okay? */
|
}
|
||||||
if ((optopt = (int)*place++) == (int)':' ||
|
|
||||||
!(oli = strchr(ostr, optopt))) {
|
/* option letter okay? */
|
||||||
|
|
||||||
|
if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(optionString, optopt))) {
|
||||||
/*
|
/*
|
||||||
* if the user didn't specify '-' as an option,
|
* if the user didn't specify '-' as an option,
|
||||||
* assume it means -1.
|
* assume it means -1.
|
||||||
@@ -82,34 +86,40 @@ getopt(nargc, nargv, ostr)
|
|||||||
return (-1);
|
return (-1);
|
||||||
if (!*place)
|
if (!*place)
|
||||||
++optind;
|
++optind;
|
||||||
if (opterr && *ostr != ':')
|
if (opterr && optionString[0] != ':')
|
||||||
/* this should really be fprintf(stderr... */
|
fprintf(stderr, "%s: illegal option -- %c\n", __progname, optopt);
|
||||||
(void)printf("%s: illegal option -- %c\n",
|
|
||||||
__progname, optopt);
|
return BADCH;
|
||||||
return (BADCH);
|
|
||||||
}
|
}
|
||||||
if (*++oli != ':') { /* don't need argument */
|
|
||||||
|
if (*++oli != ':') {
|
||||||
|
/* don't need argument */
|
||||||
optarg = NULL;
|
optarg = NULL;
|
||||||
if (!*place)
|
if (!*place)
|
||||||
++optind;
|
++optind;
|
||||||
}
|
} else {
|
||||||
else { /* need an argument */
|
/* need an argument */
|
||||||
if (*place) /* no white space */
|
if (*place) {
|
||||||
|
/* no white space */
|
||||||
optarg = place;
|
optarg = place;
|
||||||
else if (nargc <= ++optind) { /* no arg */
|
} else if (argc <= ++optind) {
|
||||||
|
/* no arg */
|
||||||
place = EMSG;
|
place = EMSG;
|
||||||
if (*ostr == ':')
|
if (optionString[0] == ':')
|
||||||
return (BADARG);
|
return BADARG;
|
||||||
|
|
||||||
if (opterr)
|
if (opterr)
|
||||||
/* XXX - This is really fprintf(stderr... */
|
fprintf(stderr, "%s: option requires an argument -- %c\n", __progname, optopt);
|
||||||
(void)printf("%s: option requires an argument -- %c\n",
|
|
||||||
__progname, optopt);
|
return BADCH;
|
||||||
return (BADCH);
|
} else {
|
||||||
|
/* white space */
|
||||||
|
optarg = argv[optind];
|
||||||
}
|
}
|
||||||
else /* white space */
|
|
||||||
optarg = nargv[optind];
|
|
||||||
place = EMSG;
|
place = EMSG;
|
||||||
++optind;
|
++optind;
|
||||||
}
|
}
|
||||||
return (optopt); /* dump back option letter */
|
|
||||||
|
/* dump back option letter */
|
||||||
|
return optopt;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user