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/executor.zig | |
| parent | 695b1fa1dc95d62016978a517b8e8544e486d9b2 (diff) | |
| download | shell-zig-2515de92a5ae28edda56deba40c852f36928b294.tar.gz shell-zig-2515de92a5ae28edda56deba40c852f36928b294.zip | |
codecrafters submit [skip ci]
Diffstat (limited to 'src/executor.zig')
| -rw-r--r-- | src/executor.zig | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/executor.zig b/src/executor.zig new file mode 100644 index 0000000..839fcda --- /dev/null +++ b/src/executor.zig @@ -0,0 +1,29 @@ +const std = @import("std"); + +pub fn runExternalProgram(allocator: std.mem.Allocator, program_path: []const u8, argv: []const []const u8) !void { + const argv_z = try allocator.allocSentinel(?[*:0]const u8, argv.len, null); + defer allocator.free(argv_z); + + for (argv, 0..) |arg, i| { + argv_z[i] = (try allocator.dupeZ(u8, arg)).ptr; + } + defer { + for (argv_z[0..argv.len]) |arg_ptr| { + if (arg_ptr) |ptr| allocator.free(std.mem.span(ptr)); + } + } + + const program_path_z = try allocator.dupeZ(u8, program_path); + defer allocator.free(program_path_z); + + const pid = try std.posix.fork(); + + if (pid == 0) { + _ = std.posix.execveZ(program_path_z, argv_z, std.c.environ) catch { + std.posix.exit(1); + }; + unreachable; + } else { + _ = std.posix.waitpid(pid, 0); + } +} |