X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fshared%2Fpath-util.c;h=8e108db53173f1c39ef1ae2965c7081031ee6fce;hp=68881357784eba69f4cd8d165b32f903f058323e;hb=c9d954b27ee125c3c90a6d2951c62eec4abb160b;hpb=116cc028742836e61abce7582ec9ecf9f0aaeb53 diff --git a/src/shared/path-util.c b/src/shared/path-util.c index 688813577..8e108db53 100644 --- a/src/shared/path-util.c +++ b/src/shared/path-util.c @@ -425,3 +425,51 @@ int path_is_os_tree(const char *path) { return r < 0 ? 0 : 1; } + +int find_binary(const char *name, char **filename) { + assert(name); + if (strchr(name, '/')) { + char *p; + + if (path_is_absolute(name)) + p = strdup(name); + else + p = path_make_absolute_cwd(name); + if (!p) + return -ENOMEM; + + *filename = p; + return 0; + } else { + const char *path; + char *state, *w; + size_t l; + + /** + * Plain getenv, not secure_getenv, because we want + * to actually allow the user to pick the binary. + */ + path = getenv("PATH"); + if (!path) + path = DEFAULT_PATH; + + FOREACH_WORD_SEPARATOR(w, l, path, ":", state) { + char *p; + + if (asprintf(&p, "%.*s/%s", l, w, name) < 0) + return -ENOMEM; + + if (access(p, X_OK) < 0) { + free(p); + continue; + } + + path_kill_slashes(p); + *filename = p; + + return 0; + } + + return -ENOENT; + } +}