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