From 2515de92a5ae28edda56deba40c852f36928b294 Mon Sep 17 00:00:00 2001 From: Lucas Faria Mendes Date: Fri, 5 Dec 2025 02:53:37 -0300 Subject: codecrafters submit [skip ci] --- src/path.zig | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/path.zig (limited to 'src/path.zig') 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; +} -- cgit v1.2.3