summaryrefslogtreecommitdiff
path: root/test/testprog.cc
blob: c1350610433cfc9f4a316da994953940391559db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <fstream>
#include <string>

#if defined(_WIN32)
#define WINDOWS_LEAN_AND_MEAN
#include <windows.h>
#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 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;
}