#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <io.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
	int i,size,k=2,ftab,sect=3;

	char buf[512];
	char vbuf;
	int dev,fil_descr,off=0;

	fil_descr=open(argv[2],O_RDONLY);
	assert(fil_descr>0);
	read(fil_descr,buf,512);
	close(fil_descr);

	printf("Bootsector file: %s\n",argv[2]);
	

	dev=_open(argv[1],O_WRONLY|O_CREAT|O_BINARY|O_TRUNC|_S_IREAD|_S_IWRITE,0);
	assert(dev>0);
	write(dev,buf,512);


	ftab = _open("filetable",O_CREAT|O_WRONLY|O_BINARY|O_TRUNC|_S_IREAD|_S_IWRITE,0);
	sprintf(buf,"{");
	write(ftab,buf,1);
	
	/*for(i=3;i<argc;i++)*/
	for(i=3;i<argc;i++)
	{	
		

		off=off+ (k * 512);
		lseek(dev,off,SEEK_SET);
		fil_descr=_open(argv[i],O_RDONLY|O_BINARY,0);
		assert(fil_descr>0);
		size=0;		
		while((read(fil_descr,&vbuf,1))!=0) 
		{
			size++;
			write(dev,&vbuf,1);
		
		}
		k = 1;

		sprintf(buf,"%s-%d,",argv[i],sect);
		write(ftab,buf,strlen(buf));
		printf("Input file \'%s\' written at offset %d\n",argv[i],off);
		close(fil_descr);
	
		sect = sect + k;
	}
	sprintf(buf,"}");
	write(ftab,buf,1);

	lseek(ftab,0,SEEK_SET);
	read(ftab,buf,512);

	lseek(dev,512,SEEK_SET);
	write(dev,buf,512);	
	close (dev);
	close(ftab);	

        system("type filetable");
}
