Article 12651 of alt.sources:
Newsgroups: alt.sources
Path: nntpd.lkg.dec.com!pa.dec.com!news1.digital.com!nntp-hub2.barrnet.net!newsfeed.internetmci.com!news.mathworks.com!tank.news.pipex.net!pipex!oleane!jussieu.fr!univ-lyon1.fr!swidir.switch.ch!CERN.ch!orel
From: orel@lpuds.oea.ihep.su (Oleg Orel)
Subject: libftp & uftp version 4.0 (Part 6)
X-Nntp-Posting-Host: dxunk1.cern.ch
Message-ID: <DBxCJH.3A3@news.cern.ch>
Sender: news@news.cern.ch (USENET News System)
Organization: CERN European Lab for Particle Physics
X-Newsreader: TIN [version 1.2 PL0]
Date: Tue, 18 Jul 1995 18:26:05 GMT
Lines: 755

#!/bin/sh
# to extract, remove the header and type "sh filename"
if `test ! -s ./FtpAbort.c`
then
echo "writing ./FtpAbort.c"
cat > ./FtpAbort.c << '\End\Of\Shar\'
static char rcsid[] = "$Id: FtpAbort.c,v 4.0 1995/07/11 07:00:26 orel Exp $";

/*
	$Log: FtpAbort.c,v $
 * Revision 4.0  1995/07/11  07:00:26  orel
 * Libftp Version 4.0
 *
 * Revision 3.0  1995/03/20  05:26:07  orel
 * *** empty log message ***
 *
 * Revision 3.0  1995/03/20  05:26:07  orel
 * *** empty log message ***
 *
 * Revision 2.4  1995/03/20  05:18:13  orel
 * *** empty log message ***
 *
 * Revision 2.3  1995/02/26  16:46:50  orel
 * *** empty log message ***
 *
 * Revision 2.3  1995/02/26  16:46:50  orel
 * *** empty log message ***
 *
 * Revision 2.2  1995/02/18  15:42:53  orel
 * modify for recurive mget
 *
 * Revision 2.2  1995/02/18  15:42:53  orel
 * modify for recurive mget
 *
 * Revision 2.1  1995/02/04  09:01:40  orel
 * add rcsid
 *
 * Revision 2.1  1995/02/04  09:01:40  orel
 * add rcsid
 *

*/
/*
		      Library for ftpd clients.(libftp)
			Copyright by Oleg Orel
			 All rights reserved.
			
This  library is desined  for  free,  non-commercial  software  creation. 
It is changeable and can be improved. The author would greatly appreciate 
any  advises, new  components  and  patches  of  the  existing  programs.
Commercial  usage is  also  possible  with  participation of it's author.



*/

#include "FtpLibrary.h"


FtpAbort(FTP *ftp)
{
  fd_set fds;
  char msgc=IAC;
  String msg;
  
  FD_ZERO(&fds);
  FD_SET(fileno(FTPCMD(ftp)),&fds);
  
  FtpPutc(ftp, FTPCMD(ftp), IAC);
  FtpPutc(ftp, FTPCMD(ftp), IP);
  
  if ( send ( fileno(FTPCMD(ftp)), &msgc , 1 ,MSG_OOB) != 1 )
    return EXIT(ftp,QUIT);
  
  FtpPutc(ftp, FTPCMD(ftp), DM);
  
  FtpSendMessage(ftp,"ABOR");
  
  while (select ( getdtablesize(), &fds, 0,0, &(ftp->timeout) )>0)
    {
      register int i;

      FtpGetMessage(ftp,msg);
      if (FtpGood(FtpNumber(msg),225,226,EOF)) break;
    }
}



\End\Of\Shar\
else
  echo "will not over write ./FtpAbort.c"
fi
if `test ! -s ./FtpArchie.c`
then
echo "writing ./FtpArchie.c"
cat > ./FtpArchie.c << '\End\Of\Shar\'
static char rcsid[] = "$Id: FtpArchie.c,v 4.0 1995/07/11 07:00:26 orel Exp $";

