/* RemUReg  - remove registry keys from HKEY_CURRENT_USER
 * 
 * Copyright (c) 2000, Matthew Lavy
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include<windows.h>
#include<stdio.h>
#define WIN32_LEAN_AND_MEAN

int usage();
void killEntry(char* key);
void killSubkey(HKEY key, char* subkey);

int main(int argc, char* argv[])
{
	if(argc != 2)
	{
		return usage();
	}
	killEntry(argv[1]);
	return 0;
}

int usage()
{
	fprintf(stderr, "RemUReg: A utility to remove a registry key from ");
    fprintf(stderr, "a user's registry hive\n");
	fprintf(stderr, "Copyright (c) 2000, Matthew Lavy\n");
	fprintf(stderr, "Licensed under the terms of the ");
    fprintf(stderr, "GNU General Public License (v.2)\n");
	fprintf(stderr, "\n");
	fprintf(stderr, "Usage: remureg <KEYNAME>\n");
	fprintf(stderr, "<KEYNAME> should not contain the HKCU prefix, ");
    fprintf(stderr, "and should be enclosed in quotes\n");
	return 0;
}


void killEntry(char* key)
{
	fprintf(stderr, "Removing HKEY_CURRENT_USER/%s...\n", key);
	killSubkey(HKEY_CURRENT_USER, key);
	fprintf(stderr, "finished.\n");
}

void killSubkey(HKEY key, char* subkey)
{
	//first open the subkey
	HKEY newHandle;
	char* returnedSubName;
	unsigned long realBufferSize;
	unsigned long returnBufferSize;
	FILETIME lastWrite;
	if( RegOpenKeyEx(
            key,
            subkey,
            NULL,
            KEY_ALL_ACCESS,
            &newHandle
        ) != ERROR_SUCCESS
    ){
		fprintf(stderr, "Oops. Could not open %s.\n", subkey);
	} else {

		//discover buffersize needed for longest string and malloc
		if( RegQueryInfoKey(
				newHandle,
				NULL, NULL, NULL, NULL, 
				&realBufferSize, 
				NULL, NULL, NULL, NULL, NULL, NULL
				) != ERROR_SUCCESS 
		){
			fprintf(
                stderr,
                "\nCould not determine size for keyname buffer. Skipping %s\n",
                subkey
            );
		} else {

			//malloc buffer
			if( (returnedSubName = (char*)malloc(realBufferSize + 1)) == 0 ){
				fprintf(stderr, "\nEek - can't allocate memory. Aborting...\n");
				RegCloseKey(newHandle);
				exit(1);
			}

			//enumerate and delete subkeys
			returnBufferSize = realBufferSize;
			while( RegEnumKeyEx(
					newHandle, //key to enumerate
					0, //index of subkey (in our case always 0)
					returnedSubName, //buffer for subkey name
					&returnBufferSize, //sz of above - modified by each call
					NULL,
					NULL,
					NULL,
					&lastWrite)
				!= ERROR_NO_MORE_ITEMS )
			{
				//copy the real buffer siz
				returnBufferSize = realBufferSize;

				//recursive call to kill the subkey
				killSubkey(newHandle, returnedSubName);
			}
			free(returnedSubName);
		}
		// having killed the subkeys, close the key, and delete it
		RegCloseKey(newHandle);
		RegDeleteKey(key, subkey);
	}
}

