diff options
| author | Lucas Faria Mendes <lucas.oliveira1676@etec.sp.gov.br> | 2025-12-05 05:55:56 +0000 |
|---|---|---|
| committer | Lucas Faria Mendes <lucas.oliveira1676@etec.sp.gov.br> | 2025-12-05 05:55:56 +0000 |
| commit | 350625683c066a4790f41cb91c8c05ac4fb5dd4f (patch) | |
| tree | 1940d0fa10e02f24d6ee79b3794738bdd2e7e0e6 | |
| parent | 2515de92a5ae28edda56deba40c852f36928b294 (diff) | |
| download | shell-zig-350625683c066a4790f41cb91c8c05ac4fb5dd4f.tar.gz shell-zig-350625683c066a4790f41cb91c8c05ac4fb5dd4f.zip | |
codecrafters submit [skip ci]
| -rw-r--r-- | src/builtins.zig | 8 | ||||
| -rw-r--r-- | src/shell.zig | 5 |
2 files changed, 12 insertions, 1 deletions
diff --git a/src/builtins.zig b/src/builtins.zig index c989a35..ea34f19 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -1,7 +1,7 @@ const std = @import("std"); const path = @import("path.zig"); -const BUILTINS = [_][]const u8{ "exit", "echo", "type" }; +const BUILTINS = [_][]const u8{ "exit", "echo", "type", "pwd" }; pub const CommandResult = enum { continue_loop, @@ -27,6 +27,12 @@ pub fn executeEcho(stdout: anytype, args: ?[]const u8) !void { } } +pub fn executePwd(allocator: std.mem.Allocator, stdout: anytype) !void { + const cwd = try std.fs.cwd().realpathAlloc(allocator, "."); + defer allocator.free(cwd); + try stdout.print("{s}\n", .{cwd}); +} + pub fn executeType(allocator: std.mem.Allocator, stdout: anytype, args: ?[]const u8) !void { if (args) |a| { if (isBuiltin(a)) { diff --git a/src/shell.zig b/src/shell.zig index c5aded1..2494fda 100644 --- a/src/shell.zig +++ b/src/shell.zig @@ -17,6 +17,11 @@ pub fn executeCommand( return .continue_loop; } + if (std.mem.eql(u8, cmd_name, "pwd")) { + try builtins.executePwd(allocator, stdout); + return .continue_loop; + } + if (std.mem.eql(u8, cmd_name, "type")) { try builtins.executeType(allocator, stdout, args); return .continue_loop; |