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