diff options
author | Ron Yorston <rmy@pobox.com> | 2021-09-12 12:26:03 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2021-09-16 01:09:32 +0200 |
commit | 83e20cb81ca6d22a1ca268a0a64523b5af67325a (patch) | |
tree | 4570072732f197fd0bddad9dd1e4892e67bac827 | |
parent | 704c596563a5b4da4349b272d0c9c71aacea34a7 (diff) |
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 <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | util-linux/getopt.c | 64 |
1 files changed, 28 insertions, 36 deletions
diff --git a/util-linux/getopt.c b/util-linux/getopt.c index 1fa402429..4148586d3 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 \<ws> */ - *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; + } } |