From cf2075ec2f89789972058ec96a5d4f1bbb1f0c53 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Tue, 16 Feb 2010 22:09:15 +0000 Subject: malloc/free vs new at posix sysexec --- src/posix/sysExec.hpp | 62 ++++++++++++++++++++++++++++++++++----------------- src/sysExec.cpp | 6 ++--- src/sysExec.h | 8 +++---- src/win32/sysExec.hpp | 2 +- 4 files changed, 49 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/posix/sysExec.hpp b/src/posix/sysExec.hpp index 48cd8f2..18fde97 100644 --- a/src/posix/sysExec.hpp +++ b/src/posix/sysExec.hpp @@ -49,21 +49,42 @@ SysExec::~SysExec() close(pipefd_); } -char** dupEnv(StringList const& env) + +template +char** dupSysStringArray(T const& array) { - char** evp; - evp = new char*[env.size() + 1]; + char** new_array; + new_array = static_cast(malloc((array.size() + 1)*sizeof(char*))); + if(!new_array) + return NULL; + unsigned int i = 0; - for(StringList::const_iterator it = env.begin(); it != env.end(); ++it) { - evp[i] = new char[it->size() + 1]; - std::strcpy(evp[i], it->c_str()); + for(typename T::const_iterator it = array.begin(); it != array.end(); ++it) { + new_array[i] = strdup(it->c_str()); + if(!new_array) { + while(i--) + free(new_array[i]); + free(new_array); + return NULL; + } ++i; } - evp[env.size()] = NULL; - return evp; + new_array[array.size()] = NULL; + return new_array; } -void SysExec::doExec(StringVector const& args, StringList const& env) +void freeSysStringArray(char** array) +{ + if(!array) + return; + + for(int i=0; array[i] ; ++i) + free(array[i]); + + free(array); +} + +void SysExec::doExec(StringVector args, StringList env) { int pipefd[2]; if(pipe(pipefd) == -1) { @@ -98,21 +119,18 @@ void SysExec::doExec(StringVector const& args, StringList const& env) cLog.msg(Log::PRIO_WARNING) << "can't open stderr"; } - char** argv = new char*[args.size() + 2]; - argv[0] = new char[script_.size() + 1]; - std::strcpy(argv[0], script_.c_str()); - for(unsigned int i=0; iscript_ << "' returned " << WEXITSTATUS(s->return_code_); else if(WIFSIGNALED(s->return_code_)) cLog.msg(Log::PRIO_NOTICE) << "script '" << s->script_ << "' terminated after signal " << WTERMSIG(s->return_code_); - else - cLog.msg(Log::PRIO_ERROR) << "executing script '" << s->script_ << "': unknown error"; + else if(WIFSTOPPED(s->return_code_)) + cLog.msg(Log::PRIO_NOTICE) << "script '" << s->script_ << "' stopped after signal " << WSTOPSIG(s->return_code_); + else if(WIFCONTINUED(s->return_code_)) + cLog.msg(Log::PRIO_NOTICE) << "script '" << s->script_ << "' continued after SIGCONT"; delete(s); s = NULL; diff --git a/src/sysExec.cpp b/src/sysExec.cpp index 9fc6e10..a69349f 100644 --- a/src/sysExec.cpp +++ b/src/sysExec.cpp @@ -51,17 +51,17 @@ SysExec::SysExec(std::string const& script) : script_(script),closed_(false),ret doExec(StringVector(), StringList()); } -SysExec::SysExec(std::string const& script, StringVector const& args) : script_(script),closed_(false),return_code_(0) +SysExec::SysExec(std::string const& script, StringVector args) : script_(script),closed_(false),return_code_(0) { doExec(args, StringList()); } -SysExec::SysExec(std::string const& script, StringList const& env) : script_(script),closed_(false),return_code_(0) +SysExec::SysExec(std::string const& script, StringList env) : script_(script),closed_(false),return_code_(0) { doExec(StringVector(), env); } -SysExec::SysExec(std::string const& script, StringVector const& args, StringList const& env) : script_(script),closed_(false),return_code_(0) +SysExec::SysExec(std::string const& script, StringVector args, StringList env) : script_(script),closed_(false),return_code_(0) { doExec(args, env); } diff --git a/src/sysExec.h b/src/sysExec.h index b6efb45..b5400b9 100644 --- a/src/sysExec.h +++ b/src/sysExec.h @@ -44,9 +44,9 @@ class SysExec { public: SysExec(std::string const& script); - SysExec(std::string const& script, StringVector const& args); - SysExec(std::string const& script, StringList const& env); - SysExec(std::string const& script, StringVector const& args, StringList const& env); + SysExec(std::string const& script, StringVector args); + SysExec(std::string const& script, StringList env); + SysExec(std::string const& script, StringVector args, StringList env); ~SysExec(); int waitForScript(); @@ -55,7 +55,7 @@ class SysExec static void waitAndDestroy(SysExec*& s); private: - void doExec(StringVector const& args, StringList const& env); + void doExec(StringVector args, StringList env); std::string script_; bool closed_; diff --git a/src/win32/sysExec.hpp b/src/win32/sysExec.hpp index eba26f4..ed5be01 100644 --- a/src/win32/sysExec.hpp +++ b/src/win32/sysExec.hpp @@ -67,7 +67,7 @@ bool endsWith(std::string const& string, std::string const& suffix) { return string.find(suffix, string.size() - suffix.size()) != std::string::npos; } -void SysExec::doExec(StringVector const& args, StringList const& env) +void SysExec::doExec(StringVector args, StringList env) { std::vector arguments; -- cgit v1.2.3