*sample program to illustrate the FTP Client Calls *

include <Sample.h>

define PUT 1
define GET 2
define STATUS 3
define REMOVE 4
define ABORT 5
define QUERY 6
define EXIT 7
define QUEUED 0

*CopyFile is not defined in ctos.h*

int CopyFile plm far(int fh, char *file_name, int file_size, char *ppasswd, 
              int size_passwd, void *pbuf, int sbuf, unsigned char foverwrite);

qentry qent;
h_addr_type *h_add;
long local_host;
long remote_host;
int tran_num;
int ret_size;
unsigned char fflag=0;
char input_string[10];
char remotehost[64];
char localhost[64]={Local};
char localuser[64];
int  usernum;
char remoteuser[64];
char localpasswd[64];
char remotepasswd[64];
char menusel[2];
char filename[64];
char tmp_filename[64];
char destfile[64];
char copy_area[1024];
int fh;
int menu_sel;
char stat[500];
int erc;
char first_line[80]={1. Put File  2. Get File  3. Transaction Status 4. Remove Transaction};
char second_line[80]={5. Abort Transaction 6. Query Transactions 7. Exit};

display_menu()
{
erc=PutFrameChars(1,0,0,first_line,80);
erc=PutFrameChars(1,0,1,second_line,80);
}

setup_qentry(long l_host, long r_host, char *l_user, char *r_user,
             char *l_passwd, char *r_passwd, char action)
{

	memset(qent, ' ', CB_QENTRY);
	qent.rgbSrcHost=l_host;
	qent.rgbDestHost=r_host;
	qent.bFunctionCode=action;
	qent.cbSrcFile=strlen(filename);
	qent.cbSrcUser=strlen(l_user);
	qent.cbSrcPassword=strlen(l_passwd);
	qent.cbSrcAccount=0;
	qent.cbDestFile=strlen(filename);
	qent.cbDestUser=strlen(r_user);
	qent.cbDestPassword=strlen(r_passwd);
	qent.cbDestAccount=0;
	qent.cbOriginUser=strlen(l_user);
	strcpy(qent.rgbSrcFile, filename);
	strcpy(qent.rgbSrcUser, l_user);
	strcpy(qent.rgbSrcPassword, l_passwd);
	strcpy(qent.rgbDestFile, filename);
	strcpy(qent.rgbDestUser, r_user);
	strcpy(qent.rgbDestPassword, r_passwd);
	qent.bType='A';
	qent.bStructure='F';
	qent.bMode='S';
	qent.bFormat='N';
	strcpy(qent.rgbOriginUser, localuser);
	qent.cbStatus=1;
	qent.bStatus=QUEUED;
	qent.erc=0;
	qent.cbTranNum=0;
	qent.wTranNum=0;
    qent.bPriority=5;
	qent.startTime=0;
	qent.stopTime=0;
	qent.FileSize=0;
	qent.cbDataFile=strlen(tmp_filename);
	strcpy(qent.rgbDataFile, tmp_filename);
    qent.bTimeoutValue=0;
	EncryptPswd(qent.rgbSrcPassword, qent.rgbDestPassword);
}

make_file_copy()
{
erc=OpenFile(fh,filename,strlen(filename),0,0,0x6D72);
if (erc) {printf(File specified cannot be opened.\n); ErrorExit(erc);};
setmem(tmp_filename,64,0);
memcpy(tmp_filename,([Sys]<tcpip.ftp>tmp.bas.file),30);
printf(Creating temp file...);
erc=CopyFile(fh,tmp_filename,strlen(tmp_filename),0,0,copy_area,1024,0xff);
if (erc) {printf(unable to create tmp file.\n); ErrorExit(erc);};
printf(done.\n);
}

char *ftp_error (char e_code)
*	return FTP Server Status (erc) as string *
{
	switch(e_code){
		case 0: return 0;
		case 1: return Invalid local user name;
		case 2: return Local CTOS File cannot be opened;
		case 3: return Request for Open Connection not successful;
		case 4: return Request for Send Tcp not successful;
		case 5: return Request for Close Tcp not successful;
		case 6: return Request for Abort Tcp not successful;
		case 7: return Request for Receive Tcp not successful;
		case 8: return Request for Query Tcp not successful;
		case 9: return TCP Connection cannot be opened;
		case 10: return Send Failure (from local TCP Transport);
		case 11: return Receive Failure (from local TCP Transport);
		case 12: return Close Failure (from local TCP Transport);
		case 13: return Abort Failure (from local TCP Transport);
		case 14: return Query TCP Failure (from local TCP Transport);
		case 15: return File Status Failure (from local CTOS);
		case 16: return Transaction aborted;
		case 17: return Error occurred during Write;
		case 18: return From Remote: 421 Service not available;
		case 19: return From Remote: 425 Cannot open data connection;
		case 20: return From Remote: 426 Connection closed; transfer aborted;
		case 21: return From Remote: 450 File unavailable;
		case 22: return From Remote: 451 Local error in processing;
		case 23: return From Remote: 452 Insufficient storage space;
		case 24: return From Remote: 500 Command syntax error;
		case 25: return From Remote: 501 Syntax error in parameters;
		case 26: return From Remote: 502 Command not implemented;
		case 27: return From Remote: 503 Bad sequence of commands;
		case 28: return From Remote: 504 Invalid command for that parameter;
		case 29: return From Remote: 530 Not logged in;
		case 30: return From Remote: 532 Need account for storing files;
		case 31: return From Remote: 550 File Unavailable;
		case 32: return From Remote: 551 Page Type unknown;
		case 33: return From Remote: 552 Exceeded storage allocation;
		case 34: return From Remote: 553 File name not allowed;
		case 35: return From Remote: Unknown error;
		case 36: return Not BTOS system.  ISAM XFER not allowed;
		case 37: return Not successfully completed;
		case 38: return Error occurred during ISAM header adjustment;
		case 41: return Queue problem;
		case 42: return Transaction aborted;
		case 44: return Transaction timed out;
		case 45: return No resources available;
		default: return Unknown FTP return code ????;
	}
}

