summaryrefslogtreecommitdiff
path: root/src/executor.zig
diff options
context:
space:
mode:
authorLucas Faria Mendes <lucas.oliveira1676@etec.sp.gov.br>2025-12-05 06:57:54 +0000
committerLucas Faria Mendes <lucas.oliveira1676@etec.sp.gov.br>2025-12-05 06:57:54 +0000
commitbd95ed67a18aa6938c76e29dff136309ba22a02a (patch)
treed0db1bcfd720c13f4423ce3460d6a588fadbe354 /src/executor.zig
parented5493f9825d7244bb489d160208bf708641d0b8 (diff)
downloadshell-zig-bd95ed67a18aa6938c76e29dff136309ba22a02a.tar.gz
shell-zig-bd95ed67a18aa6938c76e29dff136309ba22a02a.zip
codecrafters submit [skip ci]
Diffstat (limited to 'src/executor.zig')
-rw-r--r--src/executor.zig17
1 files changed, 16 insertions, 1 deletions
diff --git a/src/executor.zig b/src/executor.zig
index 3d3184c..f2db136 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) !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) !void {
const argv_z = try allocator.allocSentinel(?[*:0]const u8, argv.len, null);
defer allocator.free(argv_z);
@@ -54,6 +54,21 @@ pub fn runExternalProgramWithRedirect(allocator: std.mem.Allocator, program_path
};
defer fd.close();
try std.posix.dup2(fd.handle, 1);
+ } else if (append_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, 1);
}
if (error_file) |file| {