/*
	$Log: FtpArchie.c,v $
 * Revision 4.0  1995/07/11  07:00:26  orel
 * Libftp Version 4.0
 *
 * Revision 3.0  1995/03/20  05:26:07  orel
 * *** empty log message ***
 *
 * Revision 3.0  1995/03/20  05:26:07  orel
 * *** empty log message ***
 *
 * Revision 2.4  1995/03/20  05:18:13  orel
 * *** empty log message ***
 *
 * Revision 2.3  1995/02/26  16:46:50  orel
 * *** empty log message ***
 *
 * Revision 2.3  1995/02/26  16:46:50  orel
 * *** empty log message ***
 *
 * Revision 2.2  1995/02/18  15:42:53  orel
 * modify for recurive mget
 *
 * Revision 2.2  1995/02/18  15:42:53  orel
 * modify for recurive mget
 *
 * Revision 2.1  1995/02/04  09:01:40  orel
 * add rcsid
 *
 * Revision 2.1  1995/02/04  09:01:40  orel
 * add rcsid
 *

*/
/*
		      Library for ftpd clients.(libftp)
			Copyright by Oleg Orel
			 All rights reserved.
			
This  library is desined  for  free,  non-commercial  software  creation. 
It is changeable and can be improved. The author would greatly appreciate 
any  advises, new  components  and  patches  of  the  existing  programs.
Commercial  usage is  also  possible  with  participation of it's author.



*/

#include "FtpLibrary.h"

#define C2I(n) ((int)((n)-'0'))

int FtpArchie ( char *what, ARCHIE *result, int len)
{
  FILE *archie;
  String cmd,tmp;
  int i;
 
  bzero(result,sizeof(result[0])*len);
  
  sprintf(cmd,"archie -t -l -m %d %s",len,what);
  
  if ((archie = popen(cmd,"r"))==NULL)
    return 0;

  for(i=0;i<len;i++)
    {
      char *p, *pp;
      
      if (fgets(tmp,sizeof (tmp), archie)==NULL)
	break;


      result[i].createtime.tm_year = C2I (tmp[2 ])*10 - 70 + C2I(tmp[3]);
      result[i].createtime.tm_mday = C2I (tmp[4 ])*10 + C2I(tmp[5]);
      result[i].createtime.tm_hour = C2I (tmp[6 ])*10 + C2I(tmp[7]);
      result[i].createtime.tm_min  = C2I (tmp[8 ])*10 + C2I(tmp[9]);
      result[i].createtime.tm_sec  = C2I (tmp[10])*10 + C2I(tmp[11]);
      
      for(p=tmp; *p!=' '; p++);
      for(; *p==' '; p++);
      
      result[i].size  = atoi(p);

      for(; *p!=' '; p++);
      for(; *p==' '; p++);
      
      for (pp=result[i].host;*p!=' ';p++,pp++) *pp=*p;
      *pp=0;
      for(; *p==' '; p++);
      for (pp=result[i].file;*p!='\n';p++,pp++) *pp=*p;
      *pp=0;

    }
  
  return i;
}
\End\Of\Shar\
else
  echo "will not over write ./FtpArchie.c"
fi
if `test ! -s ./FtpBye.c`
then
echo "writing ./FtpBye.c"
cat > ./FtpBye.c << '\End\Of\Shar\'
static char rcsid[] = "$Id: FtpBye.c,v 4.0 1995/07/11 07:00:26 orel Exp $";

