summaryrefslogtreecommitdiff
path: root/src/path.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/path.zig')
-rw-r--r--src/path.zig25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/path.zig b/src/path.zig
new file mode 100644
index 0000000..2f52bc9
--- /dev/null
+++ b/src/path.zig
@@ -0,0 +1,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;
+}