sh: Use StringBuilder instead of C-like manual joining

This commit is contained in:
apio 2023-04-07 12:11:28 +02:00
parent 7b8260f3f6
commit 4cac49038c
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -1,3 +1,4 @@
#include <luna/StringBuilder.h>
#include <luna/Vector.h>
#include <errno.h>
@ -30,21 +31,23 @@ static Result<Vector<char*>> split_command_into_argv(const char* cmd)
return result;
}
static char* join_path(const char* str1, const char* str2)
static Result<String> join_path(const char* str1, const char* str2)
{
char* buf = (char*)malloc(strlen(str1) + strlen(str2) + 1);
strlcpy(buf, str1, strlen(str1) + 1);
strncat(buf, str2, strlen(str2));
return buf;
StringBuilder sb;
TRY(sb.add(StringView { str1 }));
TRY(sb.add(StringView { str2 }));
return sb.string();
}
static int execute_in_path(const char* dir, char** argv)
{
char* path = join_path(dir, argv[0]);
String path;
bool ok = join_path(dir, argv[0]).try_move_value_or_error(path, errno);
if (!ok) return -1;
int err = errno;
int status = execv(path, argv);
free(path);
int status = execv(path.chars(), argv);
if (errno == ENOENT)
{