Skip to content

Commit

Permalink
Show a friendly hint for adb installation
Browse files Browse the repository at this point in the history
Signed-off-by: Romain Vimont <[email protected]>
  • Loading branch information
yangfl authored and rom1v committed Feb 27, 2020
1 parent 1982bc4 commit d3281f4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
27 changes: 27 additions & 0 deletions app/src/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,32 @@ argv_to_string(const char *const *argv, char *buf, size_t bufsize) {
return idx;
}

static void
show_adb_installation_msg() {
#ifndef __WINDOWS__
static const struct {
const char *binary;
const char *command;
} pkg_managers[] = {
{"apt", "apt install adb"},
{"apt-get", "apt-get install adb"},
{"brew", "brew cask install android-platform-tools"},
{"dnf", "dnf install android-tools"},
{"emerge", "emerge dev-util/android-tools"},
{"pacman", "pacman -S 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;
}
}
#endif

LOGI("You may download and install 'adb' from "
"https://developer.android.com/studio/releases/platform-tools");
}

static void
show_adb_err_msg(enum process_result err, const char *const argv[]) {
char buf[512];
Expand All @@ -68,6 +94,7 @@ show_adb_err_msg(enum process_result err, const char *const argv[]) {
LOGE("Command not found: %s", buf);
LOGE("(make 'adb' accessible from your PATH or define its full"
"path in the ADB environment variable)");
show_adb_installation_msg();
break;
case PROCESS_SUCCESS:
// do nothing
Expand Down
5 changes: 5 additions & 0 deletions app/src/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ enum process_result {
PROCESS_ERROR_MISSING_BINARY,
};

#ifndef __WINDOWS__
bool
cmd_search(const char *file);
#endif

enum process_result
cmd_execute(const char *const argv[], process_t *process);

Expand Down
37 changes: 37 additions & 0 deletions app/src/sys/unix/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,50 @@
#include <limits.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.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
cmd_execute(const char *const argv[], pid_t *pid) {
int fd[2];
Expand Down

0 comments on commit d3281f4

Please sign in to comment.