Skip to content

Commit

Permalink
LibCore: Add find_executable_in_path.
Browse files Browse the repository at this point in the history
  • Loading branch information
asynts authored and awesomekling committed Sep 16, 2020
1 parent d55e3c4 commit 3283f5b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Libraries/LibCore/DirIterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/

#include <LibCore/DirIterator.h>
#include <AK/Vector.h>
#include <errno.h>

namespace Core {
Expand Down Expand Up @@ -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 {};
}

}
2 changes: 2 additions & 0 deletions Libraries/LibCore/DirIterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ class DirIterator {
bool advance_next();
};

String find_executable_in_path(String filename);

}

0 comments on commit 3283f5b

Please sign in to comment.