Article 2725 of comp.sources.unix:
Path: nntpd.lkg.dec.com!crl.dec.com!crl.dec.com!caen!spool.mu.edu!sol.ctr.columbia.edu!news.uoregon.edu!inquo!vyzynz!news1.cris.com!newshost.comco.com!news.texas.net!news.kei.com!newsfeed.internetmci.com!in1.uu.net!vixie!vixie!not-for-mail
From: orel@dxunk1.cern.ch (Oleg Orel)
Newsgroups: comp.sources.unix
Subject: v29i069: libftp - C library for FTP client access, V5.0, Part08/08
Date: 12 Dec 1995 00:36:01 -0800
Organization: Vixie Enterprises
Lines: 1809
Sender: vixie@vix.com
Approved: vixie@gw.home.vix.com
Message-ID: <4ajeth$amv@gw.home.vix.com>
References: <1.818757291.10931@gw.home.vix.com>
NNTP-Posting-Host: gw.home.vix.com

Submitted-By: orel@dxunk1.cern.ch (Oleg Orel)
Posting-Number: Volume 29, Issue 69
Archive-Name: libftp-5.0/part08

#!/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 5.0 1995/12/10 10:28:38 orel Exp $";

/*
	$Log: FtpAbort.c,v $
 * Revision 5.0  1995/12/10  10:28:38  orel
 * LIBFTP Version 5.0 (Distributed revision)
 *
 * Revision 4.2  1995/09/09  10:10:01  orel
 * Clean problems with aborting already broken connection.
 *
 * Revision 4.2  1995/09/09  10:10:01  orel
 * Clean problems with aborting already broken connection.
 *
 * Revision 4.1  1995/09/09  09:49:42  orel
 *  Change type String to FtpString for clean conflicting with
 * X11's type string which is pointer to character only.
 * Thanks for MIT for this good name of type.
 *
 * 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;
  FtpString msg;
  

  if (FTPCMD(ftp)!=NULL) /* To avoid segmentation fault
			    for abort nonexisting connections */
    {
      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 5.0 1995/12/10 10:28:38 orel Exp $";

/*
	$Log: FtpArchie.c,v $
 * Revision 5.0  1995/12/10  10:28:38  orel
 * LIBFTP Version 5.0 (Distributed revision)
 *
 * Revision 4.1  1995/09/09  09:49:42  orel
 *  Change type String to FtpString for clean conflicting with
 * X11's type string which is pointer to character only.
 * Thanks for MIT for this good name of type.
 *
 * Revision 4.1  1995/09/09  09:49:42  orel
 *  Change type String to FtpString for clean conflicting with
 * X11's type string which is pointer to character only.
 * Thanks for MIT for this good name of type.
 *
 * 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;
  FtpString 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 5.0 1995/12/10 10:28:38 orel Exp $";

/*
	$Log: FtpBye.c,v $
 * Revision 5.0  1995/12/10  10:28:38  orel
 * LIBFTP Version 5.0 (Distributed revision)
 *
 * Revision 4.1  1995/09/09  09:49:42  orel
 *  Change type String to FtpString for clean conflicting with
 * X11's type string which is pointer to character only.
 * Thanks for MIT for this good name of type.
 *
 * Revision 4.1  1995/09/09  09:49:42  orel
 *  Change type String to FtpString for clean conflicting with
 * X11's type string which is pointer to character only.
 * Thanks for MIT for this good name of type.
 *
 * 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)
{
  FtpString 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 5.0  1995/12/10  10:28:38  orel
 * LIBFTP Version 5.0 (Distributed revision)
 *
 * Revision 4.1  1995/09/09  09:49:42  orel
 *  Change type String to FtpString for clean conflicting with
 * X11's type string which is pointer to character only.
 * Thanks for MIT for this good name of type.
 *
 * Revision 4.1  1995/09/09  09:49:42  orel
 *  Change type String to FtpString for clean conflicting with
 * X11's type string which is pointer to character only.
 * Thanks for MIT for this good name of type.
 *
 * 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)
{
  FtpString 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 5.0 1995/12/10 10:28:38 orel Exp $";

/*
	$Log: FtpClose.c,v $
 * Revision 5.0  1995/12/10  10:28:38  orel
 * LIBFTP Version 5.0 (Distributed revision)
 *
 * Revision 4.1  1995/09/09  09:49:42  orel
 *  Change type String to FtpString for clean conflicting with
 * X11's type string which is pointer to character only.
 * Thanks for MIT for this good name of type.
 *
 * Revision 4.1  1995/09/09  09:49:42  orel
 *  Change type String to FtpString for clean conflicting with
 * X11's type string which is pointer to character only.
 * Thanks for MIT for this good name of type.
 *
 * 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;
  FtpString 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 5.0 1995/12/10 10:28:38 orel Exp $";

/*
	$Log: FtpCommand.c,v $
 * Revision 5.0  1995/12/10  10:28:38  orel
 * LIBFTP Version 5.0 (Distributed revision)
 *
 * Revision 4.1  1995/09/09  09:49:42  orel
 *  Change type String to FtpString for clean conflicting with
 * X11's type string which is pointer to character only.
 * Thanks for MIT for this good name of type.
 *
 * Revision 4.1  1995/09/09  09:49:42  orel
 *  Change type String to FtpString for clean conflicting with
 * X11's type string which is pointer to character only.
 * Thanks for MIT for this good name of type.
 *
 * 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;
  FtpString 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 5.0 1995/12/10 10:28:38 orel Exp $";

/*
	$Log: FtpConnect.c,v $
 * Revision 5.0  1995/12/10  10:28:38  orel
 * LIBFTP Version 5.0 (Distributed revision)
 *
 * Revision 4.1  1995/09/09  09:49:42  orel
 *  Change type String to FtpString for clean conflicting with
 * X11's type string which is pointer to character only.
 * Thanks for MIT for this good name of type.
 *
 * Revision 4.1  1995/09/09  09:49:42  orel
 *  Change type String to FtpString for clean conflicting with
 * X11's type string which is pointer to character only.
 * Thanks for MIT for this good name of type.
 *
 * 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;
  FtpString 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' )
    {
      FtpString 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 5.0 1995/12/10 10:28:38 orel Exp $";

/*
	$Log: FtpCopy.c,v $
 * Revision 5.0  1995/12/10  10:28:38  orel
 * LIBFTP Version 5.0 (Distributed revision)
 *
 * Revision 4.1  1995/12/02  11:23:07  orel
 * *** empty log message ***
 *
 * Revision 4.0  1995/07/11  07:00:26  orel
 * Libftp Version 4.0
 *
 * 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
if `test ! -s ./FtpData.c`
then
echo "writing ./FtpData.c"
cat > ./FtpData.c << '\End\Of\Shar\'
static char rcsid[] = "$Id: FtpData.c,v 5.0 1995/12/10 10:28:38 orel Exp $";

/*
	$Log: FtpData.c,v $
 * Revision 5.0  1995/12/10  10:28:38  orel
 * LIBFTP Version 5.0 (Distributed revision)
 *
 * Revision 4.1  1995/09/09  09:49:42  orel
 *  Change type String to FtpString for clean conflicting with
 * X11's type string which is pointer to character only.
 * Thanks for MIT for this good name of type.
 *
 * Revision 4.1  1995/09/09  09:49:42  orel
 *  Change type String to FtpString for clean conflicting with
 * X11's type string which is pointer to character only.
 * Thanks for MIT for this good name of type.
 *
 * Revision 4.0  1995/07/11  07:00:26  orel
 * Libftp Version 4.0
 *
 * Revision 3.1  1995/06/20  15:53:50  orel
 * Porting to AIX
 *
 * Revision 3.0  1995/03/20  05:26:07  orel
 * *** empty log message ***
 *
 * Revision 2.7  1995/03/20  05:18:13  orel
 * *** empty log message ***
 *
 * Revision 2.6  1995/02/26  16:46:50  orel
 * *** empty log message ***
 *
 * Revision 2.6  1995/02/26  16:46:50  orel
 * *** empty log message ***
 *
 * Revision 2.5  1995/02/18  15:42:53  orel
 * modify for recurive mget
 *
 * Revision 2.5  1995/02/18  15:42:53  orel
 * modify for recurive mget
 *
 * Revision 2.4  1995/02/04  09:01:40  orel
 * add rcsid
 *
 * Revision 2.4  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 FtpData(FTP * con,char * command , char * file ,char * mode)
{
  struct sockaddr_in data,from;
  register struct hostent *host;
  FtpString hostname,cmd;
  int NewSocket,Accepted_Socket,len=sizeof(data),one=1,fromlen=sizeof(from),i;
  char *a,*b;

  FREE(data);
  FREE(from);
  
  if ( gethostname( hostname , sizeof hostname ) == -1 )
    return EXIT(con,QUIT);
  
  if ((host=(struct hostent *)gethostbyname(hostname))==0)
    return EXIT(con,QUIT);
  
  data.sin_family = host -> h_addrtype;
  
  bcopy(host-> h_addr_list[0],(char *)&data.sin_addr,host->h_length);
		
  if ((NewSocket = socket ( AF_INET  , SOCK_STREAM , 0 ))<0)
    return EXIT(con,QUIT);

  if ( setsockopt ( NewSocket , SOL_SOCKET , SO_REUSEADDR ,
		    (char *)&one , sizeof(one) ) < 0 )
    {
      close(NewSocket);
      return EXIT ( con,QUIT );
    }

  data.sin_port = 0 ;

  if ( bind ( NewSocket , (struct sockaddr*) &data , sizeof data ) < 0 )
    return EXIT(con,QUIT);

  if ( getsockname ( NewSocket , (struct sockaddr*) &data , &len ) < 0 )
    return EXIT(con,QUIT);
  
  if ( listen ( NewSocket , 1 ) < 0 )
    return EXIT(con,QUIT);

  a = ( char * ) & data.sin_addr;
  b = ( char * ) & data.sin_port;

  FtpAssert(con,FtpPort(con,CUT(a[0]),CUT(a[1]),CUT(a[2]),
		  CUT(a[3]),CUT(b[0]),CUT(b[1])));

  if ( con -> seek != 0)
    {
      if ((i = FtpCommand ( con, "REST %d" , con -> seek , 0, EOF)) != 350 )
	return -i;
    }
  
  FtpAssert(con, i=FtpCommand ( con , command , file , 
			     200, 120 , 150 , 125 , 250 , EOF ));
  
  if (( Accepted_Socket = accept (NewSocket , (struct sockaddr *)&from , &fromlen )) < 0)
    {
      close(NewSocket);
      return EXIT(con,QUIT);
    }

  close(NewSocket);

  FTPDATA(con) = fdopen(Accepted_Socket, "r+");

  return i;
}





\End\Of\Shar\
else
  echo "will not over write ./FtpData.c"
fi
if `test ! -s ./FtpDebug.c`
then
echo "writing ./FtpDebug.c"
cat > ./FtpDebug.c << '\End\Of\Shar\'
static char rcsid[] = "$Id: FtpDebug.c,v 5.0 1995/12/10 10:28:38 orel Exp $";

/*
	$Log: FtpDebug.c,v $
 * Revision 5.0  1995/12/10  10:28:38  orel
 * LIBFTP Version 5.0 (Distributed revision)
 *
 * Revision 4.1  1995/09/09  09:49:42  orel
 *  Change type String to FtpString for clean conflicting with
 * X11's type string which is pointer to character only.
 * Thanks for MIT for this good name of type.
 *
 * Revision 4.1  1995/09/09  09:49:42  orel
 *  Change type String to FtpString for clean conflicting with
 * X11's type string which is pointer to character only.
 * Thanks for MIT for this good name of type.
 *
 * Revision 4.0  1995/07/11  07:00:26  orel
 * Libftp Version 4.0
 *
 * Revision 3.1  1995/06/20  15:53:50  orel
 * Porting to AIX
 *
 * 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"

void FtpDebug(FTP *ftp)
{
  STATUS FtpDebugDebug(),
         FtpDebugError(),
         FtpDebugIO();

  FtpSetDebugHandler(ftp,FtpDebugDebug);
  FtpSetErrorHandler(ftp,FtpDebugError);
  FtpSetIOHandler(ftp,FtpDebugIO);
}

STATUS FtpDebugDebug(FTP *ftp,int n, char * Message)
{
  FtpString tmp;

  
  strcpy(tmp,Message);
  
  if (strncmp(tmp,"PASS ",5)==0)
    {
      char *p=tmp+5;
      while ( *p != '\0') *p++='*';
    };
	
  FtpLog(ftp->title,tmp);
  return 1;
}

STATUS FtpDebugError(FTP *ftp,int n, char * Message)
{
  
  FtpLog("FtpDebugError","");
  FtpLog(ftp->title,Message);
  if ( ! FtpTestFlag(ftp,FTP_NOEXIT))
    exit(1);
}

STATUS FtpDebugIO(FTP *ftp,int n, char * Message)
{
  FtpLog("FtpDebugIO","");
  FtpLog(ftp->title,Message);
  if ( ! FtpTestFlag(ftp,FTP_NOEXIT))
    exit(1);
}

STATUS FtpLog(char *name,char *str)
{
  time_t t=time((time_t *)0);
  struct tm *lt=localtime(&t);
  fprintf(stderr,"%02d:%02d:%02d %s %s\n",lt->tm_hour,
	  lt->tm_min,lt->tm_sec,name,str);
  fflush(stderr);
}

FtpHash(FTP *ftp, unsigned long chars)
{
  
  if (chars==0) return ftp->counter=0;
  ftp->counter+=chars;
  fprintf(stderr,"%10u bytes transfered\r",ftp->counter);
  fflush(stderr);
  return ftp->counter;
}


STATUS FtpBadReply550(char *s)
{
  if(
     ((char *)strstr(s,"unreachable")!=NULL) ||
     ((char *)strstr(s,"Broken pipe")!=NULL)
     ) 
    return 0;
  return 1;
}











\End\Of\Shar\
else
  echo "will not over write ./FtpDebug.c"
fi
if `test ! -s ./FtpFilenameChecker.c`
then
echo "writing ./FtpFilenameChecker.c"
cat > ./FtpFilenameChecker.c << '\End\Of\Shar\'
static char rcsid[] = "$Id: FtpFilenameChecker.c,v 5.0 1995/12/10 10:28:38 orel Exp $";

/*
	$Log: FtpFilenameChecker.c,v $
 * Revision 5.0  1995/12/10  10:28:38  orel
 * LIBFTP Version 5.0 (Distributed revision)
 *
 * Revision 4.1  1995/12/02  11:23:07  orel
 * *** empty log message ***
 *
 * Revision 4.0  1995/07/11  07:00:26  orel
 * Libftp Version 4.0
 *
 * 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"

static char * simplename(char *s)
{
  char *p;
  
  if ( (p=(char *)strrchr(s,'/')) == NULL )
    return s;
  return p+1;
}
  



STATUS FtpFilenameChecker(char ** in, char ** out)
{
  struct stat st;

  if ( (stat(*out,&st) == 0) && S_ISDIR(st.st_mode))
    {
      char * sfn = simplename(*in);
      char * new = (char *) malloc ( strlen(*out)+ strlen(sfn) + 2 );

      strcpy(new,*out);
      strcat(new,"/");
      strcat(new,sfn);
      *out=new;
      return;
    };

}


\End\Of\Shar\
else
  echo "will not over write ./FtpFilenameChecker.c"
fi
if `test ! -s ./FtpFull.c`
then
echo "writing ./FtpFull.c"
cat > ./FtpFull.c << '\End\Of\Shar\'
static char  rcsid[] = "$Id: FtpFull.c,v 5.0 1995/12/10 10:28:38 orel Exp $";

/*
	$Log: FtpFull.c,v $
 * Revision 5.0  1995/12/10  10:28:38  orel
 * LIBFTP Version 5.0 (Distributed revision)
 *
 * Revision 4.1  1995/09/09  09:49:42  orel
 *  Change type String to FtpString for clean conflicting with
 * X11's type string which is pointer to character only.
 * Thanks for MIT for this good name of type.
 *
 * Revision 4.1  1995/09/09  09:49:42  orel
 *  Change type String to FtpString for clean conflicting with
 * X11's type string which is pointer to character only.
 * Thanks for MIT for this good name of type.
 *
 * 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.6  1995/03/20  05:18:13  orel
 * *** empty log message ***
 *
 * Revision 2.5  1995/02/26  16:46:50  orel
 * *** empty log message ***
 *
 * Revision 2.5  1995/02/26  16:46:50  orel
 * *** empty log message ***
 *
 * Revision 2.4  1995/02/18  15:42:53  orel
 * modify for recurive mget
 *
 * Revision 2.4  1995/02/18  15:42:53  orel
 * modify for recurive mget
 *
 * Revision 2.2  1995/02/04  09:02:53  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 <pwd.h>

static FTP *ftp_table[256];
static STATUS syntax();

FILE * FtpFullOpen(char * file , char * mode )
{
  FTP *ftp;
  FILE *tmp;
  FtpString Host,User,Passwd,RemoteFile;
  STATUS i;

  if ( ! FtpFullSyntax (file,Host,User,Passwd,RemoteFile))
    {
      tmp=fopen(file,mode);
      if (tmp==NULL) return tmp;
      ftp_table[(int)fileno(tmp)] = NULL;
      return tmp;
    }
  if ( FtpLogin(&ftp,Host,User,Passwd,NULL) < 0 )
    return NULL;

  if (mode[1]=='b') FtpBinary(ftp);

  switch(mode[0])
    {
    case 'r': 
	if (FtpError(FtpOpenRead(ftp,RemoteFile))) 
	  return NULL;
	ftp_table[fileno(FTPDATA(ftp))] = ftp;
	return FTPDATA(ftp);
    case 'w':
	if (FtpError(FtpOpenWrite(ftp,RemoteFile)))
	  return NULL;
	ftp_table[fileno(FTPDATA(ftp))] = ftp;
	return FTPDATA(ftp);
    case 'a':
	if (FtpError(FtpOpenAppend(ftp,RemoteFile)))
	  return NULL;
	ftp_table[fileno(FTPDATA(ftp))] = ftp;
	return FTPDATA(ftp);
    }
  /* Error Mode */
  return NULL;
}

