lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2006 Rich Felker <dalias@aerifal.cx> |
| 3 | * |
| 4 | * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball. |
| 5 | */ |
| 6 | |
| 7 | #include <stddef.h> |
| 8 | #include <getopt.h> |
| 9 | #include <stdio.h> |
| 10 | |
| 11 | |
| 12 | static int __getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly) |
| 13 | { |
| 14 | if (optind >= argc || !argv[optind] || argv[optind][0] != '-') return -1; |
| 15 | if ((longonly && argv[optind][1]) || |
| 16 | (argv[optind][1] == '-' && argv[optind][2])) |
| 17 | { |
| 18 | int i; |
| 19 | for (i=0; longopts[i].name; i++) { |
| 20 | const char *name = longopts[i].name; |
| 21 | char *opt = argv[optind]+2; |
| 22 | while (*name && *name++ == *opt++); |
| 23 | if (*name || (*opt && *opt != '=')) continue; |
| 24 | if (*opt == '=') { |
| 25 | if (!longopts[i].has_arg) continue; |
| 26 | optarg = opt+1; |
| 27 | } else { |
| 28 | if (longopts[i].has_arg == required_argument) { |
| 29 | if (!(optarg = argv[++optind])) |
| 30 | return ':'; |
| 31 | } else optarg = NULL; |
| 32 | } |
| 33 | optind++; |
| 34 | if (idx) *idx = i; |
| 35 | if (longopts[i].flag) { |
| 36 | *longopts[i].flag = longopts[i].val; |
| 37 | return 0; |
| 38 | } |
| 39 | return longopts[i].val; |
| 40 | } |
| 41 | if (argv[optind][1] == '-') { |
| 42 | optind++; |
| 43 | return '?'; |
| 44 | } |
| 45 | } |
| 46 | return getopt(argc, argv, optstring); |
| 47 | } |
| 48 | |
| 49 | int getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx) |
| 50 | { |
| 51 | return __getopt_long(argc, argv, optstring, longopts, idx, 0); |
| 52 | } |
| 53 | |
| 54 | int getopt_long_only(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx) |
| 55 | { |
| 56 | return __getopt_long(argc, argv, optstring, longopts, idx, 1); |
| 57 | } |