summaryrefslogtreecommitdiff
path: root/src/path.zig
blob: 2f52bc96af5bc93d9728644f3c2dfba1d3ab7ec3 (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
const std = @import("std");

pub fn findInPath(allocator: std.mem.Allocator, cmd_name: []const u8) !?[]const u8 {
    const path_env = std.process.getEnvVarOwned(allocator, "PATH") catch return null;
    defer allocator.free(path_env);

    var it = std.mem.splitScalar(u8, path_env, ':');
    while (it.next()) |dir| {
        const full_path = std.fs.path.join(allocator, &[_][]const u8{ dir, cmd_name }) catch continue;
        defer allocator.free(full_path);

        const file = std.fs.openFileAbsolute(full_path, .{}) catch continue;
        const stat = file.stat() catch {
            file.close();
            continue;
        };
        file.close();

        if ((stat.mode & 0o111) == 0) continue;

        return try allocator.dupe(u8, full_path);
    }

    return null;
}