1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- #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\""
- << " to stdout and stderr." << std::endl;
- std::cout << "This ";
- std::cout.flush();
-
- std::cerr << "is a";
-
- std::cout << " 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;
-
- }
|