• R/O
  • SSH
  • HTTPS

liboftp: Commit


Commit MetaInfo

Revisión55 (tree)
Tiempo2010-03-24 17:04:56
Autorhirohitohigashi

Log Message

Tag release 2.2

Cambiar Resumen

Diferencia incremental

--- tags/REL-2.2/liboftp.h (nonexistent)
+++ tags/REL-2.2/liboftp.h (revision 55)
@@ -0,0 +1,95 @@
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+#ifndef __libOftp_H_proto_
11+#define __libOftp_H_proto_
12+
13+/***** Feature test switches ************************************************/
14+/***** System headers *******************************************************/
15+#include <sys/types.h>
16+#include <netinet/in.h>
17+
18+
19+/***** Local headers ********************************************************/
20+/***** Constat values *******************************************************/
21+/* LIBOFTP ERROR CODE */
22+#define LIBOFTP_NOERROR 0
23+#define LIBOFTP_ERROR_OS -1
24+#define LIBOFTP_ERROR_PROTOCOL -2
25+#define LIBOFTP_ERROR -3
26+#define LIBOFTP_ERROR_BUFFER -4
27+#define LIBOFTP_ERROR_TIMEOUT -5
28+
29+
30+/***** Macros ***************************************************************/
31+
32+#ifdef __cplusplus
33+extern "C" {
34+#endif
35+
36+/***** Typedefs *************************************************************/
37+typedef struct
38+{
39+ int socket;
40+ struct sockaddr_in saddr;
41+ enum { UNIX = 1, Windows_NT = 2 } system_type;
42+ enum { ASCII = 1, IMAGE = 3 } data_type;
43+ int flag_passive;
44+ int timeout_sec;
45+
46+ int reply_code;
47+ char error_message[256];
48+
49+} LIBOFTP;
50+
51+
52+/***** Global variables *****************************************************/
53+/***** Function prototypes **************************************************/
54+int ftp_initialize( LIBOFTP *ftp );
55+int ftp_open( LIBOFTP *ftp, const char *host, int port );
56+int ftp_user( LIBOFTP *ftp, const char *user, const char *pass );
57+int ftp_passive( LIBOFTP *ftp, int flag );
58+int ftp_timeout( LIBOFTP *ftp, int sec );
59+int ftp_type( LIBOFTP *ftp, const char *type );
60+int ftp_quit( LIBOFTP *ftp );
61+int ftp_reset( LIBOFTP *ftp );
62+int ftp_site( LIBOFTP *ftp, const char *cmdline );
63+int ftp_delete( LIBOFTP *ftp, const char *fname );
64+int ftp_rename( LIBOFTP *ftp, const char *from, const char *to );
65+
66+int ftp_get_buffer( LIBOFTP *ftp, const char *fname, char *buf, int bufsiz );
67+int ftp_put_buffer( LIBOFTP *ftp, const char *buf, int bufsiz, const char *fname );
68+int ftp_append_buffer( LIBOFTP *ftp, const char *buf, int bufsiz, const char *fname );
69+int ftp_get_file( LIBOFTP *ftp, const char *fname, const char *local_fname );
70+int ftp_put_file( LIBOFTP *ftp, const char *local_fname, const char *fname );
71+int ftp_append_file( LIBOFTP *ftp, const char *local_fname, const char *fname );
72+int ftp_get_descriptor( LIBOFTP *ftp, const char *fname );
73+int ftp_get_descriptor_close( LIBOFTP *ftp, int desc );
74+int ftp_put_descriptor( LIBOFTP *ftp, const char *fname );
75+int ftp_put_descriptor_close( LIBOFTP *ftp, int desc );
76+int ftp_append_descriptor( LIBOFTP *ftp, const char *fname );
77+int ftp_append_descriptor_close( LIBOFTP *ftp, int desc );
78+
79+int ftp_list( LIBOFTP *ftp, const char *fglob, char *buf, int bufsiz );
80+int ftp_nlist( LIBOFTP *ftp, const char *fglob, char *buf, int bufsiz );
81+int ftp_mkdir( LIBOFTP *ftp, const char *dirname );
82+int ftp_rmdir( LIBOFTP *ftp, const char *dirname );
83+int ftp_pwd( LIBOFTP *ftp, char *buf, int bufsiz );
84+int ftp_cd( LIBOFTP *ftp, const char *dirname );
85+
86+
87+
88+/***** Inline functions *****************************************************/
89+
90+
91+#ifdef __cplusplus
92+}
93+#endif
94+
95+#endif
--- tags/REL-2.2/README (nonexistent)
+++ tags/REL-2.2/README (revision 55)
@@ -0,0 +1,46 @@
1+liboftp: this is an FTP library to simplify the work to a Developer
2+who want to work with FTP servers (RFC 959).
3+
4+version 2.2
5+
6+
7+■これは?
8+
9+C言語で書かれたプログラムでftp接続をサポートするライブラリです。
10+
11+
12+■使い方
13+
14+#include "liboftp.h"
15+
16+ LIBOFTP ftp;
17+ ftp_initialize( &ftp );
18+ ftp_open( &ftp, "HOSTNAME", 0 );
19+ ftp_user( &ftp, "USERNAME", "PASSWORD" );
20+ ftp_get_file( &ftp, "test.txt", "test.txt" );
21+ ftp_quit( &ftp );
22+
23+基本的にコマンドライン版 ftp を使うときのコマンドと同じセマンティクスで
24+関数をコールすることで、使えるように設計してあります。
25+example ディレクトリの中に、もう少し複雑なサンプルがあります。
26+html ディレクトリの中に、各関数のマニュアルがあります。
27+
28+
29+■歴史
30+
31+バージョン1は、Omni さんによって、開発されました。
32+ライセンスは、GPLv3で、sourceforge.net により公開されましたが、
33+2008-9-30に本人によるアナウンスでプロジェクトは停止しました。
34+
35+2009-2-18 に、hirohito が、プロジェクトを引き継ぎ、2009-2-23に
36+linux実装に依存しすぎる部分などを改良した、version 1.1をリリース
37+しました。
38+しかしながら、発見したいくつかの問題点や、私が実現したい機能を
39+実装するには、今の設計では不可能なこともわかってきました。
40+
41+そこで、バージョン2の開発に移行する形をとり、バージョン1からは
42+名前と以下の設計ポリシーのみを継承し、ゼロから設計しました。
43+
44+・ftp(CUI)のコマンドを、できるだけ真似た関数名をつける。
45+・1関数1ファイルを基本とする。
46+・できるだけシンプルに。
--- tags/REL-2.2/ftp_timeout.c (nonexistent)
+++ tags/REL-2.2/ftp_timeout.c (revision 55)
@@ -0,0 +1,63 @@
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 <sys/time.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+/***** 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 sec タイムアウト時間(秒)
37+ *@retval int エラーコード
38+ *@note
39+ */
40+int ftp_timeout( LIBOFTP *ftp, int sec )
41+{
42+ struct timeval tval;
43+
44+ ftp->timeout_sec = sec;
45+ if( ftp->socket < 0 ) {
46+ return LIBOFTP_NOERROR;
47+ }
48+
49+ tval.tv_sec = ftp->timeout_sec;
50+ tval.tv_usec = 0;
51+ if( setsockopt( ftp->socket, SOL_SOCKET, SO_SNDTIMEO, &tval, sizeof(struct timeval) ) < 0 ) {
52+ DEBUGPRINT1( "setsockopt(SO_SNDTIMEO) failed. %s\n", strerror(errno) );
53+ copy_strerror();
54+ return LIBOFTP_ERROR_OS;
55+ }
56+ if( setsockopt( ftp->socket, SOL_SOCKET, SO_RCVTIMEO, &tval, sizeof(struct timeval) ) < 0 ) {
57+ DEBUGPRINT1( "setsockopt(SO_RCVTIMEO) failed. %s\n", strerror(errno) );
58+ copy_strerror();
59+ return LIBOFTP_ERROR_OS;
60+ }
61+
62+ return LIBOFTP_NOERROR;
63+}
--- tags/REL-2.2/ftp_cd.c (nonexistent)
+++ tags/REL-2.2/ftp_cd.c (revision 55)
@@ -0,0 +1,61 @@
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+
14+
15+/***** Local headers ********************************************************/
16+#include "liboftp.h"
17+#include "sub.h"
18+
19+
20+/***** Constat values *******************************************************/
21+/***** Macros ***************************************************************/
22+/***** Typedefs *************************************************************/
23+/***** Function prototypes **************************************************/
24+/***** Local variables ******************************************************/
25+/***** Global variables *****************************************************/
26+/***** Signal catching functions ********************************************/
27+/***** Local functions ******************************************************/
28+/***** Global functions *****************************************************/
29+
30+/****************************************************************************/
31+/*! CWDコマンド
32+ *
33+ *@param ftp LIBOFTPへのポインタ。
34+ *@param dirname 対象ディレクトリ名
35+ *@retval int エラーコード
36+ *@note
37+ */
38+int ftp_cd( LIBOFTP *ftp, const char *dirname )
39+{
40+ char str1[512];
41+ int res;
42+
43+ if( ftp->socket < 0 ) return LIBOFTP_ERROR;
44+
45+ /*
46+ * send CWD command
47+ */
48+ snprintf( str1, sizeof(str1)-1, "CWD %s\r\n", dirname );
49+ if( ftp_send_command( ftp, str1 ) < 0 ) {
50+ DEBUGPRINT1( "command sending error. %s\n", str1 );
51+ return LIBOFTP_ERROR_OS;
52+ }
53+
54+ res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message)-1 );
55+ if( res != 250 ) { /* 250: Requested file action okay, completed. */
56+ DEBUGPRINT1( "command response error. %d\n", res );
57+ return res < 0? res: LIBOFTP_ERROR_PROTOCOL;
58+ }
59+
60+ return LIBOFTP_NOERROR;
61+}
--- tags/REL-2.2/ftp_pwd.c (nonexistent)
+++ tags/REL-2.2/ftp_pwd.c (revision 55)
@@ -0,0 +1,87 @@
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 <string.h>
13+
14+
15+/***** Local headers ********************************************************/
16+#include "liboftp.h"
17+#include "sub.h"
18+
19+
20+/***** Constat values *******************************************************/
21+/***** Macros ***************************************************************/
22+/***** Typedefs *************************************************************/
23+/***** Function prototypes **************************************************/
24+/***** Local variables ******************************************************/
25+/***** Global variables *****************************************************/
26+/***** Signal catching functions ********************************************/
27+/***** Local functions ******************************************************/
28+/***** Global functions *****************************************************/
29+
30+/****************************************************************************/
31+/*! PWDコマンド
32+ *
33+ *@param ftp LIBOFTPへのポインタ。
34+ *@param buf 結果代入先バッファ
35+ *@param bufsiz バッファサイズ
36+ *@retval int エラーコード
37+ *@note
38+ */
39+int ftp_pwd( LIBOFTP *ftp, char *buf, int bufsiz )
40+{
41+ char str1[512] = "PWD\r\n";
42+ int res;
43+ char *p1, *p2;
44+
45+ if( ftp->socket < 0 ) return LIBOFTP_ERROR;
46+
47+ /*
48+ * send PWD command
49+ */
50+ if( ftp_send_command( ftp, str1 ) < 0 ) {
51+ DEBUGPRINT1( "command sending error. %s\n", str1 );
52+ return LIBOFTP_ERROR_OS;
53+ }
54+
55+ if( (res = ftp_receive_response( ftp, str1, sizeof(str1) )) != 257 ) { /* 257: "PATHNAME" created. */
56+ DEBUGPRINT1( "command response error. %d\n", res );
57+ strncpy( ftp->error_message, str1, sizeof(ftp->error_message) - 1 );
58+ return res < 0? res: LIBOFTP_ERROR_PROTOCOL;
59+ }
60+
61+ /*
62+ * parse response
63+ */
64+ p1 = strchr( str1, '"' );
65+ if( p1 == NULL ) {
66+ DEBUGPRINT1( "reply string parse error. %s\n", str1 );
67+ strncpy( ftp->error_message, "reply string parse error.", sizeof( ftp->error_message ) );
68+ return LIBOFTP_ERROR_PROTOCOL;
69+ }
70+
71+ p2 = strchr( p1+1, '"' );
72+ if( p2 == NULL ) {
73+ DEBUGPRINT1( "reply string parse error. %s\n", str1 );
74+ strncpy( ftp->error_message, "reply string parse error.", sizeof( ftp->error_message ) );
75+ return LIBOFTP_ERROR_PROTOCOL;
76+ }
77+ if( bufsiz < (p2-p1) ) {
78+ DEBUGPRINT1( "buffer too small.%s", "" );
79+ strncpy( ftp->error_message, "buffer too small", sizeof( ftp->error_message ) );
80+ return LIBOFTP_ERROR_BUFFER;
81+ }
82+
83+ memcpy( buf, p1+1, p2 - p1 - 1 );
84+ buf[ p2 - p1 - 1 ] = 0;
85+
86+ return LIBOFTP_NOERROR;
87+}
--- tags/REL-2.2/ftp_open.c (nonexistent)
+++ tags/REL-2.2/ftp_open.c (revision 55)
@@ -0,0 +1,145 @@
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 <unistd.h>
13+#include <string.h>
14+#include <netdb.h>
15+#include <sys/types.h>
16+#include <sys/socket.h>
17+#include <errno.h>
18+
19+
20+/***** Local headers ********************************************************/
21+#include "liboftp.h"
22+#include "sub.h"
23+
24+
25+/***** Constat values *******************************************************/
26+/***** Macros ***************************************************************/
27+/***** Typedefs *************************************************************/
28+/***** Function prototypes **************************************************/
29+/***** Local variables ******************************************************/
30+/***** Global variables *****************************************************/
31+/***** Signal catching functions ********************************************/
32+/***** Local functions ******************************************************/
33+static const char * my_hstrerror( int no )
34+{
35+ switch( no ) {
36+ case HOST_NOT_FOUND:
37+ return "Unknown host";
38+
39+ case TRY_AGAIN:
40+ return "Host name lookup failure";
41+
42+ case NO_RECOVERY:
43+ return "Unknown server error";
44+
45+ case NO_DATA:
46+ return "No address associated with name";
47+
48+ default:
49+ return "Unknown error";
50+ }
51+}
52+
53+
54+
55+/***** Global functions *****************************************************/
56+
57+/****************************************************************************/
58+/*! ftp構造体初期化
59+ *
60+ *@param ftp LIBOFTPへのポインタ。
61+ *@retval int エラーコード
62+ *@note
63+ * ftp構造体は、呼出側で確保する。
64+ */
65+int ftp_initialize( LIBOFTP *ftp )
66+{
67+ memset( ftp, 0, sizeof( LIBOFTP ) );
68+ ftp->socket = -1;
69+ ftp->timeout_sec = 10; /* default timeout */
70+
71+ return LIBOFTP_NOERROR;
72+}
73+
74+
75+
76+/****************************************************************************/
77+/*! ftpサーバへ接続
78+ *
79+ *@param ftp LIBOFTPへのポインタ。
80+ *@param host ホスト名または、IPアドレス文字列。
81+ *@param port ポート番号。ゼロならデフォルトを使用する。
82+ *@retval int エラーコード
83+ *@note
84+ * ftp構造体は、呼出側で確保する。
85+ */
86+int ftp_open( LIBOFTP *ftp, const char *host, int port )
87+{
88+ struct hostent *p_hostent;
89+ int res;
90+
91+ if( ftp->socket >= 0 ) return LIBOFTP_ERROR;
92+ if( port == 0 ) port = 21;
93+
94+ /*
95+ * search host entry
96+ */
97+ p_hostent = gethostbyname( host );
98+ if( p_hostent == NULL ) {
99+ DEBUGPRINT1( "gethostbyname() function failed. %s\n", my_hstrerror(h_errno) );
100+ strncpy( ftp->error_message, my_hstrerror(h_errno), sizeof(ftp->error_message)-1 );
101+ return LIBOFTP_ERROR_OS;
102+ }
103+ ftp->saddr.sin_family = AF_INET;
104+ memcpy( &ftp->saddr.sin_addr, p_hostent->h_addr, p_hostent->h_length );
105+ ftp->saddr.sin_port = htons( port );
106+
107+ /*
108+ * make control connection.
109+ */
110+ ftp->socket = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP );
111+ if( ftp->socket < 0 ) {
112+ DEBUGPRINT1( "can't create socket. %s\n", strerror(errno) );
113+ copy_strerror();
114+ ftp->socket = -1;
115+ return LIBOFTP_ERROR_OS;
116+ }
117+
118+ if( connect( ftp->socket, (struct sockaddr *)&(ftp->saddr), sizeof(struct sockaddr) ) < 0 ) {
119+ DEBUGPRINT1( "can't connect socket. %s\n", strerror(errno) );
120+ copy_strerror();
121+ close( ftp->socket );
122+ ftp->socket = -1;
123+ return LIBOFTP_ERROR_OS;
124+ }
125+
126+ res = ftp_timeout( ftp, ftp->timeout_sec );
127+ if( res < 0 ) {
128+ close( ftp->socket );
129+ ftp->socket = -1;
130+ return res;
131+ }
132+
133+ /*
134+ * receive banner
135+ */
136+ res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message) );
137+ if( res != 220 ) { /* 220: Service ready for new user. */
138+ DEBUGPRINT1( "Illegal response. %d\n", res );
139+ close( ftp->socket );
140+ ftp->socket = -1;
141+ return res < 0? res: LIBOFTP_ERROR_PROTOCOL;
142+ }
143+
144+ return LIBOFTP_NOERROR;
145+}
--- tags/REL-2.2/ftp_put_buffer.c (nonexistent)
+++ tags/REL-2.2/ftp_put_buffer.c (revision 55)
@@ -0,0 +1,137 @@
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 <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+
34+
35+/****************************************************************************/
36+/*! バッファからファイル送信
37+ *
38+ *@param ftp LIBOFTPへのポインタ。
39+ *@param buf バッファへのポインタ
40+ *@param bufsiz バッファサイズ
41+ *@param fname サーバ上のファイル名
42+ *@param cmd 送信するFTPコマンド
43+ *@retval int エラーコード
44+ *@note
45+ */
46+static int ftp_put_buffer_main( LIBOFTP *ftp, const char *buf, int bufsiz, const char *fname, const char *cmd )
47+{
48+ int data_socket;
49+ const char *p = buf;
50+ int n, res;
51+
52+ if( ftp->socket < 0 ) return LIBOFTP_ERROR;
53+
54+ /*
55+ * 送信準備
56+ */
57+ if( ftp->flag_passive ) {
58+ data_socket = ftp_getready_pasv( ftp, cmd, fname );
59+ } else {
60+ data_socket = ftp_getready_active( ftp, cmd, fname );
61+ }
62+ if( data_socket < 0 ) {
63+ return data_socket;
64+ }
65+
66+ /*
67+ * タイムアウトが意図通りに働くように、分割してsendする。
68+ */
69+ while( 1 ) {
70+ int len = bufsiz;
71+ if( len > TRANSFER_SEGMENT_SIZE ) {
72+ len = TRANSFER_SEGMENT_SIZE;
73+ }
74+ n = sendn( data_socket, p, len, 0 );
75+ DEBUGPRINT1( "SEND: n=%d\n", n );
76+ if( n < 0 ) {
77+ DEBUGPRINT1( "recv error. %s\n", strerror(errno) );
78+ copy_strerror();
79+ close( data_socket );
80+ return LIBOFTP_ERROR_OS;
81+ }
82+
83+ p += n;
84+ bufsiz -= n;
85+ if( bufsiz <= 0 ) {
86+ break;
87+ }
88+ }
89+ close( data_socket );
90+
91+ /*
92+ * receive response.
93+ */
94+ res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message)-1 );
95+ if( res != 226 ) { /* 226: Closing data connection. */
96+ DEBUGPRINT1( "got illegal response %d\n", res );
97+ return res < 0? res: LIBOFTP_ERROR_PROTOCOL;
98+ }
99+
100+ return LIBOFTP_NOERROR;
101+}
102+
103+
104+
105+/***** Global functions *****************************************************/
106+
107+/****************************************************************************/
108+/*! バッファからファイル送信
109+ *
110+ *@param ftp LIBOFTPへのポインタ。
111+ *@param buf バッファへのポインタ
112+ *@param bufsiz バッファサイズ
113+ *@param fname サーバ上のファイル名
114+ *@retval int エラーコード
115+ *@note
116+ */
117+int ftp_put_buffer( LIBOFTP *ftp, const char *buf, int bufsiz, const char *fname )
118+{
119+ return ftp_put_buffer_main( ftp, buf, bufsiz, fname, "STOR" );
120+}
121+
122+
123+
124+/****************************************************************************/
125+/*! バッファからファイル送信 アペンドモード
126+ *
127+ *@param ftp LIBOFTPへのポインタ。
128+ *@param buf バッファへのポインタ
129+ *@param bufsiz バッファサイズ
130+ *@param fname サーバ上のファイル名
131+ *@retval int エラーコード
132+ *@note
133+ */
134+int ftp_append_buffer( LIBOFTP *ftp, const char *buf, int bufsiz, const char *fname )
135+{
136+ return ftp_put_buffer_main( ftp, buf, bufsiz, fname, "APPE" );
137+}
--- tags/REL-2.2/ftp_delete.c (nonexistent)
+++ tags/REL-2.2/ftp_delete.c (revision 55)
@@ -0,0 +1,61 @@
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+
14+
15+/***** Local headers ********************************************************/
16+#include "liboftp.h"
17+#include "sub.h"
18+
19+
20+/***** Constat values *******************************************************/
21+/***** Macros ***************************************************************/
22+/***** Typedefs *************************************************************/
23+/***** Function prototypes **************************************************/
24+/***** Local variables ******************************************************/
25+/***** Global variables *****************************************************/
26+/***** Signal catching functions ********************************************/
27+/***** Local functions ******************************************************/
28+/***** Global functions *****************************************************/
29+
30+/****************************************************************************/
31+/*! DELEコマンド
32+ *
33+ *@param ftp LIBOFTPへのポインタ。
34+ *@param fname 対象ファイル名
35+ *@retval int エラーコード
36+ *@note
37+ */
38+int ftp_delete( LIBOFTP *ftp, const char *fname )
39+{
40+ char str1[512];
41+ int res;
42+
43+ if( ftp->socket < 0 ) return LIBOFTP_ERROR;
44+
45+ /*
46+ * send DELE command
47+ */
48+ snprintf( str1, sizeof(str1)-1, "DELE %s\r\n", fname );
49+ if( ftp_send_command( ftp, str1 ) < 0 ) {
50+ DEBUGPRINT1( "command sending error. %s\n", str1 );
51+ return LIBOFTP_ERROR_OS;
52+ }
53+
54+ res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message)-1 );
55+ if( res != 250 ) { /* 250: Requested file action okay, completed. */
56+ DEBUGPRINT1( "command response error. %d\n", res );
57+ return res < 0? res: LIBOFTP_ERROR_PROTOCOL;
58+ }
59+
60+ return LIBOFTP_NOERROR;
61+}
--- tags/REL-2.2/ftp_site.c (nonexistent)
+++ tags/REL-2.2/ftp_site.c (revision 55)
@@ -0,0 +1,61 @@
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+
14+
15+/***** Local headers ********************************************************/
16+#include "liboftp.h"
17+#include "sub.h"
18+
19+
20+/***** Constat values *******************************************************/
21+/***** Macros ***************************************************************/
22+/***** Typedefs *************************************************************/
23+/***** Function prototypes **************************************************/
24+/***** Local variables ******************************************************/
25+/***** Global variables *****************************************************/
26+/***** Signal catching functions ********************************************/
27+/***** Local functions ******************************************************/
28+/***** Global functions *****************************************************/
29+
30+/****************************************************************************/
31+/*! SITEコマンド
32+ *
33+ *@param ftp LIBOFTPへのポインタ。
34+ *@param cmdline コマンドライン
35+ *@retval int エラーコード
36+ *@note
37+ */
38+int ftp_site( LIBOFTP *ftp, const char *cmdline )
39+{
40+ char str1[512];
41+ int res;
42+
43+ if( ftp->socket < 0 ) return LIBOFTP_ERROR;
44+
45+ /*
46+ * send SITE command
47+ */
48+ snprintf( str1, sizeof(str1)-1, "SITE %s\r\n", cmdline );
49+ if( ftp_send_command( ftp, str1 ) < 0 ) {
50+ DEBUGPRINT1( "site: command sending error. %s\n", str1 );
51+ return LIBOFTP_ERROR_OS;
52+ }
53+
54+ res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message)-1 );
55+ if( res != 200 ) { /* 200: Command okay. */
56+ DEBUGPRINT1( "command response error. %d\n", res );
57+ return res < 0? res: LIBOFTP_ERROR_PROTOCOL;
58+ }
59+
60+ return LIBOFTP_NOERROR;
61+}
--- tags/REL-2.2/ftp_mkdir.c (nonexistent)
+++ tags/REL-2.2/ftp_mkdir.c (revision 55)
@@ -0,0 +1,61 @@
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+
14+
15+/***** Local headers ********************************************************/
16+#include "liboftp.h"
17+#include "sub.h"
18+
19+
20+/***** Constat values *******************************************************/
21+/***** Macros ***************************************************************/
22+/***** Typedefs *************************************************************/
23+/***** Function prototypes **************************************************/
24+/***** Local variables ******************************************************/
25+/***** Global variables *****************************************************/
26+/***** Signal catching functions ********************************************/
27+/***** Local functions ******************************************************/
28+/***** Global functions *****************************************************/
29+
30+/****************************************************************************/
31+/*! MKDコマンド
32+ *
33+ *@param ftp LIBOFTPへのポインタ。
34+ *@param dirname 対象ディレクトリ名
35+ *@retval int エラーコード
36+ *@note
37+ */
38+int ftp_mkdir( LIBOFTP *ftp, const char *dirname )
39+{
40+ char str1[512];
41+ int res;
42+
43+ if( ftp->socket < 0 ) return LIBOFTP_ERROR;
44+
45+ /*
46+ * send MKD command
47+ */
48+ snprintf( str1, sizeof(str1)-1, "MKD %s\r\n", dirname );
49+ if( ftp_send_command( ftp, str1 ) < 0 ) {
50+ DEBUGPRINT1( "command sending error. %s\n", str1 );
51+ return LIBOFTP_ERROR_OS;
52+ }
53+
54+ res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message)-1 );
55+ if( res != 257 ) { /* 257: "PATHNAME" created. */
56+ DEBUGPRINT1( "command response error. %d\n", res );
57+ return res < 0? res: LIBOFTP_ERROR_PROTOCOL;
58+ }
59+
60+ return LIBOFTP_NOERROR;
61+}
--- tags/REL-2.2/ftp_put_descriptor.c (nonexistent)
+++ tags/REL-2.2/ftp_put_descriptor.c (revision 55)
@@ -0,0 +1,122 @@
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 <unistd.h>
13+
14+
15+/***** Local headers ********************************************************/
16+#include "liboftp.h"
17+#include "sub.h"
18+
19+
20+/***** Constat values *******************************************************/
21+/***** Macros ***************************************************************/
22+/***** Typedefs *************************************************************/
23+/***** Function prototypes **************************************************/
24+/***** Local variables ******************************************************/
25+/***** Global variables *****************************************************/
26+/***** Signal catching functions ********************************************/
27+/***** Local functions ******************************************************/
28+/***** Global functions *****************************************************/
29+
30+/****************************************************************************/
31+/*! ファイル送信用デスクリプタ生成
32+ *
33+ *@param ftp LIBOFTPへのポインタ。
34+ *@param fname サーバ上のファイル名
35+ *@retval int デスクリプタ。マイナス値ならエラーコード
36+ *@note
37+ */
38+int ftp_put_descriptor( LIBOFTP *ftp, const char *fname )
39+{
40+ int data_socket;
41+
42+ if( ftp->socket < 0 ) return LIBOFTP_ERROR;
43+
44+ if( ftp->flag_passive ) {
45+ data_socket = ftp_getready_pasv( ftp, "STOR", fname );
46+ } else {
47+ data_socket = ftp_getready_active( ftp, "STOR", fname );
48+ }
49+ return data_socket;
50+}
51+
52+
53+
54+/****************************************************************************/
55+/*! ファイル送信用デスクリプタ生成 アペンドモード
56+ *
57+ *@param ftp LIBOFTPへのポインタ。
58+ *@param fname サーバ上のファイル名
59+ *@retval int デスクリプタ。マイナス値ならエラーコード
60+ *@note
61+ */
62+int ftp_append_descriptor( LIBOFTP *ftp, const char *fname )
63+{
64+ int data_socket;
65+
66+ if( ftp->socket < 0 ) return LIBOFTP_ERROR;
67+
68+ if( ftp->flag_passive ) {
69+ data_socket = ftp_getready_pasv( ftp, "APPE", fname );
70+ } else {
71+ data_socket = ftp_getready_active( ftp, "APPE", fname );
72+ }
73+ return data_socket;
74+}
75+
76+
77+
78+/****************************************************************************/
79+/*! ファイル送信用デスクリプタクローズ
80+ *
81+ *@param ftp LIBOFTPへのポインタ。
82+ *@param desc デスクリプタ
83+ *@retval int エラーコード
84+ *@note
85+ */
86+int ftp_put_descriptor_close( LIBOFTP *ftp, int desc )
87+{
88+ int res;
89+
90+ /*
91+ * close socket.
92+ */
93+ close( desc );
94+
95+ if( ftp->socket < 0 ) return LIBOFTP_ERROR;
96+
97+ /*
98+ * receive response.
99+ */
100+ res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message)-1 );
101+ if( res != 226 ) { /* 226: Closing data connection. */
102+ DEBUGPRINT1( "got illegal response %d\n", res );
103+ return res < 0? res: LIBOFTP_ERROR_PROTOCOL;
104+ }
105+
106+ return LIBOFTP_NOERROR;
107+}
108+
109+
110+
111+/****************************************************************************/
112+/*! ファイル送信用デスクリプタクローズ アペンドモード
113+ *
114+ *@param ftp LIBOFTPへのポインタ。
115+ *@param desc デスクリプタ
116+ *@retval int エラーコード
117+ *@note
118+ */
119+int ftp_append_descriptor_close( LIBOFTP *ftp, int desc )
120+{
121+ return ftp_put_descriptor_close( ftp, desc );
122+}
--- tags/REL-2.2/ftp_rename.c (nonexistent)
+++ tags/REL-2.2/ftp_rename.c (revision 55)
@@ -0,0 +1,77 @@
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+
14+
15+/***** Local headers ********************************************************/
16+#include "liboftp.h"
17+#include "sub.h"
18+
19+
20+/***** Constat values *******************************************************/
21+/***** Macros ***************************************************************/
22+/***** Typedefs *************************************************************/
23+/***** Function prototypes **************************************************/
24+/***** Local variables ******************************************************/
25+/***** Global variables *****************************************************/
26+/***** Signal catching functions ********************************************/
27+/***** Local functions ******************************************************/
28+/***** Global functions *****************************************************/
29+
30+/****************************************************************************/
31+/*! ファイルリネーム
32+ *
33+ *@param ftp LIBOFTPへのポインタ。
34+ *@param from 対象ファイル名
35+ *@param to 変更後ファイル名
36+ *@retval int エラーコード
37+ *@note
38+ */
39+int ftp_rename( LIBOFTP *ftp, const char *from, const char *to )
40+{
41+ char str1[512];
42+ int res;
43+
44+ if( ftp->socket < 0 ) return LIBOFTP_ERROR;
45+
46+ /*
47+ * send RNFR command
48+ */
49+ snprintf( str1, sizeof(str1)-1, "RNFR %s\r\n", from );
50+ if( ftp_send_command( ftp, str1 ) < 0 ) {
51+ DEBUGPRINT1( "RNFR command sending error. %s\n", str1 );
52+ return LIBOFTP_ERROR_OS;
53+ }
54+
55+ res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message)-1 );
56+ if( res != 350 ) { /* 350: Requested file action pending further information */
57+ DEBUGPRINT1( "RNFR command response error. %d\n", res );
58+ return res < 0? res: LIBOFTP_ERROR_PROTOCOL;
59+ }
60+
61+ /*
62+ * send RNTO command
63+ */
64+ snprintf( str1, sizeof(str1)-1, "RNTO %s\r\n", to );
65+ if( ftp_send_command( ftp, str1 ) < 0 ) {
66+ DEBUGPRINT1( "rename: RNTO command sending error. %s\n", str1 );
67+ return LIBOFTP_ERROR_OS;
68+ }
69+
70+ res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message)-1 );
71+ if( res != 250 ) { /* 250: Requested file action okay, completed. */
72+ DEBUGPRINT1( "RNTO command response error. %d\n", res );
73+ return res < 0? res: LIBOFTP_ERROR_PROTOCOL;
74+ }
75+
76+ return LIBOFTP_NOERROR;
77+}
--- tags/REL-2.2/ftp_put_file.c (nonexistent)
+++ tags/REL-2.2/ftp_put_file.c (revision 55)
@@ -0,0 +1,151 @@
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 <unistd.h>
13+#include <sys/types.h>
14+#include <sys/socket.h>
15+#include <sys/uio.h>
16+#include <fcntl.h>
17+#include <string.h>
18+#include <errno.h>
19+
20+
21+/***** Local headers ********************************************************/
22+#include "liboftp.h"
23+#include "sub.h"
24+
25+
26+/***** Constat values *******************************************************/
27+#define TRANSFER_SEGMENT_SIZE 1024 /* 一度のrecv()で転送するバイト数 */
28+
29+
30+/***** Macros ***************************************************************/
31+/***** Typedefs *************************************************************/
32+/***** Function prototypes **************************************************/
33+/***** Local variables ******************************************************/
34+/***** Global variables *****************************************************/
35+/***** Signal catching functions ********************************************/
36+/***** Local functions ******************************************************/
37+/****************************************************************************/
38+/*! ファイル送信メイン
39+ *
40+ *@param ftp LIBOFTPへのポインタ。
41+ *@param local_fname ローカルファイル名
42+ *@param fname サーバ上のファイル名
43+ *@param cmd 送信するFTPコマンド
44+ *@retval int エラーコード
45+ *@note
46+ */
47+static int ftp_put_file_main( LIBOFTP *ftp, const char *local_fname, const char *fname, const char *cmd )
48+{
49+ int data_socket;
50+ int fd;
51+ char buf[TRANSFER_SEGMENT_SIZE];
52+ int res;
53+
54+ if( ftp->socket < 0 ) return LIBOFTP_ERROR;
55+
56+ /*
57+ * ローカルファイルオープン
58+ */
59+ fd = open( local_fname, O_RDONLY );
60+ if( fd < 0 ) {
61+ DEBUGPRINT1( "local file open error. %s\n", strerror(errno) );
62+ return LIBOFTP_ERROR_OS;
63+ }
64+
65+ /*
66+ * 送信準備
67+ */
68+ if( ftp->flag_passive ) {
69+ data_socket = ftp_getready_pasv( ftp, cmd, fname );
70+ } else {
71+ data_socket = ftp_getready_active( ftp, cmd, fname );
72+ }
73+ if( data_socket < 0 ) {
74+ close( fd );
75+ return data_socket;
76+ }
77+
78+ /*
79+ * タイムアウトが意図通りに働くように、分割してsendする。
80+ */
81+ while( 1 ) {
82+ int n_rd, n_wr;
83+ n_rd = read( fd, buf, TRANSFER_SEGMENT_SIZE );
84+ if( n_rd == 0 ) break;
85+
86+ if( n_rd < 0 ) {
87+ if( errno == EINTR ) continue;
88+
89+ DEBUGPRINT1( "local file read error. %s\n", strerror(errno) );
90+ copy_strerror();
91+ close( data_socket );
92+ return LIBOFTP_ERROR_OS;
93+ }
94+
95+ n_wr = sendn( data_socket, buf, n_rd, 0 );
96+ DEBUGPRINT1( "SEND: n=%d\n", n_wr );
97+ if( n_wr < 0 ) {
98+ copy_strerror();
99+ close( data_socket );
100+ return LIBOFTP_ERROR_OS;
101+ }
102+ }
103+ close( fd );
104+ close( data_socket );
105+
106+ /*
107+ * receive response.
108+ */
109+ res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message)-1 );
110+ if( res != 226 ) { /* 226: Closing data connection. */
111+ DEBUGPRINT1( "got illegal response %d\n", res );
112+ return res<0? res: LIBOFTP_ERROR_PROTOCOL;
113+ }
114+
115+ return LIBOFTP_NOERROR;
116+}
117+
118+
119+
120+/***** Global functions *****************************************************/
121+
122+/****************************************************************************/
123+/*! ファイル送信
124+ *
125+ *@param ftp LIBOFTPへのポインタ。
126+ *@param local_fname ローカルファイル名
127+ *@param fname サーバ上のファイル名
128+ *@retval int エラーコード
129+ *@note
130+ */
131+int ftp_put_file( LIBOFTP *ftp, const char *local_fname, const char *fname )
132+{
133+ return ftp_put_file_main( ftp, local_fname, fname, "STOR" );
134+}
135+
136+
137+
138+/****************************************************************************/
139+/*! ファイル送信 アペンドモード
140+ *
141+ *@param ftp LIBOFTPへのポインタ。
142+ *@param local_fname ローカルファイル名
143+ *@param fname サーバ上のファイル名
144+ *@retval int エラーコード
145+ *@note
146+ */
147+int ftp_append_file( LIBOFTP *ftp, const char *local_fname, const char *fname )
148+{
149+ return ftp_put_file_main( ftp, local_fname, fname, "APPE" );
150+}
151+
--- tags/REL-2.2/ftp_get_buffer.c (nonexistent)
+++ tags/REL-2.2/ftp_get_buffer.c (revision 55)
@@ -0,0 +1,208 @@
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 <unistd.h>
13+#include <string.h>
14+#include <sys/types.h>
15+#include <sys/socket.h>
16+
17+
18+/***** Local headers ********************************************************/
19+#include "liboftp.h"
20+#include "sub.h"
21+
22+
23+/***** Constat values *******************************************************/
24+#define TRANSFER_SEGMENT_SIZE 4096 /* 一度のrecv()で転送するバイト数 */
25+
26+
27+/***** Macros ***************************************************************/
28+/***** Typedefs *************************************************************/
29+/***** Function prototypes **************************************************/
30+/***** Local variables ******************************************************/
31+/***** Global variables *****************************************************/
32+/***** Signal catching functions ********************************************/
33+/***** Local functions ******************************************************/
34+
35+/****************************************************************************/
36+/*! バッファへリスト取得 作業ルーチン
37+ *
38+ *@param ftp LIBOFTPへのポインタ。
39+ *@param cmd コマンド (RETR|LIST|NLST)
40+ *@param fname ファイル名または、グロブ。
41+ *@param buf バッファへのポインタ
42+ *@param bufsiz バッファサイズ
43+ *@retval int 取得したバイト数 マイナス値ならエラーコード
44+ *@note
45+ */
46+static int ftp_get_buffer_main( LIBOFTP *ftp, const char *cmd, const char *fname, char *buf, int bufsiz )
47+{
48+ int data_socket;
49+ char *p = buf;
50+ int res;
51+
52+ if( ftp->socket < 0 ) return LIBOFTP_ERROR;
53+
54+ /*
55+ * 受信準備
56+ */
57+ if( ftp->flag_passive ) {
58+ data_socket = ftp_getready_pasv( ftp, cmd, fname );
59+ } else {
60+ data_socket = ftp_getready_active( ftp, cmd, fname );
61+ }
62+ if( data_socket < 0 ) {
63+ return data_socket;
64+ }
65+
66+ /*
67+ * タイムアウトが意図通りに働くように、分割してrecvする。
68+ */
69+ while( 1 ) {
70+ int n;
71+ int len = bufsiz;
72+ if( len > TRANSFER_SEGMENT_SIZE ) {
73+ len = TRANSFER_SEGMENT_SIZE;
74+ }
75+ n = recv( data_socket, p, len, 0 );
76+ DEBUGPRINT1( "RECV: n=%d\n", n );
77+ if( n < 0 ) {
78+ int ret;
79+ DEBUGPRINT1( "recv error. %s\n", strerror(errno) );
80+ if( errno == EAGAIN ) {
81+ ret = LIBOFTP_ERROR_TIMEOUT;
82+ } else {
83+ ret = LIBOFTP_ERROR_OS;
84+ copy_strerror();
85+ }
86+ close( data_socket );
87+ return ret;
88+ }
89+ if( n == 0 ) {
90+ break;
91+ }
92+ p += n;
93+ bufsiz -= n;
94+ if( bufsiz <= 0 ) {
95+ break; /* buffer too small */
96+ }
97+ }
98+
99+ if( bufsiz > 0 ) { /* バッファ不足だったか? */
100+ /*
101+ * 不足していない
102+ */
103+ close( data_socket );
104+
105+ /* receive response. */
106+ res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message)-1 );
107+ if( res != 226 ) { /* 226: Closing data connection. */
108+ DEBUGPRINT1( "got illegal response %d\n", res );
109+ return res < 0? res: LIBOFTP_ERROR_PROTOCOL;
110+ }
111+
112+ return p - buf; /* return with transfered bytes */
113+
114+ } else {
115+ /*
116+ * バッファ不足時
117+ */
118+ DEBUGPRINT1( "buffer too small. %s\n", "" );
119+ strncpy( ftp->error_message, "buffer too small", sizeof( ftp->error_message ) - 1 );
120+
121+ if( ftp_send_command( ftp, "ABOR\r\n" ) < 0 ) {
122+ DEBUGPRINT1( "command sending error. %s\n", "ABOR" );
123+ close( data_socket );
124+ return LIBOFTP_ERROR_BUFFER;
125+ }
126+
127+ res = ftp_receive_response( ftp, 0, 0 );
128+ if( res == 426 ) { /* 426: Connection closed; transfer aborted. */
129+ res = ftp_receive_response( ftp, 0, 0 );
130+ }
131+ close( data_socket );
132+ if( res != 226 ) { /* 226: Closing data connection. */
133+ DEBUGPRINT1( "got illegal response %d\n", res );
134+ return res < 0? res: LIBOFTP_ERROR_PROTOCOL;
135+ }
136+
137+ return LIBOFTP_ERROR_BUFFER;
138+ }
139+}
140+
141+
142+
143+/***** Global functions *****************************************************/
144+
145+/****************************************************************************/
146+/*! バッファへファイル取得
147+ *
148+ *@param ftp LIBOFTPへのポインタ。
149+ *@param fname サーバ上のファイル名
150+ *@param buf バッファへのポインタ
151+ *@param bufsiz バッファサイズ
152+ *@retval int 取得したバイト数 マイナス値ならエラーコード
153+ *@note
154+ */
155+int ftp_get_buffer( LIBOFTP *ftp, const char *fname, char *buf, int bufsiz )
156+{
157+ return ftp_get_buffer_main( ftp, "RETR", fname, buf, bufsiz );
158+}
159+
160+
161+
162+/****************************************************************************/
163+/*! ディレクトリリスト(LIST) 取得
164+ *
165+ *@param ftp LIBOFTPへのポインタ。
166+ *@param fglob ファイルリストグロブ (ex: *.txt) or NULL
167+ *@param buf バッファへのポインタ
168+ *@param bufsiz バッファサイズ
169+ *@retval int エラーコード
170+ *@note
171+ */
172+int ftp_list( LIBOFTP *ftp, const char *fglob, char *buf, int bufsiz )
173+{
174+ int ret;
175+
176+ ret = ftp_get_buffer_main( ftp, "LIST", fglob, buf, bufsiz );
177+ if( ret > 0 ) {
178+ buf[ret] = 0;
179+ return LIBOFTP_NOERROR;
180+ }
181+
182+ return ret;
183+}
184+
185+
186+
187+/****************************************************************************/
188+/*! ディレクトリリスト(NLST) 取得
189+ *
190+ *@param ftp LIBOFTPへのポインタ。
191+ *@param fglob ファイルリストグロブ (ex: *.txt) or NULL
192+ *@param buf バッファへのポインタ
193+ *@param bufsiz バッファサイズ
194+ *@retval int エラーコード
195+ *@note
196+ */
197+int ftp_nlist( LIBOFTP *ftp, const char *fglob, char *buf, int bufsiz )
198+{
199+ int ret;
200+
201+ ret = ftp_get_buffer_main( ftp, "NLST", fglob, buf, bufsiz );
202+ if( ret > 0 ) {
203+ buf[ret] = 0;
204+ return LIBOFTP_NOERROR;
205+ }
206+
207+ return ret;
208+}
--- tags/REL-2.2/ftp_get_descriptor.c (nonexistent)
+++ tags/REL-2.2/ftp_get_descriptor.c (revision 55)
@@ -0,0 +1,84 @@
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 <unistd.h>
13+
14+
15+/***** Local headers ********************************************************/
16+#include "liboftp.h"
17+#include "sub.h"
18+
19+
20+/***** Constat values *******************************************************/
21+/***** Macros ***************************************************************/
22+/***** Typedefs *************************************************************/
23+/***** Function prototypes **************************************************/
24+/***** Local variables ******************************************************/
25+/***** Global variables *****************************************************/
26+/***** Signal catching functions ********************************************/
27+/***** Local functions ******************************************************/
28+/***** Global functions *****************************************************/
29+
30+/****************************************************************************/
31+/*! ファイル取得用デスクリプタ生成
32+ *
33+ *@param ftp LIBOFTPへのポインタ。
34+ *@param fname サーバ上のファイル名
35+ *@retval int デスクリプタ。マイナス値ならエラーコード
36+ *@note
37+ */
38+int ftp_get_descriptor( LIBOFTP *ftp, const char *fname )
39+{
40+ int data_socket;
41+
42+ if( ftp->socket < 0 ) return LIBOFTP_ERROR;
43+
44+ if( ftp->flag_passive ) {
45+ data_socket = ftp_getready_pasv( ftp, "RETR", fname );
46+ } else {
47+ data_socket = ftp_getready_active( ftp, "RETR", fname );
48+ }
49+
50+ return data_socket;
51+}
52+
53+
54+
55+/****************************************************************************/
56+/*! ファイル取得用デスクリプタクローズ
57+ *
58+ *@param ftp LIBOFTPへのポインタ。
59+ *@param desc デスクリプタ
60+ *@retval int エラーコード
61+ *@note
62+ */
63+int ftp_get_descriptor_close( LIBOFTP *ftp, int desc )
64+{
65+ int res;
66+
67+ /*
68+ * close socket.
69+ */
70+ close( desc );
71+
72+ if( ftp->socket < 0 ) return LIBOFTP_ERROR;
73+
74+ /*
75+ * receive response.
76+ */
77+ res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message)-1 );
78+ if( res != 226 ) { /* 226: Closing data connection. */
79+ DEBUGPRINT1( "got illegal response %d\n", res );
80+ return res<0? res: LIBOFTP_ERROR_PROTOCOL;
81+ }
82+
83+ return LIBOFTP_NOERROR;
84+}
--- tags/REL-2.2/ftp_rmdir.c (nonexistent)
+++ tags/REL-2.2/ftp_rmdir.c (revision 55)
@@ -0,0 +1,61 @@
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+
14+
15+/***** Local headers ********************************************************/
16+#include "liboftp.h"
17+#include "sub.h"
18+
19+
20+/***** Constat values *******************************************************/
21+/***** Macros ***************************************************************/
22+/***** Typedefs *************************************************************/
23+/***** Function prototypes **************************************************/
24+/***** Local variables ******************************************************/
25+/***** Global variables *****************************************************/
26+/***** Signal catching functions ********************************************/
27+/***** Local functions ******************************************************/
28+/***** Global functions *****************************************************/
29+
30+/****************************************************************************/
31+/*! RMDコマンド
32+ *
33+ *@param ftp LIBOFTPへのポインタ。
34+ *@param dirname 対象ディレクトリ名
35+ *@retval int エラーコード
36+ *@note
37+ */
38+int ftp_rmdir( LIBOFTP *ftp, const char *dirname )
39+{
40+ char str1[512];
41+ int res;
42+
43+ if( ftp->socket < 0 ) return LIBOFTP_ERROR;
44+
45+ /*
46+ * send RMD command
47+ */
48+ snprintf( str1, sizeof(str1)-1, "RMD %s\r\n", dirname );
49+ if( ftp_send_command( ftp, str1 ) < 0 ) {
50+ DEBUGPRINT1( "command sending error. %s\n", str1 );
51+ return LIBOFTP_ERROR_OS;
52+ }
53+
54+ res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message)-1 );
55+ if( res != 250 ) { /* 250: Requested file action okay, completed. */
56+ DEBUGPRINT1( "command response error. %d\n", res );
57+ return res < 0? res: LIBOFTP_ERROR_PROTOCOL;
58+ }
59+
60+ return LIBOFTP_NOERROR;
61+}
--- tags/REL-2.2/ftp_get_file.c (nonexistent)
+++ tags/REL-2.2/ftp_get_file.c (revision 55)
@@ -0,0 +1,123 @@
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 <unistd.h>
13+#include <sys/types.h>
14+#include <sys/socket.h>
15+#include <sys/uio.h>
16+#include <fcntl.h>
17+#include <string.h>
18+#include <errno.h>
19+
20+
21+/***** Local headers ********************************************************/
22+#include "liboftp.h"
23+#include "sub.h"
24+
25+
26+/***** Constat values *******************************************************/
27+#define TRANSFER_SEGMENT_SIZE 1024 /* 一度のrecv()で転送するバイト数 */
28+
29+
30+/***** Macros ***************************************************************/
31+/***** Typedefs *************************************************************/
32+/***** Function prototypes **************************************************/
33+/***** Local variables ******************************************************/
34+/***** Global variables *****************************************************/
35+/***** Signal catching functions ********************************************/
36+/***** Local functions ******************************************************/
37+/***** Global functions *****************************************************/
38+
39+/****************************************************************************/
40+/*! ファイル取得してローカルファイルへ
41+ *
42+ *@param ftp LIBOFTPへのポインタ。
43+ *@param fname サーバ上のファイル名
44+ *@param local_fname ローカルファイル名
45+ *@retval int エラーコード
46+ *@note
47+ */
48+int ftp_get_file( LIBOFTP *ftp, const char *fname, const char *local_fname )
49+{
50+ int data_socket;
51+ int n, res;
52+ int fd;
53+ char buf[TRANSFER_SEGMENT_SIZE];
54+
55+ if( ftp->socket < 0 ) return LIBOFTP_ERROR;
56+
57+ /*
58+ * ローカルファイルオープン
59+ */
60+ fd = open( local_fname, O_WRONLY|O_CREAT, 0644 );
61+ if( fd < 0 ) {
62+ DEBUGPRINT1( "local file open error. %s\n", strerror(errno) );
63+ copy_strerror();
64+ return LIBOFTP_ERROR_OS;
65+ }
66+
67+ /*
68+ * 受信準備
69+ */
70+ if( ftp->flag_passive ) {
71+ data_socket = ftp_getready_pasv( ftp, "RETR", fname );
72+ } else {
73+ data_socket = ftp_getready_active( ftp, "RETR", fname );
74+ }
75+ if( data_socket < 0 ) {
76+ close( fd );
77+ return data_socket;
78+ }
79+ /*
80+ * タイムアウトが意図通りに働くように、分割してrecvする。
81+ */
82+ while( 1 ) {
83+ n = recv( data_socket, buf, TRANSFER_SEGMENT_SIZE, 0 );
84+ DEBUGPRINT1( "RECV: n=%d\n", n );
85+ if( n < 0 ) {
86+ int ret;
87+ DEBUGPRINT1( "recv error. %s\n", strerror(errno) );
88+ if( errno == EAGAIN ) {
89+ ret = LIBOFTP_ERROR_TIMEOUT;
90+ } else {
91+ ret = LIBOFTP_ERROR_OS;
92+ copy_strerror();
93+ }
94+ close( fd );
95+ close( data_socket );
96+ return ret;
97+ }
98+ if( n == 0 ) {
99+ break;
100+ }
101+
102+ if( write( fd, buf, n ) != n ) {
103+ DEBUGPRINT1( "write error. %s\n", strerror(errno) );
104+ copy_strerror();
105+ close( fd );
106+ close( data_socket );
107+ return LIBOFTP_ERROR_OS;
108+ }
109+ }
110+ close( fd );
111+ close( data_socket );
112+
113+ /*
114+ * receive response.
115+ */
116+ res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message)-1 );
117+ if( res != 226 ) { /* 226: Closing data connection. */
118+ DEBUGPRINT1( "got illegal response %d\n", res );
119+ return res < 0? res: LIBOFTP_ERROR_PROTOCOL;
120+ }
121+
122+ return LIBOFTP_NOERROR;
123+}
--- tags/REL-2.2/ftp_user.c (nonexistent)
+++ tags/REL-2.2/ftp_user.c (revision 55)
@@ -0,0 +1,102 @@
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+/***** Local headers ********************************************************/
17+#include "liboftp.h"
18+#include "sub.h"
19+
20+
21+/***** Constat values *******************************************************/
22+/***** Macros ***************************************************************/
23+/***** Typedefs *************************************************************/
24+/***** Function prototypes **************************************************/
25+/***** Local variables ******************************************************/
26+/***** Global variables *****************************************************/
27+/***** Signal catching functions ********************************************/
28+/***** Local functions ******************************************************/
29+/***** Global functions *****************************************************/
30+
31+/****************************************************************************/
32+/*! ユーザ認証
33+ *
34+ *@param ftp LIBOFTPへのポインタ。
35+ *@param user ユーザ名
36+ *@param pass パスワード
37+ *@retval int エラーコード
38+ *@note
39+ * 認証後、定型的なコマンドも送信する。
40+ */
41+int ftp_user( LIBOFTP *ftp, const char *user, const char *pass )
42+{
43+ char str1[256];
44+ int res;
45+
46+ if( ftp->socket < 0 ) return LIBOFTP_ERROR;
47+
48+ /*
49+ * send user name.
50+ */
51+ snprintf( str1, sizeof(str1)-1, "USER %s\r\n", user );
52+ if( ftp_send_command( ftp, str1 ) < 0 ) {
53+ DEBUGPRINT1( "user command sending error.%s\n", "" );
54+ return LIBOFTP_ERROR_OS;
55+ }
56+
57+ res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message) );
58+ if( res == 230 ) { /* 230: User logged in, proceed. */
59+ goto PROCEED;
60+ }
61+ if( res != 331 ) { /* 331: User name okay, need password. */
62+ DEBUGPRINT1( "USER command response error. %d\n", res );
63+ return res < 0? res: LIBOFTP_ERROR_PROTOCOL;
64+ }
65+
66+ /*
67+ * send password.
68+ */
69+ snprintf( str1, sizeof(str1)-1, "PASS %s\r\n", pass );
70+ if( ftp_send_command( ftp, str1 ) < 0 ) {
71+ DEBUGPRINT1( "pass command sending error.%s\n", "" );
72+ return LIBOFTP_ERROR_OS;
73+ }
74+
75+ res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message) );
76+ if( res != 230 ) { /* 230: User logged in, proceed. */
77+ DEBUGPRINT1( "user authentication error.%s\n", "" );
78+ return res < 0? res: LIBOFTP_ERROR_PROTOCOL;
79+ }
80+
81+PROCEED:
82+ /*
83+ * system type.
84+ */
85+ snprintf( str1, sizeof(str1)-1, "SYST\r\n" );
86+ if( ftp_send_command( ftp, str1 ) < 0 ) {
87+ DEBUGPRINT1( "SYST command sending error.%s\n", "" );
88+ return LIBOFTP_ERROR_OS;
89+ }
90+
91+ res = ftp_receive_response( ftp, str1, sizeof(str1) );
92+ if( res == 215 ) {
93+ if( strstr( str1, "UNIX" ) != 0 ) {
94+ ftp->system_type = UNIX;
95+ } else if( strstr( str1, "Windows_NT" ) != 0 ) {
96+ ftp->system_type = Windows_NT;
97+ }
98+ DEBUGPRINT1( "system type is %d\n", ftp->system_type );
99+ }
100+
101+ return LIBOFTP_NOERROR;
102+}
--- tags/REL-2.2/ftp_type.c (nonexistent)
+++ tags/REL-2.2/ftp_type.c (revision 55)
@@ -0,0 +1,82 @@
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+}
--- tags/REL-2.2/ftp_quit.c (nonexistent)
+++ tags/REL-2.2/ftp_quit.c (revision 55)
@@ -0,0 +1,69 @@
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 <unistd.h>
13+
14+
15+/***** Local headers ********************************************************/
16+#include "liboftp.h"
17+#include "sub.h"
18+
19+
20+/***** Constat values *******************************************************/
21+/***** Macros ***************************************************************/
22+/***** Typedefs *************************************************************/
23+/***** Function prototypes **************************************************/
24+/***** Local variables ******************************************************/
25+/***** Global variables *****************************************************/
26+/***** Signal catching functions ********************************************/
27+/***** Local functions ******************************************************/
28+/***** Global functions *****************************************************/
29+
30+/****************************************************************************/
31+/*! QUITコマンド
32+ *
33+ *@param ftp LIBOFTPへのポインタ。
34+ *@retval int エラーコード
35+ *@note
36+ */
37+int ftp_quit( LIBOFTP *ftp )
38+{
39+ char str1[] = "QUIT\r\n";
40+ int res;
41+
42+ if( ftp->socket < 0 ) return LIBOFTP_ERROR;
43+
44+ /*
45+ * send QUIT command
46+ */
47+ if( ftp_send_command( ftp, str1 ) < 0 ) {
48+ DEBUGPRINT1( "command sending error. %s\n", str1 );
49+ res = LIBOFTP_ERROR_OS;
50+ goto CLOSE;
51+ }
52+
53+ res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message)-1 );
54+ if( res == 221 ) { /* 221: Service closing control connection. */
55+ res = LIBOFTP_NOERROR;
56+ } else {
57+ DEBUGPRINT1( "command response error. %d\n", res );
58+ if( res >= 0 ) res = LIBOFTP_ERROR_PROTOCOL;
59+ }
60+
61+ /*
62+ * close socket.
63+ */
64+CLOSE:
65+ close( ftp->socket );
66+ ftp->socket = -1;
67+
68+ return res;
69+}
--- tags/REL-2.2/ftp_reset.c (nonexistent)
+++ tags/REL-2.2/ftp_reset.c (revision 55)
@@ -0,0 +1,72 @@
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+/***** Local headers ********************************************************/
13+#include "liboftp.h"
14+#include "sub.h"
15+
16+
17+/***** Constat values *******************************************************/
18+/***** Macros ***************************************************************/
19+/***** Typedefs *************************************************************/
20+/***** Function prototypes **************************************************/
21+/***** Local variables ******************************************************/
22+/***** Global variables *****************************************************/
23+/***** Signal catching functions ********************************************/
24+/***** Local functions ******************************************************/
25+/***** Global functions *****************************************************/
26+
27+/****************************************************************************/
28+/*! エラー回復
29+ *
30+ *@param ftp LIBOFTPへのポインタ。
31+ *@retval int エラーコード
32+ *@note
33+ * エラー等によりftpコマンドシーケンスが乱れたとき、再同期を試みるために
34+ * リプライキューをクリアします。
35+ */
36+int ftp_reset( LIBOFTP *ftp )
37+{
38+ int now_timeout_sec = ftp->timeout_sec;
39+ int ret;
40+ int i;
41+
42+ if( ftp->socket < 0 ) return LIBOFTP_ERROR;
43+
44+ /*
45+ * send "CRLF" only
46+ */
47+ if( ftp_send_command( ftp, "\r\n" ) < 0 ) {
48+ DEBUGPRINT1( "command sending error. %s\n", "CRLF" );
49+ return LIBOFTP_ERROR_OS;
50+ }
51+
52+ /*
53+ * receive response until timeout. 100 cycle maxmum.
54+ */
55+ ftp_timeout( ftp, 1 ); /* set 1 sec */
56+
57+ for( i = 0; i < 100; i++ ) {
58+ ret = ftp_receive_response( ftp, 0, 0 );
59+ if( ret == LIBOFTP_ERROR_TIMEOUT ) {
60+ ret = LIBOFTP_NOERROR;
61+ break;
62+ }
63+
64+ if( ret == LIBOFTP_ERROR_OS ) {
65+ DEBUGPRINT1( "os level error occurred.\n%s", "" );
66+ break;
67+ }
68+ }
69+
70+ ftp_timeout( ftp, now_timeout_sec );
71+ return ret;
72+}
--- tags/REL-2.2/sub.c (nonexistent)
+++ tags/REL-2.2/sub.c (revision 55)
@@ -0,0 +1,484 @@
1+/*
2+ Some global functions for libOftp.
3+
4+ Copyright (c) 2009-2009 hirohito higashi. All rights reserved.
5+ This file is distributed under BSD license.
6+*/
7+
8+
9+/***** Feature test switches ************************************************/
10+/***** System headers *******************************************************/
11+#include <stdio.h>
12+#include <stdlib.h>
13+#include <unistd.h>
14+#include <sys/types.h>
15+#include <sys/socket.h>
16+#include <errno.h>
17+#include <string.h>
18+#include <arpa/inet.h>
19+#include <sys/time.h>
20+#include <sys/select.h>
21+
22+
23+/***** Local headers ********************************************************/
24+#include "liboftp.h"
25+#include "sub.h"
26+
27+
28+/***** Constat values *******************************************************/
29+/***** Macros ***************************************************************/
30+/***** Typedefs *************************************************************/
31+/***** Function prototypes **************************************************/
32+/***** Local variables ******************************************************/
33+/***** Global variables *****************************************************/
34+/***** Signal catching functions ********************************************/
35+/***** Local functions ******************************************************/
36+/****************************************************************************/
37+/*! read a line
38+ *
39+ *@param sd socket descriptor
40+ *@param buf pointer of receive buffer
41+ *@param bufsiz buffer size
42+ *@param flags flags of recv()
43+ *@retval int number of bytes received, or -1 if an error.
44+ *@note
45+ *
46+ */
47+static int recv_line( int sd, char *buf, int bufsiz, int flags )
48+{
49+ int n;
50+ char c;
51+ char *p = buf;
52+ int total_len = 0;
53+
54+ bufsiz--;
55+
56+ while( 1 ) {
57+ n = recv( sd, &c, 1, flags );
58+ if( n < 0 ) {
59+ if( errno == EINTR ) continue;
60+ *p = 0;
61+ DEBUGPRINT1( "recv_line: get minus. %s\n", strerror(errno) );
62+ return n;
63+ }
64+ if( n == 0 ) {
65+ DEBUGPRINT1( "recv_line: get zero. %s\n", strerror(errno) );
66+ break;
67+ }
68+
69+ *p++ = c;
70+ total_len++;
71+
72+ if( c == '\n' ) break;
73+ if( bufsiz < total_len ) break;
74+ }
75+ *p = 0;
76+
77+ return total_len;
78+}
79+
80+
81+
82+/***** Global functions *****************************************************/
83+
84+/****************************************************************************/
85+/*! send n bytes.
86+ *
87+ *@param sd socket descriptor
88+ *@param buf pointer of send buffer
89+ *@param len message length
90+ *@param flags flags of send()
91+ *@retval int number of bytes sent, or -1 if an error.
92+ *@note
93+ *
94+ */
95+int sendn( int sd, const char *buf, int len, int flags )
96+{
97+ int left = len;
98+ int n;
99+
100+ while( left > 0 ) {
101+ n = send( sd, buf, left, flags );
102+ if( n < 0 ) {
103+ if( errno == EINTR ) continue;
104+ DEBUGPRINT1( "sendn: get minus. %s\n", strerror(errno) );
105+ return n;
106+ }
107+ buf += n;
108+ left -= n;
109+ }
110+
111+ return len;
112+}
113+
114+
115+
116+/****************************************************************************/
117+/*! send ftp command
118+ *
119+ *@param ftp pointer of LIBOFTP.
120+ *@param cmd ftp command.
121+ *@retval int 0 is no error, or -1 if an error.
122+ *@note
123+ *
124+ */
125+int ftp_send_command( LIBOFTP *ftp, const char *cmd )
126+{
127+ int ret;
128+ DEBUGPRINT1( "SENDC: %s", cmd );
129+ ret = sendn( ftp->socket, cmd, strlen( cmd ), 0 );
130+ if( ret < 0 ) {
131+ copy_strerror();
132+ return ret;
133+ }
134+
135+ return 0;
136+}
137+
138+
139+
140+/****************************************************************************/
141+/*! receive response
142+ *
143+ *@param ftp pointer of LIBOFTP.
144+ *@param res_buf response buffer ( null ok )
145+ *@param bufsiz response buffer size.
146+ *@retval int reply code, or error code (minus value)
147+ *@note
148+ * set reply code to ftp attribute reply_code;
149+ */
150+int ftp_receive_response( LIBOFTP *ftp, char *res_buf, int bufsiz )
151+{
152+ char str1[256];
153+ int n;
154+
155+ ftp->reply_code = 0;
156+ if( res_buf ) {
157+ *res_buf = 0;
158+ }
159+
160+ /*
161+ * receive 1st line.
162+ */
163+ if( (n = recv_line( ftp->socket, str1, sizeof(str1), 0 )) < 0 ) {
164+ if( errno == EAGAIN ) {
165+ return LIBOFTP_ERROR_TIMEOUT;
166+ }
167+ copy_strerror();
168+ return LIBOFTP_ERROR_OS;
169+ }
170+ DEBUGPRINT1( "RESP1: %s\n", str1 );
171+ if( n < 3 ) {
172+ return LIBOFTP_ERROR_PROTOCOL;
173+ }
174+ ftp->reply_code = atoi( str1 );
175+
176+ /*
177+ * if exist next line, receive all response.
178+ */
179+ if( str1[3] == '-' ) {
180+ while( 1 ) {
181+ if( (n = recv_line( ftp->socket, str1, sizeof(str1), 0 )) < 0 ) {
182+ if( errno == EAGAIN ) {
183+ return LIBOFTP_ERROR_TIMEOUT;
184+ }
185+ copy_strerror();
186+ return LIBOFTP_ERROR_OS;
187+ }
188+ DEBUGPRINT1( "RESP2: %s\n", str1 );
189+ if( n < 3 ) {
190+ return LIBOFTP_ERROR_PROTOCOL;
191+ }
192+
193+ if( atoi(str1) == ftp->reply_code && str1[3] == ' ' ) {
194+ break;
195+ }
196+ }
197+ }
198+
199+ /*
200+ * copy a response strings to user buffer, if need
201+ */
202+ if( res_buf ) {
203+ strncpy( res_buf, str1, bufsiz );
204+ res_buf[bufsiz-1] = 0;
205+ }
206+
207+ return ftp->reply_code;
208+}
209+
210+
211+
212+/****************************************************************************/
213+/*! 送受信準備 アクティブモード
214+ *
215+ *@param ftp LIBOFTPへのポインタ。
216+ *@param cmd FTPコマンド (ex: RETR, STOR, APPE)
217+ *@param fname サーバ上のファイル名
218+ *@retval int ソケット, マイナス値ならエラーコード
219+ *@note
220+ アクティブモードで送受信の際の、以下の3ステートをサポートする。
221+ 例) 受信時
222+ (port open) --> PORT --> RETR
223+ */
224+int ftp_getready_active( LIBOFTP *ftp, const char *cmd, const char *fname )
225+{
226+ int sock_listen, sock_accept = -1;
227+ struct sockaddr_in saddr;
228+ int saddr_len;
229+ unsigned char *ip, *pt;
230+ char str1[256];
231+ int res;
232+ struct timeval timeout;
233+ fd_set rfds, wfds;
234+ int flag_status_ok = 0;
235+
236+ /*
237+ * open data port.
238+ * (note)
239+ * same command port ip and automatic random port.
240+ */
241+ sock_listen = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP );
242+ if( sock_listen < 0 ) {
243+ DEBUGPRINT1( "getready_active: can't open socket. %s\n", strerror(errno) );
244+ goto ERROR_OS;
245+ }
246+
247+ memset( &saddr, 0, sizeof( saddr ) );
248+ saddr_len = sizeof( saddr );
249+ if( getsockname( ftp->socket, (struct sockaddr *)&saddr, &saddr_len ) < 0 ) {
250+ DEBUGPRINT1( "getready_active: can't get control socket name. %s\n", strerror(errno) );
251+ goto ERROR_OS;
252+ }
253+ saddr.sin_port = htons( 0 );
254+
255+ if( bind( sock_listen, (struct sockaddr *)&saddr, sizeof( saddr ) ) < 0 ) {
256+ DEBUGPRINT1( "getready_active: can't bind socket. %s\n", strerror(errno) );
257+ goto ERROR_OS;
258+ }
259+
260+ if( listen( sock_listen, 1 ) < 0 ) {
261+ DEBUGPRINT1( "getready_active: can't listen socket. %s\n", strerror(errno) );
262+ goto ERROR_OS;
263+ }
264+
265+ /*
266+ * make PORT command.
267+ */
268+ memset( &saddr, 0, sizeof( saddr ) );
269+ saddr_len = sizeof( saddr );
270+ if( getsockname( sock_listen, (struct sockaddr *)&saddr, &saddr_len ) < 0 ) {
271+ DEBUGPRINT1( "getready_active: can't get data socket name. %s\n", strerror(errno) );
272+ goto ERROR_OS;
273+ }
274+ ip = (unsigned char*)&saddr.sin_addr.s_addr;
275+ pt = (unsigned char*)&saddr.sin_port;
276+ snprintf( str1, sizeof(str1), "PORT %d,%d,%d,%d,%d,%d\r\n", ip[0], ip[1], ip[2], ip[3], pt[0], pt[1] );
277+
278+ /*
279+ * send PORT command and get status.
280+ */
281+ if( ftp_send_command( ftp, str1 ) < 0 ) {
282+ DEBUGPRINT1( "getready_active: %s command sending error.\n", "PORT" );
283+ close( sock_listen );
284+ return LIBOFTP_ERROR_OS;
285+ }
286+ res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message) - 1 );
287+ if( res != 200 ) { /* 200: Command okay. */
288+ DEBUGPRINT1( "getready_active: get PORT response code %d\n", res );
289+ close( sock_listen );
290+ return LIBOFTP_ERROR_PROTOCOL;
291+ }
292+
293+ /*
294+ * send ftp command.
295+ */
296+ if( fname ) {
297+ snprintf( str1, sizeof(str1), "%s %s\r\n", cmd, fname );
298+ } else {
299+ snprintf( str1, sizeof(str1), "%s\r\n", cmd );
300+ }
301+ if( ftp_send_command( ftp, str1 ) < 0 ) {
302+ DEBUGPRINT1( "getready_active: command sending error. %s", str1 );
303+ close( sock_listen );
304+ return LIBOFTP_ERROR_OS;
305+ }
306+
307+ /*
308+ * accept data connection with timeout, or get status by control connection.
309+ */
310+ timeout.tv_sec = ftp->timeout_sec;
311+ timeout.tv_usec = 0;
312+ do {
313+ int nfds = (sock_listen > ftp->socket)? sock_listen: ftp->socket;
314+ FD_ZERO( &rfds );
315+ FD_ZERO( &wfds );
316+ if( sock_listen >= 0) {
317+ FD_SET( sock_listen, &rfds );
318+ FD_SET( sock_listen, &wfds );
319+ }
320+ FD_SET( ftp->socket, &rfds );
321+
322+ res = select( nfds+1, &rfds, &wfds, 0, &timeout );
323+ /* ckeck timeout */
324+ if( res == 0 ) {
325+ DEBUGPRINT1( "getready_active: waiting connection timeout.%s\n", "" );
326+ close( sock_listen );
327+ strncpy( ftp->error_message, "connection timeout.", sizeof( ftp->error_message )-1 );
328+ return LIBOFTP_ERROR_TIMEOUT;
329+ }
330+
331+ /* check some OS errors. */
332+ if( res < 0 ) {
333+ if( errno == EINTR ) continue;
334+ DEBUGPRINT1( "getready_active: select error. %s\n", strerror(errno) );
335+ goto ERROR_OS;
336+ }
337+
338+ /* accept data connection. */
339+ if( FD_ISSET( sock_listen, &rfds ) || FD_ISSET( sock_listen, &wfds ) ) {
340+ sock_accept = accept( sock_listen, (struct sockaddr *)&saddr, &saddr_len );
341+ close( sock_listen );
342+ sock_listen = -1;
343+ if( sock_accept < 0 ) {
344+ DEBUGPRINT1( "getready_active: socket accept error. %s\n", strerror(errno) );
345+ goto ERROR_OS;
346+ }
347+ DEBUGPRINT1( "getready_active: socket accept. fd=%d\n", sock_accept );
348+ }
349+
350+ /* get status. */
351+ if( FD_ISSET( ftp->socket, &rfds ) ) {
352+ res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message) - 1 );
353+ DEBUGPRINT1( "getready_active: get STOR/RETR response code %d\n", res );
354+ if( res != 150 ) { /* 150: File status okay; about to open data connection. */
355+ if( sock_listen >= 0 ) close( sock_listen );
356+ if( sock_accept >= 0 ) close( sock_accept );
357+ return LIBOFTP_ERROR_PROTOCOL;
358+ }
359+ flag_status_ok = 1;
360+ }
361+
362+ } while( !( sock_accept >= 0 && flag_status_ok ) );
363+
364+ return sock_accept;
365+
366+
367+ERROR_OS:
368+ copy_strerror();
369+ if( sock_listen >= 0 ) {
370+ close( sock_listen );
371+ }
372+ return LIBOFTP_ERROR_OS;
373+}
374+
375+
376+
377+/****************************************************************************/
378+/*! 送受信準備 パッシブモード
379+ *
380+ *@param ftp LIBOFTPへのポインタ。
381+ *@param cmd FTPコマンド (ex: RETR, STOR, APPE)
382+ *@param fname サーバ上のファイル名
383+ *@retval int ソケット, マイナス値ならエラーコード
384+ *@note
385+ パッシブモードで送受信の際の、以下の3ステートをサポートする。
386+ 例) 受信時
387+ PASV --> RETR --> (port open)
388+ */
389+int ftp_getready_pasv( LIBOFTP *ftp, const char *cmd, const char *fname )
390+{
391+ char str1[256];
392+ int res;
393+ char *p;
394+ int h1, h2, h3, h4, p1, p2;
395+ struct sockaddr_in saddr;
396+ int sock;
397+ struct timeval tval;
398+
399+ /*
400+ * send "PASV" command and get status.
401+ */
402+ str1[255] = 0;
403+ snprintf( str1, sizeof(str1)-1, "PASV\r\n" );
404+ if( ftp_send_command( ftp, str1 ) < 0 ) {
405+ return LIBOFTP_ERROR_OS;
406+ }
407+ res = ftp_receive_response( ftp, str1, sizeof(str1) - 1 );
408+ if( res != 227 ) { /* 227: Entering Passive Mode */
409+ DEBUGPRINT1( "getready_pasv: get ftp response code %d\n", res );
410+ strncpy( ftp->error_message, str1, sizeof( ftp->error_message ) - 1 );
411+ return LIBOFTP_ERROR_PROTOCOL;
412+ }
413+
414+ /*
415+ * parse (h1,h2,h3,h4,p1,p2)
416+ */
417+ p = strchr( str1, '(' );
418+ if( p == NULL ) {
419+ DEBUGPRINT1( "getready_pasv: response parse error. %d", 1 );
420+ strncpy( ftp->error_message, "Illegal pasv response", sizeof(ftp->error_message) - 1 );
421+ return LIBOFTP_ERROR_PROTOCOL;
422+ }
423+ if( sscanf( p+1, "%d,%d,%d,%d,%d,%d", &h1, &h2, &h3, &h4, &p1, &p2 ) != 6 ) {
424+ DEBUGPRINT1( "getready_pasv: response parse error. %d", 2 );
425+ strncpy( ftp->error_message, "Illegal pasv response", sizeof(ftp->error_message) - 1 );
426+ return LIBOFTP_ERROR_PROTOCOL;
427+ }
428+
429+
430+ /*
431+ * send ftp command.
432+ */
433+ if( fname ) {
434+ snprintf( str1, sizeof(str1), "%s %s\r\n", cmd, fname );
435+ } else {
436+ snprintf( str1, sizeof(str1), "%s\r\n", cmd );
437+ }
438+ if( ftp_send_command( ftp, str1 ) < 0 ) {
439+ DEBUGPRINT1( "getready_pasv: command sending error. %s", str1 );
440+ return LIBOFTP_ERROR_OS;
441+ }
442+
443+ /*
444+ * open data port.
445+ */
446+ sock = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP );
447+ if( sock < 0 ) {
448+ DEBUGPRINT1( "getready_pasv: can't open socket. %s\n", strerror(errno) );
449+ copy_strerror();
450+ return LIBOFTP_ERROR_OS;
451+ }
452+
453+ memset( &saddr, 0, sizeof( saddr ) );
454+ saddr.sin_family = AF_INET;
455+ saddr.sin_addr.s_addr = htonl( (((unsigned long)h1) << 24) | (h2 << 16) | (h3 << 8) | h4 );
456+ saddr.sin_port = htons( (p1 << 8) | p2 );
457+
458+ if( connect( sock, (struct sockaddr *)&saddr, sizeof( saddr ) ) < 0 ) {
459+ DEBUGPRINT1( "getready_pasv: can't connect socket. %s\n", strerror(errno) );
460+ copy_strerror();
461+ close( sock );
462+ return LIBOFTP_ERROR_OS;
463+ }
464+
465+ tval.tv_sec = ftp->timeout_sec;
466+ tval.tv_usec = 0;
467+ if( setsockopt( sock, SOL_SOCKET, SO_RCVTIMEO, &tval, sizeof(struct timeval) ) < 0 ) {
468+ DEBUGPRINT1( "setsockopt(SO_RCVTIMEO) failed. %s\n", strerror(errno) );
469+ copy_strerror();
470+ close( sock );
471+ return LIBOFTP_ERROR_OS;
472+ }
473+
474+ /*
475+ * check response.
476+ */
477+ res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message) - 1 );
478+ if( res == 150 || res == 125 ) {
479+ return sock;
480+ } else {
481+ close( sock );
482+ return LIBOFTP_ERROR_PROTOCOL;
483+ }
484+}
--- tags/REL-2.2/sub.h (nonexistent)
+++ tags/REL-2.2/sub.h (revision 55)
@@ -0,0 +1,65 @@
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+#ifndef __libOftp_Sub_H_proto_
11+#define __libOftp_Sub_H_proto_
12+
13+/***** Feature test switches ************************************************/
14+#if 0
15+#define FTP_DEBUG
16+#endif
17+
18+
19+/***** System headers *******************************************************/
20+#include <string.h>
21+#include <errno.h>
22+#ifdef FTP_DEBUG
23+#include <stdio.h>
24+#endif
25+
26+
27+/***** Local headers ********************************************************/
28+/***** Constat values *******************************************************/
29+/***** Macros ***************************************************************/
30+/***** Typedefs *************************************************************/
31+#ifdef FTP_DEBUG
32+#define DEBUGPRINT1(f,a1) printf( "(" __FILE__ ") " f,(a1))
33+#else
34+#define DEBUGPRINT1(f,a1)
35+#endif
36+
37+#define copy_strerror() \
38+ strncpy( ftp->error_message, strerror(errno), sizeof(ftp->error_message)-1 );
39+
40+
41+
42+/***** Global variables *****************************************************/
43+/***** Function prototypes **************************************************/
44+#ifdef __cplusplus
45+extern "C" {
46+#endif
47+int sendn( int sd, const char *buf, int len, int flags );
48+int ftp_send_command( LIBOFTP *ftp, const char *cmd );
49+int ftp_receive_response( LIBOFTP *ftp, char *res_buf, int bufsiz );
50+int ftp_getready_active( LIBOFTP *ftp, const char *cmd, const char *fname );
51+int ftp_getready_pasv( LIBOFTP *ftp, const char *cmd, const char *fname );
52+
53+
54+#if defined( __QNX__ ) && !defined( __QNXNTO__ )
55+int snprintf( char *, int, char *, ... );
56+#endif
57+
58+
59+/***** Inline functions *****************************************************/
60+
61+#ifdef __cplusplus
62+}
63+#endif
64+
65+#endif
--- tags/REL-2.2/Makefile (nonexistent)
+++ tags/REL-2.2/Makefile (revision 55)
@@ -0,0 +1,48 @@
1+# Makefile
2+#
3+# How to run:
4+# $ make
5+# # make install
6+# $ make clean <- clean
7+# # make uninstall <- uninstall
8+
9+INSTDIR = /usr/local
10+TARGET = liboftp.a
11+
12+CFLAGS = -Wall -O2
13+ARFLAGS = -rs
14+SRCS = sub.c ftp_open.c ftp_user.c ftp_passive.c ftp_timeout.c ftp_type.c ftp_quit.c \
15+ ftp_site.c ftp_delete.c ftp_rename.c ftp_reset.c \
16+ ftp_get_buffer.c ftp_put_buffer.c ftp_get_file.c ftp_put_file.c \
17+ ftp_get_descriptor.c ftp_put_descriptor.c \
18+ ftp_mkdir.c ftp_rmdir.c ftp_pwd.c ftp_cd.c
19+OBJS = $(SRCS:.c=.o)
20+
21+
22+
23+$(TARGET): $(OBJS)
24+ $(AR) $(ARFLAGS) $@ $?
25+
26+
27+all: $(TARGET) examples
28+
29+
30+clean:
31+ @rm -f $(TARGET) $(OBJS) *~ *.core
32+ @cd example ; rm -f example1 example2 *~ *.core
33+
34+
35+examples:
36+ cd example; \
37+ $(CC) -I.. $(CFLAGS) -o example1 example1.c ../$(TARGET) ; \
38+ $(CC) -I.. $(CFLAGS) -o example2 example2.c ../$(TARGET)
39+
40+
41+install: $(TARGET)
42+ cp $(TARGET) $(INSTDIR)/lib/
43+ cp liboftp.h $(INSTDIR)/include/
44+
45+
46+uninstall:
47+ rm $(INSTDIR)/lib/$(TARGET)
48+ rm $(INSTDIR)/include/liboftp.h
--- tags/REL-2.2/example/example1.c (nonexistent)
+++ tags/REL-2.2/example/example1.c (revision 55)
@@ -0,0 +1,144 @@
1+#include <stdio.h>
2+#include <stdlib.h>
3+
4+#include "liboftp.h"
5+
6+
7+#define HOSTNAME "YOUR FTP HOSTNAME HERE"
8+#define USERNAME "USER NAME HERE"
9+#define PASSWORD "PASSWORD HERE"
10+
11+
12+int total_err = 0;
13+
14+void err_handle( int err, LIBOFTP *ftp )
15+{
16+ printf( "===> error code %d", err );
17+ if( err < 0 ) {
18+ total_err++;
19+ printf( " : %s", ftp->error_message );
20+ }
21+ printf( "\n\n" );
22+}
23+
24+
25+
26+int main()
27+{
28+ LIBOFTP ftp;
29+ int err;
30+
31+ ftp_initialize( &ftp );
32+
33+ printf( "=====[ Connect and login ]=====\n" );
34+ printf( "<=== Trying ftp_open()\n" );
35+ err = ftp_open( &ftp, HOSTNAME, 0 );
36+ err_handle( err, &ftp );
37+
38+ printf( "<=== Trying ftp_user()\n" );
39+ err = ftp_user( &ftp, USERNAME, PASSWORD );
40+ err_handle( err, &ftp );
41+
42+ err = ftp_passive( &ftp, 1 );
43+
44+ printf( "<=== Trying ftp_type()\n" );
45+ err = ftp_type( &ftp, "image" );
46+ err_handle( err, &ftp );
47+
48+
49+ printf( "=====[ List directory ]=====\n" );
50+ printf( "<=== Trying ftp_list()\n" );
51+ {
52+ char buf[10240];
53+ err = ftp_list( &ftp, 0, buf, sizeof(buf) );
54+ printf( "directry list: '%s'\n", buf );
55+ }
56+ err_handle( err, &ftp );
57+ printf( "<=== Trying ftp_nlist()\n" );
58+ {
59+ char buf[10240];
60+ err = ftp_nlist( &ftp, 0, buf, sizeof(buf) );
61+ printf( "directry nlist: '%s'\n", buf );
62+ }
63+ err_handle( err, &ftp );
64+
65+
66+ printf( "=====[ put/get commands ]=====\n" );
67+ printf( "<=== Trying ftp_put_buffer()\n" );
68+ {
69+ char buf[] = "The quick brown fox jumps over the lazy dogs.\n";
70+ err = ftp_put_buffer( &ftp, buf, sizeof(buf)-1, "test.txt" );
71+ }
72+ err_handle( err, &ftp );
73+
74+ printf( "<=== Trying ftp_append_buffer()\n" );
75+ {
76+ char buf[] = "Boz Scaggs. Live at great american music hall, San Fransisco.\n";
77+ err = ftp_append_buffer( &ftp, buf, sizeof(buf)-1, "test.txt" );
78+ }
79+ err_handle( err, &ftp );
80+
81+ printf( "<=== Trying ftp_get_buffer()\n" );
82+ {
83+ char buf[1024];
84+ err = ftp_get_buffer( &ftp, "test.txt", buf, sizeof(buf) );
85+ }
86+ err_handle( err, &ftp );
87+
88+ printf( "<=== Trying ftp_get_file()\n" );
89+ err = ftp_get_file( &ftp, "test.txt", "test1.txt" );
90+ err_handle( err, &ftp );
91+
92+ printf( "<=== Trying ftp_put_file()\n" );
93+ err = ftp_put_file( &ftp, "test1.txt", "test2.txt" );
94+ err_handle( err, &ftp );
95+
96+
97+ printf( "=====[ Directory commands ]=====\n" );
98+ printf( "<=== Trying ftp_pwd()\n" );
99+ {
100+ char buf[512];
101+ err = ftp_pwd( &ftp, buf, sizeof(buf) );
102+ if( err == 0 ) printf( "Current Directory is '%s'\n", buf );
103+ }
104+ err_handle( err, &ftp );
105+
106+ printf( "<=== Trying ftp_mkdir()\n" );
107+ err = ftp_mkdir( &ftp, "testdir" );
108+ err_handle( err, &ftp );
109+
110+ printf( "<=== Trying ftp_cd()\n" );
111+ err = ftp_cd( &ftp, "testdir" );
112+ err_handle( err, &ftp );
113+ err = ftp_cd( &ftp, ".." );
114+ err_handle( err, &ftp );
115+
116+ printf( "<=== Trying ftp_rmdir()\n" );
117+ err = ftp_rmdir( &ftp, "testdir" );
118+ err_handle( err, &ftp );
119+
120+
121+
122+ printf( "=====[ Other commands ]=====\n" );
123+ printf( "<=== Trying ftp_rename()\n" );
124+ err = ftp_rename( &ftp, "test.txt", "test1.txt" );
125+ err_handle( err, &ftp );
126+
127+ printf( "<=== Trying ftp_site()\n" );
128+ err = ftp_site( &ftp, "chmod 700 test1.txt" );
129+ err_handle( err, &ftp );
130+
131+ printf( "<=== Trying ftp_delete()\n" );
132+ err = ftp_delete( &ftp, "test1.txt" );
133+ err_handle( err, &ftp );
134+
135+
136+
137+ printf( "<=== Trying ftp_quit()\n" );
138+ err = ftp_quit( &ftp );
139+ err_handle( err, &ftp );
140+
141+ printf( "Total error(s): %d\n", total_err );
142+
143+ return 0;
144+}
--- tags/REL-2.2/example/example2.c (nonexistent)
+++ tags/REL-2.2/example/example2.c (revision 55)
@@ -0,0 +1,103 @@
1+#include <stdio.h>
2+#include <stdlib.h>
3+#include <string.h>
4+#include <sys/types.h>
5+#include <sys/uio.h>
6+#include <unistd.h>
7+
8+#include "liboftp.h"
9+
10+#define HOSTNAME "YOUR FTP HOSTNAME HERE"
11+#define USERNAME "USER NAME HERE"
12+#define PASSWORD "PASSWORD HERE"
13+
14+int total_err = 0;
15+
16+void err_handle( int err, LIBOFTP *ftp )
17+{
18+ printf( "===> error code %d", err );
19+ if( err < 0 ) {
20+ total_err++;
21+ printf( " : %s", ftp->error_message );
22+ }
23+ printf( "\n\n" );
24+}
25+
26+
27+
28+int main()
29+{
30+ LIBOFTP ftp;
31+ int err;
32+
33+ ftp_initialize( &ftp );
34+
35+ printf( "=====[ Connect and login ]=====\n" );
36+ printf( "<=== Trying ftp_open()\n" );
37+ err = ftp_open( &ftp, HOSTNAME, 0 );
38+ err_handle( err, &ftp );
39+
40+ printf( "<=== Trying ftp_user()\n" );
41+ err = ftp_user( &ftp, USERNAME, PASSWORD );
42+ err_handle( err, &ftp );
43+
44+ err = ftp_passive( &ftp, 1 );
45+
46+ printf( "<=== Trying ftp_type()\n" );
47+ err = ftp_type( &ftp, "image" );
48+ err_handle( err, &ftp );
49+
50+
51+ printf( "<=== Trying ftp_put_descriptor()\n" );
52+ err = ftp_put_descriptor( &ftp, "test.txt" );
53+ err_handle( err, &ftp );
54+ if( err > 0 ) {
55+ char *buf[] = { "The quick\n", "brown fox jumps\n", "over the lazy dogs.\n" };
56+ int i, fd = err;
57+
58+ for( i = 0; i < 3; i++ ) {
59+ write( fd, buf[i], strlen( buf[i] ) );
60+ }
61+
62+ printf( "<=== Trying ftp_put_descriptor_close()\n" );
63+ err = ftp_put_descriptor_close( &ftp, fd );
64+ err_handle( err, &ftp );
65+ }
66+
67+ printf( "<=== Trying ftp_get_descriptor()\n" );
68+ err = ftp_get_descriptor( &ftp, "test.txt" );
69+ err_handle( err, &ftp );
70+
71+ if( err > 0 ) {
72+ FILE *fp;
73+ int fd = err;
74+ char buf[256];
75+ int n = 1;
76+
77+ fp = fdopen( fd, "r" );
78+ if( fp == NULL ) {
79+ perror( "" );
80+ return 1;
81+ }
82+
83+ printf( "READING...\n" );
84+ while( fgets( buf, 256, fp ) ) {
85+ printf( "%d: %s", n, buf );
86+ n++;
87+ }
88+ printf( "...DONE\n" );
89+ fclose( fp );
90+
91+ printf( "<=== Trying ftp_get_descriptor_close()\n" );
92+ err = ftp_get_descriptor_close( &ftp, fd );
93+ err_handle( err, &ftp );
94+ }
95+
96+ printf( "<=== Trying ftp_quit()\n" );
97+ err = ftp_quit( &ftp );
98+ err_handle( err, &ftp );
99+
100+ printf( "Total error(s): %d\n", total_err );
101+
102+ return 0;
103+}
--- tags/REL-2.2/ftp_passive.c (nonexistent)
+++ tags/REL-2.2/ftp_passive.c (revision 55)
@@ -0,0 +1,39 @@
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+/***** Local headers ********************************************************/
13+#include "liboftp.h"
14+
15+
16+/***** Constat values *******************************************************/
17+/***** Macros ***************************************************************/
18+/***** Typedefs *************************************************************/
19+/***** Function prototypes **************************************************/
20+/***** Local variables ******************************************************/
21+/***** Global variables *****************************************************/
22+/***** Signal catching functions ********************************************/
23+/***** Local functions ******************************************************/
24+/***** Global functions *****************************************************/
25+
26+/****************************************************************************/
27+/*! パッシブモード設定/解除
28+ *
29+ *@param ftp LIBOFTPへのポインタ。
30+ *@param flag パッシブモードON/OFF
31+ *@retval int 0
32+ *@note
33+ */
34+int ftp_passive( LIBOFTP *ftp, int flag )
35+{
36+ ftp->flag_passive = flag? 1: 0;
37+
38+ return 0;
39+}
Show on old repository browser