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 /util-linux | |
parent | 704c596563a5b4da4349b272d0c9c71aacea34a7 (diff) | |
download | busybox-w32-83e20cb81ca6d22a1ca268a0a64523b5af67325a.tar.gz busybox-w32-83e20cb81ca6d22a1ca268a0a64523b5af67325a.tar.bz2 busybox-w32-83e20cb81ca6d22a1ca268a0a64523b5af67325a.zip |
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>
Diffstat (limited to 'util-linux')
-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 { | |||
156 | static const char *normalize(const char *arg) | 156 | static const char *normalize(const char *arg) |
157 | { | 157 | { |
158 | char *bufptr; | 158 | char *bufptr; |
159 | #if ENABLE_FEATURE_CLEAN_UP | ||
160 | static char *BUFFER = NULL; | ||
161 | free(BUFFER); | ||
162 | #else | ||
163 | char *BUFFER; | 159 | char *BUFFER; |
164 | #endif | ||
165 | 160 | ||
166 | if (!quote) { /* Just copy arg */ | 161 | if (!quote) { /* Just return arg */ |
167 | BUFFER = xstrdup(arg); | 162 | return arg; |
168 | return BUFFER; | ||
169 | } | 163 | } |
170 | 164 | ||
171 | /* Each character in arg may take up to four characters in the result: | 165 | /* Each character in arg may take up to four characters in the result: |
172 | For a quote we need a closing quote, a backslash, a quote and an | 166 | For a quote we need a closing quote, a backslash, a quote and an |
173 | opening quote! We need also the global opening and closing quote, | 167 | opening quote! We need also the global opening and closing quote, |
174 | and one extra character for '\0'. */ | 168 | and one extra character for '\0'. */ |
175 | BUFFER = xmalloc(strlen(arg)*4 + 3); | 169 | BUFFER = auto_string(xmalloc(strlen(arg)*4 + 3)); |
176 | 170 | ||
177 | bufptr = BUFFER; | 171 | bufptr = BUFFER; |
178 | *bufptr ++= '\''; | 172 | *bufptr ++= '\''; |
179 | 173 | ||
180 | while (*arg) { | 174 | while (*arg) { |
181 | if (*arg == '\'') { | 175 | if (shell_TCSH && *arg == '\n') { |
182 | /* Quote: replace it with: '\'' */ | ||
183 | *bufptr ++= '\''; | ||
184 | *bufptr ++= '\\'; | ||
185 | *bufptr ++= '\''; | ||
186 | *bufptr ++= '\''; | ||
187 | } else if (shell_TCSH && *arg == '!') { | ||
188 | /* Exclamation mark: replace it with: \! */ | ||
189 | *bufptr ++= '\''; | ||
190 | *bufptr ++= '\\'; | ||
191 | *bufptr ++= '!'; | ||
192 | *bufptr ++= '\''; | ||
193 | } else if (shell_TCSH && *arg == '\n') { | ||
194 | /* Newline: replace it with: \n */ | 176 | /* Newline: replace it with: \n */ |
195 | *bufptr ++= '\\'; | 177 | *bufptr++ = '\\'; |
196 | *bufptr ++= 'n'; | 178 | *bufptr++ = 'n'; |
197 | } else if (shell_TCSH && isspace(*arg)) { | ||
198 | /* Non-newline whitespace: replace it with \<ws> */ | ||
199 | *bufptr ++= '\''; | ||
200 | *bufptr ++= '\\'; | ||
201 | *bufptr ++= *arg; | ||
202 | *bufptr ++= '\''; | ||
203 | } else | 179 | } else |
180 | if ((shell_TCSH && (*arg == '!' || isspace(*arg))) | ||
181 | || *arg == '\'' | ||
182 | ) { | ||
183 | /* Quote exclamation marks, non-NL whitespace and quotes */ | ||
184 | *bufptr++ = '\''; | ||
185 | *bufptr++ = '\\'; | ||
186 | *bufptr++ = *arg; | ||
187 | *bufptr++ = '\''; | ||
188 | } else { | ||
204 | /* Just copy */ | 189 | /* Just copy */ |
205 | *bufptr ++= *arg; | 190 | *bufptr ++= *arg; |
191 | } | ||
206 | arg++; | 192 | arg++; |
207 | } | 193 | } |
208 | *bufptr ++= '\''; | 194 | *bufptr++ = '\''; |
209 | *bufptr ++= '\0'; | 195 | *bufptr++ = '\0'; |
210 | return BUFFER; | 196 | return BUFFER; |
211 | } | 197 | } |
212 | 198 | ||
@@ -327,12 +313,18 @@ static struct option *add_long_options(struct option *long_options, char *option | |||
327 | 313 | ||
328 | static void set_shell(const char *new_shell) | 314 | static void set_shell(const char *new_shell) |
329 | { | 315 | { |
330 | if (strcmp(new_shell, "bash") == 0 || strcmp(new_shell, "sh") == 0) | 316 | switch (index_in_strings("bash\0sh\0tcsh\0csh\0", new_shell)) { |
331 | return; | 317 | case 0: |
332 | if (strcmp(new_shell, "tcsh") == 0 || strcmp(new_shell, "csh") == 0) | 318 | case 1: |
319 | break; | ||
320 | case 2: | ||
321 | case 3: | ||
333 | option_mask32 |= SHELL_IS_TCSH; | 322 | option_mask32 |= SHELL_IS_TCSH; |
334 | else | 323 | break; |
324 | default: | ||
335 | bb_error_msg("unknown shell '%s', assuming bash", new_shell); | 325 | bb_error_msg("unknown shell '%s', assuming bash", new_shell); |
326 | break; | ||
327 | } | ||
336 | } | 328 | } |
337 | 329 | ||
338 | 330 | ||