#include #include #include int main(int argc, char **argv) { if (argc < 2) return 1; FILE *fd; fd = popen(argv[1], "r"); if (!fd) return 1; char buffer[256]; size_t chread; /* String to store entire command contents in */ size_t comalloc = 256; size_t comlen = 0; char *comout = malloc(comalloc); /* Use fread so binary data is dealt with correctly */ while ((chread = fread(buffer, 1, sizeof(buffer), fd)) != 0) { if (comlen + chread >= comalloc) { comalloc *= 2; comout = realloc(comout, comalloc); } memmove(comout + comlen, buffer, chread); comlen += chread; } /* We can now work with the output as we please. Just print * out to confirm output is as expected */ fwrite(comout, 1, comlen, stdout); free(comout); pclose(fd); return 0; }