- opt_table[start].arg = (void *)(intptr_t)(opt_count - start);
-}
-
-static char *make_optstring(void)
-{
- char *str = malloc(1 + opt_num_short + opt_num_short_arg + 1);
- const char *p;
- unsigned int i, num = 0;
-
- /* This tells getopt_long we want a ':' returned for missing arg. */
- str[num++] = ':';
- for (p = first_sopt(&i); p; p = next_sopt(p, &i)) {
- str[num++] = *p;
- if (opt_table[i].type == OPT_HASARG)
- str[num++] = ':';
- }
- str[num++] = '\0';
- assert(num == 1 + opt_num_short + opt_num_short_arg + 1);
- return str;
-}
-
-static struct option *make_options(void)
-{
- struct option *options = malloc(sizeof(*options) * (opt_num_long + 1));
- unsigned int i, num = 0, len = 0 /* GCC bogus warning */;
- const char *p;
-
- for (p = first_lopt(&i, &len); p; p = next_lopt(p, &i, &len)) {
- char *buf = malloc(len + 1);
- memcpy(buf, p, len);
- buf[len] = 0;
- options[num].name = buf;
- options[num].has_arg = (opt_table[i].type == OPT_HASARG);
- options[num].flag = NULL;
- options[num].val = 0;
- num++;
- }
- memset(&options[num], 0, sizeof(options[num]));
- assert(num == opt_num_long);
- return options;
-}
-
-static struct opt_table *find_short(char shortopt)
-{
- unsigned int i;
- const char *p;
-
- for (p = first_sopt(&i); p; p = next_sopt(p, &i)) {
- if (*p == shortopt)
- return &opt_table[i];
- }
- abort();
-}
-
-/* We want the index'th long entry. */
-static struct opt_table *find_long(int index, const char **name)
-{
- unsigned int i, len;
- const char *p;
-
- for (p = first_lopt(&i, &len); p; p = next_lopt(p, &i, &len)) {
- if (index == 0) {
- *name = p;
- return &opt_table[i];
- }
- index--;
- }
- abort();
-}
-
-/* glibc does this as:
-/tmp/opt-example: invalid option -- 'x'
-/tmp/opt-example: unrecognized option '--long'
-/tmp/opt-example: option '--someflag' doesn't allow an argument
-/tmp/opt-example: option '--s' is ambiguous
-/tmp/opt-example: option requires an argument -- 's'
-*/
-static void parse_fail(void (*errlog)(const char *fmt, ...),
- char shortopt, const char *longopt, const char *problem)
-{
- if (shortopt)
- errlog("%s: -%c: %s", opt_argv0, shortopt, problem);
- else
- errlog("%s: --%.*s: %s", opt_argv0,
- strcspn(longopt, "|"), longopt, problem);