diff options
| -rw-r--r-- | src/builtins.zig | 25 | ||||
| -rw-r--r-- | src/shell.zig | 2 |
2 files changed, 23 insertions, 4 deletions
diff --git a/src/builtins.zig b/src/builtins.zig index 07c278b..3686c61 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -33,15 +33,34 @@ pub fn executePwd(allocator: std.mem.Allocator, stdout: anytype) !void { try stdout.print("{s}\n", .{cwd}); } -pub fn executeCd(stdout: anytype, args: ?[]const u8) !void { +pub fn executeCd(allocator: std.mem.Allocator, stdout: anytype, args: ?[]const u8) !void { if (args == null or args.?.len == 0) { try stdout.print("cd: missing argument\n", .{}); return; } - const dir = std.mem.trim(u8, args.?, " "); + const arg = std.mem.trim(u8, args.?, " "); + var path_buf: [512]u8 = undefined; + var dir: []const u8 = arg; + + if (std.mem.eql(u8, arg, "~")) { + const home = std.process.getEnvVarOwned(allocator, "HOME") catch { + try stdout.print("cd: HOME not set\n", .{}); + return; + }; + defer allocator.free(home); + dir = try std.fmt.bufPrint(&path_buf, "{s}", .{home}); + } else if (std.mem.startsWith(u8, arg, "~/")) { + const home = std.process.getEnvVarOwned(allocator, "HOME") catch { + try stdout.print("cd: HOME not set\n", .{}); + return; + }; + defer allocator.free(home); + dir = try std.fmt.bufPrint(&path_buf, "{s}/{s}", .{ home, arg[2..] }); + } + std.posix.chdir(dir) catch { - try stdout.print("cd: {s}: No such file or directory\n", .{dir}); + try stdout.print("cd: {s}: No such file or directory\n", .{arg}); }; } diff --git a/src/shell.zig b/src/shell.zig index 56ca991..e42c131 100644 --- a/src/shell.zig +++ b/src/shell.zig @@ -23,7 +23,7 @@ pub fn executeCommand( } if (std.mem.eql(u8, cmd_name, "cd")) { - try builtins.executeCd(stdout, args); + try builtins.executeCd(allocator, stdout, args); return .continue_loop; } |