From 00ec4368b5a46df6fcf8101202acc7996c07a434 Mon Sep 17 00:00:00 2001 From: Pierre Pronchery Date: Tue, 16 Jun 2015 02:09:28 -0400 Subject: [PATCH] Correct support for "TryExec" Look for every directory from $PATH as it should (for relative pathnames). --- tools/settings.c | 72 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 60 insertions(+), 12 deletions(-) diff --git a/tools/settings.c b/tools/settings.c index ea04f69..72b3fd7 100644 --- a/tools/settings.c +++ b/tools/settings.c @@ -155,6 +155,9 @@ static void _settings_on_item_activated(GtkWidget * widget, GtkTreePath * path, /* settings_browse */ static int _settings_browse_folder(Settings * settings, Config * config, char const * folder); +static int _settings_browse_folder_access(char const * filename, int mode); +static int _settings_browse_folder_access_path(char const * path, + char const * filename, int mode); static void _settings_rtrim(String * string, char c); static int _settings_browse(Settings * settings) @@ -275,19 +278,10 @@ static int _settings_browse_folder(Settings * settings, Config * config, if((p = config_get(config, section, "Categories")) == NULL || string_find(p, "Settings") == NULL) continue; - if((p = config_get(config, section, "TryExec")) != NULL) - { - if((path = string_new_append(BINDIR "/", p, NULL)) - == NULL) - { - _settings_error(error_get(), 1); + if((p = config_get(config, section, "TryExec")) != NULL + && _settings_browse_folder_access(path, X_OK) + != 0 && errno == ENOENT) continue; - } - res = access(path, X_OK); - string_delete(path); - if(res != 0) - continue; - } if((icon = config_get(config, section, "Icon")) == NULL) icon = GTK_STOCK_PREFERENCES; #ifdef DEBUG @@ -309,6 +303,60 @@ static int _settings_browse_folder(Settings * settings, Config * config, return FALSE; } +static int _settings_browse_folder_access(char const * path, int mode) +{ + int ret = -1; + char const * p; + char * q; + size_t i; + size_t j; + +#ifdef DEBUG + fprintf(stderr, "DEBUG: %s(\"%s\", %d)\n", __func__, path, mode); +#endif + if(path[0] == '/') + return access(path, mode); + if((p = getenv("PATH")) == NULL) + return 0; + if((q = string_new(p)) == NULL) + return -1; + errno = ENOENT; + for(i = 0, j = 0;; i++) + if(q[i] == '\0') + { + ret = _settings_browse_folder_access_path(&q[j], path, + mode); + break; + } + else if(q[i] == ':') + { + q[i] = '\0'; + if((ret = _settings_browse_folder_access_path(&q[j], + path, mode)) == 0) + break; + j = i + 1; + } + string_delete(q); + return ret; +} + +static int _settings_browse_folder_access_path(char const * path, + char const * filename, int mode) +{ + int ret; + String * p; + +#ifdef DEBUG + fprintf(stderr, "DEBUG: %s(\"%s\", \"%s\", %d)\n", __func__, path, + filename, mode); +#endif + if((p = string_new_append(path, "/", filename, NULL)) == NULL) + return -1; + ret = access(p, mode); + string_delete(p); + return ret; +} + static void _settings_rtrim(String * string, char c) { size_t len;