Develop and Download Open Source Software

Browse Subversion Repository

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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