|
| 1 | +/* $NetBSD: getopt.c,v 1.29 2014/06/05 22:00:22 christos Exp $ */ |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (c) 1987, 1993, 1994 |
| 5 | + * The Regents of the University of California. All rights reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions |
| 9 | + * are met: |
| 10 | + * 1. Redistributions of source code must retain the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in the |
| 14 | + * documentation and/or other materials provided with the distribution. |
| 15 | + * 3. Neither the name of the University nor the names of its contributors |
| 16 | + * may be used to endorse or promote products derived from this software |
| 17 | + * without specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 20 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 23 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 24 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 25 | + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 26 | + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 27 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 28 | + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 29 | + * SUCH DAMAGE. |
| 30 | + */ |
| 31 | + |
| 32 | +#if defined(LIBC_SCCS) && !defined(lint) |
| 33 | +static char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95"; |
| 34 | +#endif /* LIBC_SCCS and not lint */ |
| 35 | + |
| 36 | +#include <stdio.h> |
| 37 | +#include <stdlib.h> |
| 38 | +#include <string.h> |
| 39 | + |
| 40 | +#ifdef WIN32 |
| 41 | + |
| 42 | +#ifdef _MSC_VER |
| 43 | +/* ignore MSVC++ warnings that are annoying and hard to remove: |
| 44 | + 4702 unreachable code |
| 45 | + (there is an unreachable assert(0) in case somehow it is reached) |
| 46 | + */ |
| 47 | +#pragma warning( disable : 4702 ) |
| 48 | +#endif |
| 49 | + |
| 50 | +#else /* WIN32 */ |
| 51 | +#include <sys/cdefs.h> |
| 52 | +#include <unistd.h> |
| 53 | +#endif |
| 54 | + |
| 55 | +int opterr = 1, /* if error message should be printed */ |
| 56 | + optind = 1, /* index into parent argv vector */ |
| 57 | + optopt, /* character checked for validity */ |
| 58 | + optreset; /* reset getopt */ |
| 59 | +char *optarg; /* argument associated with option */ |
| 60 | + |
| 61 | +#define BADCH (int)'?' |
| 62 | +#define BADARG (int)':' |
| 63 | +#define EMSG "" |
| 64 | + |
| 65 | +/* |
| 66 | + * getopt -- |
| 67 | + * Parse argc/argv argument vector. |
| 68 | + */ |
| 69 | +int |
| 70 | +getopt(int nargc, char * const nargv[], const char *ostr) |
| 71 | +{ |
| 72 | + static char *place = EMSG; /* option letter processing */ |
| 73 | + char *oli; /* option letter list index */ |
| 74 | + |
| 75 | + if (optreset || *place == 0) { /* update scanning pointer */ |
| 76 | + optreset = 0; |
| 77 | + place = nargv[optind]; |
| 78 | + if (optind >= nargc || *place++ != '-') { |
| 79 | + /* Argument is absent or is not an option */ |
| 80 | + place = EMSG; |
| 81 | + return (-1); |
| 82 | + } |
| 83 | + optopt = *place++; |
| 84 | + if (optopt == '-' && *place == 0) { |
| 85 | + /* "--" => end of options */ |
| 86 | + ++optind; |
| 87 | + place = EMSG; |
| 88 | + return (-1); |
| 89 | + } |
| 90 | + if (optopt == 0) { |
| 91 | + /* Solitary '-', treat as a '-' option |
| 92 | + if the program (eg su) is looking for it. */ |
| 93 | + place = EMSG; |
| 94 | + if (strchr(ostr, '-') == NULL) |
| 95 | + return (-1); |
| 96 | + optopt = '-'; |
| 97 | + } |
| 98 | + } else |
| 99 | + optopt = *place++; |
| 100 | + |
| 101 | + /* See if option letter is one the caller wanted... */ |
| 102 | + if (optopt == ':' || (oli = strchr(ostr, optopt)) == NULL) { |
| 103 | + if (*place == 0) |
| 104 | + ++optind; |
| 105 | + if (opterr && *ostr != ':') |
| 106 | + (void)fprintf(stderr, |
| 107 | + "iojs: illegal option -- %c\n", optopt); |
| 108 | + return (BADCH); |
| 109 | + } |
| 110 | + |
| 111 | + /* Does this option need an argument? */ |
| 112 | + if (oli[1] != ':') { |
| 113 | + /* don't need argument */ |
| 114 | + optarg = NULL; |
| 115 | + if (*place == 0) |
| 116 | + ++optind; |
| 117 | + } else { |
| 118 | + /* Option-argument is either the rest of this argument or the |
| 119 | + entire next argument. */ |
| 120 | + if (*place) |
| 121 | + optarg = place; |
| 122 | + else if (oli[2] == ':') |
| 123 | + /* |
| 124 | + * GNU Extension, for optional arguments if the rest of |
| 125 | + * the argument is empty, we return NULL |
| 126 | + */ |
| 127 | + optarg = NULL; |
| 128 | + else if (nargc > ++optind) |
| 129 | + optarg = nargv[optind]; |
| 130 | + else { |
| 131 | + /* option-argument absent */ |
| 132 | + place = EMSG; |
| 133 | + if (*ostr == ':') |
| 134 | + return (BADARG); |
| 135 | + if (opterr) |
| 136 | + (void)fprintf(stderr, |
| 137 | + "iojs: option requires an argument -- %c\n", optopt); |
| 138 | + return (BADCH); |
| 139 | + } |
| 140 | + place = EMSG; |
| 141 | + ++optind; |
| 142 | + } |
| 143 | + return (optopt); /* return option letter */ |
| 144 | +} |
0 commit comments