diff options
author | asynts <asynts@gmail.com> | 2020-09-16 18:55:41 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-09-16 19:39:17 +0200 |
commit | 3283f5bb5d843933b9c6070206c8a9b9846e7238 (patch) | |
tree | 05fcce91eee73fb4203277ba1716ade4403bbf46 /Libraries/LibCore | |
parent | d55e3c46424f2bb1e68e55cbff69de6d43ee7110 (diff) | |
download | serenity-3283f5bb5d843933b9c6070206c8a9b9846e7238.zip |
LibCore: Add find_executable_in_path.
Diffstat (limited to 'Libraries/LibCore')
-rw-r--r-- | Libraries/LibCore/DirIterator.cpp | 20 | ||||
-rw-r--r-- | Libraries/LibCore/DirIterator.h | 2 |
2 files changed, 22 insertions, 0 deletions
diff --git a/Libraries/LibCore/DirIterator.cpp b/Libraries/LibCore/DirIterator.cpp index 537b29eb85..3272e05921 100644 --- a/Libraries/LibCore/DirIterator.cpp +++ b/Libraries/LibCore/DirIterator.cpp @@ -25,6 +25,7 @@ */ #include <LibCore/DirIterator.h> +#include <AK/Vector.h> #include <errno.h> namespace Core { @@ -98,4 +99,23 @@ String DirIterator::next_full_path() return String::format("%s/%s", m_path.characters(), next_path().characters()); } +String find_executable_in_path(String filename) +{ + if (filename.starts_with('/')) { + if (access(filename.characters(), X_OK) == 0) + return filename; + + return {}; + } + + for (auto directory : StringView { getenv("PATH") }.split_view(':')) { + auto fullpath = String::format("%s/%s", directory, filename); + + if (access(fullpath.characters(), X_OK) == 0) + return fullpath; + } + + return {}; +} + } diff --git a/Libraries/LibCore/DirIterator.h b/Libraries/LibCore/DirIterator.h index b3a2c5a5bd..7f423b4f17 100644 --- a/Libraries/LibCore/DirIterator.h +++ b/Libraries/LibCore/DirIterator.h @@ -60,4 +60,6 @@ private: bool advance_next(); }; +String find_executable_in_path(String filename); + } |