summaryrefslogtreecommitdiff
path: root/networking/ftpgetput.c
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2003-12-20 05:43:34 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2003-12-20 05:43:34 +0000
commit236e93d133ff80fae0a0fb5f8912a049e6ead4ac (patch)
treec5406b92468eb881b76b1b62752bda3bb25252c3 /networking/ftpgetput.c
parentf62ea20affa0876756463f909d2d549d44736a26 (diff)
downloadbusybox-w32-236e93d133ff80fae0a0fb5f8912a049e6ead4ac.tar.gz
busybox-w32-236e93d133ff80fae0a0fb5f8912a049e6ead4ac.tar.bz2
busybox-w32-236e93d133ff80fae0a0fb5f8912a049e6ead4ac.zip
Allow recieving file to stdout, sending files from stdin, use the '-'
filename. Save a variable.
Diffstat (limited to '')
-rw-r--r--networking/ftpgetput.c72
1 files changed, 37 insertions, 35 deletions
diff --git a/networking/ftpgetput.c b/networking/ftpgetput.c
index 9eb54d6f0..27b272a48 100644
--- a/networking/ftpgetput.c
+++ b/networking/ftpgetput.c
@@ -101,11 +101,9 @@ static FILE *ftp_login(ftp_host_info_t *server)
101{ 101{
102 FILE *control_stream; 102 FILE *control_stream;
103 char buf[512]; 103 char buf[512];
104 int control_fd;
105 104
106 /* Connect to the command socket */ 105 /* Connect to the command socket */
107 control_fd = xconnect(server->s_in); 106 control_stream = fdopen(xconnect(server->s_in), "r+");
108 control_stream = fdopen(control_fd, "r+");
109 if (control_stream == NULL) { 107 if (control_stream == NULL) {
110 bb_perror_msg_and_die("Couldnt open control stream"); 108 bb_perror_msg_and_die("Couldnt open control stream");
111 } 109 }
@@ -136,17 +134,12 @@ static FILE *ftp_login(ftp_host_info_t *server)
136static int ftp_recieve(ftp_host_info_t *server, FILE *control_stream, 134static int ftp_recieve(ftp_host_info_t *server, FILE *control_stream,
137 const char *local_path, char *server_path) 135 const char *local_path, char *server_path)
138{ 136{
139 char *filename;
140 char *local_file;
141 char buf[512]; 137 char buf[512];
142 off_t filesize = 0; 138 off_t filesize = 0;
143 int fd_data; 139 int fd_data;
144 int fd_local; 140 int fd_local = -1;
145 off_t beg_range = 0; 141 off_t beg_range = 0;
146 142
147 filename = bb_get_last_path_component(server_path);
148 local_file = concat_path_file(local_path, filename);
149
150 /* Connect to the data socket */ 143 /* Connect to the data socket */
151 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) { 144 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
152 bb_error_msg_and_die("PASV error: %s", buf + 4); 145 bb_error_msg_and_die("PASV error: %s", buf + 4);
@@ -157,9 +150,14 @@ static int ftp_recieve(ftp_host_info_t *server, FILE *control_stream,
157 filesize = atol(buf + 4); 150 filesize = atol(buf + 4);
158 } 151 }
159 152
153 if ((local_path[0] == '-') && (local_path[1] == '\0')) {
154 fd_local = fileno(stdout);
155 do_continue = 0;
156 }
157
160 if (do_continue) { 158 if (do_continue) {
161 struct stat sbuf; 159 struct stat sbuf;
162 if (lstat(local_file, &sbuf) < 0) { 160 if (lstat(local_path, &sbuf) < 0) {
163 bb_perror_msg_and_die("fstat()"); 161 bb_perror_msg_and_die("fstat()");
164 } 162 }
165 if (sbuf.st_size > 0) { 163 if (sbuf.st_size > 0) {
@@ -183,10 +181,12 @@ static int ftp_recieve(ftp_host_info_t *server, FILE *control_stream,
183 } 181 }
184 182
185 /* only make a local file if we know that one exists on the remote server */ 183 /* only make a local file if we know that one exists on the remote server */
186 if (do_continue) { 184 if (fd_local == -1) {
187 fd_local = bb_xopen(local_file, O_APPEND | O_WRONLY); 185 if (do_continue) {
188 } else { 186 fd_local = bb_xopen(local_path, O_APPEND | O_WRONLY);
189 fd_local = bb_xopen(local_file, O_CREAT | O_TRUNC | O_WRONLY); 187 } else {
188 fd_local = bb_xopen(local_path, O_CREAT | O_TRUNC | O_WRONLY);
189 }
190 } 190 }
191 191
192 /* Copy the file */ 192 /* Copy the file */
@@ -226,21 +226,24 @@ static int ftp_send(ftp_host_info_t *server, FILE *control_stream,
226 } 226 }
227 227
228 /* get the local file */ 228 /* get the local file */
229 fd_local = bb_xopen(local_path, O_RDONLY); 229 if ((local_path[0] == '-') && (local_path[1] == '\0')) {
230 fstat(fd_local, &sbuf); 230 fd_local = fileno(stdin);
231 231 } else {
232 sprintf(buf, "ALLO %lu", (unsigned long)sbuf.st_size); 232 fd_local = bb_xopen(local_path, O_RDONLY);
233 response = ftpcmd(buf, NULL, control_stream, buf); 233 fstat(fd_local, &sbuf);
234 switch (response) { 234
235 case 200: 235 sprintf(buf, "ALLO %lu", (unsigned long)sbuf.st_size);
236 case 202: 236 response = ftpcmd(buf, NULL, control_stream, buf);
237 break; 237 switch (response) {
238 default: 238 case 200:
239 close(fd_local); 239 case 202:
240 bb_error_msg_and_die("ALLO error: %s", buf + 4); 240 break;
241 break; 241 default:
242 close(fd_local);
243 bb_error_msg_and_die("ALLO error: %s", buf + 4);
244 break;
245 }
242 } 246 }
243
244 response = ftpcmd("STOR ", local_path, control_stream, buf); 247 response = ftpcmd("STOR ", local_path, control_stream, buf);
245 switch (response) { 248 switch (response) {
246 case 125: 249 case 125:
@@ -328,6 +331,12 @@ int ftpgetput_main(int argc, char **argv)
328 */ 331 */
329 bb_applet_long_options = ftpgetput_long_options; 332 bb_applet_long_options = ftpgetput_long_options;
330 opt = bb_getopt_ulflags(argc, argv, "cvu:p:P:", &server->user, &server->password, &port); 333 opt = bb_getopt_ulflags(argc, argv, "cvu:p:P:", &server->user, &server->password, &port);
334
335 /* Process the non-option command line arguments */
336 if (argc - optind != 3) {
337 bb_show_usage();
338 }
339
331 if (opt & FTPGETPUT_OPT_CONTINUE) { 340 if (opt & FTPGETPUT_OPT_CONTINUE) {
332 do_continue = 1; 341 do_continue = 1;
333 } 342 }
@@ -335,13 +344,6 @@ int ftpgetput_main(int argc, char **argv)
335 verbose_flag = 1; 344 verbose_flag = 1;
336 } 345 }
337 346
338 /*
339 * Process the non-option command line arguments
340 */
341 if (argc - optind != 3) {
342 bb_show_usage();
343 }
344
345 /* We want to do exactly _one_ DNS lookup, since some 347 /* We want to do exactly _one_ DNS lookup, since some
346 * sites (i.e. ftp.us.debian.org) use round-robin DNS 348 * sites (i.e. ftp.us.debian.org) use round-robin DNS
347 * and we want to connect to only one IP... */ 349 * and we want to connect to only one IP... */