Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /tags/REL-1.1/ftpappend.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 14 - (hide annotations) (download) (as text)
Mon Feb 23 02:24:24 2009 UTC (15 years, 1 month ago) by hirohitohigashi
File MIME type: text/x-csrc
File size: 4807 byte(s)
Tag release 1.1
1 hirohitohigashi 10 /*
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     /***
40     ftp_append:
41     - int sck => Socket Descriptor
42     - char *FileName => The file name that you want to upload
43     - char *target => Target file name on server.
44     - int verbose => Verbose?
45     . 0 Do not Print
46     . 1 Print
47     - ret => Success?
48     . 0 Success
49     .-1 Error
50     ***/
51    
52     #define DATA_CONNECTION_ERROR -3
53     #define WRONG_DATA_P -3
54     #define USR_TOO_LONG -2
55    
56     time_t res_append;
57    
58     int ftp_append (int sck, char *FileName, char *target, int verbose)
59     {
60     char *buffer;
61     int n = 0;
62     int flags;
63     time_t start, end;
64     int sock;
65     FILE *fd;
66     int con_err = 0;
67     char buff[4096];
68     int n_bytes;
69     int n_send;
70    
71     extern int data_port;
72     extern struct sockaddr_in ftp_connection;
73    
74     if( target == NULL ) target = FileName;
75    
76     buffer = (char *)malloc(4092*sizeof(char));
77    
78     if (buffer == NULL) {
79     printf("Unable to alloc 4092 bytes to buffer (ftp_append)\n");
80     return -1;
81     }
82    
83    
84     if (data_port == 0) {
85    
86     free(buffer);
87    
88     return WRONG_DATA_P;
89     }
90    
91    
92     if (strlen(FileName) > 1024) {
93    
94     free(buffer);
95    
96     return USR_TOO_LONG;
97    
98     }
99    
100     if (access(FileName, F_OK) != 0) {
101     if (verbose) {
102     printf("--[ Error: File Name '%s' not here\n", FileName);
103     }
104    
105     free(buffer);
106    
107    
108     return 1;
109    
110     }
111    
112     sprintf(buffer, "APPE %s\r\n", target);
113    
114     if (write(sck, buffer, strlen(buffer)) == -1) {
115    
116     if (verbose) {
117    
118     if (errno == EBADF) {
119     fprintf(stderr, "Socket Descriptor Not Valid!\n");
120     free(buffer);
121    
122     return -1;
123    
124     } else if (errno == EIO) {
125    
126     fprintf(stderr, "I/O Error!\n");
127     free(buffer);
128     return -1;
129    
130     } else if (errno == EINTR) {
131    
132     fprintf(stderr, "Signal Interrupted The write() Function\n");
133     free(buffer);
134    
135     return -1;
136     }
137    
138     } else {
139    
140     if (errno == EBADF) {
141     free(buffer);
142    
143     return -1;
144    
145     } else if (errno == EIO) {
146     free(buffer);
147    
148     return -1;
149    
150     } else if (errno == EINTR) {
151     free(buffer);
152    
153     return -1;
154    
155     }
156    
157     }
158    
159     free(buffer);
160    
161     return -1;
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     ftp_connection.sin_port = htons(data_port);
175    
176     if ( (con_err = connect(sock, (struct sockaddr *)&ftp_connection, sizeof(struct sockaddr)) ) != 0) {
177    
178     data_port = 0;
179     n = recv(sck, buffer, 1024, 0);
180     memset(buffer, 0x0, 1024);
181     free(buffer);
182    
183     if (verbose)
184     printf("[-] Data Connection error (APPE)\n");
185    
186     return DATA_CONNECTION_ERROR;
187     }
188    
189     if ( (flags=fcntl(sock, F_GETFL,0)) < 0) {
190     printf("--[ fcntl get flags error.\n");
191     exit(1);
192     }
193    
194     flags |= O_NONBLOCK;
195    
196     if ( fcntl (sock, F_SETFL, flags ) < 0 ) {
197     printf("--[ Non Blocking Socket Error.\n");
198     exit(1);
199     }
200    
201     buff[4095] = '\0';
202    
203     fd = fopen(FileName, "rb");
204    
205     if (fd == NULL) {
206    
207     if (verbose) {
208     printf("--[ Error: Unable to open '%s' in ftp_append\n", FileName);
209     }
210    
211     return -1;
212    
213     }
214    
215     while (!feof(fd)) {
216    
217     n_bytes = fread(buff, sizeof(char), 4094, fd);
218     n_send = send(sock, buff, n_bytes, 0);
219     usleep(150);
220     memset(buff, 0x0, n_send);
221    
222     }
223    
224     fclose(fd);
225     close(sock);
226    
227    
228     time(&start);
229    
230     while (1) {
231    
232     sleep(1);
233     n = recv(sck, buffer, 4092, MSG_DONTWAIT);
234     if (n == 0)
235     break;
236    
237     if (n < 0) {
238    
239     if ( (errno == ESPIPE) || (errno == EAGAIN) ) {
240     break;
241     }
242    
243     if (verbose) {
244     printf("--[ Error: Error in ftp_append() while putting on the file\n");
245     }
246    
247     free(buffer);
248    
249     return -1;
250    
251     }
252    
253     buffer[n] = '\0';
254    
255     n = 0;
256     memset(buffer, 0x0, 4092);
257    
258     }
259    
260     memset(buffer, 0x0, 4092);
261    
262     time(&end);
263    
264     res_append = (int)(end - start);
265    
266     free(buffer);
267    
268    
269     return 0;
270     }

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