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 26 - (hide annotations) (download) (as text)
Sat Feb 28 02:04:32 2009 UTC (15 years, 1 month ago) by hirohitohigashi
Original Path: trunk/ftp_get_buffer.c
File MIME type: text/x-csrc
File size: 2964 byte(s)
added list and nlist functions.

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

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