diff --git a/legacy/ecore/src/lib/ecore_file/Ecore_File.h b/legacy/ecore/src/lib/ecore_file/Ecore_File.h index 283d2f126e..8a13408a89 100644 --- a/legacy/ecore/src/lib/ecore_file/Ecore_File.h +++ b/legacy/ecore/src/lib/ecore_file/Ecore_File.h @@ -76,7 +76,8 @@ extern "C" { EAPI void ecore_file_monitor_del(Ecore_File_Monitor *ecore_file_monitor); EAPI const char *ecore_file_monitor_path_get(Ecore_File_Monitor *ecore_file_monitor); - EAPI int ecore_file_app_installed(const char *app); + EAPI int ecore_file_app_installed(const char *exe); + EAPI char *ecore_file_app_exe_get(const char *app); EAPI int ecore_file_download(const char *url, const char *dst, void (*completion_cb)(void *data, diff --git a/legacy/ecore/src/lib/ecore_file/ecore_file_path.c b/legacy/ecore/src/lib/ecore_file/ecore_file_path.c index e03bbd7279..12a201271b 100644 --- a/legacy/ecore/src/lib/ecore_file/ecore_file_path.c +++ b/legacy/ecore/src/lib/ecore_file/ecore_file_path.c @@ -47,60 +47,131 @@ _ecore_file_path_from_env(const char *env) if (!*p) { ecore_list_append(path, strdup(last)); - last = p+1; + last = p + 1; } - } if (p > last) - ecore_list_append(path, last); + ecore_list_append(path, strdup(last)); free(env_path); return path; } int -ecore_file_app_installed(const char *app) +ecore_file_app_installed(const char *exe) { char *dir; char buf[PATH_MAX]; - char *file, *p; - if (!app) return 0; - if (ecore_file_exists(app) && ecore_file_can_exec(app)) return 1; + if (!exe) return 0; + if (ecore_file_can_exec(exe)) return 1; ecore_list_goto_first(__ecore_file_path_bin); while ((dir = ecore_list_next(__ecore_file_path_bin)) != NULL) { - snprintf(buf, sizeof(buf), "%s/%s", dir, app); - if (ecore_file_exists(buf) && ecore_file_can_exec(buf)) + snprintf(buf, sizeof(buf), "%s/%s", dir, exe); + if (ecore_file_can_exec(buf)) return 1; } - - /* Maybe the app has arguments */ - file = strdup(app); - if (!file) return 0; - while ((*file) && isspace(*file)) - file++; - - p = file; - while ((*p) && !isspace(*p)) - p++; - if ((*file) && (*p)) - { - *p = '\0'; - if (ecore_file_exists(file) && ecore_file_can_exec(file)) return 1; - ecore_list_goto_first(__ecore_file_path_bin); - while ((dir = ecore_list_next(__ecore_file_path_bin)) != NULL) - { - snprintf(buf, sizeof(buf), "%s/%s", dir, file); - if (ecore_file_exists(buf) && ecore_file_can_exec(buf)) - { - free(file); - return 1; - } - } - } - free(file); - return 0; } + +char * +ecore_file_app_exe_get(const char *app) +{ + char *p, *pp, *exe1 = NULL, *exe2 = NULL; + char *exe; + int in_quot_dbl = 0, in_quot_sing = 0; + + p = (char *)app; + while ((*p) && (isspace(*p))) p++; + exe1 = p; + while (*p) + { + if (in_quot_sing) + { + if (*p == '\'') + in_quot_sing = 0; + } + else if (in_quot_dbl) + { + if (*p == '\"') + in_quot_dbl = 0; + } + else + { + if (*p == '\'') + in_quot_sing = 1; + else if (*p == '\"') + in_quot_dbl = 1; + if ((isspace(*p)) && (!((p > app) && (p[-1] != '\\')))) + break; + } + p++; + } + exe2 = p; + if (exe2 == exe1) return NULL; + exe = malloc(exe2 - exe1 + 1); + if (!exe) return NULL; + p = exe1; + in_quot_dbl = 0; + in_quot_sing = 0; + pp = exe; + while (*p) + { + if (in_quot_sing) + { + if (*p == '\'') + in_quot_sing = 0; + else + { + *pp = *p; + pp++; + } + } + else if (in_quot_dbl) + { + if (*p == '\"') + in_quot_dbl = 0; + else + { + /* techcincally this is wrong. double quotes also accept + * special chars: + * + * $, `, \ + */ + *pp = *p; + pp++; + } + } + else + { + /* technically we should handle special chars: + * + * $, `, \, etc. + */ + if ((p > app) && (p[-1] == '\\')) + { + if (*p != '\n') + { + *pp = *p; + pp++; + } + } + else if (*p == '\'') + in_quot_sing = 1; + else if (*p == '\"') + in_quot_dbl = 1; + else if (isspace(*p)) + break; + else + { + *pp = *p; + pp++; + } + } + p++; + } + *pp = 0; + return exe; +}