diff --git a/apps/sh.cpp b/apps/sh.cpp index f46d1a49..eb1ade6a 100644 --- a/apps/sh.cpp +++ b/apps/sh.cpp @@ -14,7 +14,7 @@ using os::File; -static Result> split_command_into_argv(StringView cmd) +static Result> split_command_into_argv(StringView cmd, char** out) { char* str = strdup(cmd.chars()); @@ -33,13 +33,15 @@ static Result> split_command_into_argv(StringView cmd) if (segment == NULL) return result; } + if (out) *out = str; + return result; } [[noreturn]] static void execute_command(StringView command) { Vector argv; - bool ok = split_command_into_argv(command).try_move_value_or_error(argv, errno); + bool ok = split_command_into_argv(command, nullptr).try_move_value_or_error(argv, errno); if (!ok) { perror("failed to parse command"); @@ -93,17 +95,21 @@ Result luna_main(int argc, char** argv) if (!strncmp(cmd.chars(), "cd", 2)) { - auto args = TRY(split_command_into_argv(cmd.view())); + char* copy = nullptr; + + auto args = TRY(split_command_into_argv(cmd.view(), ©)); check("cd"_sv == args[0]); if (args.size() == 2) { auto home = TRY(os::FileSystem::home_directory()); if (chdir(home.chars()) < 0) perror("cd"); + free(copy); continue; } if (chdir(args[1]) < 0) perror("cd"); + free(copy); continue; }