title: Read input from popen() into char * in C
body: <p>I have a FILE pointer that contains input from popen(). I want to put all of the input into a char *str, but I don't know how to do this (new to C-programming). </p>
  
  <pre><code>void save_cmd(int fd) {
    char buf[100];
    char *str;
    FILE *ls;
    if (NULL == (ls = popen("ls", "r"))) {
      perror("popen");
      exit(EXIT_FAILURE);
    }
  
    while (fgets(buf, sizeof(buf), ls) != NULL) {
      //Don't know what to do here....
    }
    pclose(ls);
  }
  </code></pre>
  
  <p>I guess I somehow have to concatenate inside the while loop, but how is this possible when I don't know the total size in advance (I want to save the whole result in char *str). If anyone have som pointers on how to do this I would be very grateful. </p>