Home | History | Annotate | Line # | Download | only in stdlib
getopt_long.c revision 1.16.2.1
      1  1.16.2.1      tron /*	$NetBSD: getopt_long.c,v 1.16.2.1 2004/06/22 07:21:29 tron Exp $	*/
      2       1.4  christos 
      3       1.4  christos /*-
      4       1.4  christos  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5       1.4  christos  * All rights reserved.
      6       1.4  christos  *
      7       1.4  christos  * This code is derived from software contributed to The NetBSD Foundation
      8       1.4  christos  * by Dieter Baron and Thomas Klausner.
      9       1.1       mcr  *
     10       1.1       mcr  * Redistribution and use in source and binary forms, with or without
     11       1.1       mcr  * modification, are permitted provided that the following conditions
     12       1.1       mcr  * are met:
     13       1.1       mcr  * 1. Redistributions of source code must retain the above copyright
     14       1.1       mcr  *    notice, this list of conditions and the following disclaimer.
     15       1.1       mcr  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1       mcr  *    notice, this list of conditions and the following disclaimer in the
     17       1.1       mcr  *    documentation and/or other materials provided with the distribution.
     18       1.1       mcr  * 3. All advertising materials mentioning features or use of this software
     19       1.1       mcr  *    must display the following acknowledgement:
     20       1.4  christos  *        This product includes software developed by the NetBSD
     21       1.4  christos  *        Foundation, Inc. and its contributors.
     22       1.4  christos  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.4  christos  *    contributors may be used to endorse or promote products derived
     24       1.4  christos  *    from this software without specific prior written permission.
     25       1.1       mcr  *
     26       1.4  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.4  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.4  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.4  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.4  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.4  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.4  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.4  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.4  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.4  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.4  christos  * POSSIBILITY OF SUCH DAMAGE.
     37       1.1       mcr  */
     38       1.1       mcr 
     39  1.16.2.1      tron #if HAVE_NBTOOL_CONFIG_H
     40  1.16.2.1      tron #include "nbtool_config.h"
     41  1.16.2.1      tron #endif
     42  1.16.2.1      tron 
     43       1.2     lukem #include <sys/cdefs.h>
     44       1.4  christos #if defined(LIBC_SCCS) && !defined(lint)
     45  1.16.2.1      tron __RCSID("$NetBSD: getopt_long.c,v 1.16.2.1 2004/06/22 07:21:29 tron Exp $");
     46       1.4  christos #endif /* LIBC_SCCS and not lint */
     47       1.2     lukem 
     48       1.5  christos #include "namespace.h"
     49       1.5  christos 
     50       1.2     lukem #include <assert.h>
     51      1.15        tv #include <err.h>
     52       1.2     lukem #include <errno.h>
     53  1.16.2.1      tron #if HAVE_NBTOOL_CONFIG_H
     54  1.16.2.1      tron #include "compat_getopt.h"
     55  1.16.2.1      tron #else
     56      1.15        tv #include <getopt.h>
     57  1.16.2.1      tron #endif
     58       1.1       mcr #include <stdlib.h>
     59       1.1       mcr #include <string.h>
     60       1.4  christos 
     61      1.16     lukem #if HAVE_NBTOOL_CONFIG_H && !HAVE_GETOPT_LONG && !HAVE_DECL_OPTIND
     62      1.15        tv #define REPLACE_GETOPT
     63      1.14        tv #endif
     64      1.14        tv 
     65       1.4  christos #ifdef REPLACE_GETOPT
     66       1.4  christos #ifdef __weak_alias
     67       1.4  christos __weak_alias(getopt,_getopt)
     68       1.4  christos #endif
     69       1.4  christos int	opterr = 1;		/* if error message should be printed */
     70       1.4  christos int	optind = 1;		/* index into parent argv vector */
     71       1.4  christos int	optopt = '?';		/* character checked for validity */
     72       1.4  christos int	optreset;		/* reset getopt */
     73       1.4  christos char    *optarg;		/* argument associated with option */
     74      1.16     lukem #elif HAVE_NBTOOL_CONFIG_H && !HAVE_DECL_OPTRESET
     75      1.14        tv static int optreset;
     76       1.4  christos #endif
     77       1.1       mcr 
     78       1.4  christos #ifdef __weak_alias
     79       1.4  christos __weak_alias(getopt_long,_getopt_long)
     80       1.4  christos #endif
     81       1.2     lukem 
     82      1.15        tv #if !HAVE_GETOPT_LONG
     83       1.4  christos #define IGNORE_FIRST	(*options == '-' || *options == '+')
     84       1.4  christos #define PRINT_ERROR	((opterr) && ((*options != ':') \
     85       1.4  christos 				      || (IGNORE_FIRST && options[1] != ':')))
     86       1.4  christos #define IS_POSIXLY_CORRECT (getenv("POSIXLY_CORRECT") != NULL)
     87       1.4  christos #define PERMUTE         (!IS_POSIXLY_CORRECT && !IGNORE_FIRST)
     88       1.4  christos /* XXX: GNU ignores PC if *options == '-' */
     89       1.4  christos #define IN_ORDER        (!IS_POSIXLY_CORRECT && *options == '-')
     90       1.1       mcr 
     91       1.4  christos /* return values */
     92       1.1       mcr #define	BADCH	(int)'?'
     93       1.9       wiz #define	BADARG		((IGNORE_FIRST && options[1] == ':') \
     94       1.9       wiz 			 || (*options == ':') ? (int)':' : (int)'?')
     95       1.4  christos #define INORDER (int)1
     96       1.4  christos 
     97       1.1       mcr #define	EMSG	""
     98       1.1       mcr 
     99       1.4  christos static int getopt_internal __P((int, char * const *, const char *));
    100       1.4  christos static int gcd __P((int, int));
    101       1.4  christos static void permute_args __P((int, int, int, char * const *));
    102       1.4  christos 
    103       1.4  christos static char *place = EMSG; /* option letter processing */
    104       1.4  christos 
    105       1.4  christos /* XXX: set optreset to 1 rather than these two */
    106       1.4  christos static int nonopt_start = -1; /* first non option argument (for permute) */
    107       1.4  christos static int nonopt_end = -1;   /* first option after non options (for permute) */
    108       1.4  christos 
    109       1.4  christos /* Error messages */
    110       1.6   nathanw static const char recargchar[] = "option requires an argument -- %c";
    111       1.6   nathanw static const char recargstring[] = "option requires an argument -- %s";
    112       1.4  christos static const char ambig[] = "ambiguous option -- %.*s";
    113       1.4  christos static const char noarg[] = "option doesn't take an argument -- %.*s";
    114      1.12      joda static const char illoptchar[] = "unknown option -- %c";
    115      1.12      joda static const char illoptstring[] = "unknown option -- %s";
    116       1.4  christos 
    117       1.4  christos 
    118       1.1       mcr /*
    119       1.4  christos  * Compute the greatest common divisor of a and b.
    120       1.4  christos  */
    121       1.4  christos static int
    122       1.4  christos gcd(a, b)
    123       1.4  christos 	int a;
    124       1.4  christos 	int b;
    125       1.4  christos {
    126       1.4  christos 	int c;
    127       1.4  christos 
    128       1.4  christos 	c = a % b;
    129       1.4  christos 	while (c != 0) {
    130       1.4  christos 		a = b;
    131       1.4  christos 		b = c;
    132       1.4  christos 		c = a % b;
    133       1.4  christos 	}
    134       1.4  christos 
    135       1.4  christos 	return b;
    136       1.4  christos }
    137       1.4  christos 
    138       1.4  christos /*
    139       1.4  christos  * Exchange the block from nonopt_start to nonopt_end with the block
    140       1.4  christos  * from nonopt_end to opt_end (keeping the same order of arguments
    141       1.4  christos  * in each block).
    142       1.4  christos  */
    143       1.4  christos static void
    144      1.13     lukem permute_args(panonopt_start, panonopt_end, opt_end, nargv)
    145      1.13     lukem 	int panonopt_start;
    146      1.13     lukem 	int panonopt_end;
    147       1.4  christos 	int opt_end;
    148       1.4  christos 	char * const *nargv;
    149       1.4  christos {
    150       1.4  christos 	int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
    151       1.4  christos 	char *swap;
    152       1.4  christos 
    153      1.10     lukem 	_DIAGASSERT(nargv != NULL);
    154      1.10     lukem 
    155       1.4  christos 	/*
    156       1.4  christos 	 * compute lengths of blocks and number and size of cycles
    157       1.4  christos 	 */
    158      1.13     lukem 	nnonopts = panonopt_end - panonopt_start;
    159      1.13     lukem 	nopts = opt_end - panonopt_end;
    160       1.4  christos 	ncycle = gcd(nnonopts, nopts);
    161      1.13     lukem 	cyclelen = (opt_end - panonopt_start) / ncycle;
    162       1.4  christos 
    163       1.4  christos 	for (i = 0; i < ncycle; i++) {
    164      1.13     lukem 		cstart = panonopt_end+i;
    165       1.4  christos 		pos = cstart;
    166       1.4  christos 		for (j = 0; j < cyclelen; j++) {
    167      1.13     lukem 			if (pos >= panonopt_end)
    168       1.4  christos 				pos -= nnonopts;
    169       1.4  christos 			else
    170       1.4  christos 				pos += nopts;
    171       1.4  christos 			swap = nargv[pos];
    172       1.4  christos 			/* LINTED const cast */
    173       1.4  christos 			((char **) nargv)[pos] = nargv[cstart];
    174       1.4  christos 			/* LINTED const cast */
    175       1.4  christos 			((char **)nargv)[cstart] = swap;
    176       1.4  christos 		}
    177       1.4  christos 	}
    178       1.4  christos }
    179       1.4  christos 
    180       1.4  christos /*
    181       1.4  christos  * getopt_internal --
    182       1.4  christos  *	Parse argc/argv argument vector.  Called by user level routines.
    183       1.4  christos  *  Returns -2 if -- is found (can be long option or end of options marker).
    184       1.1       mcr  */
    185       1.4  christos static int
    186       1.4  christos getopt_internal(nargc, nargv, options)
    187       1.1       mcr 	int nargc;
    188       1.1       mcr 	char * const *nargv;
    189       1.4  christos 	const char *options;
    190       1.1       mcr {
    191       1.1       mcr 	char *oli;				/* option letter list index */
    192       1.4  christos 	int optchar;
    193       1.1       mcr 
    194       1.2     lukem 	_DIAGASSERT(nargv != NULL);
    195       1.4  christos 	_DIAGASSERT(options != NULL);
    196       1.4  christos 
    197       1.4  christos 	optarg = NULL;
    198       1.8   thorpej 
    199       1.8   thorpej 	/*
    200       1.8   thorpej 	 * XXX Some programs (like rsyncd) expect to be able to
    201       1.8   thorpej 	 * XXX re-initialize optind to 0 and have getopt_long(3)
    202       1.8   thorpej 	 * XXX properly function again.  Work around this braindamage.
    203       1.8   thorpej 	 */
    204       1.8   thorpej 	if (optind == 0)
    205       1.8   thorpej 		optind = 1;
    206       1.2     lukem 
    207       1.4  christos 	if (optreset)
    208       1.4  christos 		nonopt_start = nonopt_end = -1;
    209       1.4  christos start:
    210       1.1       mcr 	if (optreset || !*place) {		/* update scanning pointer */
    211       1.1       mcr 		optreset = 0;
    212       1.4  christos 		if (optind >= nargc) {          /* end of argument vector */
    213       1.4  christos 			place = EMSG;
    214       1.4  christos 			if (nonopt_end != -1) {
    215       1.4  christos 				/* do permutation, if we have to */
    216       1.4  christos 				permute_args(nonopt_start, nonopt_end,
    217       1.4  christos 				    optind, nargv);
    218       1.4  christos 				optind -= nonopt_end - nonopt_start;
    219       1.4  christos 			}
    220       1.4  christos 			else if (nonopt_start != -1) {
    221       1.4  christos 				/*
    222       1.4  christos 				 * If we skipped non-options, set optind
    223       1.4  christos 				 * to the first of them.
    224       1.4  christos 				 */
    225       1.4  christos 				optind = nonopt_start;
    226       1.4  christos 			}
    227       1.4  christos 			nonopt_start = nonopt_end = -1;
    228       1.4  christos 			return -1;
    229       1.4  christos 		}
    230       1.9       wiz 		if ((*(place = nargv[optind]) != '-')
    231       1.9       wiz 		    || (place[1] == '\0')) {    /* found non-option */
    232       1.1       mcr 			place = EMSG;
    233       1.4  christos 			if (IN_ORDER) {
    234       1.4  christos 				/*
    235       1.4  christos 				 * GNU extension:
    236       1.4  christos 				 * return non-option as argument to option 1
    237       1.4  christos 				 */
    238       1.4  christos 				optarg = nargv[optind++];
    239       1.4  christos 				return INORDER;
    240       1.4  christos 			}
    241       1.4  christos 			if (!PERMUTE) {
    242       1.4  christos 				/*
    243       1.4  christos 				 * if no permutation wanted, stop parsing
    244       1.4  christos 				 * at first non-option
    245       1.4  christos 				 */
    246       1.4  christos 				return -1;
    247       1.4  christos 			}
    248       1.4  christos 			/* do permutation */
    249       1.4  christos 			if (nonopt_start == -1)
    250       1.4  christos 				nonopt_start = optind;
    251       1.4  christos 			else if (nonopt_end != -1) {
    252       1.4  christos 				permute_args(nonopt_start, nonopt_end,
    253       1.4  christos 				    optind, nargv);
    254       1.4  christos 				nonopt_start = optind -
    255       1.4  christos 				    (nonopt_end - nonopt_start);
    256       1.4  christos 				nonopt_end = -1;
    257       1.4  christos 			}
    258       1.4  christos 			optind++;
    259       1.4  christos 			/* process next argument */
    260       1.4  christos 			goto start;
    261       1.1       mcr 		}
    262       1.4  christos 		if (nonopt_start != -1 && nonopt_end == -1)
    263       1.4  christos 			nonopt_end = optind;
    264       1.1       mcr 		if (place[1] && *++place == '-') {	/* found "--" */
    265       1.4  christos 			place++;
    266       1.4  christos 			return -2;
    267       1.4  christos 		}
    268       1.4  christos 	}
    269       1.4  christos 	if ((optchar = (int)*place++) == (int)':' ||
    270       1.4  christos 	    (oli = strchr(options + (IGNORE_FIRST ? 1 : 0), optchar)) == NULL) {
    271       1.4  christos 		/* option letter unknown or ':' */
    272       1.4  christos 		if (!*place)
    273       1.4  christos 			++optind;
    274       1.4  christos 		if (PRINT_ERROR)
    275       1.6   nathanw 			warnx(illoptchar, optchar);
    276       1.4  christos 		optopt = optchar;
    277       1.4  christos 		return BADCH;
    278       1.4  christos 	}
    279       1.4  christos 	if (optchar == 'W' && oli[1] == ';') {		/* -W long-option */
    280       1.4  christos 		/* XXX: what if no long options provided (called by getopt)? */
    281       1.4  christos 		if (*place)
    282       1.4  christos 			return -2;
    283       1.4  christos 
    284       1.4  christos 		if (++optind >= nargc) {	/* no arg */
    285       1.1       mcr 			place = EMSG;
    286       1.4  christos 			if (PRINT_ERROR)
    287       1.6   nathanw 				warnx(recargchar, optchar);
    288       1.4  christos 			optopt = optchar;
    289       1.4  christos 			return BADARG;
    290       1.4  christos 		} else				/* white space */
    291       1.4  christos 			place = nargv[optind];
    292       1.1       mcr 		/*
    293       1.4  christos 		 * Handle -W arg the same as --arg (which causes getopt to
    294       1.4  christos 		 * stop parsing).
    295       1.1       mcr 		 */
    296       1.4  christos 		return -2;
    297       1.4  christos 	}
    298       1.4  christos 	if (*++oli != ':') {			/* doesn't take argument */
    299       1.1       mcr 		if (!*place)
    300       1.1       mcr 			++optind;
    301       1.4  christos 	} else {				/* takes (optional) argument */
    302       1.1       mcr 		optarg = NULL;
    303       1.1       mcr 		if (*place)			/* no white space */
    304       1.1       mcr 			optarg = place;
    305       1.4  christos 		/* XXX: disable test for :: if PC? (GNU doesn't) */
    306       1.4  christos 		else if (oli[1] != ':') {	/* arg not optional */
    307       1.4  christos 			if (++optind >= nargc) {	/* no arg */
    308       1.4  christos 				place = EMSG;
    309       1.4  christos 				if (PRINT_ERROR)
    310       1.6   nathanw 					warnx(recargchar, optchar);
    311       1.4  christos 				optopt = optchar;
    312       1.4  christos 				return BADARG;
    313       1.4  christos 			} else
    314       1.4  christos 				optarg = nargv[optind];
    315       1.4  christos 		}
    316       1.1       mcr 		place = EMSG;
    317       1.1       mcr 		++optind;
    318       1.1       mcr 	}
    319       1.4  christos 	/* dump back option letter */
    320       1.4  christos 	return optchar;
    321       1.1       mcr }
    322       1.1       mcr 
    323       1.4  christos #ifdef REPLACE_GETOPT
    324       1.1       mcr /*
    325       1.1       mcr  * getopt --
    326       1.1       mcr  *	Parse argc/argv argument vector.
    327       1.4  christos  *
    328       1.4  christos  * [eventually this will replace the real getopt]
    329       1.1       mcr  */
    330       1.1       mcr int
    331       1.4  christos getopt(nargc, nargv, options)
    332       1.1       mcr 	int nargc;
    333       1.1       mcr 	char * const *nargv;
    334       1.4  christos 	const char *options;
    335       1.1       mcr {
    336       1.1       mcr 	int retval;
    337      1.10     lukem 
    338      1.10     lukem 	_DIAGASSERT(nargv != NULL);
    339      1.10     lukem 	_DIAGASSERT(options != NULL);
    340       1.1       mcr 
    341       1.4  christos 	if ((retval = getopt_internal(nargc, nargv, options)) == -2) {
    342       1.4  christos 		++optind;
    343       1.4  christos 		/*
    344       1.4  christos 		 * We found an option (--), so if we skipped non-options,
    345       1.4  christos 		 * we have to permute.
    346       1.4  christos 		 */
    347       1.4  christos 		if (nonopt_end != -1) {
    348       1.4  christos 			permute_args(nonopt_start, nonopt_end, optind,
    349       1.4  christos 				       nargv);
    350       1.4  christos 			optind -= nonopt_end - nonopt_start;
    351       1.4  christos 		}
    352       1.4  christos 		nonopt_start = nonopt_end = -1;
    353       1.1       mcr 		retval = -1;
    354       1.1       mcr 	}
    355       1.4  christos 	return retval;
    356       1.1       mcr }
    357       1.1       mcr #endif
    358       1.1       mcr 
    359       1.1       mcr /*
    360       1.1       mcr  * getopt_long --
    361       1.1       mcr  *	Parse argc/argv argument vector.
    362       1.1       mcr  */
    363       1.1       mcr int
    364       1.4  christos getopt_long(nargc, nargv, options, long_options, idx)
    365       1.2     lukem 	int nargc;
    366       1.4  christos 	char * const *nargv;
    367       1.4  christos 	const char *options;
    368       1.4  christos 	const struct option *long_options;
    369       1.4  christos 	int *idx;
    370       1.1       mcr {
    371       1.1       mcr 	int retval;
    372       1.1       mcr 
    373       1.2     lukem 	_DIAGASSERT(nargv != NULL);
    374       1.4  christos 	_DIAGASSERT(options != NULL);
    375       1.2     lukem 	_DIAGASSERT(long_options != NULL);
    376       1.4  christos 	/* idx may be NULL */
    377       1.2     lukem 
    378       1.1       mcr 	if ((retval = getopt_internal(nargc, nargv, options)) == -2) {
    379       1.4  christos 		char *current_argv, *has_equal;
    380       1.4  christos 		size_t current_argv_len;
    381       1.4  christos 		int i, match;
    382       1.1       mcr 
    383       1.4  christos 		current_argv = place;
    384       1.4  christos 		match = -1;
    385       1.4  christos 
    386       1.4  christos 		optind++;
    387       1.4  christos 		place = EMSG;
    388       1.4  christos 
    389       1.4  christos 		if (*current_argv == '\0') {		/* found "--" */
    390       1.4  christos 			/*
    391       1.4  christos 			 * We found an option (--), so if we skipped
    392       1.4  christos 			 * non-options, we have to permute.
    393       1.4  christos 			 */
    394       1.4  christos 			if (nonopt_end != -1) {
    395       1.4  christos 				permute_args(nonopt_start, nonopt_end,
    396       1.4  christos 				    optind, nargv);
    397       1.4  christos 				optind -= nonopt_end - nonopt_start;
    398       1.4  christos 			}
    399       1.4  christos 			nonopt_start = nonopt_end = -1;
    400       1.4  christos 			return -1;
    401       1.1       mcr 		}
    402       1.1       mcr 		if ((has_equal = strchr(current_argv, '=')) != NULL) {
    403       1.4  christos 			/* argument found (--option=arg) */
    404       1.1       mcr 			current_argv_len = has_equal - current_argv;
    405       1.1       mcr 			has_equal++;
    406       1.1       mcr 		} else
    407       1.1       mcr 			current_argv_len = strlen(current_argv);
    408       1.4  christos 
    409       1.4  christos 		for (i = 0; long_options[i].name; i++) {
    410       1.4  christos 			/* find matching long option */
    411       1.4  christos 			if (strncmp(current_argv, long_options[i].name,
    412       1.4  christos 			    current_argv_len))
    413       1.1       mcr 				continue;
    414       1.1       mcr 
    415       1.4  christos 			if (strlen(long_options[i].name) ==
    416       1.4  christos 			    (unsigned)current_argv_len) {
    417       1.4  christos 				/* exact match */
    418       1.1       mcr 				match = i;
    419       1.1       mcr 				break;
    420       1.1       mcr 			}
    421       1.4  christos 			if (match == -1)		/* partial match */
    422       1.1       mcr 				match = i;
    423       1.4  christos 			else {
    424       1.4  christos 				/* ambiguous abbreviation */
    425       1.4  christos 				if (PRINT_ERROR)
    426       1.7  sommerfe 					warnx(ambig, (int)current_argv_len,
    427       1.4  christos 					     current_argv);
    428       1.4  christos 				optopt = 0;
    429       1.4  christos 				return BADCH;
    430       1.4  christos 			}
    431       1.1       mcr 		}
    432       1.4  christos 		if (match != -1) {			/* option found */
    433       1.4  christos 		        if (long_options[match].has_arg == no_argument
    434       1.4  christos 			    && has_equal) {
    435       1.4  christos 				if (PRINT_ERROR)
    436       1.7  sommerfe 					warnx(noarg, (int)current_argv_len,
    437       1.4  christos 					     current_argv);
    438       1.4  christos 				/*
    439       1.4  christos 				 * XXX: GNU sets optopt to val regardless of
    440       1.4  christos 				 * flag
    441       1.4  christos 				 */
    442       1.4  christos 				if (long_options[match].flag == NULL)
    443       1.4  christos 					optopt = long_options[match].val;
    444       1.4  christos 				else
    445       1.4  christos 					optopt = 0;
    446       1.4  christos 				return BADARG;
    447       1.4  christos 			}
    448       1.1       mcr 			if (long_options[match].has_arg == required_argument ||
    449       1.1       mcr 			    long_options[match].has_arg == optional_argument) {
    450       1.1       mcr 				if (has_equal)
    451       1.1       mcr 					optarg = has_equal;
    452       1.4  christos 				else if (long_options[match].has_arg ==
    453       1.4  christos 				    required_argument) {
    454       1.4  christos 					/*
    455       1.4  christos 					 * optional argument doesn't use
    456       1.4  christos 					 * next nargv
    457       1.4  christos 					 */
    458       1.1       mcr 					optarg = nargv[optind++];
    459       1.4  christos 				}
    460       1.1       mcr 			}
    461       1.1       mcr 			if ((long_options[match].has_arg == required_argument)
    462       1.1       mcr 			    && (optarg == NULL)) {
    463       1.1       mcr 				/*
    464       1.4  christos 				 * Missing argument; leading ':'
    465       1.1       mcr 				 * indicates no error should be generated
    466       1.1       mcr 				 */
    467       1.4  christos 				if (PRINT_ERROR)
    468       1.6   nathanw 					warnx(recargstring, current_argv);
    469       1.4  christos 				/*
    470       1.4  christos 				 * XXX: GNU sets optopt to val regardless
    471       1.4  christos 				 * of flag
    472       1.4  christos 				 */
    473       1.4  christos 				if (long_options[match].flag == NULL)
    474       1.4  christos 					optopt = long_options[match].val;
    475       1.4  christos 				else
    476       1.4  christos 					optopt = 0;
    477       1.4  christos 				--optind;
    478       1.4  christos 				return BADARG;
    479       1.4  christos 			}
    480       1.4  christos 		} else {			/* unknown option */
    481       1.4  christos 			if (PRINT_ERROR)
    482       1.6   nathanw 				warnx(illoptstring, current_argv);
    483       1.4  christos 			optopt = 0;
    484       1.4  christos 			return BADCH;
    485       1.1       mcr 		}
    486       1.1       mcr 		if (long_options[match].flag) {
    487       1.1       mcr 			*long_options[match].flag = long_options[match].val;
    488       1.1       mcr 			retval = 0;
    489       1.1       mcr 		} else
    490       1.1       mcr 			retval = long_options[match].val;
    491       1.4  christos 		if (idx)
    492       1.4  christos 			*idx = match;
    493       1.1       mcr 	}
    494       1.4  christos 	return retval;
    495       1.1       mcr }
    496      1.15        tv #endif /* !GETOPT_LONG */
    497