/* $NetBSD: getopt.c,v 1.30 2024/01/19 18:41:38 christos Exp $ */12/*3* Copyright (c) 1987, 1993, 19944* The Regents of the University of California. All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. Neither the name of the University nor the names of its contributors15* may be used to endorse or promote products derived from this software16* without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*/3031#ifdef HAVE_CONFIG_H32# include "config.h"33#endif34#if !defined(HAVE_GETOPT) || defined(WANT_GETOPT_LONG) || defined(BROKEN_GETOPT)35#include <sys/cdefs.h>3637#include <stdio.h>38#include <stdlib.h>39#include <string.h>404142#define BADCH (int)'?'43#define BADARG (int)':'44#define EMSG ""4546int opterr = 1, /* if error message should be printed */47optind = 1, /* index into parent argv vector */48optopt = BADCH, /* character checked for validity */49optreset; /* reset getopt */50char *optarg; /* argument associated with option */5152/*53* getopt --54* Parse argc/argv argument vector.55*/56int57getopt(int nargc, char * const nargv[], const char *ostr)58{59extern char *__progname;60static const char *place = EMSG; /* option letter processing */61const char *oli; /* option letter list index */6263#ifndef BSD4_464if (!__progname) {65if (__progname = strrchr(nargv[0], '/'))66++__progname;67else68__progname = nargv[0];69}70#endif7172if (optreset || *place == 0) { /* update scanning pointer */73optreset = 0;74place = nargv[optind];75if (optind >= nargc || *place++ != '-') {76/* Argument is absent or is not an option */77place = EMSG;78return (-1);79}80optopt = *place++;81if (optopt == '-' && *place == 0) {82/* "--" => end of options */83++optind;84place = EMSG;85return (-1);86}87if (optopt == 0) {88/* Solitary '-', treat as a '-' option89if the program (eg su) is looking for it. */90place = EMSG;91if (strchr(ostr, '-') == NULL)92return -1;93optopt = '-';94}95} else96optopt = *place++;9798/* See if option letter is one the caller wanted... */99if (optopt == ':' || (oli = strchr(ostr, optopt)) == NULL) {100if (*place == 0)101++optind;102if (opterr && *ostr != ':')103(void)fprintf(stderr,104"%s: unknown option -- %c\n", __progname, optopt);105return (BADCH);106}107108/* Does this option need an argument? */109if (oli[1] != ':') {110/* don't need argument */111optarg = NULL;112if (*place == 0)113++optind;114} else {115/* Option-argument is either the rest of this argument or the116entire next argument. */117if (*place)118optarg = __UNCONST(place);119else if (oli[2] == ':')120/*121* GNU Extension, for optional arguments if the rest of122* the argument is empty, we return NULL123*/124optarg = NULL;125else if (nargc > ++optind)126optarg = nargv[optind];127else {128/* option-argument absent */129place = EMSG;130if (*ostr == ':')131return (BADARG);132if (opterr)133(void)fprintf(stderr,134"%s: option requires an argument -- %c\n",135__progname, optopt);136return (BADCH);137}138place = EMSG;139++optind;140}141return (optopt); /* return option letter */142}143#endif144#ifdef MAIN145#ifndef BSD4_4146char *__progname;147#endif148149int150main(argc, argv)151int argc;152char *argv[];153{154int c;155char *opts = argv[1];156157--argc;158++argv;159160while ((c = getopt(argc, argv, opts)) != EOF) {161switch (c) {162case '-':163if (optarg)164printf("--%s ", optarg);165break;166case '?':167exit(1);168break;169default:170if (optarg)171printf("-%c %s ", c, optarg);172else173printf("-%c ", c);174break;175}176}177178if (optind < argc) {179printf("-- ");180for (; optind < argc; ++optind) {181printf("%s ", argv[optind]);182}183}184printf("\n");185exit(0);186}187#endif188189190