1*0a6a1f1dSLionel Sambuc /* $NetBSD: getopt.c,v 1.29 2014/06/05 22:00:22 christos Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras * Copyright (c) 1987, 1993, 1994
52fe8fb19SBen Gras * The Regents of the University of California. All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras * are met:
102fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras * 3. Neither the name of the University nor the names of its contributors
162fe8fb19SBen Gras * may be used to endorse or promote products derived from this software
172fe8fb19SBen Gras * without specific prior written permission.
182fe8fb19SBen Gras *
192fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
202fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212fe8fb19SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222fe8fb19SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
232fe8fb19SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242fe8fb19SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252fe8fb19SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262fe8fb19SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272fe8fb19SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282fe8fb19SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292fe8fb19SBen Gras * SUCH DAMAGE.
302fe8fb19SBen Gras */
312fe8fb19SBen Gras
322fe8fb19SBen Gras #include <sys/cdefs.h>
33*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: getopt.c,v 1.29 2014/06/05 22:00:22 christos Exp $");
342fe8fb19SBen Gras
352fe8fb19SBen Gras #include "namespace.h"
362fe8fb19SBen Gras
372fe8fb19SBen Gras #include <assert.h>
382fe8fb19SBen Gras #include <errno.h>
392fe8fb19SBen Gras #include <stdio.h>
402fe8fb19SBen Gras #include <stdlib.h>
412fe8fb19SBen Gras #include <string.h>
422fe8fb19SBen Gras #include <unistd.h>
432fe8fb19SBen Gras
442fe8fb19SBen Gras #ifdef __weak_alias
452fe8fb19SBen Gras __weak_alias(getopt,_getopt)
462fe8fb19SBen Gras #endif
472fe8fb19SBen Gras
482fe8fb19SBen Gras int opterr = 1, /* if error message should be printed */
492fe8fb19SBen Gras optind = 1, /* index into parent argv vector */
502fe8fb19SBen Gras optopt, /* character checked for validity */
512fe8fb19SBen Gras optreset; /* reset getopt */
522fe8fb19SBen Gras char *optarg; /* argument associated with option */
532fe8fb19SBen Gras
542fe8fb19SBen Gras #define BADCH (int)'?'
552fe8fb19SBen Gras #define BADARG (int)':'
562fe8fb19SBen Gras #define EMSG ""
572fe8fb19SBen Gras
582fe8fb19SBen Gras /*
592fe8fb19SBen Gras * getopt --
602fe8fb19SBen Gras * Parse argc/argv argument vector.
612fe8fb19SBen Gras */
622fe8fb19SBen Gras int
getopt(int nargc,char * const nargv[],const char * ostr)632fe8fb19SBen Gras getopt(int nargc, char * const nargv[], const char *ostr)
642fe8fb19SBen Gras {
652fe8fb19SBen Gras static const char *place = EMSG; /* option letter processing */
662fe8fb19SBen Gras char *oli; /* option letter list index */
672fe8fb19SBen Gras
682fe8fb19SBen Gras _DIAGASSERT(nargv != NULL);
692fe8fb19SBen Gras _DIAGASSERT(ostr != NULL);
702fe8fb19SBen Gras
712fe8fb19SBen Gras if (optreset || *place == 0) { /* update scanning pointer */
722fe8fb19SBen Gras optreset = 0;
732fe8fb19SBen Gras place = nargv[optind];
742fe8fb19SBen Gras if (optind >= nargc || *place++ != '-') {
752fe8fb19SBen Gras /* Argument is absent or is not an option */
762fe8fb19SBen Gras place = EMSG;
772fe8fb19SBen Gras return (-1);
782fe8fb19SBen Gras }
792fe8fb19SBen Gras optopt = *place++;
802fe8fb19SBen Gras if (optopt == '-' && *place == 0) {
812fe8fb19SBen Gras /* "--" => end of options */
822fe8fb19SBen Gras ++optind;
832fe8fb19SBen Gras place = EMSG;
842fe8fb19SBen Gras return (-1);
852fe8fb19SBen Gras }
862fe8fb19SBen Gras if (optopt == 0) {
872fe8fb19SBen Gras /* Solitary '-', treat as a '-' option
882fe8fb19SBen Gras if the program (eg su) is looking for it. */
892fe8fb19SBen Gras place = EMSG;
902fe8fb19SBen Gras if (strchr(ostr, '-') == NULL)
912fe8fb19SBen Gras return -1;
922fe8fb19SBen Gras optopt = '-';
932fe8fb19SBen Gras }
942fe8fb19SBen Gras } else
952fe8fb19SBen Gras optopt = *place++;
962fe8fb19SBen Gras
972fe8fb19SBen Gras /* See if option letter is one the caller wanted... */
982fe8fb19SBen Gras if (optopt == ':' || (oli = strchr(ostr, optopt)) == NULL) {
992fe8fb19SBen Gras if (*place == 0)
1002fe8fb19SBen Gras ++optind;
1012fe8fb19SBen Gras if (opterr && *ostr != ':')
1022fe8fb19SBen Gras (void)fprintf(stderr,
1032fe8fb19SBen Gras "%s: unknown option -- %c\n", getprogname(),
1042fe8fb19SBen Gras optopt);
1052fe8fb19SBen Gras return (BADCH);
1062fe8fb19SBen Gras }
1072fe8fb19SBen Gras
1082fe8fb19SBen Gras /* Does this option need an argument? */
1092fe8fb19SBen Gras if (oli[1] != ':') {
1102fe8fb19SBen Gras /* don't need argument */
1112fe8fb19SBen Gras optarg = NULL;
1122fe8fb19SBen Gras if (*place == 0)
1132fe8fb19SBen Gras ++optind;
1142fe8fb19SBen Gras } else {
1152fe8fb19SBen Gras /* Option-argument is either the rest of this argument or the
1162fe8fb19SBen Gras entire next argument. */
1172fe8fb19SBen Gras if (*place)
1182fe8fb19SBen Gras optarg = __UNCONST(place);
119*0a6a1f1dSLionel Sambuc else if (oli[2] == ':')
120*0a6a1f1dSLionel Sambuc /*
121*0a6a1f1dSLionel Sambuc * GNU Extension, for optional arguments if the rest of
122*0a6a1f1dSLionel Sambuc * the argument is empty, we return NULL
123*0a6a1f1dSLionel Sambuc */
124*0a6a1f1dSLionel Sambuc optarg = NULL;
1252fe8fb19SBen Gras else if (nargc > ++optind)
1262fe8fb19SBen Gras optarg = nargv[optind];
1272fe8fb19SBen Gras else {
1282fe8fb19SBen Gras /* option-argument absent */
1292fe8fb19SBen Gras place = EMSG;
1302fe8fb19SBen Gras if (*ostr == ':')
1312fe8fb19SBen Gras return (BADARG);
1322fe8fb19SBen Gras if (opterr)
1332fe8fb19SBen Gras (void)fprintf(stderr,
1342fe8fb19SBen Gras "%s: option requires an argument -- %c\n",
1352fe8fb19SBen Gras getprogname(), optopt);
1362fe8fb19SBen Gras return (BADCH);
1372fe8fb19SBen Gras }
1382fe8fb19SBen Gras place = EMSG;
1392fe8fb19SBen Gras ++optind;
1402fe8fb19SBen Gras }
1412fe8fb19SBen Gras return (optopt); /* return option letter */
1422fe8fb19SBen Gras }
143