/* $NetBSD: getopt.c,v 1.29 2014/06/05 22:00:22 christos Exp $ */12/*-3* SPDX-License-Identifier: BSD-3-Clause4*5* Copyright (c) 1987, 1993, 19946* The Regents of the University of California. All rights reserved.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16* 3. Neither the name of the University nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "namespace.h"34#include <stdio.h>35#include <stdlib.h>36#include <string.h>37#include <unistd.h>38#include "un-namespace.h"3940#include "libc_private.h"4142int opterr = 1, /* if error message should be printed */43optind = 1, /* index into parent argv vector */44optopt, /* character checked for validity */45optreset; /* reset getopt */46char *optarg; /* argument associated with option */4748#define BADCH (int)'?'49#define BADARG (int)':'50static char EMSG[] = "";51/*52* getopt --53* Parse argc/argv argument vector.54*/55int56getopt(int nargc, char * const nargv[], const char *ostr)57{58static char *place = EMSG; /* option letter processing */59char *oli; /* option letter list index */6061if (optreset || *place == 0) { /* update scanning pointer */62optreset = 0;63place = nargv[optind];64if (optind >= nargc || *place++ != '-') {65/* Argument is absent or is not an option */66place = EMSG;67return (-1);68}69optopt = *place++;70if (optopt == '-' && *place == 0) {71/* "--" => end of options */72++optind;73place = EMSG;74return (-1);75}76if (optopt == 0) {77/* Solitary '-', treat as a '-' option78if the program (eg su) is looking for it. */79place = EMSG;80if (strchr(ostr, '-') == NULL)81return (-1);82optopt = '-';83}84} else85optopt = *place++;8687/* See if option letter is one the caller wanted... */88if (optopt == ':' || (oli = strchr(ostr, optopt)) == NULL) {89if (*place == 0)90++optind;91if (opterr && *ostr != ':')92(void)fprintf(stderr,93"%s: illegal option -- %c\n", _getprogname(),94optopt);95return (BADCH);96}9798/* Does this option need an argument? */99if (oli[1] != ':') {100/* don't need argument */101optarg = NULL;102if (*place == 0)103++optind;104} else {105/* Option-argument is either the rest of this argument or the106entire next argument. */107if (*place)108optarg = place;109else if (oli[2] == ':')110/*111* GNU Extension, for optional arguments if the rest of112* the argument is empty, we return NULL113*/114optarg = NULL;115else if (nargc > ++optind)116optarg = nargv[optind];117else {118/* option-argument absent */119place = EMSG;120if (*ostr == ':')121return (BADARG);122if (opterr)123(void)fprintf(stderr,124"%s: option requires an argument -- %c\n",125_getprogname(), optopt);126return (BADCH);127}128place = EMSG;129++optind;130}131return (optopt); /* return option letter */132}133134135