diff options
Diffstat (limited to 'main/glib/getopt.c')
-rw-r--r-- | main/glib/getopt.c | 180 |
1 files changed, 93 insertions, 87 deletions
diff --git a/main/glib/getopt.c b/main/glib/getopt.c index cf43256..84d695a 100644 --- a/main/glib/getopt.c +++ b/main/glib/getopt.c @@ -1,10 +1,10 @@ -/* $NetBSD: getopt.c,v 1.29 2014/06/05 22:00:22 christos Exp $ */ +/* $NetBSD: getopt.c,v 1.29 2014/06/05 22:00:22 christos Exp $ */ #include <string.h> extern int printf(char *fmt,...); /* * Copyright (c) 1987, 1993, 1994 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -31,96 +31,102 @@ extern int printf(char *fmt,...); * SUCH DAMAGE. */ -int opterr = 1, /* if error message should be printed */ - optind = 1, /* index into parent argv vector */ - optopt, /* character checked for validity */ - optreset; /* reset getopt */ -char *optarg; /* argument associated with option */ +int opterr = 1, /* if error message should be printed */ + optind = 1, /* index into parent argv vector */ + optopt, /* character checked for validity */ + optreset; /* reset getopt */ +char *optarg; /* argument associated with option */ -#define BADCH (int)'?' -#define BADARG (int)':' -#define EMSG "" +#define BADCH (int)'?' +#define BADARG (int)':' +#define EMSG "" /* * getopt -- - * Parse argc/argv argument vector. + * Parse argc/argv argument vector. */ int -getopt(int nargc, char * const nargv[], const char *ostr) +getopt(int nargc, char *const nargv[], const char *ostr) { - static char *place = EMSG; /* option letter processing */ - char *oli; /* option letter list index */ + static char *place = EMSG; /* option letter processing */ + char *oli; /* option letter list index */ - if (optreset || *place == 0) { /* update scanning pointer */ - optreset = 0; - place = nargv[optind]; - if (optind >= nargc || *place++ != '-') { - /* Argument is absent or is not an option */ - place = EMSG; - return (-1); - } - optopt = *place++; - if (optopt == '-' && *place == 0) { - /* "--" => end of options */ - ++optind; - place = EMSG; - return (-1); - } - if (optopt == 0) { - /* Solitary '-', treat as a '-' option - if the program (eg su) is looking for it. */ - place = EMSG; - if (strchr(ostr, '-') == 0) - return (-1); - optopt = '-'; - } - } else - optopt = *place++; + if(optreset || *place == 0) { /* update scanning pointer */ + optreset = 0; + place = nargv[optind]; + if(optind >= nargc || *place++ != '-') { + /* Argument is absent or is not an option */ + place = EMSG; + return (-1); + } + optopt = *place++; + if(optopt == '-' && *place == 0) { + /* "--" => end of options */ + ++optind; + place = EMSG; + return (-1); + } + if(optopt == 0) { + /* Solitary '-', treat as a '-' option + if the program (eg su) is looking for it. */ + place = EMSG; + if(strchr(ostr, '-') == 0) { + return (-1); + } + optopt = '-'; + } + } else { + optopt = *place++; + } - /* See if option letter is one the caller wanted... */ - if (optopt == ':' || (oli = strchr(ostr, optopt)) == 0) { - if (*place == 0) - ++optind; - if (opterr && *ostr != ':') - (void)printf( - "illegal option -- %c\n", optopt); - return (BADCH); - } + /* See if option letter is one the caller wanted... */ + if(optopt == ':' || (oli = strchr(ostr, optopt)) == 0) { + if(*place == 0) { + ++optind; + } + if(opterr && *ostr != ':') + (void)printf( + "illegal option -- %c\n", optopt); + return (BADCH); + } - /* Does this option need an argument? */ - if (oli[1] != ':') { - /* don't need argument */ - optarg = 0; - if (*place == 0) - ++optind; - } else { - /* Option-argument is either the rest of this argument or the - entire next argument. */ - if (*place) - optarg = place; - else if (oli[2] == ':') - /* - * GNU Extension, for optional arguments if the rest of - * the argument is empty, we return NULL - */ - optarg = 0; - else if (nargc > ++optind) - optarg = nargv[optind]; - else { - /* option-argument absent */ - place = EMSG; - if (*ostr == ':') - return (BADARG); - if (opterr) - (void)printf( - "option requires an argument -- %c\n", - optopt); - return (BADCH); - } - place = EMSG; - ++optind; - } - return (optopt); /* return option letter */ + /* Does this option need an argument? */ + if(oli[1] != ':') { + /* don't need argument */ + optarg = 0; + if(*place == 0) { + ++optind; + } + } else { + /* Option-argument is either the rest of this argument or the + entire next argument. */ + if(*place) { + optarg = place; + } else if(oli[2] == ':') + /* + * GNU Extension, for optional arguments if the rest of + * the argument is empty, we return NULL + */ + { + optarg = 0; + } else if(nargc > ++optind) { + optarg = nargv[optind]; + } else { + /* option-argument absent */ + place = EMSG; + if(*ostr == ':') { + return (BADARG); + } + if(opterr) + (void)printf( + "option requires an argument -- %c\n", + optopt); + return (BADCH); + } + place = EMSG; + ++optind; + } + return (optopt); /* return option letter */ } /* getoptinit(): @@ -131,9 +137,9 @@ getopt(int nargc, char * const nargv[], const char *ostr) void getoptinit(void) { - optind = 1; - opterr = 1; - optopt = 0; - optreset = 0; - optarg = (char *)0; + optind = 1; + opterr = 1; + optopt = 0; + optreset = 0; + optarg = (char *)0; } |