-static char *make_optstring(void)
-{
- /* Worst case, each one is ":x:", plus nul term. */
- char *str = malloc(1 + opt_count * 2 + 1);
- unsigned int num, i;
-
- /* This tells getopt_long we want a ':' returned for missing arg. */
- str[0] = ':';
- num = 1;
- for (i = 0; i < opt_count; i++) {
- if (!opt_table[i].shortopt)
- continue;
- str[num++] = opt_table[i].shortopt;
- if (opt_table[i].flags == OPT_HASARG)
- str[num++] = ':';
- }
- str[num] = '\0';
- return str;
-}
-
-static struct option *make_options(void)
-{
- struct option *options = malloc(sizeof(*options) * (opt_count + 1));
- unsigned int i, num;
-
- for (num = i = 0; i < opt_count; i++) {
- if (!opt_table[i].longopt)
- continue;
- options[num].name = opt_table[i].longopt;
- options[num].has_arg = (opt_table[i].flags == OPT_HASARG);
- options[num].flag = NULL;
- options[num].val = 0;
- num++;
- }
- memset(&options[num], 0, sizeof(options[num]));
- return options;
-}
-
-static struct opt_table *find_short(char shortopt)
-{
- unsigned int i;
- for (i = 0; i < opt_count; i++) {
- if (opt_table[i].shortopt == shortopt)
- return &opt_table[i];
- }
- abort();
-}
-
-/* We want the index'th long entry. */
-static struct opt_table *find_long(int index)
-{
- unsigned int i;
- for (i = 0; i < opt_count; i++) {
- if (!opt_table[i].longopt)
- continue;
- if (index == 0)
- 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, longopt, problem);
-}
-
-void dump_optstate(void);
-void dump_optstate(void)
-{
- printf("opterr = %i, optind = %i, optopt = %i, optarg = %s\n",
- opterr, optind, optopt, optarg);
-}
-