shell: Add TMPDIR capability to script()

This commit is contained in:
zawz 2020-07-07 14:43:02 +02:00
parent 8e7ec95e05
commit 42fc36e72b

View file

@ -1,4 +1,4 @@
#include "shell.hpp" include "shell.hpp"
#include <thread> #include <thread>
#include <fstream> #include <fstream>
@ -184,19 +184,19 @@ FILE* ztd::eopen(const char* type, int* pid, const char* bin, std::vector<char*>
int ztd::eclose(FILE* fd, pid_t pid) int ztd::eclose(FILE* fd, pid_t pid)
{ {
int stat; int stat;
fclose(fd); fclose(fd);
while (waitpid(pid, &stat, 0) == -1) while (waitpid(pid, &stat, 0) == -1)
{
if (errno != EINTR)
{ {
if (errno != EINTR) stat = -1;
{ break;
stat = -1;
break;
}
} }
}
return stat; return stat;
} }
// exec calls // exec calls
@ -220,8 +220,10 @@ std::pair<std::string, int> ztd::exec(std::string const& bin, std::vector<std::s
} }
std::pair<std::string, int> ztd::script(std::string const& data, std::vector<std::string> const& args) std::pair<std::string, int> ztd::script(std::string const& data, std::vector<std::string> const& args)
{ {
// generate path
const std::string tmpdir = (getenv("TMPDIR") != NULL) ? getenv("TMPDIR") : "/tmp" ;
const std::string filepath = tmpdir + "/ztdscript_" + ztd::sh("tr -dc '[:alnum:]' < /dev/urandom | head -c10");
// create stream // create stream
std::string filepath = "/tmp/ztdscript" + ztd::sh("tr -dc '[:alnum:]' < /dev/urandom | head -c10");
std::ofstream stream(filepath); std::ofstream stream(filepath);
if(!stream) if(!stream)
throw std::runtime_error("Failed to write to file '"+filepath+'\''); throw std::runtime_error("Failed to write to file '"+filepath+'\'');