From 350625683c066a4790f41cb91c8c05ac4fb5dd4f Mon Sep 17 00:00:00 2001 From: Lucas Faria Mendes Date: Fri, 5 Dec 2025 02:55:56 -0300 Subject: codecrafters submit [skip ci] --- src/builtins.zig | 8 +++++++- src/shell.zig | 5 +++++ 2 files changed, 12 insertions(+), 1 deletion(-) (limited to 'src') 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; -- cgit v1.2.3