Skip to content
This repository was archived by the owner on Jun 10, 2021. It is now read-only.

Commit 7209cb9

Browse files
committed
change to return codes rather than print statements
change the output print statements to return codes to allow for easier checking from other programs/scripts
1 parent bacab62 commit 7209cb9

4 files changed

Lines changed: 35 additions & 47 deletions

File tree

info.rc

100644100755
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
1 VERSIONINFO
2-
FILEVERSION 0,0,0,2
3-
PRODUCTVERSION 0,0,0,2
2+
FILEVERSION 0,0,0,3
3+
PRODUCTVERSION 0,0,0,3
44
BEGIN
55
BLOCK "StringFileInfo"
66
BEGIN
77
BLOCK "080904E4"
88
BEGIN
99
VALUE "FileDescription", "Command line version of Win32DiskImager"
10-
VALUE "FileVersion", "0.0.0.2"
10+
VALUE "FileVersion", "0.0.0.3"
1111
VALUE "InternalName", "CommandLineDiskImager"
1212
VALUE "OriginalFilename", "CommandLineDiskImager.exe"
1313
VALUE "ProductName", "CommandLineDiskImager"
14-
VALUE "ProductVersion", "0.0.0.2"
14+
VALUE "ProductVersion", "0.0.0.3"
1515
END
1616
END
1717

info.res

100644100755
0 Bytes
Binary file not shown.

main.cpp

100644100755
Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,11 @@ int main(int argc, char *argv[])
3939
{
4040
if ( argc != 3 )
4141
{
42-
printf("error - not enough arguments");
43-
return 1;
42+
return 1; //not enough arguments
4443
}
4544

4645
const char ltr = argv[2][0];
4746
const char *imagefile = argv[1];
48-
printf("writing file %s to device %c:", imagefile, ltr);
4947

5048
status = STATUS_IDLE;
5149
hVolume = INVALID_HANDLE_VALUE;
@@ -58,45 +56,40 @@ int main(int argc, char *argv[])
5856
{
5957
if(INVALID_FILE_ATTRIBUTES == GetFileAttributes(imagefile) && GetLastError()==ERROR_FILE_NOT_FOUND)
6058
{
61-
printf("\nerror - file not found");
62-
return 1;
59+
return 2; // .img not found
6360
}
6461
else
6562
{
6663
status = STATUS_WRITING;
6764

6865
unsigned long long i, availablesectors, numsectors;
6966
int volumeID = ltr - 'A';
70-
int deviceID = getDriveNumberFromLetter(ltr);
67+
int deviceID = getDriveNumberFromLetter(ltr);
7168
if( deviceID == -1 )
7269
{
73-
printf("\nerror - device not found");
74-
return 1;
70+
return 3; // device not found
7571
}
7672

7773
hVolume = getHandleOnVolume(volumeID, GENERIC_WRITE);
7874
if (hVolume == INVALID_HANDLE_VALUE)
7975
{
8076
status = STATUS_IDLE;
81-
printf("\nerror - invalid handle value for volume");
82-
return 1;
77+
return 4; // invalid handle value for volume
8378
}
8479
if (!getLockOnVolume(hVolume))
8580
{
8681
CloseHandle(hVolume);
8782
status = STATUS_IDLE;
8883
hVolume = INVALID_HANDLE_VALUE;
89-
printf("\nerror - can't get a lock on volume");
90-
return 1;
84+
return 5; // can't get lock on volume
9185
}
9286
if (!unmountVolume(hVolume))
9387
{
9488
removeLockOnVolume(hVolume);
9589
CloseHandle(hVolume);
9690
status = STATUS_IDLE;
9791
hVolume = INVALID_HANDLE_VALUE;
98-
printf("\nerror - can't unmount volume");
99-
return 1;
92+
return 6; // can't unmount volume
10093
}
10194
hFile = getHandleOnFile(imagefile, GENERIC_READ);
10295
if (hFile == INVALID_HANDLE_VALUE)
@@ -105,8 +98,7 @@ int main(int argc, char *argv[])
10598
CloseHandle(hVolume);
10699
status = STATUS_IDLE;
107100
hVolume = INVALID_HANDLE_VALUE;
108-
printf("\nerror - invalid handle value for file");
109-
return 1;
101+
return 7; // invalid handle value for file
110102
}
111103
hRawDisk = getHandleOnDevice(deviceID, GENERIC_WRITE);
112104
if (hRawDisk == INVALID_HANDLE_VALUE)
@@ -117,15 +109,13 @@ int main(int argc, char *argv[])
117109
status = STATUS_IDLE;
118110
hVolume = INVALID_HANDLE_VALUE;
119111
hFile = INVALID_HANDLE_VALUE;
120-
printf("\nerror - invalid handle value for disk");
121-
return 1;
112+
return 8; // invalid handle value for disk
122113
}
123114
availablesectors = getNumberOfSectors(hRawDisk, &sectorsize);
124115
numsectors = getFileSizeInSectors(hFile, sectorsize);
125116
if (numsectors > availablesectors)
126117
{
127-
printf("\nerror - not enough space");
128-
return 1;
118+
return 9; // not enough space on volume
129119
}
130120

131121
char total_string[32];
@@ -154,8 +144,7 @@ int main(int argc, char *argv[])
154144
hRawDisk = INVALID_HANDLE_VALUE;
155145
hFile = INVALID_HANDLE_VALUE;
156146
hVolume = INVALID_HANDLE_VALUE;
157-
printf("\nerror - data is null");
158-
return 1;
147+
return 10; // sector data is null
159148
}
160149
if (!writeSectorDataToHandle(hRawDisk, sectorData, i, (numsectors - i >= 1024ul) ? 1024ul:(numsectors - i), sectorsize))
161150
{
@@ -169,8 +158,7 @@ int main(int argc, char *argv[])
169158
hRawDisk = INVALID_HANDLE_VALUE;
170159
hFile = INVALID_HANDLE_VALUE;
171160
hVolume = INVALID_HANDLE_VALUE;
172-
printf("\nerror - error whilst writing");
173-
return 1;
161+
return 11; // error whilst writing
174162
}
175163
delete[] sectorData;
176164
sectorData = NULL;
@@ -183,16 +171,14 @@ int main(int argc, char *argv[])
183171
hFile = INVALID_HANDLE_VALUE;
184172
hVolume = INVALID_HANDLE_VALUE;
185173
}
186-
printf("\ninfo - write successful");
187-
return 0;
174+
return 0; // success
188175
}
189176
else
190177
{
191-
printf("\nerror - no image file specified");
192-
return 1;
178+
return 1; // not enough arguments
193179
}
194180
status = STATUS_IDLE;
195-
return 1;
181+
return 11; // error whilst writing
196182
}
197183

