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