--- getopt/trunk/getopt.c 2012/07/16 20:36:18 321 +++ getopt/trunk/getopt.c 2012/08/12 10:18:02 327 @@ -37,6 +37,8 @@ * ) * Version 1.1.4: Mon Nov 7 2005 * Fixed a few type's in the manpage + * Version 1.1.5: Mon Nov 7 2005 + * Sync with util-linux-2.21, fixed build problems, many new translations */ /* Exit codes: @@ -64,6 +66,7 @@ #endif #include "nls.h" +#include "xalloc.h" /* NON_OPT is the code that is returned when a non-option is found in '+' * mode */ @@ -81,12 +84,11 @@ static int quiet_output = 0; /* 0 is not quiet. */ static int quote = 1; /* 1 is do quote. */ +/* Allow changing which getopt is in use with function pointer */ int (*getopt_long_fp) (int argc, char *const *argv, const char *optstr, const struct option * longopts, int *longindex); /* Function prototypes */ -void *our_malloc(size_t size); -void *our_realloc(void *ptr, size_t size); static const char *normalize(const char *arg); static int generate_output(char *argv[], int argc, const char *optstr, const struct option *longopts); @@ -97,26 +99,6 @@ static void print_help(void); static void set_shell(const char *new_shell); -void *our_malloc(size_t size) -{ - void *ret=malloc(size); - if (! ret) { - fprintf(stderr, ("%s: Out of memory!\n"), "getopt"); - exit(XALLOC_EXIT_CODE); - } - return(ret); -} - -void *our_realloc(void *ptr, size_t size) -{ - void *ret=realloc(ptr, size); - if (! ret && size) { - fprintf(stderr, ("%s: Out of memory!\n"), "getopt"); - exit(XALLOC_EXIT_CODE); - } - return(ret); -} - /* * This function 'normalizes' a single argument: it puts single quotes * around it and escapes other special characters. If quote is false, it @@ -132,13 +114,11 @@ const char *argptr = arg; char *bufptr; - if (BUFFER != NULL) - free(BUFFER); + free(BUFFER); if (!quote) { /* Just copy arg */ - BUFFER = our_malloc(strlen(arg)+1); - + BUFFER = xmalloc(strlen(arg) + 1); strcpy(BUFFER, arg); return BUFFER; } @@ -149,7 +129,7 @@ * and an opening quote! We need also the global opening and closing * quote, and one extra character for '\0'. */ - BUFFER = our_malloc(strlen(arg)*4+3); + BUFFER = xmalloc(strlen(arg) * 4 + 3); bufptr = BUFFER; *bufptr++ = '\''; @@ -244,8 +224,9 @@ static void __attribute__ ((__noreturn__)) parse_error(const char *message) { if (message) - fprintf(stderr, "getopt: %s\n", message); - fputs(_("Try `getopt --help' for more information.\n"), stderr); + fprintf(stderr, "%s: %s\n", program_invocation_short_name, message); + fprintf(stderr, _("Try `%s --help' for more information.\n"), + program_invocation_short_name); exit(PARAMETER_EXIT_CODE); } @@ -269,9 +250,9 @@ if (long_options_nr == long_options_length) { long_options_length += LONG_OPTIONS_INCR; - long_options = our_realloc(long_options, - sizeof(struct option) * - long_options_length); + long_options = xrealloc(long_options, + sizeof(struct option) * + long_options_length); } long_options[long_options_nr].name = NULL; @@ -284,7 +265,7 @@ long_options[long_options_nr - 1].has_arg = has_arg; long_options[long_options_nr - 1].flag = NULL; long_options[long_options_nr - 1].val = LONG_OPT; - tmp = our_malloc(strlen(name) + 1); + tmp = xmalloc(strlen(name) + 1); strcpy(tmp, name); long_options[long_options_nr - 1].name = tmp; } @@ -343,17 +324,20 @@ fputs(_(" getopt [options] [--] optstring parameters\n"), stderr); fputs(_(" getopt [options] -o|--options optstring [options] [--]\n"), stderr); fputs(_(" parameters\n"), stderr); + fputs(_("\nOptions:\n"), stderr); fputs(_(" -a, --alternative Allow long options starting with single -\n"), stderr); fputs(_(" -h, --help This small usage guide\n"), stderr); - fputs(_(" -l, --longoptions=longopts Long options to be recognized\n"), stderr); - fputs(_(" -n, --name=progname The name under which errors are reported\n"), stderr); - fputs(_(" -o, --options=optstring Short options to be recognized\n"), stderr); + fputs(_(" -l, --longoptions Long options to be recognized\n"), stderr); + fputs(_(" -n, --name The name under which errors are reported\n"), stderr); + fputs(_(" -o, --options Short options to be recognized\n"), stderr); fputs(_(" -q, --quiet Disable error reporting by getopt(3)\n"), stderr); fputs(_(" -Q, --quiet-output No normal output\n"), stderr); - fputs(_(" -s, --shell=shell Set shell quoting conventions\n"), stderr); + fputs(_(" -s, --shell Set shell quoting conventions\n"), stderr); fputs(_(" -T, --test Test for getopt(1) version\n"), stderr); - fputs(_(" -u, --unqote Do not quote the output\n"), stderr); + fputs(_(" -u, --unquote Do not quote the output\n"), stderr); fputs(_(" -V, --version Output version information\n"), stderr); + fputc('\n', stderr); + exit(PARAMETER_EXIT_CODE); } @@ -380,12 +364,9 @@ {"version", no_argument, NULL, 'V'}, {NULL, 0, NULL, 0} }; -#if WITHOUT_GETTEXT -#else setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); -#endif init_longopt(); getopt_long_fp = getopt_long; @@ -408,7 +389,7 @@ if (argv[1][0] != '-' || compatible) { quote = 0; - optstr = our_malloc(strlen(argv[1]) + 1); + optstr = xmalloc(strlen(argv[1]) + 1); strcpy(optstr, argv[1] + strspn(argv[1], "-+")); argv[1] = argv[0]; return generate_output(argv + 1, argc - 1, optstr, @@ -424,18 +405,16 @@ case 'h': print_help(); case 'o': - if (optstr) - free(optstr); - optstr = our_malloc(strlen(optarg)+1); + free(optstr); + optstr = xmalloc(strlen(optarg) + 1); strcpy(optstr, optarg); break; case 'l': add_long_options(optarg); break; case 'n': - if (name) - free(name); - name = our_malloc(strlen(optarg)+1); + free(name); + name = xmalloc(strlen(optarg) + 1); strcpy(name, optarg); break; case 'q': @@ -453,7 +432,7 @@ quote = 0; break; case 'V': - printf(_("getopt (enhanced) 1.1.4\n")); + printf(_("%s (enhanced) %s\n"), program_invocation_short_name, program_version); return EXIT_SUCCESS; case '?': case ':': @@ -466,7 +445,7 @@ if (optind >= argc) parse_error(_("missing optstring argument")); else { - optstr=our_malloc(strlen(argv[optind])+1); + optstr = xmalloc(strlen(argv[optind]) + 1); strcpy(optstr, argv[optind]); optind++; }