#include #ifdef DEBUG_MESSAGES #include // debug #endif #include #include #include #include int main(int argc, char *argv[]) { #ifdef DEBUG_MESSAGES std::ofstream log("childProgram.log"); #endif int returnStatus = 25; // Successful return. // Output for read test. if (argc == 2) { // Write a single line and exit. if (std::string(argv[1]) == "write") { #ifdef DEBUG_MESSAGES log << "Command is \"write\". Returning \"This is a test\"" << " to stdout and stderr." << std::endl; #endif 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") { #ifdef DEBUG_MESSAGES log << "Command is \"quit\". Exiting" << std::endl; #endif goto exit; } // Wait for standard input to close, and exit. if (std::string(argv[1]) == "checkStdinEOF") { #ifdef DEBUG_MESSAGES log << "Command is \"checkStdinEOF\". Checking that stdin is closed." << std::endl; #endif int const BufferSize = 512; std::vector readBuffer; readBuffer.resize(BufferSize, 0); std::cin.read(&(readBuffer[0]), BufferSize); if (!std::cin.eof()) { #ifdef DEBUG_MESSAGES log << "stdin was not closed." << std::endl; #endif returnStatus = 10; // Unsuccessful return. } else { #ifdef DEBUG_MESSAGES log << "stdin was closed." << std::endl; #endif returnStatus = 15; // Successful return. } goto exit; } } char ch; #ifdef DEBUG_MESSAGES log << "Received \""; #endif while (std::cin >> ch) { // Exit? if ('q' == ch) { #ifdef DEBUG_MESSAGES log << (char) ch << "\"" << std::endl; #endif break; } std::cout << (char) std::toupper(ch); std::cout.flush(); #ifdef DEBUG_MESSAGES log << (char) ch; log.flush(); #endif } exit: #ifdef DEBUG_MESSAGES log.close(); #endif std::this_thread::sleep_for(std::chrono::milliseconds(50)); return returnStatus; }