/*
	$Log: FtpBye.c,v $
 * Revision 4.0  1995/07/11  07:00:26  orel
 * Libftp Version 4.0
 *
 * Revision 3.0  1995/03/20  05:26:07  orel
 * *** empty log message ***
 *
 * Revision 3.0  1995/03/20  05:26:07  orel
 * *** empty log message ***
 *
 * Revision 2.4  1995/03/20  05:18:13  orel
 * *** empty log message ***
 *
 * Revision 2.3  1995/02/26  16:46:50  orel
 * *** empty log message ***
 *
 * Revision 2.3  1995/02/26  16:46:50  orel
 * *** empty log message ***
 *
 * Revision 2.2  1995/02/18  15:42:53  orel
 * modify for recurive mget
 *
 * Revision 2.2  1995/02/18  15:42:53  orel
 * modify for recurive mget
 *
 * Revision 2.1  1995/02/04  09:01:40  orel
 * add rcsid
 *
 * Revision 2.1  1995/02/04  09:01:40  orel
 * add rcsid
 *

*/
/*
		      Library for ftpd clients.(libftp)
			Copyright by Oleg Orel
			 All rights reserved.
			
This  library is desined  for  free,  non-commercial  software  creation. 
It is changeable and can be improved. The author would greatly appreciate 
any  advises, new  components  and  patches  of  the  existing  programs.
Commercial  usage is  also  possible  with  participation of it's author.



*/

#include "FtpLibrary.h"
#include <signal.h>

STATUS FtpBye(FTP *ftp)
{
  String S1;
  int i;

  FtpAssert(ftp,FtpCommand(ftp,"QUIT",221,EOF));

  fclose(FTPCMD(ftp));
  free(ftp);
  return 0;
}
      
STATUS FtpQuickBye(FTP *ftp)
{


  if (ftp == NULL) return;
  
  if (FTPDATA(ftp)!=NULL)
    {
      shutdown(fileno(FTPDATA(ftp)),2);
      fclose(FTPDATA(ftp));
    }

  if (FTPCMD(ftp)!=NULL)
    {
      shutdown(fileno(FTPCMD(ftp)),2);
      fclose(FTPCMD(ftp));
    }
  
  free(ftp);
}

\End\Of\Shar\
else
  echo "will not over write ./FtpBye.c"
fi
if `test ! -s ./FtpChmod.c`
then
echo "writing ./FtpChmod.c"
cat > ./FtpChmod.c << '\End\Of\Shar\'
static char rcsid[] = "$Id";

/*
	$Log: FtpChmod.c,v $
 * Revision 4.0  1995/07/11  07:00:26  orel
 * Libftp Version 4.0
 *
 * Revision 3.0  1995/03/20  05:26:07  orel
 * *** empty log message ***
 *
 * Revision 3.0  1995/03/20  05:26:07  orel
 * *** empty log message ***
 *
 * Revision 1.2  1995/03/05  15:02:26  orel
 * /
 *
 * Revision 1.2  1995/03/05  15:02:26  orel
 * /
 *
 * Revision 1.1  1995/02/19  15:47:05  orel
 * Initial revision
 *
 * Revision 1.1  1995/02/19  15:47:05  orel
 * Initial revision
 *
*/
/*
		      Library for ftpd clients.(libftp)
			Copyright by Oleg Orel
			 All rights reserved.
			
This  library is desined  for  free,  non-commercial  software  creation. 
It is changeable and can be improved. The author would greatly appreciate 
any  advises, new  components  and  patches  of  the  existing  programs.
Commercial  usage is  also  possible  with  participation of it's author.



*/

#include "FtpLibrary.h"

STATUS FtpChmod(FTP *ftp,char *file,int mode)
{
  String msg;

  sprintf(msg,"SITE CHMOD %03o %s",mode,file); 
  return FtpCommand(ftp,msg,"",200,EOF);
  
}
\End\Of\Shar\
else
  echo "will not over write ./FtpChmod.c"
fi
if `test ! -s ./FtpClose.c`
then
echo "writing ./FtpClose.c"
cat > ./FtpClose.c << '\End\Of\Shar\'
static char rcsid[] = "$Id: FtpClose.c,v 4.0 1995/07/11 07:00:26 orel Exp $";

