/*Copyright (C) 2006 Matteo Lucarelli
* 
*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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
******************************************************************************/

// deldir is a recursive function to delete files and not-empty-directories

#include <stdlib.h>
#include <windows.h>

void deldir(const char* dir)
{
	char fpath[MAX_PATH];
	char fname[MAX_PATH];

	HANDLE hList;
	TCHAR szDir[MAX_PATH];
	WIN32_FIND_DATA FileData;
	snprintf(szDir,MAX_PATH, "%s\\*", dir);
	hList = FindFirstFile(szDir, &FileData);
	if (hList == INVALID_HANDLE_VALUE){
		/******************************************
		 * ADD ERROR HANDLING HERE
		 ******************************************/
		return;
	}
	do{
		strncpy(fname,FileData.cFileName,MAX_PATH);
		
		if (!strcmp(fname,".")) continue;
		if (!strcmp(fname,"..")) continue;
		
		snprintf(fpath,MAX_PATH,"%s\\%s",dir,fname);
		if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) deldir(fpath);
		else DeleteFile(fpath);
		
	} while (FindNextFile(hList, &FileData));
	
	FindClose(hList);
	RemoveDirectory(dir);
	return;
}