10Sstevel@tonic-gate /*
2*9664Sjason@ansipunx.net * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
30Sstevel@tonic-gate * Use is subject to license terms.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate
60Sstevel@tonic-gate /*
70Sstevel@tonic-gate * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com>
80Sstevel@tonic-gate * All rights reserved.
90Sstevel@tonic-gate *
100Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
110Sstevel@tonic-gate * modification, are permitted provided that the following conditions
120Sstevel@tonic-gate * are met:
130Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
140Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
150Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
170Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
180Sstevel@tonic-gate * 3. The name of the author may not be used to endorse or promote products
190Sstevel@tonic-gate * derived from this software without specific prior written permission.
200Sstevel@tonic-gate *
210Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
220Sstevel@tonic-gate * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
230Sstevel@tonic-gate * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
240Sstevel@tonic-gate * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
250Sstevel@tonic-gate * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
260Sstevel@tonic-gate * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
270Sstevel@tonic-gate * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
280Sstevel@tonic-gate * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
290Sstevel@tonic-gate * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
300Sstevel@tonic-gate * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
310Sstevel@tonic-gate */
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate * Copyright (c) 2000 The NetBSD Foundation, Inc.
340Sstevel@tonic-gate * All rights reserved.
350Sstevel@tonic-gate *
360Sstevel@tonic-gate * This code is derived from software contributed to The NetBSD Foundation
370Sstevel@tonic-gate * by Dieter Baron and Thomas Klausner.
380Sstevel@tonic-gate *
390Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
400Sstevel@tonic-gate * modification, are permitted provided that the following conditions
410Sstevel@tonic-gate * are met:
420Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
430Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
440Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
450Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
460Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
470Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
480Sstevel@tonic-gate * must display the following acknowledgement:
490Sstevel@tonic-gate * This product includes software developed by the NetBSD
500Sstevel@tonic-gate * Foundation, Inc. and its contributors.
510Sstevel@tonic-gate * 4. Neither the name of The NetBSD Foundation nor the names of its
520Sstevel@tonic-gate * contributors may be used to endorse or promote products derived
530Sstevel@tonic-gate * from this software without specific prior written permission.
540Sstevel@tonic-gate *
550Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
560Sstevel@tonic-gate * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
570Sstevel@tonic-gate * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
580Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
590Sstevel@tonic-gate * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
600Sstevel@tonic-gate * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
610Sstevel@tonic-gate * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
620Sstevel@tonic-gate * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
630Sstevel@tonic-gate * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
640Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
650Sstevel@tonic-gate * POSSIBILITY OF SUCH DAMAGE.
660Sstevel@tonic-gate */
670Sstevel@tonic-gate
686812Sraf #pragma weak _getopt_clip = getopt_clip
696812Sraf #pragma weak _getopt_long = getopt_long
706812Sraf #pragma weak _getopt_long_only = getopt_long_only
716812Sraf
726812Sraf #include "lint.h"
730Sstevel@tonic-gate #include <getopt.h>
740Sstevel@tonic-gate #include <stdio.h>
750Sstevel@tonic-gate #include <errno.h>
760Sstevel@tonic-gate #include <unistd.h>
770Sstevel@tonic-gate #include <stdlib.h>
780Sstevel@tonic-gate #include <string.h>
790Sstevel@tonic-gate #include "_libc_gettext.h"
800Sstevel@tonic-gate
810Sstevel@tonic-gate static int optreset = 0; /* keep track of first entry to getopt() */
820Sstevel@tonic-gate #define PRINT_ERROR ((opterr) && (*options != ':'))
830Sstevel@tonic-gate #define FLAG_IS_SET(flag) ((flags & flag) != 0) /* is flag turned on? */
840Sstevel@tonic-gate
850Sstevel@tonic-gate /* Determine if an argument is required for this long option */
860Sstevel@tonic-gate #define LONGOPT_REQUIRES_ARG(longopt) \
870Sstevel@tonic-gate ((((longopt).has_arg == optional_argument) && \
880Sstevel@tonic-gate (!FLAG_IS_SET(FLAG_OPTIONAL_ARGS))) || \
890Sstevel@tonic-gate ((longopt).has_arg == required_argument))
900Sstevel@tonic-gate
910Sstevel@tonic-gate #define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */
920Sstevel@tonic-gate #define FLAG_ALLARGS 0x02 /* treat non-options as args to option "1" */
930Sstevel@tonic-gate #define FLAG_LONGONLY 0x04 /* operate as getopt_long_only() */
940Sstevel@tonic-gate #define FLAG_OPTIONAL_ARGS 0x08 /* allow optional arguments to options */
950Sstevel@tonic-gate #define FLAG_REQUIRE_EQUIVALENTS 0x10 /* require short<->long equivalents */
960Sstevel@tonic-gate #define FLAG_ABBREV 0x20 /* long option abbreviation allowed. */
970Sstevel@tonic-gate #define FLAG_W_SEMICOLON 0x40 /* Support for W; in optstring */
980Sstevel@tonic-gate #define FLAG_PLUS_DASH_START 0x80 /* leading '+' or '-' in optstring */
990Sstevel@tonic-gate
1000Sstevel@tonic-gate /* return values */
1010Sstevel@tonic-gate #define BADCH (int)'?'
1020Sstevel@tonic-gate #define BADARG ((*options == ':') ? (int)':' : (int)'?')
1030Sstevel@tonic-gate #define INORDER (int)1
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate #define EMSG ""
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate static int getopt_internal(int, char * const *, const char *,
1080Sstevel@tonic-gate const struct option *, int *, uint_t);
1090Sstevel@tonic-gate static int parse_long_options(int nargc, char * const *nargv, const char *,
1100Sstevel@tonic-gate const struct option *, int *, int,
1110Sstevel@tonic-gate uint_t flags);
1120Sstevel@tonic-gate static int gcd(int, int);
1130Sstevel@tonic-gate static void permute_args(int, int, int, char * const *);
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate static char *place = EMSG; /* option letter processing */
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate /* XXX: set optreset to 1 rather than these two */
1180Sstevel@tonic-gate static int nonopt_start = -1; /* first non option argument (for permute) */
1190Sstevel@tonic-gate static int nonopt_end = -1; /* first option after non options (for permute) */
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate /*
1220Sstevel@tonic-gate * Generalized error message output.
1230Sstevel@tonic-gate *
1240Sstevel@tonic-gate * NOTE ON ERROR MESSAGES: All the error messages in this file
1254891Svk199839 * use %s (not %c) because they are all routed through warnx_getopt(),
1260Sstevel@tonic-gate * which takes a string argument. Character arguments passed
1270Sstevel@tonic-gate * to warnxchar() are converted to strings automatically before
1284891Svk199839 * being passed to warnx_getopt().
1290Sstevel@tonic-gate */
1300Sstevel@tonic-gate static void
warnx_getopt(const char * argv0,const char * msg,const char * arg)1314891Svk199839 warnx_getopt(const char *argv0, const char *msg, const char *arg) {
1320Sstevel@tonic-gate char errbuf[256];
1330Sstevel@tonic-gate (void) snprintf(errbuf, sizeof (errbuf), msg, argv0, arg);
1340Sstevel@tonic-gate (void) write(2, errbuf, strlen(errbuf));
1350Sstevel@tonic-gate (void) write(2, "\n", 1);
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate /*
1390Sstevel@tonic-gate * Generalized error message output.
1400Sstevel@tonic-gate */
1410Sstevel@tonic-gate static void
warnxchar(const char * argv0,const char * msg,const char c)1420Sstevel@tonic-gate warnxchar(const char *argv0, const char *msg, const char c) {
1430Sstevel@tonic-gate char charbuf[2];
1440Sstevel@tonic-gate charbuf[0] = c;
1450Sstevel@tonic-gate charbuf[1] = '\0';
1464891Svk199839 warnx_getopt(argv0, msg, charbuf);
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate /*
1500Sstevel@tonic-gate * Generalized error message output.
1510Sstevel@tonic-gate */
1520Sstevel@tonic-gate static void
warnxlen(const char * argv0,const char * msg,int argLen,const char * arg)1530Sstevel@tonic-gate warnxlen(const char *argv0, const char *msg, int argLen, const char *arg) {
1540Sstevel@tonic-gate char argbuf[256];
1550Sstevel@tonic-gate (void) strncpy(argbuf, arg, argLen);
1560Sstevel@tonic-gate argbuf[argLen < (sizeof (argbuf)-1)? argLen:(sizeof (argbuf)-1)] = '\0';
1574891Svk199839 warnx_getopt(argv0, msg, argbuf);
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate /*
1610Sstevel@tonic-gate * Compute the greatest common divisor of a and b.
1620Sstevel@tonic-gate */
1630Sstevel@tonic-gate static int
gcd(int a,int b)1640Sstevel@tonic-gate gcd(int a, int b)
1650Sstevel@tonic-gate {
1660Sstevel@tonic-gate int c;
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate c = a % b;
1690Sstevel@tonic-gate while (c != 0) {
1700Sstevel@tonic-gate a = b;
1710Sstevel@tonic-gate b = c;
1720Sstevel@tonic-gate c = a % b;
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate return (b);
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate /*
1790Sstevel@tonic-gate * Exchange the block from nonopt_start to nonopt_end with the block
1800Sstevel@tonic-gate * from nonopt_end to opt_end (keeping the same order of arguments
1810Sstevel@tonic-gate * in each block).
1820Sstevel@tonic-gate */
1830Sstevel@tonic-gate static void
permute_args(int panonopt_start,int panonopt_end,int opt_end,char * const * nargv)1840Sstevel@tonic-gate permute_args(int panonopt_start, int panonopt_end, int opt_end,
1850Sstevel@tonic-gate char * const *nargv)
1860Sstevel@tonic-gate {
1870Sstevel@tonic-gate int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
1880Sstevel@tonic-gate char *swap;
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate /*
1910Sstevel@tonic-gate * compute lengths of blocks and number and size of cycles
1920Sstevel@tonic-gate */
1930Sstevel@tonic-gate nnonopts = panonopt_end - panonopt_start;
1940Sstevel@tonic-gate nopts = opt_end - panonopt_end;
1950Sstevel@tonic-gate ncycle = gcd(nnonopts, nopts);
1960Sstevel@tonic-gate cyclelen = (opt_end - panonopt_start) / ncycle;
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate for (i = 0; i < ncycle; i++) {
1990Sstevel@tonic-gate cstart = panonopt_end+i;
2000Sstevel@tonic-gate pos = cstart;
2010Sstevel@tonic-gate for (j = 0; j < cyclelen; j++) {
2020Sstevel@tonic-gate if (pos >= panonopt_end)
2030Sstevel@tonic-gate pos -= nnonopts;
2040Sstevel@tonic-gate else
2050Sstevel@tonic-gate pos += nopts;
2060Sstevel@tonic-gate swap = nargv[pos];
2070Sstevel@tonic-gate ((char **)nargv)[pos] = nargv[cstart];
2080Sstevel@tonic-gate ((char **)nargv)[cstart] = swap;
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate } /* permute_args() */
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate /*
2140Sstevel@tonic-gate * Verify that each short option (character flag) has a long equivalent,
2150Sstevel@tonic-gate * and that each long option has a short option equivalent. Note that
2160Sstevel@tonic-gate * multiple long options can map to the same character.
2170Sstevel@tonic-gate *
2180Sstevel@tonic-gate * This behavior is defined by Sun's CLIP specification (11/12/02),
2190Sstevel@tonic-gate * and currently getopt_clip() is the only getopt variant that
2200Sstevel@tonic-gate * requires it.
2210Sstevel@tonic-gate *
2220Sstevel@tonic-gate * If error output is enabled and an error is found, this function
2230Sstevel@tonic-gate * prints ONE error message (the first error found) and returns an
2240Sstevel@tonic-gate * error value.
2250Sstevel@tonic-gate *
2260Sstevel@tonic-gate * ASSUMES: options != NULL
2270Sstevel@tonic-gate * ASSUMES: long_options may be NULL
2280Sstevel@tonic-gate *
2290Sstevel@tonic-gate * Returns < 0 if an error is found
2300Sstevel@tonic-gate * Returns >= 0 on success
2310Sstevel@tonic-gate */
2320Sstevel@tonic-gate static int
2330Sstevel@tonic-gate /* LINTED: argument unused in function: nargc */
verify_short_long_equivalents(int nargc,char * const * nargv,const char * options,const struct option * long_options,uint_t flags)2340Sstevel@tonic-gate verify_short_long_equivalents(int nargc,
2350Sstevel@tonic-gate char *const *nargv,
2360Sstevel@tonic-gate const char *options,
2370Sstevel@tonic-gate const struct option *long_options,
2380Sstevel@tonic-gate uint_t flags) {
2390Sstevel@tonic-gate int short_i = 0;
2400Sstevel@tonic-gate int long_i = 0;
2410Sstevel@tonic-gate int equivFound = 0;
2420Sstevel@tonic-gate int ch = 0;
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate /*
2450Sstevel@tonic-gate * Find a long option for each short option
2460Sstevel@tonic-gate */
2470Sstevel@tonic-gate equivFound = 1;
2480Sstevel@tonic-gate for (short_i = 0; equivFound && (options[short_i] != 0); ++short_i) {
2490Sstevel@tonic-gate ch = options[short_i];
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate if (ch == ':') {
2520Sstevel@tonic-gate continue;
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate if (FLAG_IS_SET(FLAG_W_SEMICOLON) &&
2550Sstevel@tonic-gate (ch == 'W') && (options[short_i+1] == ';')) {
2560Sstevel@tonic-gate /* W; is a special case */
2570Sstevel@tonic-gate ++short_i;
2580Sstevel@tonic-gate continue;
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate equivFound = 0;
2620Sstevel@tonic-gate if (long_options != NULL) {
2630Sstevel@tonic-gate for (long_i = 0; ((!equivFound) &&
2640Sstevel@tonic-gate (long_options[long_i].name != NULL));
2650Sstevel@tonic-gate ++long_i) {
2660Sstevel@tonic-gate equivFound = (ch == long_options[long_i].val);
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate if ((!equivFound) && (PRINT_ERROR)) {
2700Sstevel@tonic-gate warnxchar(nargv[0],
2710Sstevel@tonic-gate _libc_gettext(
2720Sstevel@tonic-gate "%s: equivalent long option required -- %s"),
2730Sstevel@tonic-gate ch);
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate } /* short_i */
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate /*
2780Sstevel@tonic-gate * Find a short option for each long option. Note that if we came
2790Sstevel@tonic-gate * out of the above loop with equivFound==0, we are already done.
2800Sstevel@tonic-gate */
2810Sstevel@tonic-gate if (equivFound && (long_options != NULL)) {
2820Sstevel@tonic-gate for (long_i = 0; (equivFound &&
2830Sstevel@tonic-gate (long_options[long_i].name != NULL));
2840Sstevel@tonic-gate ++long_i) {
2850Sstevel@tonic-gate equivFound = ((long_options[long_i].val != 0) &&
2860Sstevel@tonic-gate (strchr(options, long_options[long_i].val)
2870Sstevel@tonic-gate != NULL));
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate if ((!equivFound) && (PRINT_ERROR)) {
2904891Svk199839 warnx_getopt(nargv[0],
2910Sstevel@tonic-gate _libc_gettext(
2920Sstevel@tonic-gate "%s: equivalent short option required -- %s"),
2930Sstevel@tonic-gate long_options[long_i].name);
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate } /* for long_i */
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate
2980Sstevel@tonic-gate return (equivFound? 0:-1);
2990Sstevel@tonic-gate } /* verify_short_long_equivalents() */
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate /*
3020Sstevel@tonic-gate * parse_long_options --
3030Sstevel@tonic-gate * Parse long options in argc/argv argument vector.
3040Sstevel@tonic-gate * Returns -1 if short_too is set and the option does not match long_options.
3050Sstevel@tonic-gate */
3060Sstevel@tonic-gate static int
parse_long_options(int nargc,char * const * nargv,const char * options,const struct option * long_options,int * idx,int short_too,uint_t flags)3070Sstevel@tonic-gate parse_long_options(int nargc, char * const *nargv, const char *options,
3080Sstevel@tonic-gate const struct option *long_options, int *idx, int short_too,
3090Sstevel@tonic-gate uint_t flags)
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate char *current_argv = NULL;
3120Sstevel@tonic-gate char *argv_equal_ptr = NULL;
3130Sstevel@tonic-gate size_t current_argv_len = 0;
3140Sstevel@tonic-gate size_t long_option_len = 0;
3150Sstevel@tonic-gate int i = 0;
3160Sstevel@tonic-gate int match = 0;
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate current_argv = place;
3190Sstevel@tonic-gate match = -1;
3200Sstevel@tonic-gate
3210Sstevel@tonic-gate optind++;
3220Sstevel@tonic-gate
3230Sstevel@tonic-gate if ((argv_equal_ptr = strchr(current_argv, '=')) != NULL) {
3240Sstevel@tonic-gate /* argument found (--option=arg) */
3250Sstevel@tonic-gate current_argv_len = (argv_equal_ptr - current_argv);
3260Sstevel@tonic-gate argv_equal_ptr++;
3270Sstevel@tonic-gate } else {
3280Sstevel@tonic-gate current_argv_len = strlen(current_argv);
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate for (i = 0; (long_options[i].name != NULL); i++) {
3320Sstevel@tonic-gate
3330Sstevel@tonic-gate /* find matching long option */
3340Sstevel@tonic-gate if (strncmp(current_argv, long_options[i].name,
3354891Svk199839 current_argv_len) != 0) {
3360Sstevel@tonic-gate continue; /* no match */
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate long_option_len = strlen(long_options[i].name);
3390Sstevel@tonic-gate if ((!FLAG_IS_SET(FLAG_ABBREV)) &&
3400Sstevel@tonic-gate (long_option_len > current_argv_len)) {
3410Sstevel@tonic-gate continue; /* Abbreviations are disabled */
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate
3440Sstevel@tonic-gate if (long_option_len == current_argv_len) {
3450Sstevel@tonic-gate /* exact match */
3460Sstevel@tonic-gate match = i;
3470Sstevel@tonic-gate break;
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate /*
3500Sstevel@tonic-gate * If this is a known short option, don't allow
3510Sstevel@tonic-gate * a partial match of a single character.
3520Sstevel@tonic-gate */
3530Sstevel@tonic-gate if (short_too && current_argv_len == 1)
3540Sstevel@tonic-gate continue;
3550Sstevel@tonic-gate
3560Sstevel@tonic-gate if (match == -1) /* partial match */
3570Sstevel@tonic-gate match = i;
3580Sstevel@tonic-gate else {
3590Sstevel@tonic-gate /* ambiguous abbreviation */
3600Sstevel@tonic-gate if (PRINT_ERROR) {
3610Sstevel@tonic-gate warnxlen(nargv[0],
3620Sstevel@tonic-gate _libc_gettext(
3634891Svk199839 "%s: ambiguous option -- %s"),
3640Sstevel@tonic-gate (int)current_argv_len,
3650Sstevel@tonic-gate current_argv);
3660Sstevel@tonic-gate }
3670Sstevel@tonic-gate optopt = 0;
3680Sstevel@tonic-gate return (BADCH);
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate } /* for i */
3710Sstevel@tonic-gate if (match != -1) { /* option found */
3720Sstevel@tonic-gate if ((long_options[match].has_arg == no_argument) &&
3730Sstevel@tonic-gate (argv_equal_ptr != NULL)) {
3740Sstevel@tonic-gate if (PRINT_ERROR) {
3750Sstevel@tonic-gate warnxlen(nargv[0],
3764891Svk199839 _libc_gettext(
3770Sstevel@tonic-gate "%s: option doesn't take an argument -- %s"),
3784891Svk199839 (int)current_argv_len,
3794891Svk199839 current_argv);
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate /*
3820Sstevel@tonic-gate * XXX: GNU sets optopt to val regardless of flag
3830Sstevel@tonic-gate */
3840Sstevel@tonic-gate if (long_options[match].flag == NULL)
3850Sstevel@tonic-gate optopt = long_options[match].val;
3860Sstevel@tonic-gate else
3870Sstevel@tonic-gate optopt = 0;
3880Sstevel@tonic-gate return (BADARG);
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate if (long_options[match].has_arg == required_argument ||
3910Sstevel@tonic-gate long_options[match].has_arg == optional_argument) {
3920Sstevel@tonic-gate if (argv_equal_ptr != NULL) {
3930Sstevel@tonic-gate optarg = argv_equal_ptr;
3940Sstevel@tonic-gate } else if (LONGOPT_REQUIRES_ARG(long_options[match])) {
3950Sstevel@tonic-gate /* The next argv must be the option argument */
3960Sstevel@tonic-gate if (optind < nargc) {
3974891Svk199839 optarg = nargv[optind];
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate ++optind; /* code below depends on this */
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate if (LONGOPT_REQUIRES_ARG(long_options[match]) &&
4030Sstevel@tonic-gate (optarg == NULL)) {
4040Sstevel@tonic-gate /*
4050Sstevel@tonic-gate * Missing argument; leading ':' indicates no error
4060Sstevel@tonic-gate * should be generated.
4070Sstevel@tonic-gate */
4080Sstevel@tonic-gate if (PRINT_ERROR) {
4094891Svk199839 warnx_getopt(nargv[0],
4104891Svk199839 _libc_gettext(
4110Sstevel@tonic-gate "%s: option requires an argument -- %s"),
4124891Svk199839 current_argv);
4130Sstevel@tonic-gate }
4140Sstevel@tonic-gate /*
4150Sstevel@tonic-gate * XXX: GNU sets optopt to val regardless of flag
4160Sstevel@tonic-gate */
4170Sstevel@tonic-gate if (long_options[match].flag == NULL)
4180Sstevel@tonic-gate optopt = long_options[match].val;
4190Sstevel@tonic-gate else
4200Sstevel@tonic-gate optopt = 0;
4210Sstevel@tonic-gate --optind;
4220Sstevel@tonic-gate return (BADARG);
4230Sstevel@tonic-gate }
4240Sstevel@tonic-gate } else { /* unknown option */
4250Sstevel@tonic-gate if (short_too) {
4260Sstevel@tonic-gate --optind;
4270Sstevel@tonic-gate return (-1);
4280Sstevel@tonic-gate }
4290Sstevel@tonic-gate if (PRINT_ERROR) {
4304891Svk199839 warnx_getopt(nargv[0],
4314891Svk199839 _libc_gettext("%s: illegal option -- %s"),
4324891Svk199839 current_argv);
4330Sstevel@tonic-gate }
4340Sstevel@tonic-gate optopt = 0;
4350Sstevel@tonic-gate return (BADCH);
4360Sstevel@tonic-gate }
4370Sstevel@tonic-gate if (idx)
4380Sstevel@tonic-gate *idx = match;
4390Sstevel@tonic-gate if (long_options[match].flag != NULL) {
4400Sstevel@tonic-gate *long_options[match].flag = long_options[match].val;
4410Sstevel@tonic-gate return (0);
4420Sstevel@tonic-gate } else {
4430Sstevel@tonic-gate optopt = long_options[match].val;
4440Sstevel@tonic-gate return (optopt);
4450Sstevel@tonic-gate }
4460Sstevel@tonic-gate } /* parse_long_options() */
4470Sstevel@tonic-gate
4480Sstevel@tonic-gate /*
4490Sstevel@tonic-gate * getopt_internal() --
4500Sstevel@tonic-gate * Parse argc/argv argument vector. Called by user level routines.
4510Sstevel@tonic-gate *
4520Sstevel@tonic-gate * This implements all of the getopt_long(), getopt_long_only(),
4530Sstevel@tonic-gate * getopt_clip() variants.
4540Sstevel@tonic-gate */
4550Sstevel@tonic-gate static int
getopt_internal(int nargc,char * const * nargv,const char * options,const struct option * long_options,int * idx,uint_t flags)4560Sstevel@tonic-gate getopt_internal(int nargc, char * const *nargv, const char *options,
4570Sstevel@tonic-gate const struct option *long_options, int *idx, uint_t flags)
4580Sstevel@tonic-gate {
4590Sstevel@tonic-gate char *oli; /* option letter list index */
4600Sstevel@tonic-gate int optchar, short_too;
4610Sstevel@tonic-gate static int posixly_correct = -1;
4620Sstevel@tonic-gate
4630Sstevel@tonic-gate if (options == NULL)
4640Sstevel@tonic-gate return (-1);
4650Sstevel@tonic-gate
4660Sstevel@tonic-gate /*
4670Sstevel@tonic-gate * Disable GNU extensions if POSIXLY_CORRECT is set or options
4680Sstevel@tonic-gate * string begins with a '+'.
4690Sstevel@tonic-gate */
4700Sstevel@tonic-gate if (posixly_correct == -1) {
4710Sstevel@tonic-gate posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
4720Sstevel@tonic-gate }
4730Sstevel@tonic-gate if (FLAG_IS_SET(FLAG_PLUS_DASH_START)) {
4740Sstevel@tonic-gate /*
4750Sstevel@tonic-gate * + or - at start of optstring takes precedence
4760Sstevel@tonic-gate * over POSIXLY_CORRECT.
4770Sstevel@tonic-gate */
4780Sstevel@tonic-gate if (*options == '+') {
4790Sstevel@tonic-gate /*
4800Sstevel@tonic-gate * leading + means POSIX-compliant; first non-option
4810Sstevel@tonic-gate * ends option list. Therefore, don't permute args.
4820Sstevel@tonic-gate */
4830Sstevel@tonic-gate posixly_correct = 1;
4840Sstevel@tonic-gate } else if (*options == '-') {
4850Sstevel@tonic-gate posixly_correct = 0;
4860Sstevel@tonic-gate flags |= FLAG_ALLARGS;
4870Sstevel@tonic-gate }
4880Sstevel@tonic-gate if ((*options == '+') || (*options == '-')) {
4890Sstevel@tonic-gate options++;
4900Sstevel@tonic-gate }
4910Sstevel@tonic-gate } /* if FLAG_PLUS_DASH_START */
4920Sstevel@tonic-gate
4930Sstevel@tonic-gate if (posixly_correct) {
4944891Svk199839 flags &= ~FLAG_PERMUTE;
4954891Svk199839 flags &= ~FLAG_ALLARGS;
4960Sstevel@tonic-gate }
4970Sstevel@tonic-gate
4980Sstevel@tonic-gate /*
4990Sstevel@tonic-gate * Some programs (like GNU cvs) set optind to 0 to restart
5000Sstevel@tonic-gate * option processing. Work around this braindamage.
5010Sstevel@tonic-gate *
5020Sstevel@tonic-gate * The above problem comes from using global variables. We
5030Sstevel@tonic-gate * should avoid their use in the future.
5040Sstevel@tonic-gate */
5050Sstevel@tonic-gate if (optind == 0) {
5060Sstevel@tonic-gate optind = optreset = 1;
5070Sstevel@tonic-gate }
5080Sstevel@tonic-gate
5090Sstevel@tonic-gate optarg = NULL;
5100Sstevel@tonic-gate optopt = 0;
5110Sstevel@tonic-gate
5120Sstevel@tonic-gate if (optreset) {
5130Sstevel@tonic-gate nonopt_start = nonopt_end = -1;
5140Sstevel@tonic-gate }
5150Sstevel@tonic-gate
5160Sstevel@tonic-gate /*
5170Sstevel@tonic-gate * On the first call, make sure that there is a short equivalent
5180Sstevel@tonic-gate * for each long option, and vice versa. This is required by
5190Sstevel@tonic-gate * Sun's CLIP specification (11/12/02).
5200Sstevel@tonic-gate */
5210Sstevel@tonic-gate if ((optind == 1) && FLAG_IS_SET(FLAG_REQUIRE_EQUIVALENTS)) {
5224891Svk199839 if (verify_short_long_equivalents(
5234891Svk199839 nargc, nargv, options, long_options, flags) < 0) {
5244891Svk199839 /* function printed any necessary messages */
5254891Svk199839 errno = EINVAL; /* invalid argument */
5264891Svk199839 return (-1);
5274891Svk199839 }
5280Sstevel@tonic-gate }
5290Sstevel@tonic-gate
5300Sstevel@tonic-gate start:
5310Sstevel@tonic-gate if (optreset || !*place) { /* update scanning pointer */
5320Sstevel@tonic-gate optreset = 0;
5330Sstevel@tonic-gate if (optind >= nargc) { /* end of argument vector */
5340Sstevel@tonic-gate place = EMSG;
5350Sstevel@tonic-gate if (nonopt_end != -1) {
5360Sstevel@tonic-gate /* do permutation, if we have to */
5370Sstevel@tonic-gate permute_args(nonopt_start, nonopt_end,
5380Sstevel@tonic-gate optind, nargv);
5390Sstevel@tonic-gate optind -= nonopt_end - nonopt_start;
5400Sstevel@tonic-gate
5410Sstevel@tonic-gate } else if (nonopt_start != -1) {
5420Sstevel@tonic-gate /*
5430Sstevel@tonic-gate * If we skipped non-options, set optind
5440Sstevel@tonic-gate * to the first of them.
5450Sstevel@tonic-gate */
5460Sstevel@tonic-gate optind = nonopt_start;
5470Sstevel@tonic-gate }
5480Sstevel@tonic-gate nonopt_start = nonopt_end = -1;
5490Sstevel@tonic-gate return (-1);
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate if ((*(place = nargv[optind]) != '-') || (place[1] == '\0')) {
5520Sstevel@tonic-gate place = EMSG; /* found non-option */
5530Sstevel@tonic-gate if (flags & FLAG_ALLARGS) {
5540Sstevel@tonic-gate /*
5550Sstevel@tonic-gate * GNU extension:
5560Sstevel@tonic-gate * return non-option as argument to option '\1'
5570Sstevel@tonic-gate */
5580Sstevel@tonic-gate optarg = nargv[optind++];
5590Sstevel@tonic-gate return (INORDER);
5600Sstevel@tonic-gate }
5610Sstevel@tonic-gate if (!(flags & FLAG_PERMUTE)) {
5620Sstevel@tonic-gate /*
5630Sstevel@tonic-gate * If no permutation wanted, stop parsing
5640Sstevel@tonic-gate * at first non-option.
5650Sstevel@tonic-gate */
5660Sstevel@tonic-gate return (-1);
5670Sstevel@tonic-gate }
5680Sstevel@tonic-gate /* do permutation */
5690Sstevel@tonic-gate if (nonopt_start == -1)
5700Sstevel@tonic-gate nonopt_start = optind;
5710Sstevel@tonic-gate else if (nonopt_end != -1) {
5720Sstevel@tonic-gate permute_args(nonopt_start, nonopt_end,
5730Sstevel@tonic-gate optind, nargv);
5740Sstevel@tonic-gate nonopt_start = optind -
5750Sstevel@tonic-gate (nonopt_end - nonopt_start);
5760Sstevel@tonic-gate nonopt_end = -1;
5770Sstevel@tonic-gate }
5780Sstevel@tonic-gate optind++;
5790Sstevel@tonic-gate /* process next argument */
5800Sstevel@tonic-gate goto start;
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate if (nonopt_start != -1 && nonopt_end == -1)
5830Sstevel@tonic-gate nonopt_end = optind;
5840Sstevel@tonic-gate
5850Sstevel@tonic-gate /*
5860Sstevel@tonic-gate * Check for "--" or "--foo" with no long options
5870Sstevel@tonic-gate * but if place is simply "-" leave it unmolested.
5880Sstevel@tonic-gate */
5890Sstevel@tonic-gate if (place[1] != '\0' && *++place == '-' &&
5900Sstevel@tonic-gate (place[1] == '\0' || long_options == NULL)) {
5910Sstevel@tonic-gate optind++;
5920Sstevel@tonic-gate place = EMSG;
5930Sstevel@tonic-gate /*
5940Sstevel@tonic-gate * We found an option (--), so if we skipped
5950Sstevel@tonic-gate * non-options, we have to permute.
5960Sstevel@tonic-gate */
5970Sstevel@tonic-gate if (nonopt_end != -1) {
5980Sstevel@tonic-gate permute_args(nonopt_start, nonopt_end,
5990Sstevel@tonic-gate optind, nargv);
6000Sstevel@tonic-gate optind -= nonopt_end - nonopt_start;
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate nonopt_start = nonopt_end = -1;
6030Sstevel@tonic-gate return (-1);
6040Sstevel@tonic-gate }
6050Sstevel@tonic-gate }
6060Sstevel@tonic-gate
6070Sstevel@tonic-gate /*
6080Sstevel@tonic-gate * Check long options if:
6090Sstevel@tonic-gate * 1) we were passed some
6100Sstevel@tonic-gate * 2) the arg is not just "-"
6110Sstevel@tonic-gate * 3) either the arg starts with -- or we are getopt_long_only()
6120Sstevel@tonic-gate */
6130Sstevel@tonic-gate if (long_options != NULL && place != nargv[optind] &&
6140Sstevel@tonic-gate (*place == '-' || (FLAG_IS_SET(FLAG_LONGONLY)))) {
6150Sstevel@tonic-gate short_too = 0;
6160Sstevel@tonic-gate if (*place == '-')
6170Sstevel@tonic-gate place++; /* --foo long option */
6180Sstevel@tonic-gate else if (*place != ':' && strchr(options, *place) != NULL)
6190Sstevel@tonic-gate short_too = 1; /* could be short option too */
6200Sstevel@tonic-gate
6210Sstevel@tonic-gate optchar = parse_long_options(nargc, nargv, options,
6224891Svk199839 long_options, idx, short_too, flags);
6230Sstevel@tonic-gate if (optchar != -1) {
6240Sstevel@tonic-gate place = EMSG;
6250Sstevel@tonic-gate return (optchar);
6260Sstevel@tonic-gate }
6270Sstevel@tonic-gate }
6280Sstevel@tonic-gate
6290Sstevel@tonic-gate if ((optchar = (int)*place++) == (int)':' ||
6300Sstevel@tonic-gate (oli = strchr(options, optchar)) == NULL) {
6310Sstevel@tonic-gate /*
6320Sstevel@tonic-gate * If the user didn't specify '-' as an option,
6330Sstevel@tonic-gate * assume it means -1 as POSIX specifies.
6340Sstevel@tonic-gate */
6350Sstevel@tonic-gate if (optchar == (int)'-')
6360Sstevel@tonic-gate return (-1);
6370Sstevel@tonic-gate /* option letter unknown or ':' */
6380Sstevel@tonic-gate if (!*place)
6390Sstevel@tonic-gate ++optind;
6400Sstevel@tonic-gate if (PRINT_ERROR)
6410Sstevel@tonic-gate warnxchar(nargv[0],
6424891Svk199839 _libc_gettext("%s: illegal option -- %s"),
6434891Svk199839 optchar);
6440Sstevel@tonic-gate optopt = optchar;
6450Sstevel@tonic-gate return (BADCH);
6460Sstevel@tonic-gate }
6470Sstevel@tonic-gate if (FLAG_IS_SET(FLAG_W_SEMICOLON) &&
6480Sstevel@tonic-gate (long_options != NULL) && (optchar == 'W') && (oli[1] == ';')) {
6490Sstevel@tonic-gate /* -W long-option */
6500Sstevel@tonic-gate /* LINTED: statement has no consequent: if */
6510Sstevel@tonic-gate if (*place) { /* no space */
6520Sstevel@tonic-gate /* NOTHING */;
6530Sstevel@tonic-gate } else if (++optind >= nargc) { /* no long-option after -W */
6540Sstevel@tonic-gate place = EMSG;
6550Sstevel@tonic-gate if (PRINT_ERROR)
6560Sstevel@tonic-gate warnxchar(nargv[0],
6574891Svk199839 _libc_gettext(
6580Sstevel@tonic-gate "%s: option requires an argument -- %s"),
6594891Svk199839 optchar);
6600Sstevel@tonic-gate optopt = optchar;
6610Sstevel@tonic-gate return (BADARG);
6620Sstevel@tonic-gate } else { /* white space */
6630Sstevel@tonic-gate place = nargv[optind];
6640Sstevel@tonic-gate }
6650Sstevel@tonic-gate optchar = parse_long_options(
6664891Svk199839 nargc, nargv, options, long_options,
6670Sstevel@tonic-gate idx, 0, flags);
6680Sstevel@tonic-gate
6690Sstevel@tonic-gate /*
6700Sstevel@tonic-gate * PSARC 2003/645 - Match GNU behavior, set optarg to
6710Sstevel@tonic-gate * the long-option.
6720Sstevel@tonic-gate */
6730Sstevel@tonic-gate if (optarg == NULL) {
6740Sstevel@tonic-gate optarg = nargv[optind-1];
6750Sstevel@tonic-gate }
6760Sstevel@tonic-gate place = EMSG;
6770Sstevel@tonic-gate return (optchar);
6780Sstevel@tonic-gate }
6790Sstevel@tonic-gate if (*++oli != ':') { /* doesn't take argument */
6800Sstevel@tonic-gate if (!*place)
6810Sstevel@tonic-gate ++optind;
6820Sstevel@tonic-gate } else { /* takes (optional) argument */
6830Sstevel@tonic-gate optarg = NULL;
6840Sstevel@tonic-gate if (*place) { /* no white space */
6850Sstevel@tonic-gate optarg = place;
6860Sstevel@tonic-gate /* XXX: disable test for :: if PC? (GNU doesn't) */
6870Sstevel@tonic-gate } else if (!(FLAG_IS_SET(FLAG_OPTIONAL_ARGS) &&
6884891Svk199839 (oli[1] == ':'))) {
6890Sstevel@tonic-gate /* arg is required (not optional) */
6900Sstevel@tonic-gate
6910Sstevel@tonic-gate if (++optind >= nargc) { /* no arg */
6920Sstevel@tonic-gate place = EMSG;
6930Sstevel@tonic-gate if (PRINT_ERROR) {
6940Sstevel@tonic-gate warnxchar(nargv[0],
6954891Svk199839 _libc_gettext(
6960Sstevel@tonic-gate "%s: option requires an argument -- %s"),
6974891Svk199839 optchar);
6980Sstevel@tonic-gate }
6990Sstevel@tonic-gate optopt = optchar;
7000Sstevel@tonic-gate return (BADARG);
7010Sstevel@tonic-gate } else
7020Sstevel@tonic-gate optarg = nargv[optind];
7030Sstevel@tonic-gate }
7040Sstevel@tonic-gate place = EMSG;
7050Sstevel@tonic-gate ++optind;
7060Sstevel@tonic-gate }
7070Sstevel@tonic-gate /* return valid option letter */
7080Sstevel@tonic-gate optopt = optchar; /* preserve getopt() behavior */
7090Sstevel@tonic-gate return (optchar);
7100Sstevel@tonic-gate } /* getopt_internal() */
7110Sstevel@tonic-gate
7120Sstevel@tonic-gate /*
7130Sstevel@tonic-gate * getopt_long() --
7140Sstevel@tonic-gate * Parse argc/argv argument vector.
7150Sstevel@tonic-gate *
7160Sstevel@tonic-gate * Requires that long options be preceded with a two dashes
7170Sstevel@tonic-gate * (e.g., --longoption).
7180Sstevel@tonic-gate */
7190Sstevel@tonic-gate int
getopt_long(int nargc,char * const * nargv,const char * optstring,const struct option * long_options,int * long_index)7200Sstevel@tonic-gate getopt_long(int nargc, char *const *nargv,
7210Sstevel@tonic-gate const char *optstring,
7220Sstevel@tonic-gate const struct option *long_options, int *long_index)
7230Sstevel@tonic-gate {
7240Sstevel@tonic-gate
7250Sstevel@tonic-gate return (getopt_internal(
7264891Svk199839 nargc, nargv, optstring, long_options, long_index,
7274891Svk199839 FLAG_PERMUTE
7284891Svk199839 | FLAG_OPTIONAL_ARGS
7294891Svk199839 | FLAG_ABBREV
7304891Svk199839 | FLAG_W_SEMICOLON
7314891Svk199839 | FLAG_PLUS_DASH_START));
7320Sstevel@tonic-gate } /* getopt_long() */
7330Sstevel@tonic-gate
7340Sstevel@tonic-gate /*
7350Sstevel@tonic-gate * getopt_long_only() --
7360Sstevel@tonic-gate * Parse argc/argv argument vector.
7370Sstevel@tonic-gate *
7380Sstevel@tonic-gate * Long options may be preceded with a single dash (e.g., -longoption)
7390Sstevel@tonic-gate */
7400Sstevel@tonic-gate int
getopt_long_only(int nargc,char * const * nargv,const char * optstring,const struct option * long_options,int * long_index)7410Sstevel@tonic-gate getopt_long_only(int nargc, char *const *nargv,
7420Sstevel@tonic-gate const char *optstring,
7430Sstevel@tonic-gate const struct option *long_options, int *long_index)
7440Sstevel@tonic-gate {
7450Sstevel@tonic-gate
7460Sstevel@tonic-gate return (getopt_internal(
7474891Svk199839 nargc, nargv, optstring, long_options, long_index,
7484891Svk199839 FLAG_PERMUTE
7494891Svk199839 | FLAG_OPTIONAL_ARGS
7504891Svk199839 | FLAG_ABBREV
7514891Svk199839 | FLAG_W_SEMICOLON
7524891Svk199839 | FLAG_PLUS_DASH_START
7534891Svk199839 | FLAG_LONGONLY));
7540Sstevel@tonic-gate } /* getopt_long_only() */
7550Sstevel@tonic-gate
7560Sstevel@tonic-gate /*
7570Sstevel@tonic-gate * getopt_clip() --
7580Sstevel@tonic-gate * Parse argc/argv argument vector, requiring compliance with
7590Sstevel@tonic-gate * Sun's CLIP specification (11/12/02)
7600Sstevel@tonic-gate *
7610Sstevel@tonic-gate * o Does not allow arguments to be optional (optional_argument is
7620Sstevel@tonic-gate * treated as required_argument).
7630Sstevel@tonic-gate *
7640Sstevel@tonic-gate * o Does not allow long options to be abbreviated on the command line
7650Sstevel@tonic-gate *
7660Sstevel@tonic-gate * o Does not allow long argument to be preceded by a single dash
7670Sstevel@tonic-gate * (Double-dash '--' is required)
7680Sstevel@tonic-gate *
7690Sstevel@tonic-gate * o Stops option processing at the first non-option
7700Sstevel@tonic-gate *
7710Sstevel@tonic-gate * o Requires that every long option have a short-option (single
7720Sstevel@tonic-gate * character) equivalent and vice-versa. If a short option or
7730Sstevel@tonic-gate * long option without an equivalent is found, an error message
7740Sstevel@tonic-gate * is printed and -1 is returned on the first call, and errno
7750Sstevel@tonic-gate * is set to EINVAL.
7760Sstevel@tonic-gate *
7770Sstevel@tonic-gate * o Leading + or - in optstring is ignored, and opstring is
7780Sstevel@tonic-gate * treated as if it began after the + or - .
7790Sstevel@tonic-gate */
7800Sstevel@tonic-gate int
getopt_clip(int nargc,char * const * nargv,const char * optstring,const struct option * long_options,int * long_index)7810Sstevel@tonic-gate getopt_clip(int nargc, char *const *nargv,
7820Sstevel@tonic-gate const char *optstring,
7830Sstevel@tonic-gate const struct option *long_options, int *long_index)
7840Sstevel@tonic-gate {
7850Sstevel@tonic-gate return getopt_internal(
7864891Svk199839 nargc, nargv, optstring, long_options, long_index,
7870Sstevel@tonic-gate /*
7880Sstevel@tonic-gate * no permutation,
7890Sstevel@tonic-gate * no optional args,
7900Sstevel@tonic-gate * no long-only,
7910Sstevel@tonic-gate * no abbreviations
7920Sstevel@tonic-gate * no support for +- at start of optstring
7930Sstevel@tonic-gate * yes support for "W;" in optstring
7940Sstevel@tonic-gate */
7954891Svk199839 FLAG_W_SEMICOLON
7964891Svk199839 | FLAG_REQUIRE_EQUIVALENTS);
7970Sstevel@tonic-gate } /* getopt_clip() */
798