Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /trunk_1/ftprename.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 15 - (hide annotations) (download) (as text)
Wed Feb 25 13:29:32 2009 UTC (15 years, 1 month ago) by hirohitohigashi
File MIME type: text/x-csrc
File size: 4566 byte(s)
renewal all sources.

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     ftp_rename:
39     - int sck => Socket Descriptor
40     - int verbose => Verbose?
41     . 0 Do not Print
42     . 1 Print
43     - ret => Success?
44     . 0 Success
45     .-1 Error
46     . 2 Error
47     ***/
48    
49     #define DIR_TOO_LONG 2
50     #define NAME_TOO_LONG 2
51    
52    
53 hirohitohigashi 7 int ftp_rename (int sck, char *old_name, char *new_name, int verbose)
54     {
55     char buffer[1024]={'\0'};
56     char *buf;
57     int n;
58     struct timeval tm; //tv_sec - tv_usec
59     fd_set readfds;
60 hirohitohigashi 1
61 hirohitohigashi 7 buf = (char *)malloc(1024*sizeof(char));
62 hirohitohigashi 1
63 hirohitohigashi 7 memset(buffer, 0x0, 1024);
64     memset(buf, 0x0, 1024);
65 hirohitohigashi 1
66    
67    
68 hirohitohigashi 7 if ( strlen(old_name) > 1015 ) {
69 hirohitohigashi 1 free(buf);
70    
71     return NAME_TOO_LONG;
72 hirohitohigashi 7 }
73 hirohitohigashi 1
74 hirohitohigashi 7 if ( strlen(new_name) > 1015 ) {
75 hirohitohigashi 1 free(buf);
76    
77     return NAME_TOO_LONG;
78 hirohitohigashi 7 }
79 hirohitohigashi 1
80 hirohitohigashi 11 sprintf(buffer, "RNFR %s\r\n", old_name);
81 hirohitohigashi 1
82 hirohitohigashi 7 if (write(sck, buffer, strlen(buffer)) == -1) {
83 hirohitohigashi 1
84     if (verbose) {
85    
86 hirohitohigashi 7 if (errno == EBADF) {
87 hirohitohigashi 1 free(buf);
88    
89     fprintf(stderr, "Socket Descriptor Not Valid!\n");
90     return -1;
91 hirohitohigashi 7 } else if (errno == EIO) {
92 hirohitohigashi 1 free(buf);
93    
94     fprintf(stderr, "I/O Error!\n");
95     return -1;
96 hirohitohigashi 7 } else if (errno == EINTR) {
97 hirohitohigashi 1 free(buf);
98    
99     fprintf(stderr, "Signal Interrupted The write() Function\n");
100     return -1;
101 hirohitohigashi 7 }
102 hirohitohigashi 1
103     } else {
104    
105 hirohitohigashi 7 if (errno == EBADF) {
106 hirohitohigashi 1 free(buf);
107    
108     return -1;
109 hirohitohigashi 7 } else if (errno == EIO) {
110 hirohitohigashi 1 free(buf);
111    
112     return -1;
113 hirohitohigashi 7 } else if (errno == EINTR) {
114 hirohitohigashi 1 free(buf);
115    
116     return -1;
117 hirohitohigashi 7 }
118 hirohitohigashi 1 }
119    
120 hirohitohigashi 7 free(buf);
121 hirohitohigashi 1
122 hirohitohigashi 7 return -1;
123 hirohitohigashi 1
124 hirohitohigashi 7 }
125 hirohitohigashi 1
126 hirohitohigashi 7 tm.tv_sec = 3;
127     tm.tv_usec = 0;
128 hirohitohigashi 1
129 hirohitohigashi 7 FD_ZERO(&readfds);
130 hirohitohigashi 1
131 hirohitohigashi 7 FD_SET(sck, &readfds);
132 hirohitohigashi 1
133 hirohitohigashi 7 if (select(sck+1, &readfds, NULL, NULL, &tm) < 0 ) {
134     if (verbose) {
135     perror("select()");
136     }
137     free(buf);
138    
139     return -1;
140     } else {
141    
142     if (FD_ISSET(sck, &readfds)) {
143    
144     if ( (n = recv(sck, buf, 1022, 0)) < 0) {
145 hirohitohigashi 1 if (verbose) {
146 hirohitohigashi 7 perror("recv()");
147 hirohitohigashi 1 }
148     free(buf);
149 hirohitohigashi 7
150 hirohitohigashi 1 return -1;
151 hirohitohigashi 7 }
152 hirohitohigashi 1
153 hirohitohigashi 7 if (verbose)
154     printf("[Server] %s\n", buf);
155 hirohitohigashi 1
156 hirohitohigashi 7 }
157     else {
158     if (verbose)
159     printf("[-] Timeout\n");
160 hirohitohigashi 1
161 hirohitohigashi 7 return -1;
162 hirohitohigashi 1 }
163 hirohitohigashi 7 }
164 hirohitohigashi 1
165 hirohitohigashi 7 buf[n] = '\0';
166 hirohitohigashi 1
167    
168 hirohitohigashi 7 if (ftp_file_handler(buf, 0) != 0) {
169     free(buf);
170 hirohitohigashi 1
171 hirohitohigashi 7 return -1;
172     }
173 hirohitohigashi 1
174 hirohitohigashi 7 memset(buffer, 0x0, 1024);
175     memset(buf, 0x0, 1024);
176 hirohitohigashi 1
177 hirohitohigashi 11 sprintf(buffer, "RNTO %s\r\n", new_name);
178 hirohitohigashi 1
179 hirohitohigashi 7 if (write(sck, buffer, strlen(buffer)) == -1) {
180 hirohitohigashi 1
181     if (verbose) {
182    
183 hirohitohigashi 7 if (errno == EBADF) {
184 hirohitohigashi 1 free(buf);
185    
186     fprintf(stderr, "Socket Descriptor Not Valid!\n");
187     return -1;
188 hirohitohigashi 7 } else if (errno == EIO) {
189 hirohitohigashi 1 free(buf);
190    
191     fprintf(stderr, "I/O Error!\n");
192     return -1;
193 hirohitohigashi 7 } else if (errno == EINTR) {
194 hirohitohigashi 1 free(buf);
195    
196     fprintf(stderr, "Signal Interrupted The write() Function\n");
197     return -1;
198 hirohitohigashi 7 }
199 hirohitohigashi 1
200     } else {
201    
202 hirohitohigashi 7 if (errno == EBADF) {
203 hirohitohigashi 1 free(buf);
204    
205     return -1;
206 hirohitohigashi 7 } else if (errno == EIO) {
207 hirohitohigashi 1 free(buf);
208    
209     return -1;
210 hirohitohigashi 7 } else if (errno == EINTR) {
211 hirohitohigashi 1 free(buf);
212    
213     return -1;
214 hirohitohigashi 7 }
215 hirohitohigashi 1
216     }
217    
218 hirohitohigashi 7 free(buf);
219 hirohitohigashi 1
220 hirohitohigashi 7 return -1;
221 hirohitohigashi 1
222 hirohitohigashi 7 }
223 hirohitohigashi 1
224 hirohitohigashi 7 FD_ZERO(&readfds);
225 hirohitohigashi 1
226 hirohitohigashi 7 FD_SET(sck, &readfds);
227 hirohitohigashi 1
228 hirohitohigashi 7 if (select(sck+1, &readfds, NULL, NULL, &tm) < 0 ) {
229     if (verbose) {
230     perror("select()");
231     }
232     free(buf);
233    
234     return -1;
235     } else {
236    
237     if (FD_ISSET(sck, &readfds)) {
238    
239     if ( (n = recv(sck, buf, 1022, 0)) < 0) {
240 hirohitohigashi 1 if (verbose) {
241 hirohitohigashi 7 perror("recv()");
242 hirohitohigashi 1 }
243     free(buf);
244 hirohitohigashi 7
245 hirohitohigashi 1 return -1;
246 hirohitohigashi 7 }
247 hirohitohigashi 1
248 hirohitohigashi 7 if (verbose)
249     printf("[Server] %s\n", buf);
250 hirohitohigashi 1
251 hirohitohigashi 7 }
252     else {
253     if (verbose)
254     printf("[-] Timeout\n");
255 hirohitohigashi 1
256 hirohitohigashi 7 return -1;
257 hirohitohigashi 1 }
258 hirohitohigashi 7 }
259 hirohitohigashi 1
260 hirohitohigashi 7 buf[n] = '\0';
261 hirohitohigashi 1
262    
263 hirohitohigashi 7 if (ftp_file_handler(buf, 0) != 0) {
264     free(buf);
265 hirohitohigashi 1
266 hirohitohigashi 7 return -1; //Error
267     }
268 hirohitohigashi 1
269 hirohitohigashi 7 free(buf);
270 hirohitohigashi 1
271 hirohitohigashi 7 return 0; //Renamed
272 hirohitohigashi 1 }

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