#include #include #include #if defined(_WIN32) #define WINDOWS_LEAN_AND_MEAN #include #undef max #else extern char **environ; // see 'man environ' #endif int main(int argc, const char* argv[]) { if(argc < 2) { return 0; } std::string cmd = argv[1]; if(cmd == "envdump") { if(argc < 3) { return 0; } std::ofstream ostrm(argv[2], std::ios::binary); if(ostrm.bad()) { std::cout << "Error: Could not write to " << argv[2] << "\n"; } #if defined(_WIN32) auto env_strings = GetEnvironmentStrings(); const char* ptr = env_strings; std::string env; while(true) { if(*ptr == '\0') { if(env.empty()) { // no more break; } ostrm << env << "\n"; env.clear(); ++ptr; continue; } env += *ptr; ++ptr; } FreeEnvironmentStrings(env_strings); #else for(auto current = environ; *current; ++current) { ostrm << (*current) << "\n"; } #endif } if(cmd == "retval") { if(argc < 3) { return 0; } return std::stoi(argv[2]); } if(cmd == "abort") { abort(); } if(cmd == "throw") { throw "ouch"; } return 0; }