diff options
| author | Lucas Faria Mendes <lucas.oliveira1676@etec.sp.gov.br> | 2025-12-05 05:53:37 +0000 |
|---|---|---|
| committer | Lucas Faria Mendes <lucas.oliveira1676@etec.sp.gov.br> | 2025-12-05 05:53:37 +0000 |
| commit | 2515de92a5ae28edda56deba40c852f36928b294 (patch) | |
| tree | cc048d51ba7dd6fc78376f02e1c12af1161c7d38 /src/path.zig | |
| parent | 695b1fa1dc95d62016978a517b8e8544e486d9b2 (diff) | |
| download | shell-zig-2515de92a5ae28edda56deba40c852f36928b294.tar.gz shell-zig-2515de92a5ae28edda56deba40c852f36928b294.zip | |
codecrafters submit [skip ci]
Diffstat (limited to 'src/path.zig')
| -rw-r--r-- | src/path.zig | 25 |
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; +} |