Code:
#include "windows.h"
#include "stdlib.h"
#include "stdio.h"
/* buffer to hold integers */
char comma[64] = {0};
int main()
{
int i;
unsigned char data[512];
DWORD dwBytesRead = 0;
HANDLE hFile = NULL;
hFile = CreateFile("\\\\.\\E:", /* drive to open */
GENERIC_READ, /* allows drive read */
FILE_SHARE_READ, /* shares read access */
NULL, /* Security Descriptor */
OPEN_EXISTING, /* declares creation */
FILE_ATTRIBUTE_READONLY, /* access is read only */
NULL); /* handle to template */
if(hFile != NULL)
{
/* read the boot */
if (!ReadFile(hFile, &data, 512, &dwBytesRead, NULL))
{
printf("Error in reading the specified drive!\n");
}
else
{
/* perform hexdump of boot information */
for (i = 0; i<100; i++)
printf("%d %X\n",i,data[i]);
/* turn the data into usable information for output*/
int sb = *((int *)(data + 11)) & 0xFFFF;
int cs = *((int *)(data + 13)) & 0xFF;
int ns = *((int *)(data + 32)) & 0xFFFFFFFF;
int nh = *((int *)(data + 26)) & 0xFFFF;
int spf = *((int *)(data + 22)) & 0xFFFF;
int rdcn = *((int *)(data + 44)) & 0xFFFFFFFF;
int cncrd = *((int *)(data + 50)) & 0xFFFFFFFF;
int tnob = sb * ns; /* where sb= sector bytes, ns= number of sectors*/
/* prints necessary output to screen */
printf("\n ---- Disk Information ----");
/* variables for inserting commas */
char *instring = comma;
char outstring[64];
char *ptr, *optr;
int ii, length, commas;
/* sprintf converts integer to string, holds data in (char data[]) */
sprintf(comma, "%d", sb);
printf("\nSector size (in bytes): %s", comma);
sprintf(comma, "%d", cs);
printf("\nCluster size (in sectors): %s",comma);
/*************************************************************/
sprintf(comma, "%d", ns);
/* move ptr to end of instring */
for ( ptr = instring; *ptr; ptr++ );
/*calculate offset with commas*/
length = ptr - instring;
commas = ( length - 1 ) / 3;
optr = outstring + length + commas;
/*copy instring into outstring backwards inserting commas */
*optr-- = *ptr--;
for ( ii = 1; ptr >= instring; ii++ )
{
*optr-- = *ptr--;
if ( ( ii % 3 ) == 0 )
*optr-- = ',';
}
/* show the result */
printf ( "\nTotal number of sectors: %s", outstring );
/*************************************************************/
sprintf(comma, "%d", nh);
printf("\nTotal number of heads: %s",comma);
sprintf(comma, "%d", spf);
printf("\nSectors per FAT: %s",comma);
sprintf(comma, "%d", rdcn);
printf("\nRoot directory cluster number: %s",comma);
sprintf(comma, "%d", cncrd);
printf("\nCluster number of the copy of the root directory: %s",comma);
/*********************************************************************/
sprintf(comma, "%d", tnob);
/* move ptr to end of instring */
for ( ptr = instring; *ptr; ptr++ );
/*calculate offset with commas*/
length = ptr - instring;
commas = ( length - 1 ) / 3;
optr = outstring + length + commas;
/*copy instring into outstring backwards inserting commas */
*optr-- = *ptr--;
for ( ii = 1; ptr >= instring; ii++ )
{
*optr-- = *ptr--;
if ( ( ii % 3 ) == 0 )
*optr-- = ',';
}
/* show the result */
printf ( "\nTotal disk size in bytes: %s", outstring );
/************************************************************/
/* closes the handle */
CloseHandle(hFile);
}
}
}