STATUS   FtpFullClose(FILE *f)
{
  FTP *ftp=ftp_table[(int)fileno(f)];
  if (ftp == NULL)
    return fclose(f);
  FtpClose(ftp);
  return FtpQuickBye(ftp);
}


/* Format of ftp's file [user[/passord]@]hostname:filename_with_path */

STATUS FtpFullSyntax ( FtpString source ,
		       FtpString host ,
		       FtpString user ,
		       FtpString passwd ,
		       FtpString file)

{
  char *in,*out;
  FtpString tmp;
  
  host[0] = user[0] = passwd[0] = file[0] = '\0';
 
  for ( in=source, out = user;
       *in !='\0' && *in != '/' && *in!='@' && *in!=':' ;
       *out++ = *in++);
  *out = '\0';
  
  if ( *in == '\0' ) return 0;
  
  if ( *in == ':' )
    {
      strcpy(host,user);
      strcpy(user,"anonymous");
      gethostname(tmp, sizeof tmp);
      sprintf(passwd,"%s@%s",
	      getpwuid(getuid())->pw_name,gethostbyname(tmp)->h_name);
      goto file;
    } 

  if ( *in == '/' )
    {
      for ( in++, out = passwd;
	   *in !='\0' && *in!='@' ;
	   *out++ = *in++);
      *out = '\0';
      if ( *in == '\0' ) return 0;
    }
  else
    {
      gethostname(tmp, sizeof tmp);
      sprintf(passwd,"%s@%s",
	      getpwuid(getuid())->pw_name,gethostbyname(tmp)->h_name);
    }
  
  
  for ( in++, out = host;
       *in !='\0' && *in!=':' ;
       *out++ = *in++);
  *out = '\0';
  
  if ( *in == '\0' ) return 0;

file:
  
  for ( in++, out = file;
       *in !='\0';
       *out++ = *in++);
  *out = '\0';
  
  return 1;
}
  







