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 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 21 deletions(-) (limited to 'src/posix') 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; -- cgit v1.2.3