THE COMMON MODULE W/ ERROR HANDLING AND FILE HANDLING


/////////////////////////////////////////////////////////
#include <stdlib.h>
#include <stdio.h>
#include <string.h>


FILE *output,*output2,*fopen();

///////////////ERROR MESSAGES//////////////////////////
char *error[] =
{
"Could not open file!",
"Syntax error!",
"Failed to open communication!"
};

///////////////ERROR HANDLER//////////////////////////
int errhd(int errorcode)
{
	///////check which error/////////
	--errorcode;
	printf("%s\n",error[errorcode]);
return(0);
}

///////////////OPENFILE//////////////////////////////
int OpenFile(char *filename,char *mode)
{
unsigned register int result;	
	/////////open file,if error return message/
	if (!(output2=fopen(filename,mode)))
		{
		errhd(1);
		result=1;
		}
	else
		{
		result=0;
		}

	return(result);
}

//////////////CLOSE FILE////////////////////////////
int CloseFile()
{
fclose(output2);
return(0);
}


//////////////OPEN COMMUNICATION////////////////////
int OpenPort(char *mode)
{
unsigned register int result;	
	/////////open port,if error return message/
	if (!(output=fopen("com2:",mode)))
		{
		errhd(3);
		result=1;
		}
	else
		{
		result=0;
		}

	return(result);
}

/////////////CLOSE COMMUNICATION////////////////////
ClosePort()
{
fclose(output);
return(0);
}

 

THE MAIN MODULE


//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
/////////////////////    SEND/RCV V0.9 MODULE OF AMIConnect   ////////////////////////
/////////////////////////      BY HENRIK STOLPE        ///////////////////////////
//////////////////////////////////////////////////////////////////////////////////  
//////////////////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <common.c>
////////////////DEFINES & CONSTANTS////////////////////
#define MAXBYTES 100000
FILE *output,*output2,*fopen();

///////////////GET SIZE OF FILE////////////////////////
GetSize()
{
char bcount[MAXBYTES];
unsigned register int bytes;
bytes=fread(bcount, sizeof(char), MAXBYTES, output2);
rewind(output2);
return(bytes);
}                                    

/////////MASKOUT FILENAME//////////////////////////
char *MakeName(char *infilename)
{
char filename[64];
int c=92;
char *filenm=strrchr(infilename,c);
strcpy(filename,filenm+1);
return(filename);
}

/////////SEND FILENAME////////////////////////////
SendName(char *filename)
{
unsigned register int result;
unsigned register int len=strlen(filename);
result=fwrite(filename, sizeof(char),len,output);
rewind(output);
return(0);
}


//////////////START COMMUNICATION///////////////////
int PutData()
{
unsigned register int result,res;
unsigned register int size=GetSize();
char list[MAXBYTES];
printf("CONNECTION ESTABLISHED\n");
printf("BYTES TO TRANSFER: %d\n",GetSize());
printf("TRANSFER INITIATED\n");
res=fread(list, sizeof(char), size, output2);
result=fwrite(list, sizeof(char),size,output);
return(result);
}


////////////MAIN CONTROL /////////////////////////////
int main(int argc,char *argv[1])
{
int filestat,comstat=0;
char *filename;
filename=argv[1];
printf("AMIconnect SEND V0.9 By Henrik Stolpe\n");


//////////////////CHECK SYNTAX////////////////////////
if (!(argc==2))
	{
	errhd(2);
	}
///////////////////OPEN ALL///////////////////////////
    filestat=OpenFile(filename,"rb");
	comstat=OpenPort("wb");

////////////////////CONNECT///////////////////////////
if (filestat ==	0 && comstat == 0)
	{
	SendName(MakeName(filename));
	printf("%d BYTES TRANSFERED\n",PutData());
	}
///////////////////CLOSE ALL///////////////////////////
if (filestat==0)
	{
	CloseFile();
	}
if (comstat==0)
	{
	ClosePort();
	}

return(0);
}