\End\Of\Shar\
else
  echo "will not over write ./FtpFull.c"
fi
if `test ! -s ./FtpGetHost.c`
then
echo "writing ./FtpGetHost.c"
cat > ./FtpGetHost.c << '\End\Of\Shar\'
static char rcsid[] = "$Id: FtpGetHost.c,v 5.0 1995/12/10 10:28:38 orel Exp $";

/*
	$Log: FtpGetHost.c,v $
 * Revision 5.0  1995/12/10  10:28:38  orel
 * LIBFTP Version 5.0 (Distributed revision)
 *
 * Revision 4.1  1995/12/02  11:23:07  orel
 * *** empty log message ***
 *
 * Revision 4.0  1995/07/11  07:00:26  orel
 * Libftp Version 4.0
 *
 * 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.5  1995/03/20  05:18:13  orel
 * *** empty log message ***
 *
 * Revision 1.4  1995/02/26  16:46:50  orel
 * *** empty log message ***
 *
 * Revision 1.4  1995/02/26  16:46:50  orel
 * *** empty log message ***
 *
 * Revision 1.3  1995/02/18  15:42:53  orel
 * modify for recurive mget
 *
 * Revision 1.3  1995/02/18  15:42:53  orel
 * modify for recurive mget
 *
 * Revision 1.2  1995/02/04  09:02:53  orel
 * add rcsid
 *
 * Revision 1.2  1995/02/04  09:02:53  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"



struct hostent *FtpGetHost(char *host)
{

  static struct in_addr addr;
  static struct hostent _host;
  static char *point[2];
  static char *alias[1];

  bzero(&_host,sizeof _host);
  if ( (addr.s_addr=inet_addr(host)) != -1 )
    {
      _host.h_addr_list = point;
      _host.h_addr_list[0] = (char *) &addr;
      _host.h_addr_list[1] = (char *) 0;
      alias[0]=NULL;
      _host.h_aliases=alias;
      _host.h_name=host;
      _host.h_length=sizeof(unsigned long);
      _host.h_addrtype=AF_INET;
      return &_host;
    }
  
  return gethostbyname(host);
}


\End\Of\Shar\
else
  echo "will not over write ./FtpGetHost.c"
fi
if `test ! -s ./FtpGood.c`
then
echo "writing ./FtpGood.c"
cat > ./FtpGood.c << '\End\Of\Shar\'
static char rcsid[] = "$Id: FtpGood.c,v 5.0 1995/12/10 10:28:38 orel Exp $";

/*
	$Log: FtpGood.c,v $
 * Revision 5.0  1995/12/10  10:28:38  orel
 * LIBFTP Version 5.0 (Distributed revision)
 *
 * Revision 4.1  1995/12/02  11:23:07  orel
 * *** empty log message ***
 *
 * Revision 4.0  1995/07/11  07:00:26  orel
 * Libftp Version 4.0
 *
 * 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:02:53  orel
 * add rcsid
 *
 * Revision 2.1  1995/02/04  09:02:53  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 FtpGood(va_alist)
     va_dcl
{
  va_list args;
  int Number;
  int Answer[MAX_ANSWERS];
  int counter=0;
  
  va_start(args);

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

  va_end(args);
  
  return FtpGood1(Number,Answer);
}


STATUS FtpGood1(int Number , int *Answer)
{
  while (1)
    {
      if ( *Answer == Number) return 1;
      if ( *Answer == 0) return 1;
      if ( *Answer == EOF ) return 0;
      Answer++;
    }
}

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

/*
 $Log: FtpHTTP.c,v $
 * Revision 5.0  1995/12/10  10:28:38  orel
 * LIBFTP Version 5.0 (Distributed revision)
 *
 * Revision 1.2  1995/12/02  11:23:07  orel
 * *** empty log message ***
 *
 * Revision 1.1  1995/11/21  21:44:20  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"
#include <sys/file.h>

static char ContextLenght[]="Content-Length: ";


STATUS FtpHttpGet(char* server,int port,char *spec, char *newfile)
{
  struct sockaddr_in unit;
  register struct hostent *host;
  register int new_socket;
  char buf[16*1024];
  register int n;
  FILE *fp, *sock;
  register char *p;
  register int counter;
  int ContextLenghtLenght=strlen(ContextLenght);
  int LenghtOfFile=-1;
  
  if ((host=FtpGetHost(server))==NULL)
    return QUIT;
  
  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 QUIT;

  unit.sin_port = htons(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 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 QUIT;
	}
    }

  sock=fdopen(new_socket,"r+");

  if (fprintf(sock,
	      "GET %s HTTP/1.0\r\nAccept: */*\r\nUserAgent: LIBFTP\r\n\r\n",
	      spec)!=0)
    {
      
      shutdown(new_socket,2);
      fclose(sock);
      return QUIT;
    }

  fflush(sock);
  
  p=strrchr(spec,'/');
  if ( ((newfile==NULL) || !*newfile) && p!=NULL)
    strcpy(newfile,++p);

  if ((fp=fopen(newfile,"w+"))==NULL)
    {
      shutdown(new_socket,2);
      close(new_socket);
      return LQUIT;
    }

  while ( (fgets(buf,sizeof buf,sock))!=NULL )
    {
      if (FtpInit.debug!=NULL)
	printf("%s",buf);

      if (strstr(buf,"Internal error")!=NULL)
	return ENOENT;
      
      if (!strncmp(ContextLenght,buf,ContextLenghtLenght))
	{
	  sscanf (buf,"Context-Lenght: %d",&LenghtOfFile);
	}

      if (buf[0]==Ctrl('M') && buf[1]=='\n' && buf[2]==0)
	break; 
    }

  
  for (counter=0; (n=getc(sock))!=EOF ; counter++)
    if (putc(n,fp)==EOF)
      {
	fclose(fp);
	shutdown(new_socket,2);
	fclose(sock);
	return LQUIT;
      }
  fclose(fp);
  shutdown(new_socket,2);
  fclose(sock);

  if (counter==LenghtOfFile || LenghtOfFile == -1 )
    return OK;

  return QUIT;

}



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



