#include <iostream>
#include <iomanip>
#include <cmath>
#include <io.h>

using namespace std;

void listDirectories(double& totalSizeBytes,string directoryPath,string filespec);

int main (int argC,char* argV[]){
	

	if (argC < 2)
	{
		cout << "This program only accepts command line arguments" << endl;
		exit (0);
	}
	
	string dirPath = argV[1];

	double totalSizeBytes = 0;

	listDirectories (totalSizeBytes, dirPath, "*.*");

	if (totalSizeBytes == 0)
		cout <<"No files found!" << endl;
	else
		cout << "Total Space occupied in the above directory: " << fixed <<setprecision(0)<<totalSizeBytes << " Bytes" << endl 
		<< totalSizeBytes/1024 << " KiloBytes" << endl;

	return 0;
}

void listDirectories (double& totalSizeBytes, string directoryPath, string filespec) {

	struct _finddata_t fileInfo;

	string fullPath = directoryPath;
	fullPath += "\\*.*";

	double hFindFile = _findfirst(fullPath.c_str(), &fileInfo);
	
	if (hFindFile == -1L)
	{
		cout << "No file found in the specified directory!" << endl;
		exit (1);
	}
	else 
	{
		fullPath = directoryPath;
		fullPath += "\\";
		fullPath += fileInfo.name;
		do 
			if (fileInfo.attrib & _A_SUBDIR )
				if (strcmp(fileInfo.name,".") != 0  && strcmp(fileInfo.name,"..") != 0)
				{
					fullPath = directoryPath;
					fullPath += "\\";
					fullPath += fileInfo.name;

					listDirectories (totalSizeBytes, fullPath, filespec);

					totalSizeBytes += fileInfo.size;
					
				}
		while (_findnext (hFindFile, &fileInfo) == 0);
		
	_findclose (hFindFile);
	}

	fullPath = directoryPath;
	fullPath += "\\";
	fullPath += filespec;
	hFindFile = _findfirst(fullPath.c_str(), &fileInfo);

	if( hFindFile == -1L)
	{
		cout << "No file found in specified directory" << endl;
		exit (1);
	}
	else
	{
		do
			if ( !(fileInfo.attrib & _A_SUBDIR))
				totalSizeBytes += fileInfo.size;
		while (_findnext (hFindFile, &fileInfo ) == 0);

		_findclose(hFindFile);
	}
}