| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#include <stdio.h> |
| 38 |
#include <string.h> |
| 39 |
#include "aix/getopt.h" |
| 40 |
|
| 41 |
#define BADCH '?' |
| 42 |
#define BADARG ':' |
| 43 |
#define EMSG "" |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
int |
| 57 |
getopt_long(int argc, char *const argv[], |
| 58 |
const char *optstring, |
| 59 |
const struct option *longopts, int *longindex) |
| 60 |
{ |
| 61 |
static char *place = EMSG; |
| 62 |
char *oli; |
| 63 |
|
| 64 |
if (!*place) |
| 65 |
{ |
| 66 |
if (optind >= argc) |
| 67 |
{ |
| 68 |
place = EMSG; |
| 69 |
return -1; |
| 70 |
} |
| 71 |
|
| 72 |
place = argv[optind]; |
| 73 |
|
| 74 |
if (place[0] != '-') |
| 75 |
{ |
| 76 |
place = EMSG; |
| 77 |
return -1; |
| 78 |
} |
| 79 |
|
| 80 |
place++; |
| 81 |
|
| 82 |
if (!*place) |
| 83 |
{ |
| 84 |
|
| 85 |
place = EMSG; |
| 86 |
return -1; |
| 87 |
} |
| 88 |
|
| 89 |
if (place[0] == '-' && place[1] == '\0') |
| 90 |
{ |
| 91 |
|
| 92 |
++optind; |
| 93 |
place = EMSG; |
| 94 |
return -1; |
| 95 |
} |
| 96 |
|
| 97 |
if (place[0] == '-' && place[1]) |
| 98 |
{ |
| 99 |
|
| 100 |
size_t namelen; |
| 101 |
int i; |
| 102 |
|
| 103 |
place++; |
| 104 |
|
| 105 |
namelen = strcspn(place, "="); |
| 106 |
for (i = 0; longopts[i].name != NULL; i++) |
| 107 |
{ |
| 108 |
if (strlen(longopts[i].name) == namelen |
| 109 |
&& strncmp(place, longopts[i].name, namelen) == 0) |
| 110 |
{ |
| 111 |
int has_arg = longopts[i].has_arg; |
| 112 |
|
| 113 |
if (has_arg != no_argument) |
| 114 |
{ |
| 115 |
if (place[namelen] == '=') |
| 116 |
optarg = place + namelen + 1; |
| 117 |
else if (optind < argc - 1 && |
| 118 |
has_arg == required_argument) |
| 119 |
{ |
| 120 |
optind++; |
| 121 |
optarg = argv[optind]; |
| 122 |
} |
| 123 |
else |
| 124 |
{ |
| 125 |
if (optstring[0] == ':') |
| 126 |
return BADARG; |
| 127 |
|
| 128 |
if (opterr && has_arg == required_argument) |
| 129 |
fprintf(stderr, |
| 130 |
"%s: option requires an argument -- %s\n", |
| 131 |
argv[0], place); |
| 132 |
|
| 133 |
place = EMSG; |
| 134 |
optind++; |
| 135 |
|
| 136 |
if (has_arg == required_argument) |
| 137 |
return BADCH; |
| 138 |
optarg = NULL; |
| 139 |
} |
| 140 |
} |
| 141 |
else |
| 142 |
{ |
| 143 |
optarg = NULL; |
| 144 |
if (place[namelen] != 0) |
| 145 |
{ |
| 146 |
|
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
optind++; |
| 151 |
|
| 152 |
if (longindex) |
| 153 |
*longindex = i; |
| 154 |
|
| 155 |
place = EMSG; |
| 156 |
|
| 157 |
if (longopts[i].flag == NULL) |
| 158 |
return longopts[i].val; |
| 159 |
else |
| 160 |
{ |
| 161 |
*longopts[i].flag = longopts[i].val; |
| 162 |
return 0; |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
if (opterr && optstring[0] != ':') |
| 168 |
fprintf(stderr, |
| 169 |
"%s: illegal option -- %s\n", argv[0], place); |
| 170 |
place = EMSG; |
| 171 |
optind++; |
| 172 |
return BADCH; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
optopt = (int) *place++; |
| 178 |
|
| 179 |
oli = strchr(optstring, optopt); |
| 180 |
if (!oli) |
| 181 |
{ |
| 182 |
if (!*place) |
| 183 |
++optind; |
| 184 |
if (opterr && *optstring != ':') |
| 185 |
fprintf(stderr, |
| 186 |
"%s: illegal option -- %c\n", argv[0], optopt); |
| 187 |
return BADCH; |
| 188 |
} |
| 189 |
|
| 190 |
if (oli[1] != ':') |
| 191 |
{ |
| 192 |
optarg = NULL; |
| 193 |
if (!*place) |
| 194 |
++optind; |
| 195 |
} |
| 196 |
else |
| 197 |
{ |
| 198 |
if (*place) |
| 199 |
optarg = place; |
| 200 |
else if (argc <= ++optind) |
| 201 |
{ |
| 202 |
place = EMSG; |
| 203 |
if (*optstring == ':') |
| 204 |
return BADARG; |
| 205 |
if (opterr) |
| 206 |
fprintf(stderr, |
| 207 |
"%s: option requires an argument -- %c\n", |
| 208 |
argv[0], optopt); |
| 209 |
return BADCH; |
| 210 |
} |
| 211 |
else |
| 212 |
|
| 213 |
optarg = argv[optind]; |
| 214 |
place = EMSG; |
| 215 |
++optind; |
| 216 |
} |
| 217 |
return optopt; |
| 218 |
} |