#include "closestream.h"
#include "nls.h"
+#include "strutils.h"
#include "xalloc.h"
/* NON_OPT is the code that is returned getopt(3) when a non-option is
if (ctl->long_options_nr == ctl->long_options_length) {
ctl->long_options_length += REALLOC_INCREMENT;
- ctl->long_options = xrealloc(ctl->long_options,
- sizeof(struct option) *
- ctl->long_options_length);
+ ctl->long_options = xreallocarray(ctl->long_options,
+ ctl->long_options_length,
+ sizeof(struct option));
}
if (name) {
/* Not for init! */
}
+static void add_short_options(struct getopt_control *ctl, char *options)
+{
+ free(ctl->optstr);
+ if (*options != '+' && getenv("POSIXLY_CORRECT"))
+ ctl->optstr = strconcat("+", options);
+ else
+ ctl->optstr = xstrdup(options);
+ if (!ctl->optstr)
+ err_oom();
+}
+
+
/*
* Register several long options. options is a string of long options,
* separated by commas or whitespace. This nukes options!
fputs(_(" -T, --test test for getopt(1) version\n"), stdout);
fputs(_(" -u, --unquoted do not quote the output\n"), stdout);
fputs(USAGE_SEPARATOR, stdout);
- printf(USAGE_HELP_OPTIONS(31));
- printf(USAGE_MAN_TAIL("getopt(1)"));
+ fprintf(stdout, USAGE_HELP_OPTIONS(31));
+ fprintf(stdout, USAGE_MAN_TAIL("getopt(1)"));
exit(EXIT_SUCCESS);
}
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
- atexit(close_stdout);
+ close_stdout_atexit();
if (getenv("GETOPT_COMPATIBLE"))
ctl.compatible = 1;
*/
printf(" --\n");
return EXIT_SUCCESS;
- } else
- parse_error(_("missing optstring argument"));
+ }
+ parse_error(_("missing optstring argument"));
}
add_longopt(&ctl, NULL, 0); /* init */
case 'a':
getopt_long_fp = getopt_long_only;
break;
- case 'h':
- usage();
case 'o':
- free(ctl.optstr);
- ctl.optstr = xstrdup(optarg);
+ add_short_options(&ctl, optarg);
break;
case 'l':
add_long_options(&ctl, optarg);
case 'u':
ctl.quote = 0;
break;
+
case 'V':
- printf(UTIL_LINUX_VERSION);
- return EXIT_SUCCESS;
+ print_version(EXIT_SUCCESS);
case '?':
case ':':
parse_error(NULL);
+ case 'h':
+ usage();
default:
parse_error(_("internal error, contact the author."));
}
if (optind >= argc)
parse_error(_("missing optstring argument"));
else {
- ctl.optstr = xstrdup(argv[optind]);
+ add_short_options(&ctl, argv[optind]);
optind++;
}
}