| 1 |
/* |
| 2 |
* Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved. |
| 3 |
* |
| 4 |
* This Software is licensed under one of the following licenses: |
| 5 |
* |
| 6 |
* 1) under the terms of the "Common Public License 1.0" a copy of which is |
| 7 |
* in the file LICENSE.txt in the root directory. The license is also |
| 8 |
* available from the Open Source Initiative, see |
| 9 |
* http://www.opensource.org/licenses/cpl.php. |
| 10 |
* |
| 11 |
* 2) under the terms of the "The BSD License" a copy of which is in the file |
| 12 |
* LICENSE2.txt in the root directory. The license is also available from |
| 13 |
* the Open Source Initiative, see |
| 14 |
* http://www.opensource.org/licenses/bsd-license.php. |
| 15 |
* |
| 16 |
* 3) under the terms of the "GNU General Public License (GPL) Version 2" a |
| 17 |
* copy of which is in the file LICENSE3.txt in the root directory. The |
| 18 |
* license is also available from the Open Source Initiative, see |
| 19 |
* http://www.opensource.org/licenses/gpl-license.php. |
| 20 |
* |
| 21 |
* Licensee has the right to choose one of the above licenses. |
| 22 |
* |
| 23 |
* Redistributions of source code must retain the above copyright |
| 24 |
* notice and one of the license notices. |
| 25 |
* |
| 26 |
* Redistributions in binary form must reproduce both the above copyright |
| 27 |
* notice, one of the license notices in the documentation |
| 28 |
* and/or other materials provided with the distribution. |
| 29 |
*/ |
| 30 |
|
| 31 |
#include "dapl_proto.h" |
| 32 |
|
| 33 |
#define GETOPT_MAGIC 0x04030201 |
| 34 |
|
| 35 |
#define BADCH (int)'?' |
| 36 |
#define BADARG (int)':' |
| 37 |
#define EMSG "" |
| 38 |
|
| 39 |
/** |
| 40 |
* Initialize the getopt fields in preparation for parsing |
| 41 |
* a command line. |
| 42 |
*/ |
| 43 |
void DT_mygetopt_init(mygetopt_t * opts) |
| 44 |
{ |
| 45 |
opts->magic = GETOPT_MAGIC; |
| 46 |
opts->place = EMSG; |
| 47 |
opts->opterr = 1; |
| 48 |
opts->optind = 1; |
| 49 |
opts->optopt = 0; |
| 50 |
opts->optarg = 0; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Parse command line arguments. |
| 55 |
* |
| 56 |
* Return either the option discovered, or |
| 57 |
* (int) -1 when there are no more options |
| 58 |
* (int) '?' when an illegal option is found |
| 59 |
* (int) ':' when a required option argument is missing. |
| 60 |
*/ |
| 61 |
int |
| 62 |
DT_mygetopt_r(int argc, char *const *argv, const char *ostr, mygetopt_t * opts) |
| 63 |
{ |
| 64 |
char *p; |
| 65 |
char *oli; /* option letter list index */ |
| 66 |
if (GETOPT_MAGIC != opts->magic) { |
| 67 |
DT_Mdep_printf("%s: getopt warning: " |
| 68 |
"option argument is not properly initialized.\n", |
| 69 |
argc > 0 ? argv[0] : "unknown command"); |
| 70 |
DT_mygetopt_init(opts); |
| 71 |
} |
| 72 |
if (!*(opts->place)) { /* update scanning pointer */ |
| 73 |
if ((opts->optind) >= argc || |
| 74 |
*((opts->place) = argv[(opts->optind)]) != '-') { |
| 75 |
(opts->place) = EMSG; |
| 76 |
return (EOF); |
| 77 |
} |
| 78 |
if ((opts->place)[0] != '-') { |
| 79 |
/* Invalid 1st argument */ |
| 80 |
return (BADCH); |
| 81 |
} |
| 82 |
if ((opts->place)[1] && *++(opts->place) == '-') { |
| 83 |
/* found "--" which is an invalid option */ |
| 84 |
++(opts->optind); |
| 85 |
(opts->place) = EMSG; |
| 86 |
return (BADCH); |
| 87 |
} |
| 88 |
} /* option letter okay? */ |
| 89 |
opts->optopt = (int)*(opts->place)++; |
| 90 |
oli = strchr(ostr, (opts->optopt)); |
| 91 |
if (opts->optopt == (int)':' || !oli) { |
| 92 |
/* |
| 93 |
* if the user didn't specify '-' as an option, assume it means EOF. |
| 94 |
*/ |
| 95 |
if ((opts->optopt) == (int)'-') { |
| 96 |
/* return (EOF); */ |
| 97 |
return (BADCH); |
| 98 |
} |
| 99 |
if (!*(opts->place)) { |
| 100 |
++(opts->optind); |
| 101 |
} |
| 102 |
if ((opts->opterr) && *ostr != ':') { |
| 103 |
p = strchr(*argv, '/'); |
| 104 |
if (!p) { |
| 105 |
p = *argv; |
| 106 |
} else { |
| 107 |
++p; |
| 108 |
} |
| 109 |
|
| 110 |
if (opts->optopt != '?') { /* Anything but '?' needs error */ |
| 111 |
DT_Mdep_printf("%s: Illegal option -- %c\n", |
| 112 |
p, (opts->optopt)); |
| 113 |
} |
| 114 |
} |
| 115 |
return (BADCH); |
| 116 |
} |
| 117 |
if (*++oli != ':') { /* don't need argument */ |
| 118 |
(opts->optarg) = NULL; |
| 119 |
if (!*(opts->place)) { |
| 120 |
++(opts->optind); |
| 121 |
} |
| 122 |
} else { /* need an argument */ |
| 123 |
|
| 124 |
if (*(opts->place)) { /* no white space */ |
| 125 |
(opts->optarg) = (opts->place); |
| 126 |
} else { |
| 127 |
if (argc <= ++(opts->optind)) { /* no arg */ |
| 128 |
(opts->place) = EMSG; |
| 129 |
if (*ostr == ':') { |
| 130 |
return (BADARG); |
| 131 |
} |
| 132 |
p = strchr(*argv, '/'); |
| 133 |
if (!p) { |
| 134 |
p = *argv; |
| 135 |
} else { |
| 136 |
++p; |
| 137 |
} |
| 138 |
if ((opts->opterr)) { |
| 139 |
DT_Mdep_printf |
| 140 |
("%s: option requires an argument -- %c\n", |
| 141 |
p, (opts->optopt)); |
| 142 |
} |
| 143 |
return (BADCH); |
| 144 |
} else { /* white space */ |
| 145 |
|
| 146 |
(opts->optarg) = argv[(opts->optind)]; |
| 147 |
} |
| 148 |
} |
| 149 |
(opts->place) = EMSG; |
| 150 |
++(opts->optind); |
| 151 |
} |
| 152 |
return (opts->optopt); /* dump back option letter */ |
| 153 |
} |