summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVojtech Bocek <vbocek@gmail.com>2013-06-18 15:56:59 +0200
committerGerrit Code Review <gerrit@5.9.244.119>2013-07-03 20:28:00 +0200
commitc5754cf9355056ee5f2aebaa15a47b62c5cbeb42 (patch)
treefc6d6117b40b736d2bd1abc8bebd528866210371
parentAdd additional build variable checks to Android.mk files (diff)
downloadandroid_bootable_recovery-c5754cf9355056ee5f2aebaa15a47b62c5cbeb42.tar
android_bootable_recovery-c5754cf9355056ee5f2aebaa15a47b62c5cbeb42.tar.gz
android_bootable_recovery-c5754cf9355056ee5f2aebaa15a47b62c5cbeb42.tar.bz2
android_bootable_recovery-c5754cf9355056ee5f2aebaa15a47b62c5cbeb42.tar.lz
android_bootable_recovery-c5754cf9355056ee5f2aebaa15a47b62c5cbeb42.tar.xz
android_bootable_recovery-c5754cf9355056ee5f2aebaa15a47b62c5cbeb42.tar.zst
android_bootable_recovery-c5754cf9355056ee5f2aebaa15a47b62c5cbeb42.zip
-rw-r--r--twrp-functions.cpp55
1 files changed, 43 insertions, 12 deletions
diff --git a/twrp-functions.cpp b/twrp-functions.cpp
index d9014b364..463522664 100644
--- a/twrp-functions.cpp
+++ b/twrp-functions.cpp
@@ -36,21 +36,52 @@ extern "C" {
/* Execute a command */
int TWFunc::Exec_Cmd(string cmd, string &result) {
- 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;
+ 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);
result += buffer;
}
+
+ waitpid(pid, &ret, 0);
+ return WEXITSTATUS(ret);
}
- ret = __pclose(exec);
- return ret;
+ return -1;
}
// Returns "file.name" from a full /path/to/file.name