summaryrefslogtreecommitdiff
path: root/Userland/which.cpp
blob: 708c14deb5bfc3022611338d51d0158300d65781 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <AK/String.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;
}