198184
int getDriveNumberFromLetter(char lookingforname)
@@ -230,7 +216,7 @@ HANDLE getHandleOnFile(const char *filelocation, DWORD access)
230216
hFile = CreateFileA(filelocation, access, (access == GENERIC_READ) ? FILE_SHARE_READ : 0, NULL, (access == GENERIC_READ) ? OPEN_EXISTING:CREATE_ALWAYS, 0, NULL);
231217
if (hFile == INVALID_HANDLE_VALUE)
232218
{
233-
printf("\nerror - not able to get handle on image file");
219+
//printf("error - not able to get handle on image file");
234220
}
235221
return hFile;
236222
}
@@ -244,7 +230,7 @@ HANDLE getHandleOnDevice(int device, DWORD access)
244230
hDevice = CreateFile(devicename, access, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
245231
if (hDevice == INVALID_HANDLE_VALUE)
246232
{
247-
printf("\nerror - not able to get handle on device");
233+
//printf("error - not able to get handle on device");
248234
}
249235
return hDevice;
250236
}
@@ -258,7 +244,7 @@ HANDLE getHandleOnVolume(int volume, DWORD access)
258244
hVolume = CreateFile(volumename, access, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
259245
if (hVolume == INVALID_HANDLE_VALUE)
260246
{
261-
printf("\nerror - not able to get handle on volume");
247+
//printf("error - not able to get handle on volume");
262248
}
263249
return hVolume;
264250
}
@@ -270,7 +256,7 @@ bool getLockOnVolume(HANDLE handle)
270256
bResult = DeviceIoControl(handle, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &bytesreturned, NULL);
271257
if (!bResult)
272258
{
273-
printf("\nerror - not able to lock volume");
259+
//printf("error - not able to lock volume");
274260
}
275261
return (bResult);
276262
}
@@ -282,7 +268,7 @@ bool removeLockOnVolume(HANDLE handle)
282268
bResult = DeviceIoControl(handle, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &junk, NULL);
283269
if (!bResult)
284270
{
285-
printf("\nerror - not able to unlock volume");
271+
//printf("error - not able to unlock volume");
286272
}
287273
return (bResult);
288274
}
@@ -294,7 +280,7 @@ bool unmountVolume(HANDLE handle)
294280
bResult = DeviceIoControl(handle, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0, &junk, NULL);
295281
if (!bResult)
296282
{
297-
printf("\nerror - not able to dismount volume");
283+
//printf("error - not able to dismount volume");
298284
}
299285
return (bResult);
300286
}
@@ -308,7 +294,7 @@ char *readSectorDataFromHandle(HANDLE handle, unsigned long long startsector, un
308294
SetFilePointer(handle, li.LowPart, &li.HighPart, FILE_BEGIN);
309295
if (!ReadFile(handle, data, sectorsize * numsectors, &bytesread, NULL))
310296
{
311-
printf("\nerror - not able to read data from handle");
297+
//printf("error - not able to read data from handle");
312298
delete[] data;
313299
data = NULL;
314300
}
@@ -329,7 +315,7 @@ bool writeSectorDataToHandle(HANDLE handle, char *data, unsigned long long start
329315
bResult = WriteFile(handle, data, sectorsize * numsectors, &byteswritten, NULL);
330316
if (!bResult)
331317
{
332-
printf("\nerror - not able to write data from handle");
318+
//printf("error - not able to write data from handle");
333319
}
334320
return (bResult);
335321
}
@@ -342,8 +328,7 @@ unsigned long long getNumberOfSectors(HANDLE handle, unsigned long long *sectors
342328
bResult = DeviceIoControl(handle, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, NULL, 0, &diskgeometry, sizeof(diskgeometry), &junk, NULL);
343329
if (!bResult)
344330
{
345-
printf("\nerror - not able to get number of sectors");
346-
return 0;
331+
return 12;
347332
}
348333
if (sectorsize != NULL)
349334
{
@@ -358,7 +343,7 @@ unsigned long long getFileSizeInSectors(HANDLE handle, unsigned long long sector
358343
LARGE_INTEGER filesize;
359344
if(GetFileSizeEx(handle, &filesize) == 0)
360345
{
361-
printf("\nerror - not able to get image file size");
346+
//printf("error - not able to get image file size");
362347
retVal = 0;
363348
}
364349
else
@@ -393,15 +378,17 @@ BOOL GetDisksProperty(HANDLE hDevice, PSTORAGE_DEVICE_DESCRIPTOR pDevDesc,
393378
if (!bResult)
394379
{
395380
retVal = false;
396-
printf("\nerror - not able to get device number, is something accessing the device?");
381+
//printf("error - not able to get device number, is something accessing the device?");
382+
//setbuf(stdout, NULL);
397383
}
398384
}
399385
else
400386
{
401387
if (DeviceIoControl(hDevice, IOCTL_STORAGE_CHECK_VERIFY2, NULL, 0, NULL, 0, &cbBytesReturned,
402388
(LPOVERLAPPED) NULL))
403389
{
404-
printf("\nerror - not able to get device properties, is something accessing the device?");
390+
//printf("error - not able to get device properties, is something accessing the device?");
391+
//setbuf(stdout, NULL);
405392
}
406393
retVal = false;
407394
}
@@ -466,7 +453,8 @@ bool checkDriveType(char *name, ULONG *pid)
466453
hDevice = CreateFile(nameNoSlash, FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
467454
if (hDevice == INVALID_HANDLE_VALUE)
468455
{
469-
printf("\nerror - not able to get handle on device");
456+
//printf("error - not able to get handle on device");
457+
//setbuf(stdout, NULL);
470458
}
471459
else
472460
{

main.h

100644100755
File mode changed.

0 commit comments

Comments
 (0)