/*
	$Log: FtpClose.c,v $
 * Revision 4.0  1995/07/11  07:00:26  orel
 * Libftp Version 4.0
 *
 * Revision 3.0  1995/03/20  05:26:07  orel
 * *** empty log message ***
 *
 * Revision 3.0  1995/03/20  05:26:07  orel
 * *** empty log message ***
 *
 * Revision 2.4  1995/03/20  05:18:13  orel
 * *** empty log message ***
 *
 * Revision 2.3  1995/02/26  16:46:50  orel
 * *** empty log message ***
 *
 * Revision 2.3  1995/02/26  16:46:50  orel
 * *** empty log message ***
 *
 * Revision 2.2  1995/02/18  15:42:53  orel
 * modify for recurive mget
 *
 * Revision 2.2  1995/02/18  15:42:53  orel
 * modify for recurive mget
 *
 * Revision 2.1  1995/02/04  09:01:40  orel
 * add rcsid
 *
 * Revision 2.1  1995/02/04  09:01:40  orel
 * add rcsid
 *

*/
/*		      Library for ftpd clients.(libftp)
			Copyright by Oleg Orel
			 All rights reserved.
			
This  library is desined  for  free,  non-commercial  software  creation. 
It is changeable and can be improved. The author would greatly appreciate 
any  advises, new  components  and  patches  of  the  existing  programs.
Commercial  usage is  also  possible  with  participation of it's author.
*/

#include "FtpLibrary.h"

STATUS FtpClose(FTP *ftp)
{
  int i;
  String S1;

  fflush(FTPDATA(ftp));
  shutdown(fileno(FTPDATA(ftp)),2);
  fclose(FTPDATA(ftp));
  
  FtpAssert(ftp,i=FtpGetMessage(ftp,S1));
  if ( i != 226 )
    return EXIT(ftp,-i);
  return EXIT(ftp,i);
}
\End\Of\Shar\
else
  echo "will not over write ./FtpClose.c"
fi
if `test ! -s ./FtpCommand.c`
then
echo "writing ./FtpCommand.c"
cat > ./FtpCommand.c << '\End\Of\Shar\'
static char rcsid[] = "$Id: FtpCommand.c,v 4.0 1995/07/11 07:00:26 orel Exp $";

/*
	$Log: FtpCommand.c,v $
 * Revision 4.0  1995/07/11  07:00:26  orel
 * Libftp Version 4.0
 *
 * Revision 3.0  1995/03/20  05:26:07  orel
 * *** empty log message ***
 *
 * Revision 3.0  1995/03/20  05:26:07  orel
 * *** empty log message ***
 *
 * Revision 2.4  1995/03/20  05:18:13  orel
 * *** empty log message ***
 *
 * Revision 2.3  1995/02/26  16:46:50  orel
 * *** empty log message ***
 *
 * Revision 2.3  1995/02/26  16:46:50  orel
 * *** empty log message ***
 *
 * Revision 2.2  1995/02/18  15:42:53  orel
 * modify for recurive mget
 *
 * Revision 2.2  1995/02/18  15:42:53  orel
 * modify for recurive mget
 *
 * Revision 2.1  1995/02/04  09:01:40  orel
 * add rcsid
 *
 * Revision 2.1  1995/02/04  09:01:40  orel
 * add rcsid
 *

*/
/*
		      Library for ftpd clients.(libftp)
			Copyright by Oleg Orel
			 All rights reserved.
			
This  library is desined  for  free,  non-commercial  software  creation. 
It is changeable and can be improved. The author would greatly appreciate 
any  advises, new  components  and  patches  of  the  existing  programs.
Commercial  usage is  also  possible  with  participation of it's author.



*/

#include "FtpLibrary.h"

