From 9906faf2dff6fd9033cb711619528501cae11721 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Thu, 6 Dec 2018 15:16:49 +0000 Subject: win32: rework adding of extensions to filenames Previously there was one function to handle adding extensions to executable filenames, add_win32_extension(). Refactor this into three functions: add_win32_extension() appends the suffix to the argument string in-place. The argument must be long enough to cope with this, as is the case in ash where path_advance() adds 4 bytes to each filename for just this reason. alloc_win32_extension() is equivalent to the old add_win32_extension(). It allocates a string to hold the new filename then calls the new add_win32_extension() function. The caller is responsible for managing the returned string. auto_win32_extension() calls alloc_win32_extension() and saves the resulting string using auto_string(). It's used where the new filename is consumed immediately or the actual value isn't needed. Rewrite code to use the most appropriate function. Also reorder some code in find_executable() and find_command(). --- libbb/executable.c | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) (limited to 'libbb') diff --git a/libbb/executable.c b/libbb/executable.c index aec829945..835341ed9 100644 --- a/libbb/executable.c +++ b/libbb/executable.c @@ -28,10 +28,6 @@ int FAST_FUNC file_is_executable(const char *name) * in all cases (*PATHp) contents are temporarily modified * but are restored on return (s/:/NUL/ and back). */ -#if !ENABLE_PLATFORM_MINGW32 -#define next_path_sep(s) strchr(s, ':') -#endif - char* FAST_FUNC find_executable(const char *filename, char **PATHp) { /* About empty components in $PATH: @@ -44,38 +40,32 @@ char* FAST_FUNC find_executable(const char *filename, char **PATHp) */ char *p, *n; #if ENABLE_PLATFORM_MINGW32 - char *w; + char sep, *w; #endif p = *PATHp; while (p) { int ex; -#if ENABLE_PLATFORM_MINGW32 - char sep; - n = (char*)next_path_sep(p); - if (n) { - sep = *n; - *n = '\0'; - } -#else +#if !ENABLE_PLATFORM_MINGW32 n = strchr(p, ':'); if (n) *n = '\0'; +#else + n = (char*)next_path_sep(p); + if (n) { sep = *n; *n = '\0'; } #endif p = concat_path_file( p[0] ? p : ".", /* handle "::" case */ filename ); -#if ENABLE_PLATFORM_MINGW32 - if (n) *n++ = sep; -#else +#if !ENABLE_PLATFORM_MINGW32 if (n) *n++ = ':'; -#endif -#if ENABLE_PLATFORM_MINGW32 - if ((w=add_win32_extension(p))) { - *PATHp = n; +#else + if (n) *n++ = sep; + if ((w=alloc_win32_extension(p))) { free(p); - return w; + p = w; + /* following test will succeed */ } #endif ex = file_is_executable(p); -- cgit v1.2.3-55-g6feb