From 83e20cb81ca6d22a1ca268a0a64523b5af67325a Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sun, 12 Sep 2021 12:26:03 +0100 Subject: getopt: code shrink function old new delta .rodata 99277 99290 +13 normalize 177 142 -35 getopt_main 675 622 -53 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 1/2 up/down: 13/-88) Total: -75 bytes Signed-off-by: Ron Yorston Signed-off-by: Denys Vlasenko --- util-linux/getopt.c | 64 +++++++++++++++++++++++------------------------------ 1 file changed, 28 insertions(+), 36 deletions(-) (limited to 'util-linux/getopt.c') diff --git a/util-linux/getopt.c b/util-linux/getopt.c index 1fa4024..4148586 100644 --- a/util-linux/getopt.c +++ b/util-linux/getopt.c @@ -156,57 +156,43 @@ enum { static const char *normalize(const char *arg) { char *bufptr; -#if ENABLE_FEATURE_CLEAN_UP - static char *BUFFER = NULL; - free(BUFFER); -#else char *BUFFER; -#endif - if (!quote) { /* Just copy arg */ - BUFFER = xstrdup(arg); - return BUFFER; + if (!quote) { /* Just return arg */ + return arg; } /* Each character in arg may take up to four characters in the result: For a quote we need a closing quote, a backslash, a quote and an opening quote! We need also the global opening and closing quote, and one extra character for '\0'. */ - BUFFER = xmalloc(strlen(arg)*4 + 3); + BUFFER = auto_string(xmalloc(strlen(arg)*4 + 3)); bufptr = BUFFER; *bufptr ++= '\''; while (*arg) { - if (*arg == '\'') { - /* Quote: replace it with: '\'' */ - *bufptr ++= '\''; - *bufptr ++= '\\'; - *bufptr ++= '\''; - *bufptr ++= '\''; - } else if (shell_TCSH && *arg == '!') { - /* Exclamation mark: replace it with: \! */ - *bufptr ++= '\''; - *bufptr ++= '\\'; - *bufptr ++= '!'; - *bufptr ++= '\''; - } else if (shell_TCSH && *arg == '\n') { + if (shell_TCSH && *arg == '\n') { /* Newline: replace it with: \n */ - *bufptr ++= '\\'; - *bufptr ++= 'n'; - } else if (shell_TCSH && isspace(*arg)) { - /* Non-newline whitespace: replace it with \ */ - *bufptr ++= '\''; - *bufptr ++= '\\'; - *bufptr ++= *arg; - *bufptr ++= '\''; + *bufptr++ = '\\'; + *bufptr++ = 'n'; } else + if ((shell_TCSH && (*arg == '!' || isspace(*arg))) + || *arg == '\'' + ) { + /* Quote exclamation marks, non-NL whitespace and quotes */ + *bufptr++ = '\''; + *bufptr++ = '\\'; + *bufptr++ = *arg; + *bufptr++ = '\''; + } else { /* Just copy */ *bufptr ++= *arg; + } arg++; } - *bufptr ++= '\''; - *bufptr ++= '\0'; + *bufptr++ = '\''; + *bufptr++ = '\0'; return BUFFER; } @@ -327,12 +313,18 @@ static struct option *add_long_options(struct option *long_options, char *option static void set_shell(const char *new_shell) { - if (strcmp(new_shell, "bash") == 0 || strcmp(new_shell, "sh") == 0) - return; - if (strcmp(new_shell, "tcsh") == 0 || strcmp(new_shell, "csh") == 0) + switch (index_in_strings("bash\0sh\0tcsh\0csh\0", new_shell)) { + case 0: + case 1: + break; + case 2: + case 3: option_mask32 |= SHELL_IS_TCSH; - else + break; + default: bb_error_msg("unknown shell '%s', assuming bash", new_shell); + break; + } } -- cgit v1.1