| 33 |
*@param ftp LIBOFTPへのポインタ。 |
*@param ftp LIBOFTPへのポインタ。 |
| 34 |
*@param buf 結果代入先バッファ |
*@param buf 結果代入先バッファ |
| 35 |
*@param bufsiz バッファサイズ |
*@param bufsiz バッファサイズ |
| 36 |
*@retval int マイナス値ならエラー |
*@retval int エラーコード |
| 37 |
*@note |
*@note |
| 38 |
*/ |
*/ |
| 39 |
int ftp_pwd( LIBOFTP *ftp, char *buf, int bufsiz ) |
int ftp_pwd( LIBOFTP *ftp, char *buf, int bufsiz ) |
| 40 |
{ |
{ |
| 41 |
char str1[512] = "PWD\r\n"; |
char str1[512] = "PWD\r\n"; |
| 42 |
int ret; |
int res; |
| 43 |
char *p1, *p2; |
char *p1, *p2; |
| 44 |
|
|
| 45 |
|
if( ftp->socket < 0 ) return LIBOFTP_ERROR; |
| 46 |
|
|
| 47 |
/* |
/* |
| 48 |
* send PWD command |
* send PWD command |
| 49 |
*/ |
*/ |
| 50 |
if( ftp_send_command( ftp, str1 ) < 0 ) { |
if( ftp_send_command( ftp, str1 ) < 0 ) { |
| 51 |
DEBUGPRINT1( "pwd: command sending error. %s\n", str1 ); |
DEBUGPRINT1( "command sending error. %s\n", str1 ); |
| 52 |
return -1; |
return LIBOFTP_ERROR_OS; |
| 53 |
} |
} |
| 54 |
|
|
| 55 |
if( (ret = ftp_receive_response( ftp, str1, sizeof(str1) )) != 257 ) { /* 257: "PATHNAME" created. */ |
if( (res = ftp_receive_response( ftp, str1, sizeof(str1) )) != 257 ) { /* 257: "PATHNAME" created. */ |
| 56 |
DEBUGPRINT1( "pwd: command response error. %d\n", ret ); |
DEBUGPRINT1( "command response error. %d\n", res ); |
| 57 |
return -2; |
strncpy( ftp->error_message, str1, sizeof(ftp->error_message) - 1 ); |
| 58 |
|
return res < 0? res: LIBOFTP_ERROR_PROTOCOL; |
| 59 |
} |
} |
| 60 |
|
|
| 61 |
/* |
/* |
| 63 |
*/ |
*/ |
| 64 |
p1 = strchr( str1, '"' ); |
p1 = strchr( str1, '"' ); |
| 65 |
if( p1 == NULL ) { |
if( p1 == NULL ) { |
| 66 |
DEBUGPRINT1( "pwd: reply string parse error. %s\n", str1 ); |
DEBUGPRINT1( "reply string parse error. %s\n", str1 ); |
| 67 |
return -2; |
strncpy( ftp->error_message, "reply string parse error.", sizeof( ftp->error_message ) ); |
| 68 |
|
return LIBOFTP_ERROR_PROTOCOL; |
| 69 |
} |
} |
| 70 |
|
|
| 71 |
p2 = strchr( p1+1, '"' ); |
p2 = strchr( p1+1, '"' ); |
| 72 |
if( p2 == NULL ) { |
if( p2 == NULL ) { |
| 73 |
DEBUGPRINT1( "pwd: reply string parse error. %s\n", str1 ); |
DEBUGPRINT1( "reply string parse error. %s\n", str1 ); |
| 74 |
return -2; |
strncpy( ftp->error_message, "reply string parse error.", sizeof( ftp->error_message ) ); |
| 75 |
|
return LIBOFTP_ERROR_PROTOCOL; |
| 76 |
} |
} |
| 77 |
if( bufsiz < (p2-p1) ) { |
if( bufsiz < (p2-p1) ) { |
| 78 |
DEBUGPRINT1( "pwd: buffer too small.%s", "" ); |
DEBUGPRINT1( "buffer too small.%s", "" ); |
| 79 |
return -3; |
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 ); |
memcpy( buf, p1+1, p2 - p1 - 1 ); |
| 84 |
buf[ p2 - p1 - 1 ] = 0; |
buf[ p2 - p1 - 1 ] = 0; |
| 85 |
|
|
| 86 |
return 0; |
return LIBOFTP_NOERROR; |
| 87 |
} |
} |