The following code is producing the complete strace output. Why wouldn't it produce only the first 10 characters? How can I control the output of strace in my code?
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#define ARRL 250
void execute_ps(){
int count=0;
char p;
FILE *fp;
char cmd[ARRL];
//insert system command
sprintf(cmd, "strace echo hello");
//open channel to see output of command executed
fp = popen(cmd,"r");
if(fp==NULL){printf("popen err:%s\n",strerror(errno));exit(1);}
while((p=fgetc(fp))!=EOF){
printf("%c",p);
if (count==10){break;}
count++;
}
pclose(fp);
}
int main(int argc, char **argv)
{
execute_ps();
exit(0);
}
strace
prints to stderr
, while popen
only looks at stdout
. Removing the print statement completely will still cause output to be produced. Changing strace echo hello
to strace echo hello 2>&1
(see this) seems to fix the problem and cause only ten characters to be produced.