| #include <iostream>
#include <fstream> // debug
#include <string>
#include <thread>
#include <chrono>
int
main(int argc, char *argv[]) {
  std::ofstream log("childProgram.log");
  int returnStatus = 25; // Successful return.
  // Output for read test.
  if (argc == 2) {
    // Write a single line and exit.
    if (std::string(argv[1]) == "write") {
      log << "Command is \"write\".  Returning \"This is a test\"" << std::endl;
      std::cout << "This is a test";
      std::cout.flush();
      if (!std::cout) {
        returnStatus = 10; // Unsuccessful return.
      }
      goto exit;
    }
    // Exit without writing anything.
    if (std::string(argv[1]) == "quit") {
      log << "Command is \"quit\".  Exiting" << std::endl;
      goto exit;
    }
    // Wait for standard input to close, and exit.
    if (std::string(argv[1]) == "checkStdinEOF") {
      log << "Command is \"checkStdinEOF\".  Checking that stdin is closed."
          << std::endl;
      std::string temp;
      std::cin >> temp;
      if (std::cin.eof()) {
        log << "stdin was closed." << std::endl;
        returnStatus = 15; // Successful return.
      } else {
        log << "stdin was not closed." << std::endl;
        returnStatus = 10; // Unsuccessful return.
      }
      goto exit;
    }
  }
  char ch;
  log << "Received \"";
  while (std::cin >> ch) {
    // Exit?
    if ('q' == ch) {
      log << (char) ch << "\"" << std::endl;
      break;
    }
    std::cout << (char) std::toupper(ch);
    std::cout.flush();
    log << (char) ch;
    log.flush();
  }
 exit:
  log.close();
  std::this_thread::sleep_for(std::chrono::milliseconds(50));
  return returnStatus;
}
 |