summaryrefslogtreecommitdiff
path: root/src/shell.zig
diff options
context:
space:
mode:
authorLucas Faria Mendes <lucas.oliveira1676@etec.sp.gov.br>2025-12-05 06:49:44 +0000
committerLucas Faria Mendes <lucas.oliveira1676@etec.sp.gov.br>2025-12-05 06:49:44 +0000
commited5493f9825d7244bb489d160208bf708641d0b8 (patch)
treea412af2160d7a8ce1d6a240e3eaed839138a8115 /src/shell.zig
parent6f1c762e789cd746e20cd7597aab0316d2c2fcd9 (diff)
downloadshell-zig-ed5493f9825d7244bb489d160208bf708641d0b8.tar.gz
shell-zig-ed5493f9825d7244bb489d160208bf708641d0b8.zip
codecrafters submit [skip ci]
Diffstat (limited to 'src/shell.zig')
-rw-r--r--src/shell.zig13
1 files changed, 10 insertions, 3 deletions
diff --git a/src/shell.zig b/src/shell.zig
index c95d9d0..ac16cab 100644
--- a/src/shell.zig
+++ b/src/shell.zig
@@ -10,11 +10,18 @@ pub fn executeCommand(
cmd_name: []const u8,
args: ?[]const u8,
output_redirect: ?[]const u8,
+ error_redirect: ?[]const u8,
) !builtins.CommandResult {
if (std.mem.eql(u8, cmd_name, "exit")) return builtins.executeExit();
if (std.mem.eql(u8, cmd_name, "echo")) {
- if (output_redirect) |file| {
+ if (error_redirect) |file| {
+ const fd = try std.fs.cwd().createFile(file, .{});
+ fd.close();
+ }
+
+ if (output_redirect != null) {
+ const file = output_redirect.?;
const fd = try std.fs.cwd().createFile(file, .{});
defer fd.close();
@@ -90,8 +97,8 @@ pub fn executeCommand(
const argv = try parser.parseArgs(allocator, cmd_name, args);
defer allocator.free(argv);
- if (output_redirect) |file| {
- try executor.runExternalProgramWithRedirect(allocator, program_path, argv, file);
+ if (output_redirect != null or error_redirect != null) {
+ try executor.runExternalProgramWithRedirect(allocator, program_path, argv, output_redirect, error_redirect);
} else {
try executor.runExternalProgram(allocator, program_path, argv);
}