Skip to content

Commit

Permalink
✨ Linux and Mac implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
JulesFouchy committed Dec 5, 2024
1 parent 356acb7 commit 0153f3f
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/spawn_process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
namespace {

#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

std::string GetLastErrorAsString()
{
DWORD errorMessageID = ::GetLastError();
if (errorMessageID == 0)
{
return "No error";
}

LPSTR messageBuffer = nullptr;

// TODO is this implementation safe ? no strings too long ?
size_t size = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
Expand All @@ -25,9 +25,7 @@ std::string GetLastErrorAsString()

std::string message(messageBuffer, size);

// Free the buffer allocated by FormatMessage
LocalFree(messageBuffer);

return message;
}

Expand All @@ -47,6 +45,27 @@ auto spawn_process_impl(std::filesystem::path const& executable_absolute_path) -
}

#elif defined(__linux__) || defined(__APPLE__)
#include <unistd.h>
#include <sys/types.h>
#include <cerrno>
#include <cstring>

auto spawn_process_impl(std::filesystem::path const& executable_absolute_path) -> std::optional<std::string>

Check failure on line 53 in src/spawn_process.cpp

View workflow job for this annotation

GitHub Actions / Linux Clang Debug

no member named 'filesystem' in namespace '(anonymous namespace)::std'; did you mean '::std::filesystem'?

Check failure on line 53 in src/spawn_process.cpp

View workflow job for this annotation

GitHub Actions / Linux Clang Debug

invalid parameter name: 'const' is a keyword

Check failure on line 53 in src/spawn_process.cpp

View workflow job for this annotation

GitHub Actions / Linux Clang Debug

expected ')'

Check failure on line 53 in src/spawn_process.cpp

View workflow job for this annotation

GitHub Actions / Linux Clang Debug

no template named 'optional' in namespace '(anonymous namespace)::std'; did you mean '::std::optional'?

Check failure on line 53 in src/spawn_process.cpp

View workflow job for this annotation

GitHub Actions / Linux Clang Debug

no member named 'string' in namespace '(anonymous namespace)::std'

Check failure on line 53 in src/spawn_process.cpp

View workflow job for this annotation

GitHub Actions / Linux Clang Release

no member named 'filesystem' in namespace '(anonymous namespace)::std'; did you mean '::std::filesystem'?

Check failure on line 53 in src/spawn_process.cpp

View workflow job for this annotation

GitHub Actions / Linux Clang Release

invalid parameter name: 'const' is a keyword

Check failure on line 53 in src/spawn_process.cpp

View workflow job for this annotation

GitHub Actions / Linux Clang Release

expected ')'

Check failure on line 53 in src/spawn_process.cpp

View workflow job for this annotation

GitHub Actions / Linux Clang Release

no template named 'optional' in namespace '(anonymous namespace)::std'; did you mean '::std::optional'?

Check failure on line 53 in src/spawn_process.cpp

View workflow job for this annotation

GitHub Actions / Linux Clang Release

no member named 'string' in namespace '(anonymous namespace)::std'

Check failure on line 53 in src/spawn_process.cpp

View workflow job for this annotation

GitHub Actions / Linux GCC Debug

‘{anonymous}::std::filesystem’ has not been declared

Check failure on line 53 in src/spawn_process.cpp

View workflow job for this annotation

GitHub Actions / Linux GCC Debug

expected ‘,’ or ‘;’ before ‘->’ token

Check failure on line 53 in src/spawn_process.cpp

View workflow job for this annotation

GitHub Actions / Linux GCC Release

‘{anonymous}::std::filesystem’ has not been declared

Check failure on line 53 in src/spawn_process.cpp

View workflow job for this annotation

GitHub Actions / Linux GCC Release

expected ‘,’ or ‘;’ before ‘->’ token
{
pid_t pid = fork();

if (pid < 0)
{
// Fork failed
std::cerr << "Fork failed: " << strerror(errno) << std::endl;

Check failure on line 60 in src/spawn_process.cpp

View workflow job for this annotation

GitHub Actions / Linux Clang Debug

no member named 'cerr' in namespace '(anonymous namespace)::std'

Check failure on line 60 in src/spawn_process.cpp

View workflow job for this annotation

GitHub Actions / Linux Clang Debug

no member named 'endl' in namespace '(anonymous namespace)::std'

Check failure on line 60 in src/spawn_process.cpp

View workflow job for this annotation

GitHub Actions / Linux Clang Release

no member named 'cerr' in namespace '(anonymous namespace)::std'

Check failure on line 60 in src/spawn_process.cpp

View workflow job for this annotation

GitHub Actions / Linux Clang Release

no member named 'endl' in namespace '(anonymous namespace)::std'

Check failure on line 60 in src/spawn_process.cpp

View workflow job for this annotation

GitHub Actions / MacOS Clang Debug

no member named 'cerr' in namespace 'std'

Check failure on line 60 in src/spawn_process.cpp

View workflow job for this annotation

GitHub Actions / MacOS Clang Release

no member named 'cerr' in namespace 'std'
}
else if (pid == 0)
{
// In child process:
execl(executable_absolute_path.string().c_str(), executable_absolute_path.string().c_str(), nullptr);

Check failure on line 65 in src/spawn_process.cpp

View workflow job for this annotation

GitHub Actions / Linux Clang Debug

use of undeclared identifier 'executable_absolute_path'

Check failure on line 65 in src/spawn_process.cpp

View workflow job for this annotation

GitHub Actions / Linux Clang Debug

use of undeclared identifier 'executable_absolute_path'

Check failure on line 65 in src/spawn_process.cpp

View workflow job for this annotation

GitHub Actions / Linux Clang Release

use of undeclared identifier 'executable_absolute_path'

Check failure on line 65 in src/spawn_process.cpp

View workflow job for this annotation

GitHub Actions / Linux Clang Release

use of undeclared identifier 'executable_absolute_path'
_exit(1); // Ensure the child process exits
}
}

#else
#error "Unsupported platform"
Expand Down

0 comments on commit 0153f3f

Please sign in to comment.