#include <stdio.h>
#include <stdlib.h>
#include "./ubixfs.h"
struct blockAllocationTableEntry *BAT = 0x0;
int findFreeBlock(int cBlock,int size) {
int i = 0x0;
for (i=1;i<size;i++) {
if (BAT[i].attributes == 0x0) {
if (cBlock != -1) {
BAT[cBlock].nextBlock = i;
}
BAT[i].attributes = 1;
return(i);
}
}
return(0x0);
}
int main(int argc,char **argv) {
FILE *driveFd,*fileFd;
int startSector = 0x0,size = 0x0,structSize = 0x0,i = 0x0,x = 0x3,block = 0x0,batSize = 0x0;
int blockCount = 0x0,file = 0x0;
unsigned long counter = 0x0;
struct directoryEntry *dirEntry = 0x0;
struct partitionInformation *partInfo = 0x0;
if (argc <= 3) {
printf("Error:\nformat start size files....\n");
exit(1);
}
driveFd = fopen("/dev/fd0","wb");
startSector = atoi(argv[1]);
size = atoi(argv[2])*512;
structSize = sizeof(struct blockAllocationTableEntry);
structSize *= (size/4096);
batSize = (((structSize+511)/512)*512);
BAT = (struct blockAllocationTableEntry *)malloc(structSize);
dirEntry = (struct directoryEntry *)malloc(4096);
partInfo = (struct partitionInformation *)malloc(512);
partInfo->size = (size/512);
partInfo->startSector = (startSector+1);
partInfo->blockAllocationTable = (startSector+1);
partInfo->rootDirectory = ((startSector+1) + (batSize/512));
fseek(driveFd,(startSector * 512),0);
fwrite(partInfo,512,1,driveFd);
startSector++;
BAT[0].nextBlock = 0x0;
BAT[0].attributes = 1;
BAT[0].realSector = (startSector + (batSize/512) + 1);
for (i=1;i<(size/4096);i++) {
BAT[i].nextBlock = 0x0;
BAT[i].attributes = 0x0;
BAT[i].realSector = (startSector + (batSize/512) + (i*8));
BAT[i].reserved = 0x0;
}
file = 0;
while (x < argc) {
counter = 0x0;
blockCount = 0x0;
fileFd = fopen(argv[x],"rb");
block = findFreeBlock(-1,(size/4096));
dirEntry[file].startCluster = block;
rewind(driveFd);
//fseek(driveFd,((BAT[startSector].realSector * 512) + ((batSize/512)+(BAT[block].realSector*4096))),0);
fseek(driveFd,(BAT[block].realSector * 512),0);
while (!feof(fileFd)) {
if (4096 == (counter - (blockCount * 4096))) {
block = findFreeBlock(block,(size/4096));
rewind(driveFd);
//fseek(driveFd,((startSector * 512) + ((batSize/512)+(BAT[block].realSector*4096))),0);
fseek(driveFd,(BAT[block].realSector * 512),0);
blockCount++;
}
fputc(fgetc(fileFd),driveFd);
counter++;
}
fclose(fileFd);
dirEntry[file].size = counter;
sprintf(dirEntry[file].fileName,"%s",argv[x]);
x++;
file++;
}
rewind(driveFd);
fseek(driveFd,(long)((startSector * 512) + batSize),0);
fwrite(dirEntry,4096,1,driveFd);
rewind(driveFd);
fseek(driveFd,(startSector * 512),0);
if (fwrite(BAT,batSize,1,driveFd) >= 1) {
printf("Formatted!\n");
}
else {
printf("Error Formatting!!\n");
}
fclose(driveFd);
return(0);
}