| 1 |
hirohitohigashi |
1 |
/* |
| 2 |
|
|
|
| 3 |
|
|
libftp 1.0 (stable): this is an FTP library to simplify the work to a Developer |
| 4 |
|
|
who want to work with FTP servers (RFC 959). |
| 5 |
|
|
Copyright (C) 2007/2008 omnipresent - omnipresent[at]email.it |
| 6 |
|
|
|
| 7 |
|
|
This program is free software: you can redistribute it and/or modify |
| 8 |
|
|
it under the terms of the GNU General Public License as published by |
| 9 |
|
|
the Free Software Foundation, either version 3 of the License, or |
| 10 |
|
|
(at your option) any later version. |
| 11 |
|
|
|
| 12 |
|
|
This program is distributed in the hope that it will be useful, |
| 13 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
|
|
GNU General Public License for more details. |
| 16 |
|
|
|
| 17 |
|
|
You should have received a copy of the GNU General Public License |
| 18 |
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
|
|
| 20 |
|
|
*/ |
| 21 |
|
|
|
| 22 |
|
|
#include <stdio.h> |
| 23 |
|
|
#include <string.h> |
| 24 |
|
|
#include <errno.h> |
| 25 |
|
|
#include <stdlib.h> |
| 26 |
|
|
#include <unistd.h> |
| 27 |
|
|
#include <sys/types.h> |
| 28 |
|
|
#include <sys/time.h> |
| 29 |
|
|
#include <sys/socket.h> |
| 30 |
|
|
#include <sys/stat.h> |
| 31 |
|
|
#include <netinet/in.h> |
| 32 |
|
|
#include <netdb.h> |
| 33 |
|
|
#include <fcntl.h> |
| 34 |
|
|
|
| 35 |
|
|
#include "ftp_main.h" |
| 36 |
|
|
|
| 37 |
|
|
|
| 38 |
|
|
/* Global variable used in the ftp_system subroutine */ |
| 39 |
|
|
|
| 40 |
|
|
char system_name[1050]; |
| 41 |
|
|
|
| 42 |
|
|
/*** |
| 43 |
|
|
ftp_system: |
| 44 |
|
|
- int sck => Socket Descriptor |
| 45 |
|
|
- int verbose => Verbose? |
| 46 |
|
|
. 0 Do not Print |
| 47 |
|
|
. 1 Print |
| 48 |
|
|
- ret => Success? |
| 49 |
|
|
. 0 Success |
| 50 |
|
|
.-1 Error |
| 51 |
|
|
***/ |
| 52 |
|
|
|
| 53 |
|
|
|
| 54 |
hirohitohigashi |
7 |
int ftp_system (int sck, int verbose) |
| 55 |
|
|
{ |
| 56 |
|
|
char *buf; |
| 57 |
|
|
int len = 0; |
| 58 |
|
|
int n = 0; |
| 59 |
|
|
struct timeval tm; //tv_sec - tv_usec |
| 60 |
|
|
fd_set readfds; |
| 61 |
hirohitohigashi |
1 |
|
| 62 |
|
|
|
| 63 |
hirohitohigashi |
7 |
buf = (char *)malloc(1024*sizeof(char)); |
| 64 |
|
|
memset(buf, 0x0, 1024); |
| 65 |
hirohitohigashi |
1 |
|
| 66 |
|
|
|
| 67 |
|
|
if (write(sck, "SYST\n", strlen("SYST\n")) == -1) { |
| 68 |
|
|
|
| 69 |
hirohitohigashi |
7 |
if (verbose) { |
| 70 |
|
|
if (errno == EBADF) { |
| 71 |
|
|
fprintf(stderr, "Socket Descriptor Not Valid!\n"); |
| 72 |
|
|
free(buf); |
| 73 |
hirohitohigashi |
1 |
|
| 74 |
hirohitohigashi |
7 |
return -1; |
| 75 |
|
|
} else if (errno == EIO) { |
| 76 |
|
|
fprintf(stderr, "I/O Error!\n"); |
| 77 |
|
|
free(buf); |
| 78 |
hirohitohigashi |
1 |
|
| 79 |
hirohitohigashi |
7 |
return -1; |
| 80 |
|
|
} else if (errno == EINTR) { |
| 81 |
|
|
fprintf(stderr, "Signal Interrupted The write() Function\n"); |
| 82 |
|
|
free(buf); |
| 83 |
hirohitohigashi |
1 |
|
| 84 |
hirohitohigashi |
7 |
return -1; |
| 85 |
|
|
} |
| 86 |
hirohitohigashi |
1 |
|
| 87 |
hirohitohigashi |
7 |
} else { |
| 88 |
hirohitohigashi |
1 |
|
| 89 |
hirohitohigashi |
7 |
if (errno == EBADF) { |
| 90 |
|
|
free(buf); |
| 91 |
hirohitohigashi |
1 |
|
| 92 |
hirohitohigashi |
7 |
return -1; |
| 93 |
|
|
} else if (errno == EIO) { |
| 94 |
|
|
free(buf); |
| 95 |
hirohitohigashi |
1 |
|
| 96 |
hirohitohigashi |
7 |
return -1; |
| 97 |
|
|
} else if (errno == EINTR) { |
| 98 |
|
|
free(buf); |
| 99 |
hirohitohigashi |
1 |
|
| 100 |
hirohitohigashi |
7 |
return -1; |
| 101 |
|
|
} |
| 102 |
hirohitohigashi |
1 |
|
| 103 |
hirohitohigashi |
7 |
} |
| 104 |
hirohitohigashi |
1 |
|
| 105 |
hirohitohigashi |
7 |
free(buf); |
| 106 |
hirohitohigashi |
1 |
|
| 107 |
hirohitohigashi |
7 |
return -1; |
| 108 |
hirohitohigashi |
1 |
|
| 109 |
hirohitohigashi |
7 |
} |
| 110 |
hirohitohigashi |
1 |
|
| 111 |
hirohitohigashi |
7 |
tm.tv_sec = 3; |
| 112 |
|
|
tm.tv_usec = 0; |
| 113 |
hirohitohigashi |
1 |
|
| 114 |
hirohitohigashi |
7 |
FD_ZERO(&readfds); |
| 115 |
hirohitohigashi |
1 |
|
| 116 |
hirohitohigashi |
7 |
FD_SET(sck, &readfds); |
| 117 |
hirohitohigashi |
1 |
|
| 118 |
hirohitohigashi |
7 |
if (select(sck+1, &readfds, NULL, NULL, &tm) < 0 ) { |
| 119 |
|
|
if (verbose) { |
| 120 |
|
|
perror("select()"); |
| 121 |
|
|
} |
| 122 |
|
|
free(buf); |
| 123 |
|
|
|
| 124 |
|
|
return -1; |
| 125 |
|
|
} else { |
| 126 |
|
|
|
| 127 |
|
|
if (FD_ISSET(sck, &readfds)) { |
| 128 |
|
|
|
| 129 |
|
|
if<
| |