[go: up one dir, main page]

Menu

[r1]: / MakeFile.cpp  Maximize  Restore  History

Download this file

167 lines (139 with data), 5.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// 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;
}