Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /tags/REL-2.1/ftp_get_buffer.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 34 - (hide annotations) (download) (as text)
Sun Mar 1 12:43:14 2009 UTC (15 years, 1 month ago) by hirohitohigashi
Original Path: trunk/ftp_get_buffer.c
File MIME type: text/x-csrc
File size: 3300 byte(s)
added COPYRIGHT file. and fix some bugs.

1 hirohitohigashi 17 /*
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 <unistd.h>
13     #include <sys/types.h>
14     #include <sys/socket.h>
15 hirohitohigashi 28 #include <string.h>
16     #include <errno.h>
17 hirohitohigashi 17
18    
19     /***** Local headers ********************************************************/
20     #include "liboftp.h"
21     #include "sub.h"
22    
23    
24     /***** Constat values *******************************************************/
25     #define TRANSFER_SEGMENT_SIZE 4096 /* 一度のrecv()で転送するバイト数 */
26    
27    
28     /***** Macros ***************************************************************/
29     /***** Typedefs *************************************************************/
30     /***** Function prototypes **************************************************/
31     /***** Local variables ******************************************************/
32     /***** Global variables *****************************************************/
33     /***** Signal catching functions ********************************************/
34     /***** Local functions ******************************************************/
35     /***** Global functions *****************************************************/
36    
37     /****************************************************************************/
38     /*! バッファへファイル取得
39     *
40     *@param ftp LIBOFTPへのポインタ。
41     *@param fname サーバ上のファイル名
42     *@param buf バッファへのポインタ
43     *@param bufsiz バッファサイズ
44 hirohitohigashi 34 *@retval int 取得したバイト数 マイナス値ならエラーコード
45 hirohitohigashi 17 *@note
46     */
47     int ftp_get_buffer( LIBOFTP *ftp, const char *fname, char *buf, int bufsiz )
48     {
49     int data_socket;
50     char *p = buf;
51 hirohitohigashi 34 int res;
52 hirohitohigashi 17
53     /*
54     * 受信準備
55     */
56     if( ftp->flag_passive ) {
57 hirohitohigashi 26 data_socket = ftp_getready_pasv( ftp, "RETR", fname );
58 hirohitohigashi 17 } else {
59 hirohitohigashi 26 data_socket = ftp_getready_active( ftp, "RETR", fname );
60 hirohitohigashi 17 }
61 hirohitohigashi 20 if( data_socket < 0 ) {
62     return data_socket;
63     }
64 hirohitohigashi 17
65     /*
66     * タイムアウトが意図通りに働くように、分割してrecvする。
67     */
68     while( 1 ) {
69 hirohitohigashi 34 int n;
70 hirohitohigashi 17 int len = bufsiz;
71     if( len > TRANSFER_SEGMENT_SIZE ) {
72     len = TRANSFER_SEGMENT_SIZE;
73     }
74     n = recv( data_socket, p, len, 0 );
75     DEBUGPRINT1( "RECV: n=%d\n", n );
76     if( n < 0 ) {
77 hirohitohigashi 29 DEBUGPRINT1( "recv error. %s\n", strerror(errno) );
78     copy_strerror();
79 hirohitohigashi 17 close( data_socket );
80 hirohitohigashi 29 return LIBOFTP_ERROR_OS;
81 hirohitohigashi 17 }
82     if( n == 0 ) {
83     break;
84     }
85     p += n;
86     bufsiz -= n;
87     if( bufsiz <= 0 ) {
88     break;
89     }
90     }
91     close( data_socket );
92    
93     /*
94     * receive response.
95     */
96 hirohitohigashi 29 res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message)-1 );
97     if( res != 226 ) { /* 226: Closing data connection. */
98     DEBUGPRINT1( "got illegal response %d\n", res );
99     return res<0? res: LIBOFTP_ERROR_PROTOCOL;
100 hirohitohigashi 17 }
101    
102     if( bufsiz <= 0 ) {
103 hirohitohigashi 29 DEBUGPRINT1( "buffer too small. %s\n", "" );
104     strncpy( ftp->error_message, "buffer too small", sizeof( ftp->error_message ) - 1 );
105     return LIBOFTP_ERROR_BUFFER;
106 hirohitohigashi 17 }
107 hirohitohigashi 29
108 hirohitohigashi 34 return p - buf; /* return with transfered bytes */
109 hirohitohigashi 17 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26