mirror of
https://github.com/fergalmoran/ladybird.git
synced 2025-12-22 09:19:03 +00:00
LibCore: Add find_executable_in_path.
This commit is contained in:
@@ -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 {};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -60,4 +60,6 @@ private:
|
||||
bool advance_next();
|
||||
};
|
||||
|
||||
String find_executable_in_path(String filename);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user