| 1 |
/* |
| 2 |
* Copyright (c) 1987, 1993, 1994, 1995 |
| 3 |
* The Regents of the University of California. All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* 3. Neither the names of the copyright holders nor the names of its |
| 14 |
* contributors may be used to endorse or promote products derived from |
| 15 |
* this software without specific prior written permission. |
| 16 |
* |
| 17 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS |
| 18 |
* IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 19 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 20 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE |
| 21 |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 |
* POSSIBILITY OF SUCH DAMAGE. |
| 28 |
*/ |
| 29 |
|
| 30 |
#include <assert.h> |
| 31 |
#include <errno.h> |
| 32 |
#include <stdio.h> |
| 33 |
#include <string.h> |
| 34 |
|
| 35 |
#ifndef __P |
| 36 |
#define __P(x) x |
| 37 |
#endif |
| 38 |
#define _DIAGASSERT(x) assert(x) |
| 39 |
|
| 40 |
#ifdef __weak_alias |
| 41 |
__weak_alias(getopt, _getopt); |
| 42 |
#endif |
| 43 |
|
| 44 |
int opterr = 1, /* if error message should be printed */ |
| 45 |
optind = 1, /* index into parent argv vector */ |
| 46 |
optopt, /* character checked for validity */ |
| 47 |
optreset; /* reset getopt */ |
| 48 |
char *optarg; /* argument associated with option */ |
| 49 |
|
| 50 |
static char *_progname __P((char *)); |
| 51 |
|
| 52 |
int getopt_internal __P((int, char *const *, const char *)); |
| 53 |
|
| 54 |
static char *_progname(nargv0) |
| 55 |
|
| 56 |
char *nargv0; |
| 57 |
{ |
| 58 |
char *tmp; |
| 59 |
|
| 60 |
_DIAGASSERT(nargv0 != NULL); |
| 61 |
|
| 62 |
tmp = strrchr(nargv0, '/'); |
| 63 |
if (tmp) |
| 64 |
tmp++; |
| 65 |
else |
| 66 |
tmp = nargv0; |
| 67 |
return (tmp); |
| 68 |
} |
| 69 |
|
| 70 |
#define BADCH (int)'?' |
| 71 |
#define BADARG (int)':' |
| 72 |
#define EMSG "" |
| 73 |
|
| 74 |
/* |
| 75 |
* getopt -- |
| 76 |
* Parse argc/argv argument vector. |
| 77 |
*/ |
| 78 |
int getopt(nargc, nargv, ostr) |
| 79 |
|
| 80 |
int nargc; |
| 81 |
char *const nargv[]; |
| 82 |
const char *ostr; |
| 83 |
{ |
| 84 |
static char *__progname = 0; |
| 85 |
static char *place = EMSG; /* option letter processing */ |
| 86 |
char *oli; /* option letter list index */ |
| 87 |
__progname = __progname ? __progname : _progname(*nargv); |
| 88 |
|
| 89 |
_DIAGASSERT(nargv != NULL); |
| 90 |
_DIAGASSERT(ostr != NULL); |
| 91 |
|
| 92 |
if (optreset || !*place) { /* update scanning pointer */ |
| 93 |
optreset = 0; |
| 94 |
if (optind >= nargc || *(place = nargv[optind]) != '-') { |
| 95 |
place = EMSG; |
| 96 |
return (-1); |
| 97 |
} |
| 98 |
if (place[1] && *++place == '-' /* found "--" */ |
| 99 |
&& place[1] == '\0') { |
| 100 |
++optind; |
| 101 |
place = EMSG; |
| 102 |
return (-1); |
| 103 |
} |
| 104 |
} /* option letter okay? */ |
| 105 |
if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr, optopt))) { |
| 106 |
/* |
| 107 |
* if the user didn't specify '-' as an option, |
| 108 |
* assume it means -1. |
| 109 |
*/ |
| 110 |
if (optopt == (int)'-') |
| 111 |
return (-1); |
| 112 |
if (!*place) |
| 113 |
++optind; |
| 114 |
if (opterr && *ostr != ':') |
| 115 |
(void)fprintf(stderr, "%s: illegal option -- %c\n", __progname, optopt); |
| 116 |
return (BADCH); |
| 117 |
} |
| 118 |
if (*++oli != ':') { /* don't need argument */ |
| 119 |
optarg = NULL; |
| 120 |
if (!*place) |
| 121 |
++optind; |
| 122 |
} else { /* need an argument */ |
| 123 |
if (*place) /* no white space */ |
| 124 |
optarg = place; |
| 125 |
else if (nargc <= ++optind) { /* no arg */ |
| 126 |
place = EMSG; |
| 127 |
if (*ostr == ':') |
| 128 |
return (BADARG); |
| 129 |
if (opterr) |
| 130 |
(void)fprintf(stderr, "%s: option requires an argument -- %c\n", |
| 131 |
__progname, optopt); |
| 132 |
return (BADCH); |
| 133 |
} else /* white space */ |
| 134 |
optarg = nargv[optind]; |
| 135 |
place = EMSG; |
| 136 |
++optind; |
| 137 |
} |
| 138 |
return (optopt); /* dump back option letter */ |
| 139 |
} |