STATUS FtpCommand(va_alist)
     va_dcl
{
  FTP *con;
  char *command, *param;
  int Answer[MAX_ANSWERS];
  va_list args;
  String S1;
  int i,counter=0;
  
  va_start(args);

  con = va_arg(args,FTP *);
  command = va_arg(args,char *);
  param = va_arg(args, char *);

  while ( 1 )
    {
      if (counter == MAX_ANSWERS)
	return EXIT(con,QUIT);
      Answer[counter] = va_arg(args,int);
      if (Answer[counter] == EOF ) break;
      counter++;
    }

  va_end(args);
  
  
  sprintf(S1,command,param);

  if ( FtpSendMessage(con,S1) == QUIT )
    return EXIT(con,QUIT);
  
  if  ( (i=FtpGetMessage(con,S1)) == QUIT )
    return EXIT(con,QUIT);
  
  if ( ! FtpGood1 ( i , Answer ))
    return EXIT(con,-i);

  return EXIT(con,i);
}
  
  
   
\End\Of\Shar\
else
  echo "will not over write ./FtpCommand.c"
fi
if `test ! -s ./FtpConnect.c`
then
echo "writing ./FtpConnect.c"
cat > ./FtpConnect.c << '\End\Of\Shar\'
static char rcsid[] = "$Id: FtpConnect.c,v 4.0 1995/07/11 07:00:26 orel Exp $";

/*
	$Log: FtpConnect.c,v $
 * Revision 4.0  1995/07/11  07:00:26  orel
 * Libftp Version 4.0
 *
 * Revision 3.1  1995/06/20  15:53:50  orel
 * *** empty log message ***
 *
 * Revision 3.0  1995/03/20  05:26:07  orel
 * *** empty log message ***
 *
 * Revision 2.5  1995/03/05  15:02:26  orel
 * /
 *
 * Revision 2.5  1995/03/05  15:02:26  orel
 * /
 *
 * Revision 2.4  1995/02/26  16:46:50  orel
 * *** empty log message ***
 *
 * Revision 2.3  1995/02/18  15:42:53  orel
 * modify for recurive mget
 *
 * Revision 2.3  1995/02/18  15:42:53  orel
 * modify for recurive mget
 *
 * Revision 2.2  1995/02/04  10:00:21  orel
 * *** empty log message ***
 *
 * Revision 2.2  1995/02/04  10:00:21  orel
 * *** empty log message ***
 *
 * Revision 2.1  1995/02/04  09:01:40  orel
 * add rcsid
 *
 * Revision 2.1  1995/02/04  09:01:40  orel
 * add rcsid
 *

*/
/*
		      Library for ftpd clients.(libftp)
			Copyright by Oleg Orel
			 All rights reserved.
			
This  library is desined  for  free,  non-commercial  software  creation. 
It is changeable and can be improved. The author would greatly appreciate 
any  advises, new  components  and  patches  of  the  existing  programs.
Commercial  usage is  also  possible  with  participation of it's author.



*/

#include "FtpLibrary.h"

STATUS FtpConnect(FTP **con,char * hostname)
{
  struct sockaddr_in unit;
  register struct hostent *host;
  register int new_socket;
  String S1;
  STATUS x;
  
  *con = FtpCreateObject();
  
  if ((host=FtpGetHost(hostname))==NULL)
    return EXIT((*con),QUIT);
  
  strcpy((*con) -> title,host -> h_name); /* Default title for FtpLog */
  
  unit.sin_family = host -> h_addrtype; /* AF_INET */
  
  bcopy(host-> h_addr_list[0],(char *)&unit.sin_addr,host->h_length);
  if ( ( new_socket = socket ( unit.sin_family , SOCK_STREAM , 0)) < 0)
    return EXIT((*con),QUIT);

  unit.sin_port = htons((*con)->port);
  
  while ( connect ( new_socket , (struct sockaddr*) &unit , sizeof unit ) < 0 )
    {
      host -> h_addr_list ++;
      if (host -> h_addr_list[0]==NULL) {
	close(new_socket);
	return EXIT((*con),QUIT);
      }
      bcopy(host -> h_addr_list[0],(char *)&unit,host->h_length);
      close(new_socket);
      if ( ( new_socket = socket ( unit.sin_family , SOCK_STREAM , 0)) < 0)
	{
	  close(new_socket);
	  return EXIT((*con),QUIT);
	}
    }
  
  FTPCMD(*con) = fdopen(new_socket,"r+");

  if ( (x=FtpGetMessage(*con,S1)) == QUIT )
    return EXIT((*con),QUIT);
  if ( ! FtpGood(x,120,220,EOF))
    {
      close(new_socket);
      return EXIT((*con),-x);
    }

  if ( (*con)->mode != 'A' )
    {
      String x;

      sprintf (x,"TYPE %c",(*con)->mode);
      FtpSendMessage(*con,x);
      FtpGetMessage(*con,x);
    }

  return EXIT((*con),x);
}



