From 8489ebce32f4b6cf5c1c1120fd474e26f7659db7 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sat, 13 Feb 2021 10:17:16 +0000 Subject: win32: code shrink Rewrite the recent change to tab completion so it only needs one call to sprintf. Then replace sprintf with strcpy/stpcpy, both there and in a couple of other places. Saves 40 bytes. --- libbb/lineedit.c | 20 +++++++++++++++----- shell/ash.c | 2 +- win32/mingw.c | 2 +- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/libbb/lineedit.c b/libbb/lineedit.c index 4802b46b4..55141e141 100644 --- a/libbb/lineedit.c +++ b/libbb/lineedit.c @@ -1251,6 +1251,8 @@ static NOINLINE void input_tab(smallint *lastWasTab) unsigned match_pfx_len = match_pfx_len; #if ENABLE_PLATFORM_MINGW32 unsigned orig_pfx_len; + char *target; + const char *source; #endif int find_type; # if ENABLE_UNICODE_SUPPORT @@ -1393,13 +1395,21 @@ static NOINLINE void input_tab(smallint *lastWasTab) /* save tail */ strcpy(match_buf, &command_ps[cursor]); /* add match and tail */ -#if ENABLE_PLATFORM_MINGW32 +# if ENABLE_PLATFORM_MINGW32 if (match_pfx_len == orig_pfx_len) { - sprintf(&command_ps[cursor-match_pfx_len], "%s%s", - chosen_match, match_buf); - } else -#endif + /* replace match prefix to allow for altered case */ + target = &command_ps[cursor-match_pfx_len]; + source = chosen_match; + } + else { + /* only replace tail of match if special characters are quoted */ + target = &command_ps[cursor]; + source = chosen_match + match_pfx_len; + } + strcpy(stpcpy(target, source), match_buf); +# else sprintf(&command_ps[cursor], "%s%s", chosen_match + match_pfx_len, match_buf); +# endif command_len = strlen(command_ps); /* new pos */ pos = cursor + len_found - match_pfx_len; diff --git a/shell/ash.c b/shell/ash.c index c400612d9..f8a18cdc5 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -2119,7 +2119,7 @@ stack_add_system_drive(const char *path) const char *sd = need_system_drive(path); char *p = growstackto(strlen(path) + 5 + (sd ? strlen(sd) : 0)); - sprintf(p, "%s%s", sd ?: "", path); + strcpy(stpcpy(p, sd ?: ""), path); return p; } #endif diff --git a/win32/mingw.c b/win32/mingw.c index a6362333d..75635fdf1 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -1729,7 +1729,7 @@ char *alloc_system_drive(const char *path) { const char *sd = need_system_drive(path); char *s = xmalloc(strlen(path) + 5 + (sd ? strlen(sd) : 0)); - sprintf(s, "%s%s", sd ?: "", path); + strcpy(stpcpy(s, sd ?: ""), path); return s; } -- cgit v1.2.3-55-g6feb