summaryrefslogtreecommitdiff
path: root/src/shared
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/path-util.c48
-rw-r--r--src/shared/path-util.h8
2 files changed, 56 insertions, 0 deletions
diff --git a/src/shared/path-util.c b/src/shared/path-util.c
index 6888135778..8e108db531 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;
+ }
+}
diff --git a/src/shared/path-util.h b/src/shared/path-util.h
index d187743769..9452931586 100644
--- a/src/shared/path-util.h
+++ b/src/shared/path-util.h
@@ -25,6 +25,12 @@
#include "macro.h"
+#ifdef HAVE_SPLIT_USR
+# define DEFAULT_PATH "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
+#else
+# define DEFAULT_PATH "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
+#endif
+
bool is_path(const char *p) _pure_;
char** path_split_and_make_absolute(const char *p);
char* path_get_file_name(const char *p) _pure_;
@@ -43,3 +49,5 @@ char** path_strv_canonicalize_uniq(char **l);
int path_is_mount_point(const char *path, bool allow_symlink);
int path_is_read_only_fs(const char *path);
int path_is_os_tree(const char *path);
+
+int find_binary(const char *name, char **filename);