This is a utility function we can use to see whether an executable is available. --- util/Makefile.local | 2 +- util/search-path.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ util/search-path.h | 24 ++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 util/search-path.c create mode 100644 util/search-path.h
diff --git a/util/Makefile.local b/util/Makefile.local index 905f237..8b2b91b 100644 --- a/util/Makefile.local +++ b/util/Makefile.local @@ -5,7 +5,7 @@ extra_cflags += -I$(srcdir)/$(dir) libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c \ $(dir)/string-util.c $(dir)/talloc-extra.c $(dir)/zlib-extra.c \ - $(dir)/util.c + $(dir)/util.c $(dir)/search-path.c libutil_modules := $(libutil_c_srcs:.c=.o) diff --git a/util/search-path.c b/util/search-path.c new file mode 100644 index 0000000..9da21cb --- /dev/null +++ b/util/search-path.c @@ -0,0 +1,50 @@ +#include "search-path.h" +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> + + +notmuch_bool_t +test_for_executable (const char *exename) +{ + char *path = NULL, *save = NULL, *tok; + notmuch_bool_t ret = FALSE; + + if (strchr (exename, '/')) { + if (0 == access (exename, X_OK)) + return TRUE; + else + return FALSE; + } + + path = getenv ("PATH"); + if (path) + path = strdup (path); + else { + size_t n = confstr (_CS_PATH, NULL, 0); + path = (char *) malloc (n); + if (! path) + return FALSE; + confstr (_CS_PATH, path, n); + } + + tok = strtok_r (path, ":", &save); + while (tok) { + int dir_fd = open (tok, O_DIRECTORY | O_RDONLY); + if (dir_fd != -1) { + int access = faccessat (dir_fd, exename, X_OK, 0); + close (dir_fd); + if (access == 0) { + ret = TRUE; + break; + } + } + tok = strtok_r (NULL, ":", &save); + } + if (path) + free (path); + return ret; +} diff --git a/util/search-path.h b/util/search-path.h new file mode 100644 index 0000000..14c4d14 --- /dev/null +++ b/util/search-path.h @@ -0,0 +1,24 @@ +#ifndef _SEARCH_PATH_H +#define _SEARCH_PATH_H + +#include "notmuch.h" + +/* can an executable be found with the given name? + * + * Return TRUE only if we can find something to execute with the + * associated name. + * + * if the name has a '/' in it, we look for it directly with + * access(exename, X_OK). + * + * otherwise, we look for it in $PATH (or in confstr(_CS_PATH), if + * $PATH is unset). + * + * This should match the logic for execvp (as well as matching user + * expectations, hopefully). + */ + +notmuch_bool_t +test_for_executable (const char *exename); + +#endif -- 2.8.1 _______________________________________________ notmuch mailing list notmuch@notmuchmail.org https://notmuchmail.org/mailman/listinfo/notmuch