Return NULL on miss in r_file_path ##util (#21461)

* Lots of R2_590 guards
This commit is contained in:
Lazula 2023-03-10 14:56:42 -06:00 committed by GitHub
parent 619de0ffa0
commit 7528553d16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 285 additions and 96 deletions

View File

@ -104,7 +104,12 @@ static bool r2r_chdir(const char *argv0) {
return false;
}
char *r2r_path = r_file_path (argv0);
#if R2_590
if (!r2r_path) {
#else
if (!strcmp (r2r_path, argv0)) {
free (r2r_path);
#endif
free (src_path);
return false;
}
@ -135,9 +140,17 @@ static bool r2r_chdir(const char *argv0) {
static bool r2r_test_run_unit(void) {
char *make = r_file_path ("gmake");
#if R2_590
if (!make) {
#else
if (!strcmp (make, "gmake")) {
#endif
make = r_file_path ("make");
#if R2_590
if (!make) {
#else
if (!strcmp (make, "make")) {
#endif
eprintf ("Cannot find `make` in PATH\n");
return false;
}

View File

@ -1247,7 +1247,13 @@ static bool require_check(const char *require) {
}
bool res = true;
if (strstr (require, "gas")) {
res &= r_file_exists ("/usr/bin/as");
char *as_bin = r_file_path ("as");
#if R2_590
res &= (bool)as_bin;
#else
res &= (bool)strcmp (as_bin, "as");
#endif
free (as_bin);
}
if (strstr (require, "unix")) {
#if R2__UNIX__

View File

@ -131,16 +131,22 @@ static char *swift_demangle_cmd(const char *s) {
if (!swift_demangle) {
have_swift_demangle = 0;
swift_demangle = r_file_path ("swift-demangle");
if (!swift_demangle || !strcmp (swift_demangle, "swift-demangle")) {
#if R2_590
if (!swift_demangle) {
#else
if (!strcmp (swift_demangle, "swift-demangle")) {
#endif
char *xcrun = r_file_path ("xcrun");
#if R2_590
if (xcrun) {
if (strcmp (xcrun, "xcrun")) {
free (swift_demangle);
swift_demangle = r_str_newf ("%s swift-demangle", xcrun);
have_swift_demangle = 1;
}
free (xcrun);
#else
if (strcmp (xcrun, "xcrun")) {
#endif
free (swift_demangle);
swift_demangle = r_str_newf ("%s swift-demangle", xcrun);
have_swift_demangle = 1;
}
free (xcrun);
}
}
}

View File

@ -3359,18 +3359,22 @@ static bool cb_dbg_verbose(void *user, void *data) {
static bool cb_prjvctype(void *user, void *data) {
RConfigNode *node = data;
char *p = r_file_path ("git");
bool found = (p && (*p == 'g' ||*p == '/'));
free (p);
char *git = r_file_path ("git");
#if R2_590
bool have_git = (bool)git;
#else
bool have_git = strcmp (git, "git");
#endif
free (git);
if (*node->value == '?') {
if (found) {
if (have_git) {
r_cons_println ("git");
}
r_cons_println ("rvc");
return true;
}
if (!strcmp (node->value, "git")) {
if (found) {
if (have_git) {
return true;
}
return false;
@ -4044,21 +4048,57 @@ R_API int r_core_config_init(RCore *core) {
#if R2__WINDOWS__
r_config_set (cfg, "http.browser", "start");
#else
if (r_file_exists ("/usr/bin/openURL")) { // iOS ericautils
r_config_set (cfg, "http.browser", "/usr/bin/openURL");
} else if (r_file_exists (TERMUX_PREFIX "/bin/termux-open")) {
r_config_set (cfg, "http.browser", TERMUX_PREFIX "/bin/termux-open");
} else if (r_file_exists ("/system/bin/toolbox")) {
r_config_set (cfg, "http.browser",
"LD_LIBRARY_PATH=/system/lib am start -a android.intent.action.VIEW -d");
} else if (r_file_exists ("/usr/bin/xdg-open")) {
r_config_set (cfg, "http.browser", "xdg-open");
} else if (r_file_exists ("/usr/bin/open")) {
r_config_set (cfg, "http.browser", "open");
} else {
r_config_set (cfg, "http.browser", "firefox");
{
/* bin_name, standard_path, http.browser value override */
static const char *bin_data[] = {
"openURL", "/usr/bin/openURL", "", // iOS ericautils
"termux-open", TERMUX_PREFIX "/bin/termux-open", "",
"toolbox", "/system/bin/toolbox", "LD_LIBRARY_PATH=/system/lib am start -a android.intent.action.VIEW -d",
"xdg-open", "/usr/bin/xdg-open", "",
"open", "/usr/bin/open", "",
NULL
};
int i;
bool fallback = true;
/* Attempt to find binary in path before falling back to
* standard locations */
for (i = 0; bin_data[i]; i += 3) {
const char *bin_name = bin_data[i];
const char *standard_path = bin_data[i+1];
const char *browser_override = bin_data[i+2];
const char *path;
/* Try to find bin in path */
char *bin_path = r_file_path (bin_name);
path = bin_path;
#if !R2_590
/* Not in path, old API returns strdup (arg) */
if (!strcmp (bin_name, bin_path)) {
R_FREE (bin_path);
path = NULL;
}
#endif
/* Not in path, but expected location exists */
if (!path && r_file_exists (standard_path)) {
path = standard_path;
}
if (path) {
r_config_set (cfg, "http.browser", r_str_get_fail (browser_override, path));
fallback = false;
}
free (bin_path);
}
if (fallback) {
r_config_set (cfg, "http.browser", "firefox");
}
r_config_desc (cfg, "http.browser", "command to open HTTP URLs");
}
r_config_desc (cfg, "http.browser", "command to open HTTP URLs");
#endif
SETI ("http.maxsize", 0, "maximum file size for upload");
SETPREF ("http.index", "index.html", "main html file to check in directory");

View File

@ -1466,23 +1466,47 @@ R_API bool r_core_run_script(RCore *core, const char *file) {
ret = lang_run_file (core, core->lang, cmd);
free (cmd);
} else if (!strcmp (ext, "py")) {
char *fp = r_file_path ("python3");
if (!fp) {
fp = r_file_path ("python2");
if (!fp) {
fp = r_file_path ("python");
}
}
if (fp) {
#if R2__WINDOWS__
char *cmd = r_str_newf ("%s %s", fp, file);
static const char *python_bins[] = {
"python3",
"python2",
"python",
NULL
};
const char *bin;
char *bin_path;
int i;
#if !R2_590
bool found = false;
#endif
for (i = 0; python_bins[i]; i++) {
bin = python_bins[i];
bin_path = r_file_path (bin);
#if R2_590
if (bin_path) {
#else
char *cmd = r_str_newf ("%s '%s'", fp, file);
if (strcmp (bin_path, bin)) {
found = true;
#endif
break;
}
free (bin_path);
}
#if R2_590
if (bin_path) {
#else
if (found) {
#endif
#if R2__WINDOWS__
char *cmd = r_str_newf ("%s %s", bin_path, file);
#else
char *cmd = r_str_newf ("%s '%s'", bin_path, file);
#endif
r_lang_use (core->lang, "pipe");
ret = lang_run_file (core, core->lang, cmd);
free (cmd);
free (fp);
free (bin_path);
} else {
R_LOG_ERROR ("Cannot find python in PATH");
ret = false;
@ -1496,7 +1520,6 @@ R_API bool r_core_run_script(RCore *core, const char *file) {
}
}
} else {
char *abspath = r_file_path (file);
char *lang = langFromHashbang (core, file);
if (lang) {
r_lang_use (core->lang, "pipe");
@ -1511,7 +1534,6 @@ R_API bool r_core_run_script(RCore *core, const char *file) {
ret = 1;
}
}
free (abspath);
}
if (!ret) {
ret = r_core_cmd_file (core, file);

View File

@ -10015,7 +10015,11 @@ static char *getViewerPath(void) {
};
for (i = 0; viewers[i]; i++) {
char *viewerPath = r_file_path (viewers[i]);
if (viewerPath && strcmp (viewerPath, viewers[i])) {
#if R2_590
if (viewerPath) {
#else
if (strcmp (viewerPath, viewers[i])) {
#endif
return viewerPath;
}
free (viewerPath);
@ -10026,14 +10030,13 @@ static char *getViewerPath(void) {
static char *dot_executable_path(void) {
const char *dot = "dot";
char *dotPath = r_file_path (dot);
#if R2_590
if (!dotPath) {
#else
if (!strcmp (dotPath, dot)) {
free (dotPath);
dot = "xdot";
dotPath = r_file_path (dot);
if (!strcmp (dotPath, dot)) {
free (dotPath);
return NULL;
}
#endif
dotPath = r_file_path ("xdot");
}
return dotPath;
}

View File

@ -961,17 +961,23 @@ static int cmd_w6(void *data, const char *input) {
}
static int cmd_wh(void *data, const char *input) {
char *p = strchr (input, ' ');
if (p) {
while (*p == ' ')
p++;
p = r_file_path (p);
if (p) {
r_cons_println (p);
free (p);
const char *arg = r_str_trim_head_ro (strchr (input, ' '));
if (arg) {
char *path = r_file_path (arg);
#if R2_590
if (path) {
#else
if (strcmp (path, arg)) {
#endif
r_cons_println (path);
free (path);
return 0;
}
#if !R2_590
free (path);
#endif
}
return 0;
return 1;
}
static int cmd_we(void *data, const char *input) {

View File

@ -16,22 +16,28 @@ struct cEnv_t {
const char *TEXT;
};
static char* r_egg_cfile_getCompiler(void) {
size_t i;
const char *compilers[] = { "llvm-gcc", "clang", "gcc" };
char *output = r_sys_getenv ("CC");
static char *r_egg_cfile_getCompiler(void) {
const char *compilers[] = { "llvm-gcc", "clang", "gcc", NULL };
const char *compiler = compilers[0];
char *env_cc = r_sys_getenv ("CC");
char *compiler_path;
int i;
if (output) {
return output;
if (env_cc) {
return env_cc;
}
for (i = 0; i < 3; i++) {
output = r_file_path (compilers[i]);
if (strcmp (output, compilers[i])) {
free (output);
return strdup (compilers[i]);
for (i = 0; (compiler = compilers[i]); i++) {
compiler_path = r_file_path (compiler);
#if R2_590
if (compiler_path) {
#else
if (strcmp (compiler_path, compiler)) {
#endif
free (compiler_path);
return strdup (compiler);
}
free (output);
free (compiler_path);
}
R_LOG_ERROR ("Couldn't find a compiler! Please set CC");

View File

@ -80,16 +80,24 @@ static int git_pull(const char *dir, bool reset) {
}
static int git_clone(const char *dir, const char *url) {
char *git;
if (strchr (dir, ' ')) {
R_LOG_ERROR ("Directory '%s' cannot contain spaces", dir);
return -1;
}
char *git = r_file_path ("git");
if (!git || !strcmp (git, "git")) {
git = r_file_path ("git");
#if R2_590
if (!git) {
#else
if (!strcmp (git, "git")) {
free (git);
#endif
R_LOG_ERROR ("Cannot find `git` in $PATH");
return 1;
}
free (git);
char *cmd = r_str_newf ("git clone --depth=10 --recursive %s %s", url, dir);
R_LOG_INFO ("%s", cmd);
int rc = r_sandbox_system (cmd, 1);
@ -325,12 +333,17 @@ static int r2pm_update(bool force) {
static void r2pm_setenv(void) {
char *gmake = r_file_path ("gmake");
if (gmake && *gmake == '/') {
#if R2_590
if (gmake) {
#else
if (strcmp (gmake, "gmake")) {
#endif
r_sys_setenv ("MAKE", gmake);
} else {
r_sys_setenv ("MAKE", "make");
}
free (gmake);
char *r2_plugdir = r_xdg_datadir ("plugins");
r_sys_setenv ("R2PM_PLUGDIR", r2_plugdir);
free (r2_plugdir);
@ -402,20 +415,48 @@ static void r2pm_setenv(void) {
}
// GLOBAL = 0 # depends on r2pm.global, which is set on r2pm_install
char *python = r_sys_getenv ("PYTHON");
if (!python) {
python = r_file_path ("python3");
if (!python) {
python = r_file_path ("python");
if (!python) {
python = r_file_path ("python2");
}
}
if (python) {
r_sys_setenv ("PYTHON", python);
}
static const char *python_bins[] = {
"python3",
"python2",
"python",
NULL
};
const char *bin = python_bins[0];
char *bin_path;
int i;
char *env_python = r_sys_getenv ("PYTHON");
#if !R2_590
bool found;
#endif
if (R_STR_ISNOTEMPTY (env_python)) {
free (env_python);
return;
}
free (python);
for (i = 0; python_bins[i]; i++) {
bin = python_bins[i];
bin_path = r_file_path (bin);
#if R2_590
if (bin_path) {
#else
if (strcmp (bin_path, bin)) {
found = true;
#endif
break;
}
free (bin_path);
}
#if R2_590
if (bin_path) {
#else
if (found) {
#endif
r_sys_setenv ("PYTHON", bin_path);
}
free (bin_path);
}
@ -512,18 +553,32 @@ static bool download(const char *url, const char *outfile) {
char *tool = r_file_path ("curl");
int res = 1;
R_LOG_INFO ("download: %s into %s", url, outfile);
if (tool && strcmp (tool, "curl")) {
#if R2_590
if (tool) {
#else
if (strcmp (tool, "curl")) {
#endif
res = r_sys_cmdf ("%s -sfL -o '%s' '%s'", tool, outfile, url);
free (tool);
return res == 0;
}
#if !R2_590
free (tool);
#endif
tool = r_file_path ("wget");
if (tool && strcmp (tool, "wget")) {
#if R2_590
if (tool) {
#else
if (strcmp (tool, "wget")) {
#endif
res = r_sys_cmdf ("%s -qO '%s' '%s'", tool, outfile, url);
free (tool);
return res == 0;
}
#if !R2_590
free (tool);
#endif
R_LOG_ERROR ("Please install `curl` or `wget`");
return false;
}
@ -589,7 +644,11 @@ static int r2pm_clone(const char *pkg) {
static bool r2pm_check(const char *program) {
char *s = r_file_path (program);
bool found = s && strcmp (s, program);
#if R2_590
bool found = (bool)s;
#else
bool found = strcmp (s, program);
#endif
free (s);
return found;
}

View File

@ -1341,14 +1341,17 @@ R_API bool r_run_start(RRunProfile *p) {
if (p->_program) {
if (!r_file_exists (p->_program)) {
char *progpath = r_file_path (p->_program);
if (progpath && *progpath) {
free (p->_program);
p->_program = progpath;
} else {
#if R2_590
if (!progpath) {
#else
if (!strcmp (progpath, p->_program)) {
free (progpath);
#endif
R_LOG_ERROR ("file not found: %s", p->_program);
return false;
}
free (p->_program);
p->_program = progpath;
}
#if R2__UNIX__
// XXX HACK close all non-tty fds

View File

@ -293,25 +293,35 @@ R_API char *r_file_binsh(void) {
if (R_STR_ISEMPTY (bin_sh)) {
free (bin_sh);
bin_sh = r_file_path ("sh");
if (!bin_sh || *bin_sh != '/') {
#if R2_590
if (!bin_sh) {
#else
if (!strcmp (bin_sh, "sh")) {
free (bin_sh);
#endif
bin_sh = strdup (SHELL_PATH);
}
}
return bin_sh;
}
#if R2_590
// Returns bin location in PATH, NULL if not found
#else
// Returns bin location in PATH, strdup(bin) if not found
#endif
R_API char *r_file_path(const char *bin) {
r_return_val_if_fail (bin, NULL);
char *file = NULL;
char *path = NULL;
char *str, *ptr;
const char *extension = "";
if (!strncmp (bin, "./", 2)) {
if (r_str_startswith (bin, "./")) {
return r_file_exists (bin)
? r_file_abspath (bin): NULL;
? r_file_abspath (bin)
: NULL;
}
char *path_env = (char *)r_sys_getenv ("PATH");
char *path_env = r_sys_getenv ("PATH");
#if R2__WINDOWS__
if (!r_str_endswith (bin, ".exe")) {
extension = ".exe";
@ -338,7 +348,11 @@ R_API char *r_file_path(const char *bin) {
}
free (path_env);
free (path);
#if R2_590
return NULL;
#else
return strdup (bin);
#endif
}
R_API char *r_stdin_slurp(int *sz) {

View File

@ -271,7 +271,11 @@ R_API int r_sandbox_system(const char *x, int n) {
argv = r_str_argv (cmd, &argc);
if (argv) {
char *argv0 = r_file_path (argv[0]);
#if R2_590
if (!argv0) {
#else
if (!strcmp (argv0, argv[0])) {
#endif
R_LOG_ERROR ("Cannot find '%s'", argv[0]);
return -1;
}

View File

@ -1335,13 +1335,20 @@ R_API bool r_sys_tts(const char *txt, bool bg) {
};
for (i = 0; says[i]; i++) {
char *sayPath = r_file_path (says[i]);
#if R2_590
if (sayPath) {
#else
if (strcmp (sayPath, says[i])) {
#endif
char *line = r_str_replace (strdup (txt), "'", "\"", 1);
r_sys_cmdf ("\"%s\" '%s'%s", sayPath, line, bg? " &": "");
free (line);
free (sayPath);
return true;
}
#if !R2_590
free (sayPath);
#endif
}
return false;
}