diff --git a/app/src/command.c b/app/src/command.c index e6e63ca7..b5bb9572 100644 --- a/app/src/command.c +++ b/app/src/command.c @@ -6,6 +6,7 @@ #include "common.h" #include "log.h" +#include "str_util.h" static const char *adb_command; @@ -89,23 +90,49 @@ process_t adb_reverse_remove(const char *serial, const char *device_socket_name) } process_t adb_push(const char *serial, const char *local, const char *remote) { +#ifdef __WINDOWS__ + // Windows will parse the string, so the paths must be quoted + // (see sys/win/command.c) + local = strquote(local); + if (!local) { + return PROCESS_NONE; + } + remote = strquote(remote); + if (!remote) { + free((void *) local); + return PROCESS_NONE; + } +#endif + const char *const adb_cmd[] = {"push", local, remote}; - return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd)); + process_t proc = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd)); + +#ifdef __WINDOWS__ + free((void *) remote); + free((void *) local); +#endif + + return proc; } process_t adb_install(const char *serial, const char *local) { #ifdef __WINDOWS__ - // Windows will parse the string, so the local name must be quoted (see sys/win/command.c) - size_t len = strlen(local); - char quoted[len + 3]; - memcpy("ed[1], local, len); - quoted[0] = '"'; - quoted[len + 1] = '"'; - quoted[len + 2] = '\0'; - local = quoted; + // Windows will parse the string, so the local name must be quoted + // (see sys/win/command.c) + local = strquote(local); + if (!local) { + return PROCESS_NONE; + } #endif + const char *const adb_cmd[] = {"install", "-r", local}; - return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd)); + process_t proc = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd)); + +#ifdef __WINDOWS__ + free((void *) local); +#endif + + return proc; } process_t adb_remove_path(const char *serial, const char *path) { diff --git a/app/src/str_util.c b/app/src/str_util.c index 0e090403..f234346e 100644 --- a/app/src/str_util.c +++ b/app/src/str_util.c @@ -1,5 +1,8 @@ #include "str_util.h" +#include +#include + size_t xstrncpy(char *dest, const char *src, size_t n) { size_t i; for (i = 0; i < n - 1 && src[i] != '\0'; ++i) @@ -31,3 +34,16 @@ truncated: dst[n - 1] = '\0'; return n; } + +char *strquote(const char *src) { + size_t len = strlen(src); + char *quoted = malloc(len + 3); + if (!quoted) { + return NULL; + } + memcpy("ed[1], src, len); + quoted[0] = '"'; + quoted[len + 1] = '"'; + quoted[len + 2] = '\0'; + return quoted; +} diff --git a/app/src/str_util.h b/app/src/str_util.h index 1bf8088e..9433f768 100644 --- a/app/src/str_util.h +++ b/app/src/str_util.h @@ -16,4 +16,8 @@ size_t xstrncpy(char *dest, const char *src, size_t n); // occurred, or n if truncated size_t xstrjoin(char *dst, const char *const tokens[], char sep, size_t n); +// quote a string +// returns the new allocated string, to be freed by the caller +char *strquote(const char *src); + #endif