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.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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\"" << std::endl;
  15. std::cout << "This is a test";
  16. std::cout.flush();
  17. if (!std::cout) {
  18. returnStatus = 10; // Unsuccessful return.
  19. }
  20. goto exit;
  21. }
  22. // Exit without writing anything.
  23. if (std::string(argv[1]) == "quit") {
  24. log << "Command is \"quit\". Exiting" << std::endl;
  25. goto exit;
  26. }
  27. // Wait for standard input to close, and exit.
  28. if (std::string(argv[1]) == "checkStdinEOF") {
  29. log << "Command is \"checkStdinEOF\". Checking that stdin is closed."
  30. << std::endl;
  31. std::string temp;
  32. std::cin >> temp;
  33. if (std::cin.eof()) {
  34. log << "stdin was closed." << std::endl;
  35. returnStatus = 15; // Successful return.
  36. } else {
  37. log << "stdin was not closed." << std::endl;
  38. returnStatus = 10; // Unsuccessful return.
  39. }
  40. goto exit;
  41. }
  42. }
  43. char ch;
  44. log << "Received \"";
  45. while (std::cin >> ch) {
  46. // Exit?
  47. if ('q' == ch) {
  48. log << (char) ch << "\"" << std::endl;
  49. break;
  50. }
  51. std::cout << (char) std::toupper(ch);
  52. std::cout.flush();
  53. log << (char) ch;
  54. log.flush();
  55. }
  56. exit:
  57. log.close();
  58. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  59. return returnStatus;
  60. }