* getopt_internal -- * Parse argc/argv argument vector. Called by user level routines. */
| 236 | * Parse argc/argv argument vector. Called by user level routines. |
| 237 | */ |
| 238 | static int |
| 239 | getopt_internal(int nargc, char **nargv, const char *options, |
| 240 | const struct option *long_options, int *idx, int flags) |
| 241 | { |
| 242 | char *oli; /* option letter list index */ |
| 243 | int optchar, short_too; |
| 244 | static int posixly_correct = -1; |
| 245 | size_t len; |
| 246 | int optreset = 0; |
| 247 | |
| 248 | if (options == NULL) |
| 249 | return (-1); |
| 250 | |
| 251 | /* |
| 252 | * Disable GNU extensions if POSIXLY_CORRECT is set or options |
| 253 | * string begins with a '+'. |
| 254 | */ |
| 255 | if (posixly_correct == -1) { |
| 256 | errno_t err = _wgetenv_s(&len, NULL, 0, L"POSIXLY_CORRECT"); |
| 257 | posixly_correct = (err == 0) && (len > 0); |
| 258 | } |
| 259 | if (!posixly_correct || *options == '+') |
| 260 | flags &= ~FLAG_PERMUTE; |
| 261 | else if (*options == '-') |
| 262 | flags |= FLAG_ALLARGS; |
| 263 | if (*options == '+' || *options == '-') |
| 264 | options++; |
| 265 | /* |
| 266 | * reset if requested |
| 267 | */ |
| 268 | if (optind == 0) |
| 269 | optind = optreset = 1; |
| 270 | |
| 271 | optarg = NULL; |
| 272 | if (optreset) |
| 273 | nonopt_start = nonopt_end = -1; |
| 274 | start: |
| 275 | if (optreset || !*place) { /* update scanning pointer */ |
| 276 | optreset = 0; |
| 277 | if (optind >= nargc) { /* end of argument vector */ |
| 278 | place = EMSG; |
| 279 | if (nonopt_end != -1) { |
| 280 | /* do permutation, if we have to */ |
| 281 | permute_args(nonopt_start, nonopt_end, |
| 282 | optind, nargv); |
| 283 | optind -= nonopt_end - nonopt_start; |
| 284 | } else if (nonopt_start != -1) { |
| 285 | /* |
| 286 | * If we skipped non-options, set optind |
| 287 | * to the first of them. |
| 288 | */ |
| 289 | optind = nonopt_start; |
| 290 | } |
| 291 | nonopt_start = nonopt_end = -1; |
| 292 | return (-1); |
| 293 | } |
| 294 | place = nargv[optind]; |
| 295 | if (*place != '-' || |
no test coverage detected