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
|
#define HW_HARDDISK_PRIVATE
#include "hw_harddisk.h"
#include <stdio.h>
int HW_HARDDISK_Create(char* hdname,int sectors)
{
int _cpt;
StructSector _sector;
StructSector _bootsector;
FILE* _filestruct;
int _result;
for (_cpt = 0; _cpt < HW_HARDDISK_SECTOR_SIZE; _cpt++)
{
_sector.Bytes[_cpt] = HW_HARDDISK_DEFAULT_BYTE;
_bootsector.Bytes[_cpt] = HW_HARDDISK_DEFAULT_BYTE;
}
_bootsector.Bytes[3] = sectors & 0xff;
_bootsector.Bytes[2] = (sectors >> 8) & 0xff;
_bootsector.Bytes[1] = (sectors >> 16)& 0xff;
_bootsector.Bytes[0] = (sectors >> 24)& 0xff;
if ((_filestruct = fopen(hdname,"wb")) == NULL)
{
#ifdef __DEBUG__
printf("i/o error creating \"%s\"\n",hdname);
#endif
return 1;
}
fwrite(&_bootsector, sizeof(StructSector), 1, _filestruct);
sectors--;
while(sectors--)
{
_result = fwrite(&_sector, sizeof(StructSector), 1, _filestruct);
}
fclose(_filestruct);
return 0;
}
int HW_HARDDISK_ReadSector(char* hdname,StructSector* sector,int sectornum)
{
FILE* _filestruct;
int _result;
if ((_filestruct = fopen(hdname,"rb")) == NULL)
{
#ifdef __DEBUG__
printf("i/o error opening hard disk \"%s\" on sector %i for read\n",hdname,sectornum);
#endif
return 1;
}
if (fseek(_filestruct,sectornum * sizeof(StructSector),SEEK_SET))
{
#ifdef __DEBUG__
printf("i/o error seeking sector %i on harddisk \"%s\" for read\n",sectornum,hdname);
#endif
fclose(_filestruct);
return 1;
}
_result = fread(sector, sizeof(StructSector), 1, _filestruct);
if(!_result)
{
#ifdef __DEBUG__
printf("i/o error reading sector %i on harddisk \"%s\"\n",sectornum,hdname);
#endif
fclose(_filestruct);
return 1;
}
fclose(_filestruct);
return 0;
}
int HW_HARDDISK_WriteSector(char* hdname,StructSector* sector,int sectornum)
{
FILE* _filestruct;
int _result;
if ((_filestruct = fopen(hdname,"r+")) == NULL)
{
#ifdef __DEBUG__
printf("i/o error opening hard disk \"%s\" on sector %i for write\n",hdname,sectornum);
#endif
return 1;
}
if (fseek(_filestruct,sectornum * sizeof(StructSector),SEEK_SET))
{
#ifdef __DEBUG__
printf("i/o error seeking sector %i on harddisk \"%s\" for write\n",sectornum,hdname);
#endif
fclose(_filestruct);
return 1;
}
_result = fwrite(sector, sizeof(StructSector), 1, _filestruct);
if(!_result)
{
#ifdef __DEBUG__
printf("i/o error writing sector %i on harddisk \"%s\"\n",sectornum,hdname);
#endif
fclose(_filestruct);
return 1;
}
fclose(_filestruct);
return 0;
} |
Partager