* Parse an integer from an optarg. */
| 665 | * Parse an integer from an optarg. |
| 666 | */ |
| 667 | static intptr_t parseIntOptArg(const char *option, const char *optarg, |
| 668 | intptr_t lb, intptr_t ub) |
| 669 | { |
| 670 | const char *optarg_0 = optarg; |
| 671 | bool neg = (optarg[0] == '-'); |
| 672 | if (neg) |
| 673 | optarg++; |
| 674 | int base = 10; |
| 675 | if (optarg[0] == '0' && optarg[1] == 'x') |
| 676 | base = 16; |
| 677 | errno = 0; |
| 678 | char *end = nullptr; |
| 679 | intptr_t r = (intptr_t)strtoul(optarg, &end, base); |
| 680 | r = (neg? -r: r); |
| 681 | if (errno != 0 || end == optarg || |
| 682 | (end != nullptr && *end != '\0') || r < lb || r > ub) |
| 683 | { |
| 684 | error("failed to parse argument \"%s\" for the `%s' option; " |
| 685 | "expected a number within the range %zd..%zd", option, |
| 686 | optarg_0, lb, ub); |
| 687 | } |
| 688 | return r; |
| 689 | } |
| 690 | |
| 691 | /* |
| 692 | * Entry. |