mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-10 13:00:29 +03:00
Userland: Add 'which' command (#497)
This mimics the shell's path resolution to locate the executable that would execute if typing it as a command.
This commit is contained in:
parent
50ef2216fa
commit
23e8715022
Notes:
sideshowbarker
2024-07-19 12:28:51 +09:00
Author: https://github.com/xeons 🔰 Commit: https://github.com/SerenityOS/serenity/commit/23e8715022d Pull-request: https://github.com/SerenityOS/serenity/pull/497 Reviewed-by: https://github.com/awesomekling
29
Userland/which.cpp
Normal file
29
Userland/which.cpp
Normal file
@ -0,0 +1,29 @@
|
||||
#include <AK/AKString.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc < 2) {
|
||||
printf("usage: which <executable>\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
char* filename = argv[1];
|
||||
|
||||
String path = getenv("PATH");
|
||||
if (path.is_empty())
|
||||
path = "/bin:/usr/bin";
|
||||
|
||||
auto parts = path.split(':');
|
||||
for (auto& part : parts) {
|
||||
auto candidate = String::format("%s/%s", part.characters(), filename);
|
||||
if(access(candidate.characters(), X_OK) == 0) {
|
||||
printf("%s\n", candidate.characters());
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
Loading…
Reference in New Issue
Block a user