diff options
| author | Lucas Faria Mendes <lucas.oliveira1676@etec.sp.gov.br> | 2025-12-05 07:07:36 +0000 |
|---|---|---|
| committer | Lucas Faria Mendes <lucas.oliveira1676@etec.sp.gov.br> | 2025-12-05 07:07:36 +0000 |
| commit | 9a135e52fccafabb494a05c19d9874dbf23c6f03 (patch) | |
| tree | 95df20713bfa1af7a071052a0f3ff4c010ef343f /src/shell.zig | |
| parent | bd95ed67a18aa6938c76e29dff136309ba22a02a (diff) | |
| download | shell-zig-9a135e52fccafabb494a05c19d9874dbf23c6f03.tar.gz shell-zig-9a135e52fccafabb494a05c19d9874dbf23c6f03.zip | |
codecrafters submit [skip ci]
Diffstat (limited to 'src/shell.zig')
| -rw-r--r-- | src/shell.zig | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/src/shell.zig b/src/shell.zig index 23e64d2..b15003e 100644 --- a/src/shell.zig +++ b/src/shell.zig @@ -12,6 +12,7 @@ pub fn executeCommand( output_redirect: ?[]const u8, error_redirect: ?[]const u8, append_output: ?[]const u8, + append_error: ?[]const u8, ) !builtins.CommandResult { if (std.mem.eql(u8, cmd_name, "exit")) return builtins.executeExit(); @@ -21,6 +22,17 @@ pub fn executeCommand( fd.close(); } + if (append_error) |file| { + _ = std.fs.cwd().openFile(file, .{ .mode = .write_only }) catch |err| { + if (err == error.FileNotFound) { + const new_fd = try std.fs.cwd().createFile(file, .{}); + new_fd.close(); + } else { + return err; + } + }; + } + if (output_redirect != null or append_output != null) { const file = if (output_redirect) |f| f else append_output.?; const is_append = append_output != null; @@ -111,8 +123,8 @@ pub fn executeCommand( const argv = try parser.parseArgs(allocator, cmd_name, args); defer allocator.free(argv); - if (output_redirect != null or error_redirect != null or append_output != null) { - try executor.runExternalProgramWithRedirect(allocator, program_path, argv, output_redirect, error_redirect, append_output); + if (output_redirect != null or error_redirect != null or append_output != null or append_error != null) { + try executor.runExternalProgramWithRedirect(allocator, program_path, argv, output_redirect, error_redirect, append_output, append_error); } else { try executor.runExternalProgram(allocator, program_path, argv); } |