#include #include #include #include char* getoutput(char* command){ int pipefd[2]; pipe(pipefd); pid_t pid = fork(); if(pid){ // We're the parent size_t bufsize = 1000; size_t readlen = 0; char *buf = (char*)malloc(bufsize); close(pipefd[1]); ssize_t result; while(result = read(pipefd[0], buf + readlen, bufsize - readlen)){ bufsize += 1000; readlen += result; buf = (char*)realloc(buf, bufsize); } close(pipefd[0]); return buf; } else { // We're the child close(pipefd[0]); dup2(pipefd[1], 1); execl("/bin/sh", "sh", "-c", command, 0); close(pipefd[1]); printf("Warning: %s did not run correctly\n", command); } } int main(){ char* cmdoutput = getoutput("cat get_apache_directory.py"); // char* cmdoutput = getoutput("ls"); printf("The command output: %s\n", cmdoutput); free(cmdoutput); // Now we do something else unrelated for the next 1000 years return 0; }