#1: I do not know the size of output produced by stdout, given a command.
#2: I don't want to use char array like
char buffer[1024];
as it will result in memory shortage or wastage.
#3: If I use character pointer like
char *buffer;
I will have to allocate allocate memory for it like
buffer = (char *)malloc(1024 * sizeof(char));
#4: If I use getc() within while loop like
char *buf, c;
int i=0, j=1;
int pipefd[2];
int stdout_bk;
code[message_read] = '\0';
stdout_bk = dup(fileno(stdout));
pipe(pipefd);
dup2(pipefd[1], STDOUT_FILENO);
system(code);
close(pipefd[1]);
dup2(stdout_bk, STDOUT_FILENO);
buf = (char*)malloc(sizeof(char));
while(c!=End_Of_File) // What should I replace End_Of_File with?
{
c = getc(pipefd[0]);
buf = (char*)realloc(buf, j * sizeof(char));
buf[i] = c;
i++;
j++;
}
I do not know what is the End_Of_file for the stdout data.
P.S.: the program runs a command, e.g., system("setarch x86_64 -R dd if=/proc/self/maps | grep bin/dd") or system("ls -al") and I need to get the STDOUT. For that I have used dup2 and need to pipe the output to a buffer.
NOTE: the output can be of variable length.