summaryrefslogtreecommitdiff
path: root/sftp-int.c
diff options
context:
space:
mode:
Diffstat (limited to 'sftp-int.c')
-rw-r--r--sftp-int.c46
1 files changed, 33 insertions, 13 deletions
diff --git a/sftp-int.c b/sftp-int.c
index 53136be07..7aa7abdb9 100644
--- a/sftp-int.c
+++ b/sftp-int.c
@@ -28,7 +28,7 @@
28/* XXX: recursive operations */ 28/* XXX: recursive operations */
29 29
30#include "includes.h" 30#include "includes.h"
31RCSID("$OpenBSD: sftp-int.c,v 1.24 2001/03/04 17:42:28 millert Exp $"); 31RCSID("$OpenBSD: sftp-int.c,v 1.25 2001/03/06 06:11:44 deraadt Exp $");
32 32
33#include "buffer.h" 33#include "buffer.h"
34#include "xmalloc.h" 34#include "xmalloc.h"
@@ -40,6 +40,8 @@ RCSID("$OpenBSD: sftp-int.c,v 1.24 2001/03/04 17:42:28 millert Exp $");
40#include "sftp-client.h" 40#include "sftp-client.h"
41#include "sftp-int.h" 41#include "sftp-int.h"
42 42
43extern FILE* infile;
44
43/* Seperators for interactive commands */ 45/* Seperators for interactive commands */
44#define WHITESPACE " \t\r\n" 46#define WHITESPACE " \t\r\n"
45 47
@@ -444,6 +446,7 @@ parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
444 unsigned long n_arg; 446 unsigned long n_arg;
445 Attrib a, *aa; 447 Attrib a, *aa;
446 char path_buf[MAXPATHLEN]; 448 char path_buf[MAXPATHLEN];
449 int err = 0;
447 450
448 path1 = path2 = NULL; 451 path1 = path2 = NULL;
449 cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2); 452 cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2);
@@ -454,49 +457,54 @@ parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
454 break; 457 break;
455 case I_GET: 458 case I_GET:
456 path1 = make_absolute(path1, *pwd); 459 path1 = make_absolute(path1, *pwd);
457 do_download(in, out, path1, path2, pflag); 460 err = do_download(in, out, path1, path2, pflag);
458 break; 461 break;
459 case I_PUT: 462 case I_PUT:
460 path2 = make_absolute(path2, *pwd); 463 path2 = make_absolute(path2, *pwd);
461 do_upload(in, out, path1, path2, pflag); 464 err = do_upload(in, out, path1, path2, pflag);
462 break; 465 break;
463 case I_RENAME: 466 case I_RENAME:
464 path1 = make_absolute(path1, *pwd); 467 path1 = make_absolute(path1, *pwd);
465 path2 = make_absolute(path2, *pwd); 468 path2 = make_absolute(path2, *pwd);
466 do_rename(in, out, path1, path2); 469 err = do_rename(in, out, path1, path2);
467 break; 470 break;
468 case I_RM: 471 case I_RM:
469 path1 = make_absolute(path1, *pwd); 472 path1 = make_absolute(path1, *pwd);
470 do_rm(in, out, path1); 473 err = do_rm(in, out, path1);
471 break; 474 break;
472 case I_MKDIR: 475 case I_MKDIR:
473 path1 = make_absolute(path1, *pwd); 476 path1 = make_absolute(path1, *pwd);
474 attrib_clear(&a); 477 attrib_clear(&a);
475 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; 478 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
476 a.perm = 0777; 479 a.perm = 0777;
477 do_mkdir(in, out, path1, &a); 480 err = do_mkdir(in, out, path1, &a);
478 break; 481 break;
479 case I_RMDIR: 482 case I_RMDIR:
480 path1 = make_absolute(path1, *pwd); 483 path1 = make_absolute(path1, *pwd);
481 do_rmdir(in, out, path1); 484 err = do_rmdir(in, out, path1);
482 break; 485 break;
483 case I_CHDIR: 486 case I_CHDIR:
484 path1 = make_absolute(path1, *pwd); 487 path1 = make_absolute(path1, *pwd);
485 if ((tmp = do_realpath(in, out, path1)) == NULL) 488 if ((tmp = do_realpath(in, out, path1)) == NULL) {
489 err = 1;
486 break; 490 break;
491 }
487 if ((aa = do_stat(in, out, tmp)) == NULL) { 492 if ((aa = do_stat(in, out, tmp)) == NULL) {
488 xfree(tmp); 493 xfree(tmp);
494 err = 1;
489 break; 495 break;
490 } 496 }
491 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) { 497 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
492 error("Can't change directory: Can't check target"); 498 error("Can't change directory: Can't check target");
493 xfree(tmp); 499 xfree(tmp);
500 err = 1;
494 break; 501 break;
495 } 502 }
496 if (!S_ISDIR(aa->perm)) { 503 if (!S_ISDIR(aa->perm)) {
497 error("Can't change directory: \"%s\" is not " 504 error("Can't change directory: \"%s\" is not "
498 "a directory", tmp); 505 "a directory", tmp);
499 xfree(tmp); 506 xfree(tmp);
507 err = 1;
500 break; 508 break;
501 } 509 }
502 xfree(*pwd); 510 xfree(*pwd);
@@ -522,14 +530,18 @@ parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
522 do_ls(in, out, path1); 530 do_ls(in, out, path1);
523 break; 531 break;
524 case I_LCHDIR: 532 case I_LCHDIR:
525 if (chdir(path1) == -1) 533 if (chdir(path1) == -1) {
526 error("Couldn't change local directory to " 534 error("Couldn't change local directory to "
527 "\"%s\": %s", path1, strerror(errno)); 535 "\"%s\": %s", path1, strerror(errno));
536 err = 1;
537 }
528 break; 538 break;
529 case I_LMKDIR: 539 case I_LMKDIR:
530 if (mkdir(path1, 0777) == -1) 540 if (mkdir(path1, 0777) == -1) {
531 error("Couldn't create local directory " 541 error("Couldn't create local directory "
532 "\"%s\": %s", path1, strerror(errno)); 542 "\"%s\": %s", path1, strerror(errno));
543 err = 1;
544 }
533 break; 545 break;
534 case I_LLS: 546 case I_LLS:
535 local_do_ls(cmd); 547 local_do_ls(cmd);
@@ -598,6 +610,11 @@ parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
598 xfree(path1); 610 xfree(path1);
599 if (path2) 611 if (path2)
600 xfree(path2); 612 xfree(path2);
613
614 /* If an error occurs in batch mode we should abort. */
615 if (infile != stdin && err > 0)
616 return -1;
617
601 return(0); 618 return(0);
602} 619}
603 620
@@ -612,7 +629,7 @@ interactive_loop(int fd_in, int fd_out)
612 fatal("Need cwd"); 629 fatal("Need cwd");
613 630
614 setvbuf(stdout, NULL, _IOLBF, 0); 631 setvbuf(stdout, NULL, _IOLBF, 0);
615 setvbuf(stdin, NULL, _IOLBF, 0); 632 setvbuf(infile, NULL, _IOLBF, 0);
616 633
617 for(;;) { 634 for(;;) {
618 char *cp; 635 char *cp;
@@ -620,13 +637,16 @@ interactive_loop(int fd_in, int fd_out)
620 printf("sftp> "); 637 printf("sftp> ");
621 638
622 /* XXX: use libedit */ 639 /* XXX: use libedit */
623 if (fgets(cmd, sizeof(cmd), stdin) == NULL) { 640 if (fgets(cmd, sizeof(cmd), infile) == NULL) {
624 printf("\n"); 641 printf("\n");
625 break; 642 break;
626 } 643 } else if (infile != stdin) /* Bluff typing */
644 printf("%s", cmd);
645
627 cp = strrchr(cmd, '\n'); 646 cp = strrchr(cmd, '\n');
628 if (cp) 647 if (cp)
629 *cp = '\0'; 648 *cp = '\0';
649
630 if (parse_dispatch_command(fd_in, fd_out, cmd, &pwd)) 650 if (parse_dispatch_command(fd_in, fd_out, cmd, &pwd))
631 break; 651 break;
632 } 652 }