Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk_1/ftprename.c

Parent Directory Parent Directory | Revision Log Revision Log


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

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 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
61 buf = (char *)malloc(1024*sizeof(char));
62
63 memset(buffer, 0x0, 1024);
64 memset(buf, 0x0, 1024);
65
66
67
68 if ( strlen(old_name) > 1015 ) {
69 free(buf);
70
71 return NAME_TOO_LONG;
72 }
73
74 if ( strlen(new_name) > 1015 ) {
75 free(buf);
76
77 return NAME_TOO_LONG;
78 }
79
80 sprintf(buffer, "RNFR %s\r\n", old_name);
81
82 if (write(sck, buffer, strlen(buffer)) == -1) {
83
84 if (verbose) {
85
86 if (errno == EBADF) {
87 free(buf);
88
89 fprintf(stderr, "Socket Descriptor Not Valid!\n");
90 return -1;
91 } else if (errno == EIO) {
92 free(buf);
93
94 fprintf(stderr, "I/O Error!\n");
95 return -1;
96 } else if (errno == EINTR) {
97 free(buf);
98
99 fprintf(stderr, "Signal Interrupted The write() Function\n");
100 return -1;
101 }
102
103 } else {
104
105 if (errno == EBADF) {
106 free(buf);
107
108 return -1;
109 } else if (errno == EIO) {
110 free(buf);
111
112 return -1;
113 } else if (errno == EINTR) {
114 free(buf);
115
116 return -1;
117 }
118 }
119
120 free(buf);
121
122 return -1;
123
124 }
125
126 tm.tv_sec = 3;
127 tm.tv_usec = 0;
128
129 FD_ZERO(&readfds);
130
131 FD_SET(sck, &readfds);
132
133 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 if (verbose) {
146 perror("recv()");
147 }
148 free(buf);
149
150 return -1;
151 }
152
153 if (verbose)
154 printf("[Server] %s\n", buf);
155
156 }
157 else {
158 if (verbose)
159 printf("[-] Timeout\n");
160
161 return -1;
162 }
163 }
164
165 buf[n] = '\0';
166
167
168 if (ftp_file_handler(buf, 0) != 0) {
169 free(buf);
170
171 return -1;
172 }
173
174 memset(buffer, 0x0, 1024);
175 memset(buf, 0x0, 1024);
176
177 sprintf(buffer, "RNTO %s\r\n", new_name);
178
179 if (write(sck, buffer, strlen(buffer)) == -1) {
180
181 if (verbose) {
182
183 if (errno == EBADF) {
184 free(buf);
185
186 fprintf(stderr, "Socket Descriptor Not Valid!\n");
187 return -1;
188 } else if (errno == EIO) {
189 free(buf);
190
191 fprintf(stderr, "I/O Error!\n");
192 return -1;
193 } else if (errno == EINTR) {
194 free(buf);
195
196 fprintf(stderr, "Signal Interrupted The write() Function\n");
197 return -1;
198 }
199
200 } else {
201
202 if (errno == EBADF) {
203 free(buf);
204
205 return -1;
206 } else if (errno == EIO) {
207 free(buf);
208
209 return -1;
210 } else if (errno == EINTR) {
211 free(buf);
212
213 return -1;
214 }
215
216 }
217
218 free(buf);
219
220 return -1;
221
222 }
223
224 FD_ZERO(&readfds);
225
226 FD_SET(sck, &readfds);
227
228 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 if (verbose) {
241 perror("recv()");
242 }
243 free(buf);
244
245 return -1;
246 }
247
248 if (verbose)
249 printf("[Server] %s\n", buf);
250
251 }
252 else {
253 if (verbose)
254 printf("[-] Timeout\n");
255
256 return -1;
257 }
258 }
259
260 buf[n] = '\0';
261
262
263 if (ftp_file_handler(buf, 0) != 0) {
264 free(buf);
265
266 return -1; //Error
267 }
268
269 free(buf);
270
271 return 0; //Renamed
272 }

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