LibCore: Add find_executable_in_path.

This commit is contained in:
asynts 2020-09-16 18:55:41 +02:00 committed by Andreas Kling
parent d55e3c4642
commit 3283f5bb5d
Notes: sideshowbarker 2024-07-19 02:22:57 +09:00
2 changed files with 22 additions and 0 deletions

View file

@ -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 {};
}
}

View file

@ -60,4 +60,6 @@ private:
bool advance_next();
};
String find_executable_in_path(String filename);
}