mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 21:57:35 +00:00
LibCore: Add find_executable_in_path.
This commit is contained in:
parent
d55e3c4642
commit
3283f5bb5d
2 changed files with 22 additions and 0 deletions
|
@ -25,6 +25,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <LibCore/DirIterator.h>
|
#include <LibCore/DirIterator.h>
|
||||||
|
#include <AK/Vector.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
@ -98,4 +99,23 @@ String DirIterator::next_full_path()
|
||||||
return String::format("%s/%s", m_path.characters(), next_path().characters());
|
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();
|
bool advance_next();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
String find_executable_in_path(String filename);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue