From 29a0635b93738a3b7b322360437b548664b0437d Mon Sep 17 00:00:00 2001 From: Dees_Troy Date: Sat, 24 Aug 2013 12:06:47 +0000 Subject: Revert "Rewrite TWFunc::Exec_Cmd() to use pipe() instead of popen()" This reverts commit c5754cf9355056ee5f2aebaa15a47b62c5cbeb42. popen already uses fork and pipe and __popen works on more devices. --- twrp-functions.cpp | 55 ++++++++++++------------------------------------------ 1 file changed, 12 insertions(+), 43 deletions(-) diff --git a/twrp-functions.cpp b/twrp-functions.cpp index 463522664..d9014b364 100644 --- a/twrp-functions.cpp +++ b/twrp-functions.cpp @@ -36,52 +36,21 @@ extern "C" { /* Execute a command */ int TWFunc::Exec_Cmd(string cmd, string &result) { - int fd[2]; - int ret = -1; - if(pipe(fd) < 0) - return -1; - - pid_t pid = fork(); - if (pid < 0) - { - close(fd[0]); - close(fd[1]); - return -1; - } - - if(pid == 0) // child - { - close(fd[0]); - dup2(fd[1], 1); // send stdout to the pipe - dup2(fd[1], 2); // send stderr to the pipe - close(fd[1]); - - ret = system(cmd.c_str()); - if(ret != -1) - ret = WEXITSTATUS(ret); - else - LOGERR("Exec_Cmd: system() failed with -1 (%d)!\n", errno); - exit(ret); - } - else - { - close(fd[1]); - - int len; - char buffer[128]; - buffer[sizeof(buffer)-1] = 0; - while ((len = read(fd[0], buffer, sizeof(buffer)-1)) > 0) - { - buffer[len] = 0; - buffer[sizeof(buffer)-2] = '\n'; - LOGINFO("%s", buffer); + FILE* exec; + char buffer[130]; + int ret = 0; + exec = __popen(cmd.c_str(), "r"); + if (!exec) return -1; + while(!feof(exec)) { + memset(&buffer, 0, sizeof(buffer)); + if (fgets(buffer, 128, exec) != NULL) { + buffer[128] = '\n'; + buffer[129] = NULL; result += buffer; } - - waitpid(pid, &ret, 0); - return WEXITSTATUS(ret); } - return -1; + ret = __pclose(exec); + return ret; } // Returns "file.name" from a full /path/to/file.name -- cgit v1.2.3