Ported version

posted at: 04:00 | path: /libftp | permanent link to this entry

    #2 bmp_kudzu

    thank you
    I am searching for code samples to write an ftp client for lab experiments.
    your attempt is interesting
    regards
    bmp kudzu

    #14 Carlo Wood

    Here's what I hacked together as patch.
    With this patch it compiles without any warnings or errors.

    diff -c libftp/FtpArchie.c libftp-fixed/FtpArchie.c
    *** libftp/FtpArchie.c 2002-02-16 11:09:17.000000000 +0100
    --- libftp-fixed/FtpArchie.c 2007-10-01 03:51:08.587990031 +0200
    ***************
    *** 22,28 ****
    String cmd,tmp;
    int i;

    ! bzero(result,sizeof(result[0])*len);

    sprintf(cmd,"archie -l -m %d %s",len,what);

    --- 22,28 ----
    String cmd,tmp;
    int i;

    ! memset(result, 0, sizeof(result[0]) * len);

    sprintf(cmd,"archie -l -m %d %s",len,what);

    diff -c libftp/FtpCommand.c libftp-fixed/FtpCommand.c
    *** libftp/FtpCommand.c 2002-02-16 11:09:22.000000000 +0100
    --- libftp-fixed/FtpCommand.c 2007-10-01 03:40:33.143778129 +0200
    ***************
    *** 14,33 ****

    #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 )
    --- 14,29 ----

    #include "FtpLibrary.h"

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

    ! va_start(args, command);

    param = va_arg(args, char *);

    while ( 1 )
    diff -c libftp/FtpConnect.c libftp-fixed/FtpConnect.c
    *** libftp/FtpConnect.c 2002-02-16 11:09:22.000000000 +0100
    --- libftp-fixed/FtpConnect.c 2007-10-01 03:51:13.124248535 +0200
    ***************
    *** 37,43 ****

    unit.sin_port = htons((*con)->port);

    ! while ( connect ( new_socket , &unit , sizeof unit ) < 0 )
    {
    host -> h_addr_list ++;
    if (host -> h_addr_list[0]==NULL) {
    --- 37,43 ----

    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) {
    diff -c libftp/FtpData.c libftp-fixed/FtpData.c
    *** libftp/FtpData.c 2002-02-16 11:09:22.000000000 +0100
    --- libftp-fixed/FtpData.c 2007-10-01 03:49:50.071515636 +0200
    ***************
    *** 47,56 ****

    data.sin_port = 0 ;

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

    ! if ( getsockname ( NewSocket , &data , &len ) < 0 )
    return EXIT(con,QUIT);

    if ( listen ( NewSocket , 1 ) < 0 )
    --- 47,56 ----

    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 )
    ***************
    *** 71,77 ****
    FtpAssert(con, FtpCommand ( con , command , file ,
    200, 120 , 150 , 125 , 250 , EOF ));

    ! if (( Accepted_Socket = accept (NewSocket , &from , &fromlen )) < 0)
    {
    close(NewSocket);
    return EXIT(con,QUIT);
    --- 71,77 ----
    FtpAssert(con, 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);
    diff -c libftp/FtpGood.c libftp-fixed/FtpGood.c
    *** libftp/FtpGood.c 2002-02-16 11:09:22.000000000 +0100
    --- libftp-fixed/FtpGood.c 2007-10-01 03:43:22.093406021 +0200
    ***************
    *** 14,31 ****

    #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)
    --- 14,27 ----

    #include "FtpLibrary.h"

    ! STATUS FtpGood(int Number, ...)
    {
    va_list args;
    int Answer[MAX_ANSWERS];
    int counter=0;

    ! va_start(args, Number);

    while ( 1 )
    {
    if (counter == MAX_ANSWERS)
    diff -c libftp/FtpLibrary.h libftp-fixed/FtpLibrary.h
    *** libftp/FtpLibrary.h 2002-02-16 11:09:22.000000000 +0100
    --- libftp-fixed/FtpLibrary.h 2007-10-01 04:02:49.787949142 +0200
    ***************
    *** 27,39 ****
    #include
    #include
    #include
    - #include
    #include
    #include


    - extern char *sys_errlist[];
    - extern int errno;


    /* Standard Macros & Definitions */
    --- 27,41 ----
    #include
    #include
    #include
    #include
    #include
    + #include
    + #include
    + #include
    + #include
    + #include




    /* Standard Macros & Definitions */
    ***************
    *** 41,47 ****


    #define EXIT(con,e) \
    ! ( con -> errno = e, \
    ((e==QUIT||e==LQUIT)&&(con->IO != NULL))?(*(con->IO))(con,e,sys_errlist[errno]):0,\
    ((con->error != NULL) && (e < 0) )?(*(con->error))(con,e,FtpMessage(e)):0,\
    e)
    --- 43,49 ----


    #define EXIT(con,e) \
    ! ( con -> last_errno = e, \
    ((e==QUIT||e==LQUIT)&&(con->IO != NULL))?(*(con->IO))(con,e,sys_errlist[errno]):0,\
    ((con->error != NULL) && (e < 0) )?(*(con->error))(con,e,FtpMessage(e)):0,\
    e)
    ***************
    *** 58,64 ****
    #define FREE(x) memset ( &(x) , '\0' , sizeof (x) )
    #define CUT(x) ((x)&0xff)
    #define FtpError(x) ((x)<0)
    ! #define FtpAssert(ftp,x) if (FtpError(x)) return EXIT((ftp),(ftp)->errno);

    typedef int STATUS;
    typedef char String[256];
    --- 60,66 ----
    #define FREE(x) memset ( &(x) , '\0' , sizeof (x) )
    #define CUT(x) ((x)&0xff)
    #define FtpError(x) ((x)<0)
    ! #define FtpAssert(ftp,x) if (FtpError(x)) return EXIT((ftp),(ftp)->last_errno);

    typedef int STATUS;
    typedef char String[256];
    ***************
    *** 83,89 ****

    FILE *data; /* Data stream to server */
    char mode; /* Binary, Ascii, ......... */
    ! int errno; /* Last error code */
    int ch; /* Help character for ascii streams */

    STATUS (*error)();
    --- 85,91 ----

    FILE *data; /* Data stream to server */
    char mode; /* Binary, Ascii, ......... */
    ! int last_errno; /* Last error code */
    int ch; /* Help character for ascii streams */

    STATUS (*error)();
    ***************
    *** 179,189 ****

    /* Manipulation commands for remote server */

    ! STATUS FtpCommand ();
    #define FtpChdir(ftp,dir) FtpCommand(ftp,"CWD %s",dir,200,250,EOF)
    #define FtpMkdir(ftp,dir) FtpCommand(ftp,"MKD %s",dir,200,257,EOF)
    #define FtpRm(ftp,dir) FtpCommand(ftp,"DELE %s",dir,200,250,EOF)
    ! char *FtpPwd(FTP *con);
    int FtpSize(FTP *con,char *filename);
    STATUS FtpMove ( FTP *con,char * old,char *new);

    --- 181,191 ----

    /* Manipulation commands for remote server */

    ! STATUS FtpCommand(FTP* con, char* command, ...);
    #define FtpChdir(ftp,dir) FtpCommand(ftp,"CWD %s",dir,200,250,EOF)
    #define FtpMkdir(ftp,dir) FtpCommand(ftp,"MKD %s",dir,200,257,EOF)
    #define FtpRm(ftp,dir) FtpCommand(ftp,"DELE %s",dir,200,250,EOF)
    ! int FtpPwd(FTP * con, char* tmp1);
    int FtpSize(FTP *con,char *filename);
    STATUS FtpMove ( FTP *con,char * old,char *new);

    ***************
    *** 192,198 ****
    STATUS FtpInitMessageList();
    STATUS FtpSendMessage( FTP * con , char * Message );
    int FtpGetMessage( FTP * con , char * Message);
    ! char *FtpMessage(int Number);
    int FtpNumber ( char * Message );


    --- 194,200 ----
    STATUS FtpInitMessageList();
    STATUS FtpSendMessage( FTP * con , char * Message );
    int FtpGetMessage( FTP * con , char * Message);
    ! char const* FtpMessage(int Number);
    int FtpNumber ( char * Message );


    ***************
    *** 204,212 ****
    #define FtpSetHashHandler(con,f) (con)->hash =f
    #define FtplibDebug(t) FtpDebug(&FtpInit)

    ! STATUS FtpDebugDebug ( FTP *con, int errno, char * Message);
    ! STATUS FtpDebugError ( FTP *con, int errno, char * Message);
    ! STATUS FtpDebugIO ( FTP *con, int errno, char * Message);
    STATUS FtpLog(char *progtitle, char *msg);
    STATUS FtpHash ( FTP *con, unsigned long number_of_bytes );
    void FtpDebug ( FTP * con );
    --- 206,214 ----
    #define FtpSetHashHandler(con,f) (con)->hash =f
    #define FtplibDebug(t) FtpDebug(&FtpInit)

    ! STATUS FtpDebugDebug ( FTP *con, int errno_, char * Message);
    ! STATUS FtpDebugError ( FTP *con, int errno_, char * Message);
    ! STATUS FtpDebugIO ( FTP *con, int errno_, char * Message);
    STATUS FtpLog(char *progtitle, char *msg);
    STATUS FtpHash ( FTP *con, unsigned long number_of_bytes );
    void FtpDebug ( FTP * con );
    ***************
    *** 221,227 ****
    FILE *Ftpfopen(char *filename,char *mode);
    STATUS Ftpfclose(FILE *);
    STATUS FtpFullClose(FILE *);
    ! STATUS FtpGood ();
    STATUS FtpGood1 (int, int *);
    struct hostent *FtpGetHost(char *host);
    STATUS FtpFilenameChecher(char *input, char *output);
    --- 223,229 ----
    FILE *Ftpfopen(char *filename,char *mode);
    STATUS Ftpfclose(FILE *);
    STATUS FtpFullClose(FILE *);
    ! STATUS FtpGood(int Number, ...);
    STATUS FtpGood1 (int, int *);
    struct hostent *FtpGetHost(char *host);
    STATUS FtpFilenameChecher(char *input, char *output);
    diff -c libftp/FtpLogin.c libftp-fixed/FtpLogin.c
    *** libftp/FtpLogin.c 2002-02-16 11:09:22.000000000 +0100
    --- libftp-fixed/FtpLogin.c 2007-10-01 03:28:02.945026741 +0200
    ***************
    *** 20,34 ****

    FtpAssert((*con),FtpConnect(con,host));
    FtpAssert((*con),FtpUser((*con),user));
    ! if (((*con)->errno)==230 )
    ! return ((*con)->errno);
    ! if (((*con)->errno)==332)
    {
    if ( account == NULL )
    ! return EXIT(((*con)),(*con)->errno);
    FtpAssert((*con),FtpAccount( (*con) , account ));
    ! if ( ((*con)->errno)==230 )
    ! return (*con)->errno;
    }
    return FtpPassword((*con),password);
    }
    --- 20,34 ----

    FtpAssert((*con),FtpConnect(con,host));
    FtpAssert((*con),FtpUser((*con),user));
    ! if (((*con)->last_errno)==230 )
    ! return ((*con)->last_errno);
    ! if (((*con)->last_errno)==332)
    {
    if ( account == NULL )
    ! return EXIT(((*con)),(*con)->last_errno);
    FtpAssert((*con),FtpAccount( (*con) , account ));
    ! if ( ((*con)->last_errno)==230 )
    ! return (*con)->last_errno;
    }
    return FtpPassword((*con),password);
    }
    diff -c libftp/FtpMessage.c libftp-fixed/FtpMessage.c
    *** libftp/FtpMessage.c 2002-02-16 11:09:22.000000000 +0100
    --- libftp-fixed/FtpMessage.c 2007-10-01 03:52:29.380594143 +0200
    ***************
    *** 86,95 ****
    return 1;
    }

    ! char *FtpMessage(int number)
    {
    ! extern int sys_nerr,errno;
    ! extern char *sys_errlist[];

    FtpInitMessageList();
    if ( number == 0 )
    --- 86,94 ----
    return 1;
    }

    ! char const* FtpMessage(int number)
    {
    ! extern int sys_nerr;

    FtpInitMessageList();
    if ( number == 0 )
    diff -c libftp/FtpPasv.c libftp-fixed/FtpPasv.c
    *** libftp/FtpPasv.c 2002-02-16 11:09:22.000000000 +0100
    --- libftp-fixed/FtpPasv.c 2007-10-01 03:56:57.535875439 +0200
    ***************
    *** 15,29 ****
    #include "FtpLibrary.h"


    ! char * FtpPasv (FTP *ftp)
    {
    ! char *msg;
    ! String PORT;
    char *p=PORT;

    if FtpError(FtpCommand(ftp,"PASV","",227,EOF))
    ! return "";
    !
    msg = FtpMessage (227);

    msg+=3;
    --- 15,31 ----
    #include "FtpLibrary.h"


    ! void FtpPasv (FTP *ftp, char* PORT)
    {
    ! char const* msg;
    char *p=PORT;

    if FtpError(FtpCommand(ftp,"PASV","",227,EOF))
    ! {
    ! *PORT = 0;
    ! return;
    ! }
    !
    msg = FtpMessage (227);

    msg+=3;
    ***************
    *** 33,40 ****

    while (isdigit(*msg)||*msg==',') *p++=*msg++;
    *p=0;
    -
    - return PORT;
    }


    --- 35,40 ----
    ***************
    *** 43,49 ****

    String PORT;

    ! strcpy(PORT,FtpPasv(ftp1));

    FtpCommand(ftp2,"PORT %s",PORT,200,EOF);
    }
    --- 43,49 ----

    String PORT;

    ! FtpPasv(ftp1, PORT);

    FtpCommand(ftp2,"PORT %s",PORT,200,EOF);
    }
    diff -c libftp/FtpPwd.c libftp-fixed/FtpPwd.c
    *** libftp/FtpPwd.c 2002-02-16 11:09:22.000000000 +0100
    --- libftp-fixed/FtpPwd.c 2007-10-01 04:02:25.478563827 +0200
    ***************
    *** 14,33 ****

    #include "FtpLibrary.h"

    ! char * FtpPwd(FTP * con)
    {
    ! String tmp,tmp1;
    int i;

    if ( FtpSendMessage(con,"PWD") == QUIT )
    ! return (char *) EXIT(con,QUIT);
    if ( (i=FtpGetMessage(con,tmp)) == QUIT )
    ! return (char *) EXIT(con,QUIT);

    if ( i != 257 )
    ! return (char *) EXIT(con,-i);

    sscanf(tmp,"%*[^\"]%*c%[^\"]%*s",tmp1);
    ! con -> errno = i;
    ! return tmp1;
    }
    --- 14,33 ----

    #include "FtpLibrary.h"

    ! int FtpPwd(FTP * con, char* tmp1)
    {
    ! String tmp;
    int i;

    if ( FtpSendMessage(con,"PWD") == QUIT )
    ! return EXIT(con,QUIT);
    if ( (i=FtpGetMessage(con,tmp)) == QUIT )
    ! return EXIT(con,QUIT);

    if ( i != 257 )
    ! return EXIT(con,-i);

    sscanf(tmp,"%*[^\"]%*c%[^\"]%*s",tmp1);
    ! con -> last_errno = i;
    ! return 0;
    }
    diff -c libftp/FtpRetr.c libftp-fixed/FtpRetr.c
    *** libftp/FtpRetr.c 2002-02-16 11:09:22.000000000 +0100
    --- libftp-fixed/FtpRetr.c 2007-10-01 03:28:43.943363103 +0200
    ***************
    *** 42,55 ****
    if ( FtpError(FtpData(con,command,in,"r")))
    {

    ! if (con->seek==0) return EXIT(con,con->errno);

    con -> seek = 0;
    fclose(o);

    if ( FtpError(FtpData(con,command,in,"r")) )
    {
    ! return EXIT(con,con->errno);
    }

    if ((o=Ftpfopen(out,"w+"))==NULL)
    --- 42,55 ----
    if ( FtpError(FtpData(con,command,in,"r")))
    {

    ! if (con->seek==0) return EXIT(con,con->last_errno);

    con -> seek = 0;
    fclose(o);

    if ( FtpError(FtpData(con,command,in,"r")) )
    {
    ! return EXIT(con,con->last_errno);
    }

    if ((o=Ftpfopen(out,"w+"))==NULL)
    diff -c libftp/FtpSize.c libftp-fixed/FtpSize.c
    *** libftp/FtpSize.c 2002-02-16 11:09:22.000000000 +0100
    --- libftp-fixed/FtpSize.c 2007-10-01 03:28:48.151602917 +0200
    ***************
    *** 28,34 ****
    return EXIT(con,QUIT);

    if ( i != 213 )
    ! return con -> errno = (-i);

    sscanf(tmp,"%*d %d",&size);
    return size;
    --- 28,34 ----
    return EXIT(con,QUIT);

    if ( i != 213 )
    ! return con -> last_errno = (-i);

    sscanf(tmp,"%*d %d",&size);
    return size;
    diff -c libftp/FtpStor.c libftp-fixed/FtpStor.c
    *** libftp/FtpStor.c 2002-02-16 11:09:22.000000000 +0100
    --- libftp-fixed/FtpStor.c 2007-10-01 03:28:56.892101008 +0200
    ***************
    *** 33,43 ****

    if ( FtpError(FtpData(con,command,out,"w")))
    {
    ! if (con->seek==0) return EXIT(con,con->errno);

    con -> seek =0;
    if ( FtpError(FtpData(con,command,out,"w")) )
    ! return EXIT(con,con->errno);
    }

    if (con->seek) fseek(i,con->seek,0);
    --- 33,43 ----

    if ( FtpError(FtpData(con,command,out,"w")))
    {
    ! if (con->seek==0) return EXIT(con,con->last_errno);

    con -> seek =0;
    if ( FtpError(FtpData(con,command,out,"w")) )
    ! return EXIT(con,con->last_errno);
    }

    if (con->seek) fseek(i,con->seek,0);

    Add a comment to this post:

    Your name:

    Your email: Email me new comments on this post
      (Your email will not be published on this site, and will only be used to contact you directly with a reply to your comment if needed. Oh, and we'll use it to send you new comments on this post it you selected that checkbox.)


    Your website:

    Comments: