#include "tools.h"
#include <string>
#include <process>
using namespace std;
static int writeRegEnv ( const string& name, const string& value,
int optcu = 0, int optrm = 0, int optkeep = 0 );
int writeRegEnv( const string& name, const string& value, int optcu )
{
return writeRegEnv ( name, value, optcu, 0, optkeep );
}
int deleteRegEnv( const string& name, int optcu )
{
return writeRegEnv ( name, "", optcu, 1, optkeep );
}
static int writeRegEnv ( const string& name, const string& value,
int optcu , int optrm , int optkeep )
{
string regName( basename( NULL ) + string(".reg") );
FILE *regFile = fopen( regName.c_str(), "w" );
if ( NULL == regFile )
return error( E_PS, "cannot open temporary file '%s' with regedit source:", regName.c_str() );
fprintf( regFile,
optcu ? "REGEDIT4\n"
"\n"
"[HKEY_CURRENT_USER\\Environment]\n"
: "REGEDIT4\n"
"\n"
"[HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment]\n"
);
fprintf( regFile, optrm ? "\"%s\"=-\n" : "\"%s\"=\"%s\"\n", name.c_str(), value.c_str() );
if ( EOF == fclose( regFile ) )
return error( E_PS, "cannot close temporary file '%s' with regedit source:", regName.c_str() );
int status = spawnlp ( P_WAIT, "regedit", "regedit", "/s", regName.c_str(), NULL );
if ( status < 0 ) return error( E_PS, "spawn of regedit failed:" );
else if ( status > 0 ) return error( E_PS, "regedit returned error" );
if ( !optkeep && remove( regName.c_str() ) )
return error( E_PS, "cannot remove temporary file '%s' with regedit source:", regName.c_str() );
return E_OK;
}