char *ftp_status (char s_code)
*	return FTP Status as string *
{
	switch(s_code){
		case QUEUED: return Queued;
		case ACTIVE: return Active;
		case SERVER: return Queued by remote server;
		case DONE: return Done;
		case ABORTED: return Aborted;
		default: return Unknown;
	}
}

main()
{
 erc=GetUserNumber(usernum);
 erc=GetWSUserName(usernum,localuser,64);
 if (erc) ErrorExit(erc);
 display_menu();
 while (1)
 {
   printf(Menu Selection: );
   gets(menusel);
   menu_sel=atoi(menusel);
   switch(menu_sel) 
   {
    case PUT:{
              h_add=gethostbyname(localhost);
              if (h_add)
                   local_host=ntohl(*((long *)h_add>h_addr));
               else {printf(Invalid local host specified.\n); break;}
              printf(Host Name: );
              gets(remotehost);
              h_add=gethostbyname(remotehost);
              if (h_add)
                   remote_host=ntohl(*((long *)h_add>h_addr));
               else {printf(Invalid remote host.\n); break;}
              printf(Username: );
              gets(remoteuser);
              printf(Password: );
              gets(remotepasswd);
              printf(File Name: );
              gets(filename);
              make_file_copy();
              setup_qentry(local_host,remote_host,localuser,
                  remoteuser,localpasswd,remotepasswd,COPY);
              erc=AddTransaction (qent,CB_QENTRY,tran_num);

              if (erc)
                 printf(CTOS error = u\n,erc);
              else  
              { 
               if (FTP_ERC) printf(FTP ERC = s\n,ftp_error(FTP_ERC)); 
               else
                 printf(Assigned transaction number = d \n,tran_num);
              }
              break;
             }
    case GET:{
              h_add=gethostbyname(localhost);
              if (h_add)
                   local_host=ntohl(*((long *)h_add>h_addr));
               else {printf(Invalid local host specified.\n); break;}
              printf(Host Name: );
              gets(remotehost);
              h_add=gethostbyname(remotehost);
              if (h_add)
                   remote_host=ntohl(*((long *)h_add>h_addr));
               else {printf(Invalid remote host.\n); break;}
              printf(Username: );
              gets(remoteuser);
              printf(Password: );
              gets(remotepasswd);
              printf(File Name: );
              gets(filename);
              setup_qentry(remote_host,local_host,remoteuser,
                  localuser,remotepasswd,localpasswd,COPY);
              erc=AddTransaction (qent,CB_QENTRY,tran_num);
              if (erc)
                 printf(CTOS error = u\n,erc);
              else  
              {
               if (FTP_ERC) printf(s\n,ftp_error(FTP_ERC));
               else
                 printf(Assigned transaction number = d \n,tran_num);
              }
              break;
             }
    case STATUS:
           {
             printf(Transaction Number: );
             gets(input_string);
             tran_num=atoi(input_string);     
             erc=QueryTransaction(tran_num,qent,CB_QENTRY,ret_size,2);
            if (erc)
                 printf(CTOS error = u\n,erc);
             else
               printf(Transaction d is s with error s\n,qent.wTranNum,
                       ftp_status(qent.bStatus),ftp_error(FTP_ERC));
             break;
           }
    case REMOVE:
           {
             printf(Transaction Number: );
             gets(input_string);
             tran_num=atoi(input_string);     
             erc=RemoveTransaction(tran_num);
             if (erc) 
                 printf(CTOS error = u\n,erc);
              else  
              {
               if (FTP_ERC) printf(s\n,ftp_error(FTP_ERC)); 
               else
                printf(Transaction d removed\n,tran_num);
              }

             break;
           }
    case ABORT:
           {
             printf(Transaction Number: );
             gets(input_string);
             tran_num=atoi(input_string);     
             erc=AbortTransaction(tran_num,localuser,strlen(localuser));
            if (erc)
                 printf(CTOS error = u\n,erc);
              else  
              {
               if (FTP_ERC) printf(s\n,ftp_error(FTP_ERC)); 
               else
                 printf(Transaction d aborted\n,tran_num);
              }
             break;
           }
    case QUERY:
           {
             erc=0;fflag=1;
             erc=ReadTransactionStatus(1,qent,CB_QENTRY,ret_size,2);
             if (erc) 
                 printf(CTOS error = u\n,erc);
             while(erc)
              {
               printf(Transaction d is s with error s\n,qent.wTranNum,
                       ftp_status(qent.bStatus),ftp_error(FTP_ERC));
               erc=ReadTransactionStatus(0,qent,CB_QENTRY,ret_size,2);
              }
             if (erc==1) printf(End of queue\n);
              else
                 printf(CTOS error = u\n,erc);

             break;
           }
    case EXIT: exit(0);
    default: printf(Invalid Selection. Try Again....\n);
   }
 }
}

