From 8bc22ef101d3266c60ce53dfddeb2e096ca870f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Tue, 14 Sep 2004 20:19:50 +0000 Subject: [PATCH] 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 --- src/kernel/libroot/posix/unistd/getopt.c | 80 +++++++++++++----------- 1 file changed, 45 insertions(+), 35 deletions(-) diff --git a/src/kernel/libroot/posix/unistd/getopt.c b/src/kernel/libroot/posix/unistd/getopt.c index 7984053a97..cd0c45a777 100644 --- a/src/kernel/libroot/posix/unistd/getopt.c +++ b/src/kernel/libroot/posix/unistd/getopt.c @@ -31,11 +31,14 @@ * SUCH DAMAGE. */ + #include #include #include #include + +/* required global variables */ int opterr = 1; /* if error message should be printed */ int optind = 1; /* index into parent argv vector */ int optopt = 0; /* character checked for validity */ @@ -46,34 +49,35 @@ char *optarg = NULL; /* argument associated with option */ #define BADARG (int)':' #define EMSG "" -/* - * getopt -- - * Parse argc/argv argument vector. + +/** getopt -- Parse argc/argv argument vector. */ + int -getopt(nargc, nargv, ostr) - int nargc; - char * const *nargv; - const char *ostr; +getopt(int argc, char * const *argv, const char *optionString) { extern char *__progname; 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; - if (optind >= nargc || *(place = nargv[optind]) != '-') { + if (optind >= argc || *(place = argv[optind]) != '-') { place = EMSG; - return (-1); + return -1; } - if (place[1] && *++place == '-') { /* found "--" */ + if (place[1] && *++place == '-') { + /* found "--" */ ++optind; 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, * assume it means -1. @@ -82,34 +86,40 @@ getopt(nargc, nargv, ostr) return (-1); if (!*place) ++optind; - if (opterr && *ostr != ':') - /* this should really be fprintf(stderr... */ - (void)printf("%s: illegal option -- %c\n", - __progname, optopt); - return (BADCH); + if (opterr && optionString[0] != ':') + fprintf(stderr, "%s: illegal option -- %c\n", __progname, optopt); + + return BADCH; } - if (*++oli != ':') { /* don't need argument */ + + if (*++oli != ':') { + /* don't need argument */ optarg = NULL; if (!*place) ++optind; - } - else { /* need an argument */ - if (*place) /* no white space */ + } else { + /* need an argument */ + if (*place) { + /* no white space */ optarg = place; - else if (nargc <= ++optind) { /* no arg */ + } else if (argc <= ++optind) { + /* no arg */ place = EMSG; - if (*ostr == ':') - return (BADARG); + if (optionString[0] == ':') + return BADARG; + if (opterr) - /* XXX - This is really fprintf(stderr... */ - (void)printf("%s: option requires an argument -- %c\n", - __progname, optopt); - return (BADCH); + fprintf(stderr, "%s: option requires an argument -- %c\n", __progname, optopt); + + return BADCH; + } else { + /* white space */ + optarg = argv[optind]; } - else /* white space */ - optarg = nargv[optind]; place = EMSG; ++optind; } - return (optopt); /* dump back option letter */ + + /* dump back option letter */ + return optopt; }