Skip to content

Commit

Permalink
Process: Add Process::setWorkingDirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
Pagghiu committed Apr 6, 2024
1 parent 1a2681b commit ea5e2c0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Libraries/Process/Internal/ProcessPosix.inl
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@ SC::Result SC::Process::launchImplementation()
SC_TRY(stdOutFd.close());
SC_TRY(stdErrFd.close());

// Switch to wanted current directory (if provided)
if (not currentDirectory.isEmpty())
{
int res = ::chdir(currentDirectory.view().getNullTerminatedNative());
if (res < 0)
{
return Result::Error("chdir failed");
}
}

// Construct the argv pointers array, from the command string, that contains the
// executable and all arguments separated by null terminators
// First parameter is executable path and also argv[0]
Expand Down
5 changes: 5 additions & 0 deletions Libraries/Process/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,8 @@ SC::Result SC::Process::launch(const StdOut& stdOutput, const StdIn& stdInput, c

return Result(true);
}

SC::Result SC::Process::setWorkingDirectory(StringView processWorkingDirectory)
{
return Result(StringConverter(currentDirectory).appendNullTerminated(processWorkingDirectory));
}
4 changes: 3 additions & 1 deletion Libraries/Process/Process.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ struct SC::Process
/// @brief gets the return code from the exited child process (valid only after exec or waitForExitSync)
int32_t getExitStatus() const { return exitStatus.status; }

/// @brief Sets the starting working directory of the process that will be launched / executed
[[nodiscard]] Result setWorkingDirectory(StringView processWorkingDirectory);

/// @brief Returns number of (virtual) processors available
static size_t getNumberOfProcessors();

Expand All @@ -209,7 +212,6 @@ struct SC::Process

[[nodiscard]] Result formatArguments(Span<const StringView> cmd);

// TODO: These must be exposed and filled properly with existing values
StringNative<255> currentDirectory = StringEncoding::Native;
StringNative<1024> environment = StringEncoding::Native;

Expand Down

0 comments on commit ea5e2c0

Please sign in to comment.