Develop and Download Open Source Software

Browse Subversion Repository

Contents of /branches/RB-1.1/ftpget.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 7 - (show annotations) (download) (as text)
Fri Feb 20 12:10:40 2009 UTC (15 years, 1 month ago) by hirohitohigashi
Original Path: trunk/ftpget.c
File MIME type: text/x-csrc
File size: 4825 byte(s)
moved variable declaration  for traditional compilers. such as QNX6.2.

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 <time.h>
30 #include <sys/socket.h>
31 #include <sys/stat.h>
32 #include <netinet/in.h>
33 #include <netdb.h>
34 #include <fcntl.h>
35
36 #include "ftp_main.h"
37
38 /***
39 ftp_get:
40 - int sck => Socket Descriptor
41 - char *FileName => The file name that you want to download
42 - int verbose => Verbose?
43 . 0 Do not Print
44 . 1 Print
45 - ret => Success?
46 . 0 Success
47 .-1 Error
48 ***/
49
50 #define DATA_CONNECTION_ERROR -3
51 #define WRONG_DATA_P -3
52 #define USR_TOO_LONG -2
53
54 time_t res_get;
55
56 int ftp_get (int sck, char *FileName, int verbose)
57 {
58 char *buffer;
59 int n = 0;
60 int flags;
61 time_t start, end;
62 int sock;
63 int con_err = 0;
64 FILE *fd;
65 char buff[4096];
66 int n_bytes;
67 int n_recv=0;
68
69
70 extern int data_port;
71 extern struct sockaddr_in ftp_connection;
72
73
74 buffer = (char *)malloc(4092*sizeof(char));
75 if (buffer == NULL) {
76 printf("Unable to alloc 4092 bytes to buffer (ftp_get)\n");
77 return -1;
78 }
79
80 memset(buffer, 0x0, 4092);
81
82
83 if (data_port == 0) {
84
85 free(buffer);
86
87 return WRONG_DATA_P;
88
89 }
90
91 if (strlen(FileName) > 1024) {
92
93 return USR_TOO_LONG;
94
95 }
96
97 sprintf(buffer, "RETR %s\n", FileName);
98
99 if (write(sck, buffer, strlen(buffer)) == -1) {
100
101 if (verbose) {
102 if (errno == EBADF) {
103 fprintf(stderr, "Socket Descriptor Not Valid!\n");
104 free(buffer);
105
106 return -1;
107
108 } else if (errno == EIO) {
109
110 fprintf(stderr, "I/O Error!\n");
111 free(buffer);
112
113 return -1;
114
115 } else if (errno == EINTR) {
116
117 fprintf(stderr, "Signal Interrupted The write() Function\n");
118 free(buffer);
119
120 return -1;
121 }
122
123 } else {
124
125 if (errno == EBADF) {
126 free(buffer);
127
128 return -1;
129
130 } else if (errno == EIO) {
131 free(buffer);
132
133 return -1;
134
135 } else if (errno == EINTR) {
136 free(buffer);
137
138 return -1;
139 }
140
141 }
142
143 free(buffer);
144
145 return -1;
146
147 }
148
149 memset(buffer, 0x0, 4092);
150
151 sleep(1);
152 n = recv(sck, buffer, 4092, MSG_DONTWAIT);
153
154 if (strlen(buffer) != 0) {
155
156 if (ftp_file_handler (buffer, 0) != 0) {
157 free(buffer);
158
159 return 1;
160 }
161
162 }
163
164
165 sock = socket(AF_INET, SOCK_STREAM, 0);
166 if (sock == -1) {
167 free(buffer);
168 if (verbose) {
169 perror("Socket");
170 }
171 return -1;
172 }
173
174
175 ftp_connection.sin_port = htons(data_port);
176
177 if ( (flags=fcntl(sock, F_GETFL,0)) < 0) {
178 printf("--[ fcntl get flags error.\n");
179 exit(1);
180 }
181
182 flags |= O_NONBLOCK;
183
184 if ( fcntl (sock, F_SETFL, flags < 0 ) ) {
185 printf("--[ Non Blocking Socket Error.\n");
186 exit(1);
187 }
188
189
190 if ( (con_err = connect(sock, (struct sockaddr *)&ftp_connection, sizeof(struct sockaddr)) ) != 0) {
191 if (con_err == ECONNREFUSED) {
192 free(buffer);
193
194 return -1;
195 }
196
197 data_port = 0;
198 if (verbose) {
199 perror("connect");
200 }
201
202 free(buffer);
203
204 return -1;
205 }
206
207
208 buff[4095] = '\0';
209
210 fd = fopen(FileName, "wb");
211
212 if (fd == NULL) {
213 if (verbose) {
214 printf("--[ Error: Unable to open '%s' in ftp_get function\n", FileName);
215 }
216
217 free(buffer);
218
219 return -1;
220 }
221
222 time(&start);
223
224 while (1) {
225
226 usleep(150);
227 n_recv = recv(sock, buff, 4094, 0);
228 n_bytes = fwrite(buff, sizeof(char), n_recv, fd);
229
230 if (n_recv == 0) {
231 break;
232 }
233
234 n_recv = 0;
235
236 memset(buff, 0x0, n_bytes);
237
238 }
239
240
241 fclose(fd);
242 close(sock);
243
244
245
246 memset(buffer, 0x0, 4092);
247
248 while (1) {
249
250 sleep(1);
251 n = recv(sck, buffer, 4092, MSG_DONTWAIT);
252 if (n == 0)
253 break;
254
255 if (n < 0) {
256
257 if ( (errno == ESPIPE) || (errno == EAGAIN) ) {
258 break;
259 }
260
261 if (verbose) {
262 printf("--[ Error: Error in ftp_get() while getting the file\n");
263 }
264 free(buffer);
265
266
267 return -1;
268
269 }
270
271 buffer[n] = '\0';
272
273
274 n = 0;
275 memset(buffer, 0x0, 4092);
276
277 }
278
279 memset(buffer, 0x0, 4092);
280
281 time(&end);
282
283 res_get = (int)(end - start);
284
285 free(buffer);
286
287
288 return 0;
289 }

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