// MakeFile.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "makefile.h"
#include <iostream>
#include <io.h>
#include "windows.h"
#include "fcntl.h"
//** 2010-05-19 22-23-23 Rett Pop [+]: Program version
#define PROGRAM_NAME "MKEF"
#define VER_MAJOR 1
#define VER_MINOR 0
using namespace std;
//** 2010-05-19 22-23-13 Rett Pop [+]: Global variables
__int64 giFSize = 0;
int giMult = MULTIPL_M;
_TCHAR* cpDestPath = NULL;
_TCHAR* cpFName = NULL;
int _tmain(int argc, _TCHAR* argv[])
{
//if ( 1 == argc )
{
printf("\n%s ver: %i.%i\n\n", PROGRAM_NAME, VER_MAJOR, VER_MINOR);
cout << "Make empty file.\n\nUsage: mkfe [-n <file name>] [-p <file path>] [-s <file size>]\n\t[-u <units>] [-h|/h|-?]\n\n";
cout << " -n <file name>\tFile of new file.\n\tBy default - unique name for gived directory\n";
cout << " -p <file path>\tDestination path where file will be created.\n\tBy default - current directory\n";
cout << " -s <file size>\tSize of new file.\n\tBy default - whole empty space on <file path>\n";
cout << " -u <units>\tUnits of gived size. Understood: B, K, M, G.\n\tBy default - M(egabytes)\n";
cout << " -h|/? \tShow this help and exit\n";
// return ERR_NOPARAMS;
}
//** 2010-05-20 01-18-57 Rett Pop [+]: If no params gived, ask to start
if ( 1 == argc ) {
cout << "\n It will create file which fill all free space on current drive!\nAre you sure?: (case sensitive Y/N)";
if ( 'Y' != getchar() )
return ERR_OK;
}
//** 2010-05-19 22-25-50 Rett Pop [+]: Process command line params
try
{
for ( int i=1; i<argc; i++ )
{
if ( ( 0 == wcscmp(argv[i] ,_T("-h")) ) ||
(0 == wcscmp( argv[i] ,_T("/?") )) ||
(0 == wcscmp( argv[i] ,_T("-?") )) )
{
return ERR_OK;
}
if ( (0 == wcscmp( argv[i] ,_T("-h") )) && (i<argc) ){
return ERR_OK;
}
if ( (0 == wcscmp( argv[i] ,_T("-n") )) && (i<argc) ){
cpFName = argv[((++i)++)]; // get next and increase to next+next
}
if ( i >= argc ) break; //
if ( (0 == wcscmp( argv[i] ,_T("-p") )) && (i<argc) ){
cpDestPath = argv[((++i)++)]; // get next and increase to next
}
if ( i >= argc ) break; //
if ( (0 == wcscmp( argv[i] ,_T("-s") )) && (i<argc) ){
giFSize = _wtoi( argv[((++i)++)] ); // get next and increase to next
}
if ( i >= argc ) break; //
if ( (0 == wcscmp( argv[i] ,_T("-u") )) && (i<argc) ){
_TCHAR* cUn = argv[((++i)++)];
if ( 0 == wcscmp( cUn, _T("B")) ) giMult = MULTIPL_B;
if ( 0 == wcscmp( cUn, _T("K")) ) giMult = MULTIPL_K;
if ( 0 == wcscmp( cUn, _T("M")) ) giMult = MULTIPL_M;
if ( 0 == wcscmp( cUn, _T("G")) ) giMult = MULTIPL_G;
}
}
}
catch(...){
cerr << "Error processing command line parameters\n";
return ERR_BADRAMS;
}
//** 2010-05-19 22-39-17 Rett Pop [+]: If destination path not gived, takes current directory
if ( NULL == cpDestPath ){
cpDestPath = new _TCHAR[_MAX_PATH];
if ( NULL == _wgetcwd(cpDestPath, _MAX_PATH) ){
cerr << "Error determining currend working directory";
return ERR_PATH;
}
} else { // else try to change current directory to given
if ( 0 != _wchdir( cpDestPath ) ) {
cerr << "Error changing working directory";
return ERR_PATH;
}
}
//** 2010-05-19 22-37-51 Rett Pop [+]: Calculate target size, if given
if ( giFSize > 0 ){
giFSize *= giMult;
}
else
{
//** 2010-05-19 23-12-03 Rett Pop [+]: Try to determine free disk space
__int64 i64FreeBytesToCaller, i64TotalBytes, i64FreeBytes;
if ( 0 == GetDiskFreeSpaceEx( cpDestPath,
(PULARGE_INTEGER) &i64FreeBytesToCaller,
(PULARGE_INTEGER) &i64TotalBytes,
(PULARGE_INTEGER) &giFSize)
) {
cerr << "Error determining free disk space\n";
return ERR_DISK;
}
}
//** 2010-05-19 22-47-40 Rett Pop [+]: If file name not given, take unique temp-file name
if ( NULL == cpFName )
{
_TCHAR* cpT = _T("mkfXXXXX");
cpFName = new _TCHAR[MAX_PATH];
if ( 0 == GetTempFileName(cpDestPath, _T("mkf"), 0, cpFName ) ) {
cerr << "Error generating unique filename\n";
return ERR_FILENAME;
}
}
//** 2010-05-19 23-07-10 Rett Pop [+]: try to create file
int fh = _wopen(cpFName, _O_CREAT | _O_WRONLY );
//** 2010-05-19 23-10-03 Rett Pop [+]: If error on creating file occurs - exit
if ( -1 == fh ) {
cerr << "Error creating file " << cpFName << endl;
return ERR_FILE;
}
wprintf(_T("Start to create file %s on path %s with size %i bytes\n"), cpFName, cpDestPath, giFSize);
cout << "Please, be patient. It can takes a few minutes.\n";
//_chsize(fh, giFSize);
DWORD iTicks = GetTickCount();
char* pEB = new char[4*MULTIPL_K];
memset(pEB, 0x0, 4*MULTIPL_K);
for ( __int64 i = 0; i <= giFSize; i+=(4*MULTIPL_K) )
{
_write(fh, pEB, 4*MULTIPL_K);
}
_close (fh);
cout << "File succefully created. It took " << ((GetTickCount() - iTicks)/1000) << " seconds.\n";
delete[] pEB;
delete[] cpFName;
return 0;
}