1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <string.h>
31*0Sstevel@tonic-gate #include "volume_error.h"
32*0Sstevel@tonic-gate #include "getopt_ext.h"
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gate /*
35*0Sstevel@tonic-gate * Functions
36*0Sstevel@tonic-gate */
37*0Sstevel@tonic-gate
38*0Sstevel@tonic-gate /*
39*0Sstevel@tonic-gate * Identical to getopt(3), except that
40*0Sstevel@tonic-gate *
41*0Sstevel@tonic-gate * 1. If "-" is the first character of optstring, each non-option argv
42*0Sstevel@tonic-gate * element is handled as if it were the argument of an option with
43*0Sstevel@tonic-gate * character code GETOPT_NON_OPTION_ARG. The result is that
44*0Sstevel@tonic-gate * GETOPT_DONE_PARSING will not be returned until the end of the
45*0Sstevel@tonic-gate * argument list has been reached.
46*0Sstevel@tonic-gate *
47*0Sstevel@tonic-gate * This mirrors the functionality provided by GNU getopt.
48*0Sstevel@tonic-gate *
49*0Sstevel@tonic-gate * 2. GETOPT_ERR_INVALID_OPT or GETOPT_ERR_MISSING_ARG is returned
50*0Sstevel@tonic-gate * instead of '?'. Subsequently "-?" can be used as a valid
51*0Sstevel@tonic-gate * option.
52*0Sstevel@tonic-gate *
53*0Sstevel@tonic-gate * 3. GETOPT_DONE_PARSING, GETOPT_ERR_INVALID_ARG, or
54*0Sstevel@tonic-gate * GETOPT_NON_OPTION_ARG is returned instead of -1.
55*0Sstevel@tonic-gate *
56*0Sstevel@tonic-gate * @param argc
57*0Sstevel@tonic-gate * The number of arguments in the array
58*0Sstevel@tonic-gate *
59*0Sstevel@tonic-gate * @param argv
60*0Sstevel@tonic-gate * The argument array
61*0Sstevel@tonic-gate *
62*0Sstevel@tonic-gate * @param optstring
63*0Sstevel@tonic-gate * The option letters, with ':' following options with
64*0Sstevel@tonic-gate * required arguments. See note about "-" as the first
65*0Sstevel@tonic-gate * character.
66*0Sstevel@tonic-gate *
67*0Sstevel@tonic-gate * @return GETOPT_ERR_INVALID_OPT
68*0Sstevel@tonic-gate * if the option is not found in optstring
69*0Sstevel@tonic-gate *
70*0Sstevel@tonic-gate * GETOPT_ERR_MISSING_ARG
71*0Sstevel@tonic-gate * if the option requires an argument which is missing
72*0Sstevel@tonic-gate *
73*0Sstevel@tonic-gate * GETOPT_ERR_INVALID_ARG
74*0Sstevel@tonic-gate * if "-" is not the first character in optstring and a
75*0Sstevel@tonic-gate * non-option argument is encountered
76*0Sstevel@tonic-gate *
77*0Sstevel@tonic-gate * GETOPT_NON_OPTION_ARG
78*0Sstevel@tonic-gate * if "-" is the first character in optstring and a
79*0Sstevel@tonic-gate * non-option argument is encountered
80*0Sstevel@tonic-gate *
81*0Sstevel@tonic-gate * GETOPT_DONE_PARSING
82*0Sstevel@tonic-gate * if the end of the argument list is reached
83*0Sstevel@tonic-gate *
84*0Sstevel@tonic-gate * <optopt>
85*0Sstevel@tonic-gate * the option character itself, if none of the above
86*0Sstevel@tonic-gate * scenarios applies.
87*0Sstevel@tonic-gate */
88*0Sstevel@tonic-gate extern int
getopt_ext(int argc,char * const argv[],const char * optstring)89*0Sstevel@tonic-gate getopt_ext(
90*0Sstevel@tonic-gate int argc,
91*0Sstevel@tonic-gate char * const argv[],
92*0Sstevel@tonic-gate const char *optstring)
93*0Sstevel@tonic-gate {
94*0Sstevel@tonic-gate int c;
95*0Sstevel@tonic-gate int handle_non_options = (*optstring == '-');
96*0Sstevel@tonic-gate
97*0Sstevel@tonic-gate /* Is "-" the first character of optstring? */
98*0Sstevel@tonic-gate if (handle_non_options) {
99*0Sstevel@tonic-gate /* getopt(3) doesn't understand "-" */
100*0Sstevel@tonic-gate optstring++;
101*0Sstevel@tonic-gate }
102*0Sstevel@tonic-gate
103*0Sstevel@tonic-gate switch (c = getopt(argc, argv, optstring)) {
104*0Sstevel@tonic-gate
105*0Sstevel@tonic-gate /*
106*0Sstevel@tonic-gate * getopt(3) returns -1 when 1) it encounters a non-option
107*0Sstevel@tonic-gate * argument or 2) reaches the end of the argument list.
108*0Sstevel@tonic-gate * Distinguish from the two possibilities.
109*0Sstevel@tonic-gate */
110*0Sstevel@tonic-gate case -1:
111*0Sstevel@tonic-gate if (optind < argc) {
112*0Sstevel@tonic-gate optarg = argv[optind];
113*0Sstevel@tonic-gate
114*0Sstevel@tonic-gate /* Non-option argument found */
115*0Sstevel@tonic-gate if (handle_non_options) {
116*0Sstevel@tonic-gate /* Non-option arguments are valid */
117*0Sstevel@tonic-gate c = GETOPT_NON_OPTION_ARG;
118*0Sstevel@tonic-gate optind++;
119*0Sstevel@tonic-gate } else {
120*0Sstevel@tonic-gate /* Non-option arguments are invalid */
121*0Sstevel@tonic-gate c = GETOPT_ERR_INVALID_ARG;
122*0Sstevel@tonic-gate }
123*0Sstevel@tonic-gate } else {
124*0Sstevel@tonic-gate /* End of the argument list reached */
125*0Sstevel@tonic-gate c = GETOPT_DONE_PARSING;
126*0Sstevel@tonic-gate }
127*0Sstevel@tonic-gate break;
128*0Sstevel@tonic-gate
129*0Sstevel@tonic-gate /*
130*0Sstevel@tonic-gate * getopt(3) returns '?' when 1) the "-?" option is
131*0Sstevel@tonic-gate * encountered, 2) an invalid option is given or 3) a
132*0Sstevel@tonic-gate * valid option requiring an argument is found but no
133*0Sstevel@tonic-gate * argument is specified. Distinguish from the three
134*0Sstevel@tonic-gate * possibilities.
135*0Sstevel@tonic-gate */
136*0Sstevel@tonic-gate case '?':
137*0Sstevel@tonic-gate /* Is this an error or was -? encountered? */
138*0Sstevel@tonic-gate if (optopt != '?') {
139*0Sstevel@tonic-gate if (strchr(optstring, optopt) == NULL) {
140*0Sstevel@tonic-gate /* Invalid option */
141*0Sstevel@tonic-gate c = GETOPT_ERR_INVALID_OPT;
142*0Sstevel@tonic-gate optarg = argv[optind-1];
143*0Sstevel@tonic-gate } else {
144*0Sstevel@tonic-gate /* Valid option without required argument */
145*0Sstevel@tonic-gate c = GETOPT_ERR_MISSING_ARG;
146*0Sstevel@tonic-gate }
147*0Sstevel@tonic-gate }
148*0Sstevel@tonic-gate }
149*0Sstevel@tonic-gate
150*0Sstevel@tonic-gate return (c);
151*0Sstevel@tonic-gate }
152