\End\Of\Shar\
else
  echo "will not over write ./FtpConnect.c"
fi
if `test ! -s ./FtpCopy.c`
then
echo "writing ./FtpCopy.c"
cat > ./FtpCopy.c << '\End\Of\Shar\'
static char rcsid[] = "$Id: FtpCopy.c,v 4.0 1995/07/11 07:00:26 orel Exp $";

/*
	$Log: FtpCopy.c,v $
 * Revision 4.0  1995/07/11  07:00:26  orel
 * Libftp Version 4.0
 *
 * Revision 3.0  1995/03/20  05:26:07  orel
 * *** empty log message ***
 *
 * Revision 3.0  1995/03/20  05:26:07  orel
 * *** empty log message ***
 *
 * Revision 2.4  1995/03/20  05:18:13  orel
 * *** empty log message ***
 *
 * Revision 2.3  1995/02/26  16:46:50  orel
 * *** empty log message ***
 *
 * Revision 2.3  1995/02/26  16:46:50  orel
 * *** empty log message ***
 *
 * Revision 2.2  1995/02/18  15:42:53  orel
 * modify for recurive mget
 *
 * Revision 2.2  1995/02/18  15:42:53  orel
 * modify for recurive mget
 *
 * Revision 2.1  1995/02/04  09:01:40  orel
 * add rcsid
 *
 * Revision 2.1  1995/02/04  09:01:40  orel
 * add rcsid
 *

*/
/*
		      Library for ftpd clients.(libftp)
			Copyright by Oleg Orel
			 All rights reserved.
			
This  library is desined  for  free,  non-commercial  software  creation. 
It is changeable and can be improved. The author would greatly appreciate 
any  advises, new  components  and  patches  of  the  existing  programs.
Commercial  usage is  also  possible  with  participation of it's author.



*/

#include "FtpLibrary.h"

STATUS FtpCopy (FTP * ftp1 , FTP * ftp2 ,char *in , char * out)
{
  int size;
  char buffer[FTPBUFSIZ];

  if (!*out) out=in;

  if ( FtpTestFlag(ftp1,FTP_REST) &&  FtpTestFlag(ftp2,FTP_REST) 
      && (size=FtpSize(ftp1,in))>0
      && FtpCommand(ftp1,"REST 0",0,0,EOF)==350 && FtpCommand(ftp2,"REST 0",0,0,EOF)==350 )
    ftp1->seek=ftp2->seek=size;
  else
    ftp1->seek=ftp2->seek=0;
  
  FtpAssert(ftp1,FtpData(ftp1,"RETR %s",in,"r"));
  FtpAssert(ftp2,FtpData(ftp2,"STOR %s",out,"w"));

  while ((size=FtpReadBlock(ftp1,buffer,FTPBUFSIZ))>0)
    {
      if (FtpWriteBlock(ftp2,buffer,size)!=size)  
	return EXIT(ftp2,QUIT);
    }

  FtpAssert(ftp1,FtpClose(ftp1));
  FtpAssert(ftp2,FtpClose(ftp2));
  return 0;
}







\End\Of\Shar\
else
  echo "will not over write ./FtpCopy.c"
fi
echo "Finished archive 6 of 6"
exit


--
``


