Compare commits

...

1 Commits

Author SHA1 Message Date
yangfl
665488298e Show a friendly hint for adb installation
Signed-off-by: Romain Vimont <rom@rom1v.com>
2020-01-18 17:48:42 +01:00
4 changed files with 69 additions and 0 deletions

View File

@ -55,6 +55,28 @@ argv_to_string(const char *const *argv, char *buf, size_t bufsize) {
return idx; return idx;
} }
static void
show_adb_installation_msg() {
static const struct {
const char *binary;
const char *command;
} pkg_managers[] = {
{"apt", "apt install adb"},
{"apt-get", "apt-get install adb"},
{"brew", "brew install android-platform-tools"},
{"dnf", "dnf install android-tools"},
};
for (size_t i = 0; i < ARRAY_LEN(pkg_managers); ++i) {
if (cmd_search(pkg_managers[i].binary)) {
LOGI("You may install 'adb' by \"%s\"", pkg_managers[i].command);
return;
}
}
LOGI("You may download and install 'adb' from "
"https://developer.android.com/studio/releases/platform-tools");
}
static void static void
show_adb_err_msg(enum process_result err, const char *const argv[]) { show_adb_err_msg(enum process_result err, const char *const argv[]) {
char buf[512]; char buf[512];
@ -68,6 +90,7 @@ show_adb_err_msg(enum process_result err, const char *const argv[]) {
LOGE("Command not found: %s", buf); LOGE("Command not found: %s", buf);
LOGE("(make 'adb' accessible from your PATH or define its full" LOGE("(make 'adb' accessible from your PATH or define its full"
"path in the ADB environment variable)"); "path in the ADB environment variable)");
show_adb_installation_msg();
break; break;
case PROCESS_SUCCESS: case PROCESS_SUCCESS:
// do nothing // do nothing

View File

@ -43,6 +43,9 @@ enum process_result {
PROCESS_ERROR_MISSING_BINARY, PROCESS_ERROR_MISSING_BINARY,
}; };
bool
cmd_search(const char *file);
enum process_result enum process_result
cmd_execute(const char *const argv[], process_t *process); cmd_execute(const char *const argv[], process_t *process);

View File

@ -14,6 +14,7 @@
#include <limits.h> #include <limits.h>
#include <signal.h> #include <signal.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/wait.h> #include <sys/wait.h>
@ -21,6 +22,42 @@
#include "util/log.h" #include "util/log.h"
bool
cmd_search(const char *file) {
char *path = getenv("PATH");
if (!path)
return false;
path = strdup(path);
if (!path)
return false;
bool ret = false;
size_t file_len = strlen(file);
char *saveptr;
for (char *dir = strtok_r(path, ":", &saveptr); dir;
dir = strtok_r(NULL, ":", &saveptr)) {
size_t dir_len = strlen(dir);
char *fullpath = malloc(dir_len + file_len + 2);
if (!fullpath)
continue;
memcpy(fullpath, dir, dir_len);
fullpath[dir_len] = '/';
memcpy(fullpath + dir_len + 1, file, file_len + 1);
struct stat sb;
bool fullpath_executable = stat(fullpath, &sb) == 0 &&
sb.st_mode & S_IXUSR;
free(fullpath);
if (fullpath_executable) {
ret = true;
break;
}
}
free(path);
return ret;
}
enum process_result enum process_result
cmd_execute(const char *const argv[], pid_t *pid) { cmd_execute(const char *const argv[], pid_t *pid) {
int fd[2]; int fd[2];

View File

@ -6,6 +6,12 @@
#include "util/log.h" #include "util/log.h"
#include "util/str_util.h" #include "util/str_util.h"
bool
cmd_search(const char *file) {
// :/
return false;
}
static int static int
build_cmd(char *cmd, size_t len, const char *const argv[]) { build_cmd(char *cmd, size_t len, const char *const argv[]) {
// Windows command-line parsing is WTF: // Windows command-line parsing is WTF: