mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-20 11:36:10 +00:00
LibCore: Add find_executable_in_path.
This commit is contained in:
parent
d55e3c4642
commit
3283f5bb5d
Notes:
sideshowbarker
2024-07-19 02:22:57 +09:00
Author: https://github.com/asynts Commit: https://github.com/SerenityOS/serenity/commit/3283f5bb5d8 Pull-request: https://github.com/SerenityOS/serenity/pull/3511 Reviewed-by: https://github.com/nico
2 changed files with 22 additions and 0 deletions
|
@ -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);
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue