#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {

  if (argc < 2) {
    printf("Usage: ./popen_example.o some_command\n");
    printf("       runs the command, and prints the output to the screen.\n");
    exit(0);
  }

  FILE * p = popen(argv[1], "r"); // like fopen

  // treat p just like a file
  int ch;
  while ((ch = fgetc(p)) != EOF) {
    printf("%c", ch);
  }
  printf("\n");

  /*
  could have done instead
  char buf[99];
  int numBytes;
  while ((numBytes = fread(buf, 1, 100, p)) > 0) {
  }
   */

  pclose(p); // like fclose
  
  return 0;
}