From 9a135e52fccafabb494a05c19d9874dbf23c6f03 Mon Sep 17 00:00:00 2001 From: Lucas Faria Mendes Date: Fri, 5 Dec 2025 04:07:36 -0300 Subject: codecrafters submit [skip ci] --- src/executor.zig | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'src/executor.zig') diff --git a/src/executor.zig b/src/executor.zig index f2db136..cdddb4f 100644 --- a/src/executor.zig +++ b/src/executor.zig @@ -28,7 +28,7 @@ pub fn runExternalProgram(allocator: std.mem.Allocator, program_path: []const u8 } } -pub fn runExternalProgramWithRedirect(allocator: std.mem.Allocator, program_path: []const u8, argv: []const []const u8, output_file: ?[]const u8, error_file: ?[]const u8, append_file: ?[]const u8) !void { +pub fn runExternalProgramWithRedirect(allocator: std.mem.Allocator, program_path: []const u8, argv: []const []const u8, output_file: ?[]const u8, error_file: ?[]const u8, append_file: ?[]const u8, append_error_file: ?[]const u8) !void { const argv_z = try allocator.allocSentinel(?[*:0]const u8, argv.len, null); defer allocator.free(argv_z); @@ -78,6 +78,21 @@ pub fn runExternalProgramWithRedirect(allocator: std.mem.Allocator, program_path }; defer fd.close(); try std.posix.dup2(fd.handle, 2); + } else if (append_error_file) |file| { + const cwd = std.fs.cwd(); + const fd = cwd.openFile(file, .{ .mode = .write_only }) catch |err| blk: { + if (err == error.FileNotFound) { + break :blk cwd.createFile(file, .{}) catch { + std.posix.exit(1); + }; + } + std.posix.exit(1); + }; + defer fd.close(); + fd.seekFromEnd(0) catch { + std.posix.exit(1); + }; + try std.posix.dup2(fd.handle, 2); } _ = std.posix.execveZ(program_path_z, argv_z, std.c.environ) catch { -- cgit v1.2.3