From 239bba0059e5c8284c7f21e2b65b0467b20c6bd7 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Fri, 5 Oct 2012 11:18:05 +0100 Subject: Revised quoting of command line arguments for WIN32 * We only need to quote an argument if it contains whitespace. * There's no point in quoting ? or * because Windows still expands them. Anyhow, if the command line comes from a BusyBox shell it will usually have handled the globbing, so we should turn off Windows globbing for any wildcard that has made it through the shell without expansion. This means that BusyBox commands run from cmd.exe won't do wildcard expansion, though. * Backslashes only need to be doubled if they occur immediately before double quotes. * Double quotes need to be escaped with a backslash. --- applets/applets.c | 5 +++++ libbb/appletlib.c | 5 +++++ win32/process.c | 14 +++++++------- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/applets/applets.c b/applets/applets.c index 98c2b44f5..7ffa7a896 100644 --- a/applets/applets.c +++ b/applets/applets.c @@ -9,6 +9,11 @@ #include "busybox.h" #if ENABLE_BUILD_LIBBUSYBOX +#if ENABLE_PLATFORM_MINGW32 +/* disable MSVCRT command line globbing */ +int _CRT_glob = 0; +#endif + int main(int argc UNUSED_PARAM, char **argv) { return lbb_main(argv); diff --git a/libbb/appletlib.c b/libbb/appletlib.c index 0c0d14499..9f3584755 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c @@ -773,6 +773,11 @@ void FAST_FUNC run_applet_and_exit(const char *name, char **argv) #if ENABLE_BUILD_LIBBUSYBOX int lbb_main(char **argv) #else +#if ENABLE_PLATFORM_MINGW32 +/* disable MSVCRT command line globbing */ +int _CRT_glob = 0; +#endif + int main(int argc UNUSED_PARAM, char **argv) #endif { diff --git a/win32/process.c b/win32/process.c index 53ad44a42..59bc5b4a0 100644 --- a/win32/process.c +++ b/win32/process.c @@ -117,7 +117,7 @@ quote_arg(const char *arg) const char *p = arg; if (!*p) force_quotes = 1; while (*p) { - if (isspace(*p) || *p == '*' || *p == '?' || *p == '{' || *p == '\'') + if (isspace(*p)) force_quotes = 1; else if (*p == '"') n++; @@ -130,8 +130,6 @@ quote_arg(const char *arg) } if (*p == '"') n += count*2 + 1; - else - n += count; continue; } len++; @@ -142,7 +140,8 @@ quote_arg(const char *arg) /* insert \ where necessary */ d = q = xmalloc(len+n+3); - *d++ = '"'; + if (force_quotes) + *d++ = '"'; while (*arg) { if (*arg == '"') *d++ = '\\'; @@ -152,15 +151,16 @@ quote_arg(const char *arg) count++; *d++ = *arg++; } - while (count-- > 0) - *d++ = '\\'; if (*arg == '"') { + while (count-- > 0) + *d++ = '\\'; *d++ = '\\'; } } *d++ = *arg++; } - *d++ = '"'; + if (force_quotes) + *d++ = '"'; *d++ = 0; return q; } -- cgit v1.2.3-55-g6feb