Path: blob/main/crypto/krb5/src/util/support/getopt.c
34889 views
/* -*- mode: c; c-file-style: "bsd"; indent-tabs-mode: t -*- */1/* $NetBSD: getopt.c,v 1.16 1999/12/02 13:15:56 kleink Exp $ */23/*4* Copyright (c) 1987, 1993, 19945* The Regents of the University of California. All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15* 3. All advertising materials mentioning features or use of this software16* must display the following acknowledgement:17* This product includes software developed by the University of18* California, Berkeley and its contributors.19* 4. Neither the name of the University nor the names of its contributors20* may be used to endorse or promote products derived from this software21* without specific prior written permission.22*23* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND24* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE25* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE26* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE27* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL28* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS29* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)30* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT31* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY32* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF33* SUCH DAMAGE.34*/3536#if !defined(HAVE_GETOPT) || !defined(HAVE_UNISTD_H)37#if 038static char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95";39#endif4041#define K5_GETOPT_C4243#include <assert.h>44#include <errno.h>45#include <stdio.h>46#include <string.h>47#include "k5-platform.h"4849#define __P(x) x50#define _DIAGASSERT(x) assert(x)5152int opterr = 1, /* if error message should be printed */53optind = 1, /* index into parent argv vector */54optopt; /* character checked for validity */55char *optarg; /* argument associated with option */5657static char * _progname __P((char *));58int getopt_internal __P((int, char * const *, const char *));5960static char *61_progname(nargv0)62char * nargv0;63{64char * tmp;6566_DIAGASSERT(nargv0 != NULL);6768tmp = strrchr(nargv0, '/');69if (tmp)70tmp++;71else72tmp = nargv0;73return(tmp);74}7576#define BADCH (int)'?'77#define BADARG (int)':'78#define EMSG ""7980/*81* getopt --82* Parse argc/argv argument vector.83*/84int85getopt(nargc, nargv, ostr)86int nargc;87char * const nargv[];88const char *ostr;89{90static char *__progname = 0;91static char *place = EMSG; /* option letter processing */92char *oli; /* option letter list index */93__progname = __progname?__progname:_progname(*nargv);9495_DIAGASSERT(nargv != NULL);96_DIAGASSERT(ostr != NULL);9798if (!*place) { /* update scanning pointer */99if (optind >= nargc || *(place = nargv[optind]) != '-') {100place = EMSG;101return (-1);102}103if (place[1] && *++place == '-' /* found "--" */104&& place[1] == '\0') {105++optind;106place = EMSG;107return (-1);108}109} /* option letter okay? */110if ((optopt = (int)*place++) == (int)':' ||111!(oli = strchr(ostr, optopt))) {112/*113* if the user didn't specify '-' as an option,114* assume it means -1.115*/116if (optopt == (int)'-')117return (-1);118if (!*place)119++optind;120if (opterr && *ostr != ':')121(void)fprintf(stderr,122"%s: illegal option -- %c\n", __progname, optopt);123return (BADCH);124}125if (*++oli != ':') { /* don't need argument */126optarg = NULL;127if (!*place)128++optind;129}130else { /* need an argument */131if (*place) /* no white space */132optarg = place;133else if (nargc <= ++optind) { /* no arg */134place = EMSG;135if (*ostr == ':')136return (BADARG);137if (opterr)138(void)fprintf(stderr,139"%s: option requires an argument -- %c\n",140__progname, optopt);141return (BADCH);142}143else /* white space */144optarg = nargv[optind];145place = EMSG;146++optind;147}148return (optopt); /* dump back option letter */149}150#endif /* !defined(HAVE_GETOPT) || !defined(HAVE_UNISTD_H) */151152153