| 1 |
/* |
| 2 |
liboftp: this is an FTP library to simplify the work to a Developer |
| 3 |
who want to work with FTP servers (RFC 959). |
| 4 |
|
| 5 |
Copyright (c) 2009 hirohito higashi. All rights reserved. |
| 6 |
This file is distributed under BSD license. |
| 7 |
*/ |
| 8 |
|
| 9 |
|
| 10 |
/***** Feature test switches ************************************************/ |
| 11 |
/***** System headers *******************************************************/ |
| 12 |
#include <stdio.h> |
| 13 |
#include <string.h> |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
/***** Local headers ********************************************************/ |
| 18 |
#include "liboftp.h" |
| 19 |
#include "sub.h" |
| 20 |
|
| 21 |
|
| 22 |
/***** Constat values *******************************************************/ |
| 23 |
/***** Macros ***************************************************************/ |
| 24 |
/***** Typedefs *************************************************************/ |
| 25 |
/***** Function prototypes **************************************************/ |
| 26 |
/***** Local variables ******************************************************/ |
| 27 |
/***** Global variables *****************************************************/ |
| 28 |
/***** Signal catching functions ********************************************/ |
| 29 |
/***** Local functions ******************************************************/ |
| 30 |
/***** Global functions *****************************************************/ |
| 31 |
|
| 32 |
/****************************************************************************/ |
| 33 |
/*! ��������������������� |
| 34 |
* |
| 35 |
*@param ftp LIBOFTP��������������������� |
| 36 |
*@param type {ascii|binary|image} |
| 37 |
*@retval int ������������������ |
| 38 |
*@note |
| 39 |
* FTP���TYPE������������������������������������������������������������������������������������ |
| 40 |
* CR => CRLF ������������������������������������������������������ |
| 41 |
*/ |
| 42 |
int ftp_type( LIBOFTP *ftp, const char *type ) |
| 43 |
{ |
| 44 |
char str1[256]; |
| 45 |
int res; |
| 46 |
|
| 47 |
if( ftp->socket < 0 ) return LIBOFTP_ERROR; |
| 48 |
|
| 49 |
if( strcmp( type, "ascii" ) == 0 ) { |
| 50 |
snprintf( str1, sizeof(str1)-1, "TYPE A\r\n" ); |
| 51 |
ftp->data_type = ASCII; |
| 52 |
|
| 53 |
} else if( strcmp( type, "binary" ) == 0 ) { |
| 54 |
snprintf( str1, sizeof(str1)-1, "TYPE I\r\n" ); |
| 55 |
ftp->data_type = IMAGE; |
| 56 |
|
| 57 |
} else if( strcmp( type, "image" ) == 0 ) { |
| 58 |
snprintf( str1, sizeof(str1)-1, "TYPE I\r\n" ); |
| 59 |
ftp->data_type = IMAGE; |
| 60 |
|
| 61 |
} else { |
| 62 |
DEBUGPRINT1( "argument error. %s\n", type ); |
| 63 |
strncpy( ftp->error_message, "argument error.", sizeof(ftp->error_message) ); |
| 64 |
return LIBOFTP_ERROR; |
| 65 |
} |
| 66 |
|
| 67 |
/* |
| 68 |
* send TYPE command |
| 69 |
*/ |
| 70 |
if( ftp_send_command( ftp, str1 ) < 0 ) { |
| 71 |
DEBUGPRINT1( "command sending error. %s\n", str1 ); |
| 72 |
return LIBOFTP_ERROR_OS; |
| 73 |
} |
| 74 |
|
| 75 |
res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message)-1 ); |
| 76 |
if( res != 200 ) { /* 200: Command okay. */ |
| 77 |
DEBUGPRINT1( "command response error. %d\n", res ); |
| 78 |
return res < 0? res: LIBOFTP_ERROR_PROTOCOL; |
| 79 |
} |
| 80 |
|
| 81 |
return LIBOFTP_NOERROR; |
| 82 |
} |