diff options
author | Ron Yorston <rmy@pobox.com> | 2018-12-06 15:16:49 +0000 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2018-12-06 15:16:49 +0000 |
commit | 9906faf2dff6fd9033cb711619528501cae11721 (patch) | |
tree | a85bb3ee78f5d58378f23842e0e762e879db23bc /win32/mingw.c | |
parent | d257c7f2b6c44662c793d00d0b9286d6246b84a3 (diff) | |
download | busybox-w32-9906faf2dff6fd9033cb711619528501cae11721.tar.gz busybox-w32-9906faf2dff6fd9033cb711619528501cae11721.tar.bz2 busybox-w32-9906faf2dff6fd9033cb711619528501cae11721.zip |
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().
Diffstat (limited to 'win32/mingw.c')
-rw-r--r-- | win32/mingw.c | 52 |
1 files changed, 32 insertions, 20 deletions
diff --git a/win32/mingw.c b/win32/mingw.c index 707e6a3d2..08d955527 100644 --- a/win32/mingw.c +++ b/win32/mingw.c | |||
@@ -1160,7 +1160,7 @@ int mingw_rmdir(const char *path) | |||
1160 | return rmdir(path); | 1160 | return rmdir(path); |
1161 | } | 1161 | } |
1162 | 1162 | ||
1163 | const char win_suffix[4][4] = { "com", "exe", "bat", "cmd" }; | 1163 | static const char win_suffix[4][4] = { "com", "exe", "bat", "cmd" }; |
1164 | 1164 | ||
1165 | static int has_win_suffix(const char *name, int start) | 1165 | static int has_win_suffix(const char *name, int start) |
1166 | { | 1166 | { |
@@ -1191,33 +1191,45 @@ int has_exe_suffix_or_dot(const char *name) | |||
1191 | return last_char_is(name, '.') || has_win_suffix(name, 0); | 1191 | return last_char_is(name, '.') || has_win_suffix(name, 0); |
1192 | } | 1192 | } |
1193 | 1193 | ||
1194 | /* check if path can be made into an executable by adding a suffix; | 1194 | /* Check if path can be made into an executable by adding a suffix. |
1195 | * return an allocated string containing the path if it can; | 1195 | * The suffix is added to the end of the argument which must be |
1196 | * return NULL if not. | 1196 | * long enough to allow this. |
1197 | * | 1197 | * |
1198 | * if path already has a suffix don't even bother trying | 1198 | * If the return value is TRUE the argument contains the new path, |
1199 | * if FALSE the argument is unchanged. | ||
1199 | */ | 1200 | */ |
1200 | char *add_win32_extension(const char *p) | 1201 | int add_win32_extension(char *p) |
1201 | { | 1202 | { |
1202 | char *path; | 1203 | if (!has_exe_suffix_or_dot(p)) { |
1203 | int i, len; | 1204 | int i, len = strlen(p); |
1204 | 1205 | ||
1205 | if (has_exe_suffix_or_dot(p)) { | 1206 | p[len] = '.'; |
1206 | return NULL; | 1207 | for (i=0; i<4; ++i) { |
1208 | memcpy(p+len+1, win_suffix[i], 4); | ||
1209 | if (file_is_executable(p)) | ||
1210 | return TRUE; | ||
1211 | } | ||
1212 | p[len] = '\0'; | ||
1207 | } | 1213 | } |
1214 | return FALSE; | ||
1215 | } | ||
1208 | 1216 | ||
1209 | len = strlen(p); | 1217 | /* Check if path can be made into an executable by adding a suffix. |
1210 | path = xasprintf("%s.com", p); | 1218 | * Return an allocated string containing the path if it can; |
1219 | * return NULL if not. | ||
1220 | * | ||
1221 | * If path already has a suffix don't even bother trying. | ||
1222 | */ | ||
1223 | char *alloc_win32_extension(const char *p) | ||
1224 | { | ||
1225 | if (!has_exe_suffix_or_dot(p)) { | ||
1226 | int len = strlen(p); | ||
1227 | char *path = strcpy(xmalloc(len+5), p); | ||
1211 | 1228 | ||
1212 | for (i=0; i<4; ++i) { | 1229 | if (add_win32_extension(path)) |
1213 | memcpy(path+len+1, win_suffix[i], 4); | ||
1214 | if (file_is_executable(path)) { | ||
1215 | return path; | 1230 | return path; |
1216 | } | 1231 | free(path); |
1217 | } | 1232 | } |
1218 | |||
1219 | free(path); | ||
1220 | |||
1221 | return NULL; | 1233 | return NULL; |
1222 | } | 1234 | } |
1223 | 1235 | ||