You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

childProgram.cpp 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include <iostream>
  2. #include <fstream> // debug
  3. #include <string>
  4. #include <thread>
  5. #include <chrono>
  6. int
  7. main(int argc, char *argv[]) {
  8. std::ofstream log("childProgram.log");
  9. int returnStatus = 25; // Successful return.
  10. // Output for read test.
  11. if (argc == 2) {
  12. // Write a single line and exit.
  13. if (std::string(argv[1]) == "write") {
  14. log << "Command is \"write\". Returning \"This is a test\""
  15. << " to stdout and stderr." << std::endl;
  16. std::cout << "This ";
  17. std::cout.flush();
  18. std::cerr << "is a";
  19. std::cout << " test";
  20. std::cout.flush();
  21. if (!std::cout) {
  22. returnStatus = 10; // Unsuccessful return.
  23. }
  24. goto exit;
  25. }
  26. // Exit without writing anything.
  27. if (std::string(argv[1]) == "quit") {
  28. log << "Command is \"quit\". Exiting" << std::endl;
  29. goto exit;
  30. }
  31. // Wait for standard input to close, and exit.
  32. if (std::string(argv[1]) == "checkStdinEOF") {
  33. log << "Command is \"checkStdinEOF\". Checking that stdin is closed."
  34. << std::endl;
  35. std::string temp;
  36. std::cin >> temp;
  37. if (std::cin.eof()) {
  38. log << "stdin was closed." << std::endl;
  39. returnStatus = 15; // Successful return.
  40. } else {
  41. log << "stdin was not closed." << std::endl;
  42. returnStatus = 10; // Unsuccessful return.
  43. }
  44. goto exit;
  45. }
  46. }
  47. char ch;
  48. log << "Received \"";
  49. while (std::cin >> ch) {
  50. // Exit?
  51. if ('q' == ch) {
  52. log << (char) ch << "\"" << std::endl;
  53. break;
  54. }
  55. std::cout << (char) std::toupper(ch);
  56. std::cout.flush();
  57. log << (char) ch;
  58. log.flush();
  59. }
  60. exit:
  61. log.close();
  62. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  63. return returnStatus;
  64. }