14# define pclose _pclose
16#elif defined(__APPLE__)
17# include <mach-o/dyld.h>
24using namespace std::string_literals;
29 std::vector<char> path_buffer;
32 _NSGetExecutablePath(
nullptr, &read);
33 path_buffer.resize(read + 1);
34 if (_NSGetExecutablePath(path_buffer.data(), &read) != 0)
return {};
35 return fs::path{path_buffer.data()};
40 path_buffer.resize(std::max(path_buffer.size(),
static_cast<size_t>(128)) * 2);
41 read = GetModuleFileNameA(
nullptr, path_buffer.data(),
static_cast<DWORD
>(path_buffer.size()));
42 }
while (read == path_buffer.size());
45 path_buffer.resize(read + 1);
46 path_buffer.back() = 0;
47 return fs::path{path_buffer.data()};
50 if (fs::exists(
"/proc/self/exe"))
return fs::canonical(
"/proc/self/exe");
56std::string
exec(std::string cmd) {
57 std::array<char, 128> buffer;
59 std::unique_ptr<FILE,
decltype(&pclose)> pipe(popen(cmd.c_str(),
"r"), pclose);
60 if (!pipe)
error(
"popen() failed!");
61 while (fgets(buffer.data(), buffer.size(), pipe.get()) !=
nullptr) result += buffer.data();
67 if (
auto it = out.find(
'\n'); it != std::string::npos) out.erase(it);
72 std::cout << cmd << std::endl;
73 int status = std::system(cmd.c_str());
74 return WEXITSTATUS(status);
77int run(std::string cmd, std::string args ) {
86std::string
escape(
const std::filesystem::path& path) {
88 for (
char c : path.string()) {
89 if (isspace(c)) str +=
'\\';
int run(std::string cmd, std::string args={})
Wraps sys::system and puts .exe at the back (Windows) and ./ at the front (otherwise) of cmd.
int system(std::string)
Wraps std::system and makes the return value usable.
std::optional< fs::path > path_to_curr_exe()
Yields std::nullopt if an error occurred.
std::string escape(const std::filesystem::path &path)
Returns the path as std::string and escapes all whitespaces with backslash.
std::string find_cmd(std::string)
std::string exec(std::string cmd)
Executes command cmd.
void error(Loc loc, const char *f, Args &&... args)