[BACK]Return to scp.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Diff for /src/usr.bin/ssh/scp.c between version 1.43 and 1.43.2.5

version 1.43, 2000/10/18 18:23:02 version 1.43.2.5, 2001/09/27 00:15:42
Line 14 
Line 14 
  * called by a name other than "ssh" or "Secure Shell".   * called by a name other than "ssh" or "Secure Shell".
  */   */
 /*  /*
  * Copyright (c) 1999 Theo de Raadt. All rights reserved.   * Copyright (c) 1999 Theo de Raadt.  All rights reserved.
  * Copyright (c) 1999 Aaron Campbell. All rights reserved.   * Copyright (c) 1999 Aaron Campbell.  All rights reserved.
  *   *
  * Redistribution and use in source and binary forms, with or without   * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions   * modification, are permitted provided that the following conditions
Line 77 
Line 77 
 #include "includes.h"  #include "includes.h"
 RCSID("$OpenBSD$");  RCSID("$OpenBSD$");
   
 #include "ssh.h"  
 #include "xmalloc.h"  #include "xmalloc.h"
   #include "atomicio.h"
   #include "pathnames.h"
   #include "log.h"
   #include "misc.h"
   
 #define _PATH_CP "cp"  
   
 /* For progressmeter() -- number of seconds before xfer considered "stalled" */  /* For progressmeter() -- number of seconds before xfer considered "stalled" */
 #define STALLTIME       5  #define STALLTIME       5
   /* alarm() interval for updating progress meter */
   #define PROGRESSTIME    1
   
 /* Visual statistics about files as they are transferred. */  /* Visual statistics about files as they are transferred. */
 void progressmeter(int);  void progressmeter(int);
Line 92 
Line 95 
 int getttywidth(void);  int getttywidth(void);
 int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc);  int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc);
   
 /* setup arguments for the call to ssh */  /* Struct for addargs */
 void addargs(char *fmt, ...) __attribute__((format(printf, 1, 2)));  arglist args;
   
 /* Time a transfer started. */  /* Time a transfer started. */
 static struct timeval start;  static struct timeval start;
   
 /* Number of bytes of current file transferred so far. */  /* Number of bytes of current file transferred so far. */
 volatile unsigned long statbytes;  volatile off_t statbytes;
   
 /* Total size of current file. */  /* Total size of current file. */
 off_t totalbytes = 0;  off_t totalbytes = 0;
Line 110 
Line 113 
 /* This is set to non-zero to enable verbose mode. */  /* This is set to non-zero to enable verbose mode. */
 int verbose_mode = 0;  int verbose_mode = 0;
   
 /* This is set to non-zero if compression is desired. */  
 int compress = 0;  
   
 /* This is set to zero if the progressmeter is not desired. */  /* This is set to zero if the progressmeter is not desired. */
 int showprogress = 1;  int showprogress = 1;
   
 /* This is the program to execute for the secured connection. ("ssh" or -S) */  /* This is the program to execute for the secured connection. ("ssh" or -S) */
 char *ssh_program = SSH_PROGRAM;  char *ssh_program = _PATH_SSH_PROGRAM;
   
 /* This is the list of arguments that scp passes to ssh */  
 struct {  
         char    **list;  
         int     num;  
         int     nalloc;  
 } args;  
   
 /*  /*
  * This function executes the given command as the specified user on the   * This function executes the given command as the specified user on the
  * given host.  This returns < 0 if execution fails, and >= 0 otherwise. This   * given host.  This returns < 0 if execution fails, and >= 0 otherwise. This
Line 138 
Line 131 
         int pin[2], pout[2], reserved[2];          int pin[2], pout[2], reserved[2];
   
         if (verbose_mode)          if (verbose_mode)
                 fprintf(stderr, "Executing: program %s host %s, user %s, command %s\n",                  fprintf(stderr,
                     ssh_program, host, remuser ? remuser : "(unspecified)", cmd);                      "Executing: program %s host %s, user %s, command %s\n",
                       ssh_program, host,
                       remuser ? remuser : "(unspecified)", cmd);
   
         /*          /*
          * Reserve two descriptors so that the real pipes won't get           * Reserve two descriptors so that the real pipes won't get
Line 169 
Line 164 
   
                 args.list[0] = ssh_program;                  args.list[0] = ssh_program;
                 if (remuser != NULL)                  if (remuser != NULL)
                         addargs("-l%s", remuser);                          addargs(&args, "-l%s", remuser);
                 addargs("%s", host);                  addargs(&args, "%s", host);
                 addargs("%s", cmd);                  addargs(&args, "%s", cmd);
   
                 execvp(ssh_program, args.list);                  execvp(ssh_program, args.list);
                 perror(ssh_program);                  perror(ssh_program);
Line 185 
Line 180 
         return 0;          return 0;
 }  }
   
 void  
 fatal(const char *fmt,...)  
 {  
         va_list ap;  
         char buf[1024];  
   
         va_start(ap, fmt);  
         vsnprintf(buf, sizeof(buf), fmt, ap);  
         va_end(ap);  
         fprintf(stderr, "%s\n", buf);  
         exit(255);  
 }  
   
 typedef struct {  typedef struct {
         int cnt;          int cnt;
         char *buf;          char *buf;
 } BUF;  } BUF;
   
 extern int iamremote;  
   
 BUF *allocbuf(BUF *, int, int);  BUF *allocbuf(BUF *, int, int);
 char *colon(char *);  
 void lostconn(int);  void lostconn(int);
 void nospace(void);  void nospace(void);
 int okname(char *);  int okname(char *);
Line 240 
Line 219 
         extern int optind;          extern int optind;
   
         args.list = NULL;          args.list = NULL;
         addargs("ssh");         /* overwritten with ssh_program */          addargs(&args, "ssh");          /* overwritten with ssh_program */
         addargs("-x");          addargs(&args, "-x");
         addargs("-oFallBackToRsh no");          addargs(&args, "-oForwardAgent no");
           addargs(&args, "-oFallBackToRsh no");
           addargs(&args, "-oClearAllForwardings yes");
   
         fflag = tflag = 0;          fflag = tflag = 0;
         while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:")) != EOF)          while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:F:")) != -1)
                 switch (ch) {                  switch (ch) {
                 /* User-visible flags. */                  /* User-visible flags. */
                 case '4':                  case '4':
                 case '6':                  case '6':
                 case 'C':                  case 'C':
                         addargs("-%c", ch);                          addargs(&args, "-%c", ch);
                         break;                          break;
                 case 'o':                  case 'o':
                 case 'c':                  case 'c':
                 case 'i':                  case 'i':
                         addargs("-%c%s", ch, optarg);                  case 'F':
                           addargs(&args, "-%c%s", ch, optarg);
                         break;                          break;
                 case 'P':                  case 'P':
                         addargs("-p%s", optarg);                          addargs(&args, "-p%s", optarg);
                         break;                          break;
                 case 'B':                  case 'B':
                         addargs("-oBatchmode yes");                          addargs(&args, "-oBatchmode yes");
                         break;                          break;
                 case 'p':                  case 'p':
                         pflag = 1;                          pflag = 1;
Line 274 
Line 256 
                         ssh_program = xstrdup(optarg);                          ssh_program = xstrdup(optarg);
                         break;                          break;
                 case 'v':                  case 'v':
                           addargs(&args, "-v");
                         verbose_mode = 1;                          verbose_mode = 1;
                         break;                          break;
                 case 'q':                  case 'q':
Line 292 
Line 275 
                         iamremote = 1;                          iamremote = 1;
                         tflag = 1;                          tflag = 1;
                         break;                          break;
                 case '?':  
                 default:                  default:
                         usage();                          usage();
                 }                  }
Line 308 
Line 290 
         remin = STDIN_FILENO;          remin = STDIN_FILENO;
         remout = STDOUT_FILENO;          remout = STDOUT_FILENO;
   
         if (fflag) {          if (fflag) {
                 /* Follow "protocol", send data. */                  /* Follow "protocol", send data. */
                 (void) response();                  (void) response();
                 source(argc, argv);                  source(argc, argv);
Line 326 
Line 308 
   
         remin = remout = -1;          remin = remout = -1;
         /* Command to be executed on remote system using "ssh". */          /* Command to be executed on remote system using "ssh". */
         (void) sprintf(cmd, "scp%s%s%s%s", verbose_mode ? " -v" : "",          (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
               verbose_mode ? " -v" : "",
             iamrecursive ? " -r" : "", pflag ? " -p" : "",              iamrecursive ? " -r" : "", pflag ? " -p" : "",
             targetshouldbedirectory ? " -d" : "");              targetshouldbedirectory ? " -d" : "");
   
Line 342 
Line 325 
         exit(errs != 0);          exit(errs != 0);
 }  }
   
 char *  
 cleanhostname(host)  
         char *host;  
 {  
         if (*host == '[' && host[strlen(host) - 1] == ']') {  
                 host[strlen(host) - 1] = '\0';  
                 return (host + 1);  
         } else  
                 return host;  
 }  
   
 void  void
 toremote(targ, argc, argv)  toremote(targ, argc, argv)
         char *targ, *argv[];          char *targ, *argv[];
Line 381 
Line 353 
         for (i = 0; i < argc - 1; i++) {          for (i = 0; i < argc - 1; i++) {
                 src = colon(argv[i]);                  src = colon(argv[i]);
                 if (src) {      /* remote to remote */                  if (src) {      /* remote to remote */
                           static char *ssh_options =
                               "-x -o'FallBackToRsh no' "
                               "-o'ClearAllForwardings yes'";
                         *src++ = 0;                          *src++ = 0;
                         if (*src == 0)                          if (*src == 0)
                                 src = ".";                                  src = ".";
                         host = strchr(argv[i], '@');                          host = strchr(argv[i], '@');
                         len = strlen(ssh_program) + strlen(argv[i]) +                          len = strlen(ssh_program) + strlen(argv[i]) +
                             strlen(src) + (tuser ? strlen(tuser) : 0) +                              strlen(src) + (tuser ? strlen(tuser) : 0) +
                             strlen(thost) + strlen(targ) + CMDNEEDS + 32;                              strlen(thost) + strlen(targ) +
                               strlen(ssh_options) + CMDNEEDS + 20;
                         bp = xmalloc(len);                          bp = xmalloc(len);
                         if (host) {                          if (host) {
                                 *host++ = 0;                                  *host++ = 0;
Line 397 
Line 373 
                                         suser = pwd->pw_name;                                          suser = pwd->pw_name;
                                 else if (!okname(suser))                                  else if (!okname(suser))
                                         continue;                                          continue;
                                 (void) sprintf(bp,                                  snprintf(bp, len,
                                     "%s%s -x -o'FallBackToRsh no' -n -l %s %s %s %s '%s%s%s:%s'",                                      "%s%s %s -n "
                                      ssh_program, verbose_mode ? " -v" : "",                                      "-l %s %s %s %s '%s%s%s:%s'",
                                      suser, host, cmd, src,                                      ssh_program, verbose_mode ? " -v" : "",
                                      tuser ? tuser : "", tuser ? "@" : "",                                      ssh_options, suser, host, cmd, src,
                                      thost, targ);                                      tuser ? tuser : "", tuser ? "@" : "",
                                       thost, targ);
                         } else {                          } else {
                                 host = cleanhostname(argv[i]);                                  host = cleanhostname(argv[i]);
                                 (void) sprintf(bp,                                  snprintf(bp, len,
                                     "exec %s%s -x -o'FallBackToRsh no' -n %s %s %s '%s%s%s:%s'",                                      "exec %s%s %s -n %s "
                                      ssh_program, verbose_mode ? " -v" : "",                                      "%s %s '%s%s%s:%s'",
                                      host, cmd, src,                                      ssh_program, verbose_mode ? " -v" : "",
                                      tuser ? tuser : "", tuser ? "@" : "",                                      ssh_options, host, cmd, src,
                                      thost, targ);                                      tuser ? tuser : "", tuser ? "@" : "",
                                       thost, targ);
                         }                          }
                         if (verbose_mode)                          if (verbose_mode)
                                 fprintf(stderr, "Executing: %s\n", bp);                                  fprintf(stderr, "Executing: %s\n", bp);
Line 420 
Line 398 
                         if (remin == -1) {                          if (remin == -1) {
                                 len = strlen(targ) + CMDNEEDS + 20;                                  len = strlen(targ) + CMDNEEDS + 20;
                                 bp = xmalloc(len);                                  bp = xmalloc(len);
                                 (void) sprintf(bp, "%s -t %s", cmd, targ);                                  (void) snprintf(bp, len, "%s -t %s", cmd, targ);
                                 host = cleanhostname(thost);                                  host = cleanhostname(thost);
                                 if (do_cmd(host, tuser, bp, &remin,                                  if (do_cmd(host, tuser, bp, &remin,
                                     &remout, argc) < 0)                                      &remout, argc) < 0)
Line 447 
Line 425 
                         len = strlen(_PATH_CP) + strlen(argv[i]) +                          len = strlen(_PATH_CP) + strlen(argv[i]) +
                             strlen(argv[argc - 1]) + 20;                              strlen(argv[argc - 1]) + 20;
                         bp = xmalloc(len);                          bp = xmalloc(len);
                         (void) sprintf(bp, "exec %s%s%s %s %s", _PATH_CP,                          (void) snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
                             iamrecursive ? " -r" : "", pflag ? " -p" : "",                              iamrecursive ? " -r" : "", pflag ? " -p" : "",
                             argv[i], argv[argc - 1]);                              argv[i], argv[argc - 1]);
                         if (verbose_mode)                          if (verbose_mode)
Line 474 
Line 452 
                 host = cleanhostname(host);                  host = cleanhostname(host);
                 len = strlen(src) + CMDNEEDS + 20;                  len = strlen(src) + CMDNEEDS + 20;
                 bp = xmalloc(len);                  bp = xmalloc(len);
                 (void) sprintf(bp, "%s -f %s", cmd, src);                  (void) snprintf(bp, len, "%s -f %s", cmd, src);
                 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {                  if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
                         (void) xfree(bp);                          (void) xfree(bp);
                         ++errs;                          ++errs;
Line 495 
Line 473 
         struct stat stb;          struct stat stb;
         static BUF buffer;          static BUF buffer;
         BUF *bp;          BUF *bp;
         off_t i;          off_t i, amt, result;
         int amt, fd, haderr, indx, result;          int fd, haderr, indx;
         char *last, *name, buf[2048];          char *last, *name, buf[2048];
           int len;
   
         for (indx = 0; indx < argc; ++indx) {          for (indx = 0; indx < argc; ++indx) {
                 name = argv[indx];                  name = argv[indx];
                 statbytes = 0;                  statbytes = 0;
                   len = strlen(name);
                   while (len > 1 && name[len-1] == '/')
                           name[--len] = '\0';
                 if ((fd = open(name, O_RDONLY, 0)) < 0)                  if ((fd = open(name, O_RDONLY, 0)) < 0)
                         goto syserr;                          goto syserr;
                 if (fstat(fd, &stb) < 0) {                  if (fstat(fd, &stb) < 0) {
Line 531 
Line 513 
                          * Make it compatible with possible future                           * Make it compatible with possible future
                          * versions expecting microseconds.                           * versions expecting microseconds.
                          */                           */
                         (void) sprintf(buf, "T%lu 0 %lu 0\n",                          (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
                             (unsigned long) stb.st_mtime,                              (u_long) stb.st_mtime,
                             (unsigned long) stb.st_atime);                              (u_long) stb.st_atime);
                         (void) atomicio(write, remout, buf, strlen(buf));                          (void) atomicio(write, remout, buf, strlen(buf));
                         if (response() < 0)                          if (response() < 0)
                                 goto next;                                  goto next;
                 }                  }
 #define FILEMODEMASK    (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)  #define FILEMODEMASK    (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
                 (void) sprintf(buf, "C%04o %lu %s\n",                  snprintf(buf, sizeof buf, "C%04o %lld %s\n",
                              (unsigned int) (stb.st_mode & FILEMODEMASK),                      (u_int) (stb.st_mode & FILEMODEMASK),
                                (unsigned long) stb.st_size,                      (long long)stb.st_size, last);
                                last);  
                 if (verbose_mode) {                  if (verbose_mode) {
                         fprintf(stderr, "Sending file modes: %s", buf);                          fprintf(stderr, "Sending file modes: %s", buf);
                         fflush(stderr);                          fflush(stderr);
Line 609 
Line 590 
         else          else
                 last++;                  last++;
         if (pflag) {          if (pflag) {
                 (void) sprintf(path, "T%lu 0 %lu 0\n",                  (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
                     (unsigned long) statp->st_mtime,                      (u_long) statp->st_mtime,
                     (unsigned long) statp->st_atime);                      (u_long) statp->st_atime);
                 (void) atomicio(write, remout, path, strlen(path));                  (void) atomicio(write, remout, path, strlen(path));
                 if (response() < 0) {                  if (response() < 0) {
                         closedir(dirp);                          closedir(dirp);
                         return;                          return;
                 }                  }
         }          }
         (void) sprintf(path, "D%04o %d %.1024s\n",          (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
             (unsigned int) (statp->st_mode & FILEMODEMASK), 0, last);              (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
         if (verbose_mode)          if (verbose_mode)
                 fprintf(stderr, "Entering directory: %s", path);                  fprintf(stderr, "Entering directory: %s", path);
         (void) atomicio(write, remout, path, strlen(path));          (void) atomicio(write, remout, path, strlen(path));
Line 627 
Line 608 
                 closedir(dirp);                  closedir(dirp);
                 return;                  return;
         }          }
         while ((dp = readdir(dirp))) {          while ((dp = readdir(dirp)) != NULL) {
                 if (dp->d_ino == 0)                  if (dp->d_ino == 0)
                         continue;                          continue;
                 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))                  if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
Line 636 
Line 617 
                         run_err("%s/%s: name too long", name, dp->d_name);                          run_err("%s/%s: name too long", name, dp->d_name);
                         continue;                          continue;
                 }                  }
                 (void) sprintf(path, "%s/%s", name, dp->d_name);                  (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
                 vect[0] = path;                  vect[0] = path;
                 source(1, vect);                  source(1, vect);
         }          }
Line 661 
Line 642 
         off_t size;          off_t size;
         int setimes, targisdir, wrerrno = 0;          int setimes, targisdir, wrerrno = 0;
         char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];          char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
         int dummy_usec;  
         struct timeval tv[2];          struct timeval tv[2];
   
 #define SCREWUP(str)    { why = str; goto screwup; }  #define atime   tv[0]
   #define mtime   tv[1]
   #define SCREWUP(str)    do { why = str; goto screwup; } while (0)
   
         setimes = targisdir = 0;          setimes = targisdir = 0;
         mask = umask(0);          mask = umask(0);
Line 697 
Line 679 
                 if (buf[0] == '\01' || buf[0] == '\02') {                  if (buf[0] == '\01' || buf[0] == '\02') {
                         if (iamremote == 0)                          if (iamremote == 0)
                                 (void) atomicio(write, STDERR_FILENO,                                  (void) atomicio(write, STDERR_FILENO,
                                              buf + 1, strlen(buf + 1));                                      buf + 1, strlen(buf + 1));
                         if (buf[0] == '\02')                          if (buf[0] == '\02')
                                 exit(1);                                  exit(1);
                         ++errs;                          ++errs;
Line 710 
Line 692 
                 if (ch == '\n')                  if (ch == '\n')
                         *--cp = 0;                          *--cp = 0;
   
 #define getnum(t) (t) = 0; \  
   while (*cp >= '0' && *cp <= '9') (t) = (t) * 10 + (*cp++ - '0');  
                 cp = buf;                  cp = buf;
                 if (*cp == 'T') {                  if (*cp == 'T') {
                         setimes++;                          setimes++;
                         cp++;                          cp++;
                         getnum(tv[1].tv_sec);                          mtime.tv_sec = strtol(cp, &cp, 10);
                         if (*cp++ != ' ')                          if (!cp || *cp++ != ' ')
                                 SCREWUP("mtime.sec not delimited");                                  SCREWUP("mtime.sec not delimited");
                         getnum(dummy_usec);                          mtime.tv_usec = strtol(cp, &cp, 10);
                         tv[1].tv_usec = 0;                          if (!cp || *cp++ != ' ')
                         if (*cp++ != ' ')  
                                 SCREWUP("mtime.usec not delimited");                                  SCREWUP("mtime.usec not delimited");
                         getnum(tv[0].tv_sec);                          atime.tv_sec = strtol(cp, &cp, 10);
                         if (*cp++ != ' ')                          if (!cp || *cp++ != ' ')
                                 SCREWUP("atime.sec not delimited");                                  SCREWUP("atime.sec not delimited");
                         getnum(dummy_usec);                          atime.tv_usec = strtol(cp, &cp, 10);
                         tv[0].tv_usec = 0;                          if (!cp || *cp++ != '\0')
                         if (*cp++ != '\0')  
                                 SCREWUP("atime.usec not delimited");                                  SCREWUP("atime.usec not delimited");
                         (void) atomicio(write, remout, "", 1);                          (void) atomicio(write, remout, "", 1);
                         continue;                          continue;
Line 756 
Line 734 
                 if (*cp++ != ' ')                  if (*cp++ != ' ')
                         SCREWUP("mode not delimited");                          SCREWUP("mode not delimited");
   
                 for (size = 0; *cp >= '0' && *cp <= '9';)                  for (size = 0; isdigit(*cp);)
                         size = size * 10 + (*cp++ - '0');                          size = size * 10 + (*cp++ - '0');
                 if (*cp++ != ' ')                  if (*cp++ != ' ')
                         SCREWUP("size not delimited");                          SCREWUP("size not delimited");
Line 766 
Line 744 
                         size_t need;                          size_t need;
   
                         need = strlen(targ) + strlen(cp) + 250;                          need = strlen(targ) + strlen(cp) + 250;
                         if (need > cursize)                          if (need > cursize) {
                                   if (namebuf)
                                           xfree(namebuf);
                                 namebuf = xmalloc(need);                                  namebuf = xmalloc(need);
                         (void) sprintf(namebuf, "%s%s%s", targ,                                  cursize = need;
                           }
                           (void) snprintf(namebuf, need, "%s%s%s", targ,
                             *targ ? "/" : "", cp);                              *targ ? "/" : "", cp);
                         np = namebuf;                          np = namebuf;
                 } else                  } else
Line 791 
Line 773 
                                 if (mkdir(np, mode | S_IRWXU) < 0)                                  if (mkdir(np, mode | S_IRWXU) < 0)
                                         goto bad;                                          goto bad;
                         }                          }
                         vect[0] = np;                          vect[0] = xstrdup(np);
                         sink(1, vect);                          sink(1, vect);
                         if (setimes) {                          if (setimes) {
                                 setimes = 0;                                  setimes = 0;
                                 if (utimes(np, tv) < 0)                                  if (utimes(vect[0], tv) < 0)
                                         run_err("%s: set times: %s",                                          run_err("%s: set times: %s",
                                                 np, strerror(errno));                                              vect[0], strerror(errno));
                         }                          }
                         if (mod_flag)                          if (mod_flag)
                                 (void) chmod(np, mode);                                  (void) chmod(vect[0], mode);
                           if (vect[0])
                                   xfree(vect[0]);
                         continue;                          continue;
                 }                  }
                 omode = mode;                  omode = mode;
                 mode |= S_IWRITE;                  mode |= S_IWRITE;
                 if ((ofd = open(np, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {                  if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
 bad:                    run_err("%s: %s", np, strerror(errno));  bad:                    run_err("%s: %s", np, strerror(errno));
                         continue;                          continue;
                 }                  }
Line 829 
Line 813 
                         count += amt;                          count += amt;
                         do {                          do {
                                 j = read(remin, cp, amt);                                  j = read(remin, cp, amt);
                                 if (j == -1 && (errno == EINTR || errno == EAGAIN)) {                                  if (j == -1 && (errno == EINTR ||
                                       errno == EAGAIN)) {
                                         continue;                                          continue;
                                 } else if (j <= 0) {                                  } else if (j <= 0) {
                                         run_err("%s", j ? strerror(errno) :                                          run_err("%s", j ? strerror(errno) :
                                                 "dropped connection");                                              "dropped connection");
                                         exit(1);                                          exit(1);
                                 }                                  }
                                 amt -= j;                                  amt -= j;
Line 860 
Line 845 
                         wrerr = YES;                          wrerr = YES;
                         wrerrno = j >= 0 ? EIO : errno;                          wrerrno = j >= 0 ? EIO : errno;
                 }                  }
 #if 0  
                 if (ftruncate(ofd, size)) {                  if (ftruncate(ofd, size)) {
                         run_err("%s: truncate: %s", np, strerror(errno));                          run_err("%s: truncate: %s", np, strerror(errno));
                         wrerr = DISPLAYED;                          wrerr = DISPLAYED;
                 }                  }
 #endif  
                 if (pflag) {                  if (pflag) {
                         if (exists || omode != mode)                          if (exists || omode != mode)
                                 if (fchmod(ofd, omode))                                  if (fchmod(ofd, omode))
                                         run_err("%s: set mode: %s",                                          run_err("%s: set mode: %s",
                                                 np, strerror(errno));                                              np, strerror(errno));
                 } else {                  } else {
                         if (!exists && omode != mode)                          if (!exists && omode != mode)
                                 if (fchmod(ofd, omode & ~mask))                                  if (fchmod(ofd, omode & ~mask))
                                         run_err("%s: set mode: %s",                                          run_err("%s: set mode: %s",
                                                 np, strerror(errno));                                              np, strerror(errno));
                 }                  }
                 if (close(ofd) == -1) {                  if (close(ofd) == -1) {
                         wrerr = YES;                          wrerr = YES;
Line 886 
Line 869 
                         setimes = 0;                          setimes = 0;
                         if (utimes(np, tv) < 0) {                          if (utimes(np, tv) < 0) {
                                 run_err("%s: set times: %s",                                  run_err("%s: set times: %s",
                                         np, strerror(errno));                                      np, strerror(errno));
                                 wrerr = DISPLAYED;                                  wrerr = DISPLAYED;
                         }                          }
                 }                  }
Line 942 
Line 925 
 void  void
 usage()  usage()
 {  {
         (void) fprintf(stderr, "usage: scp "          (void) fprintf(stderr,
             "[-pqrvC46] [-S ssh] [-P port] [-c cipher] [-i identity] f1 f2; or:\n"              "usage: scp [-pqrvBC46] [-F config] [-S ssh] [-P port] [-c cipher] [-i identity]\n"
             "       scp [options] f1 ... fn directory\n");              "           [-o option] f1 f2\n"
               "   or: scp [options] f1 ... fn directory\n");
         exit(1);          exit(1);
 }  }
   
Line 953 
Line 937 
 {  {
         static FILE *fp;          static FILE *fp;
         va_list ap;          va_list ap;
         va_start(ap, fmt);  
   
         ++errs;          ++errs;
         if (fp == NULL && !(fp = fdopen(remout, "w")))          if (fp == NULL && !(fp = fdopen(remout, "w")))
                 return;                  return;
         (void) fprintf(fp, "%c", 0x01);          (void) fprintf(fp, "%c", 0x01);
         (void) fprintf(fp, "scp: ");          (void) fprintf(fp, "scp: ");
           va_start(ap, fmt);
         (void) vfprintf(fp, fmt, ap);          (void) vfprintf(fp, fmt, ap);
           va_end(ap);
         (void) fprintf(fp, "\n");          (void) fprintf(fp, "\n");
         (void) fflush(fp);          (void) fflush(fp);
   
         if (!iamremote) {          if (!iamremote) {
                   va_start(ap, fmt);
                 vfprintf(stderr, fmt, ap);                  vfprintf(stderr, fmt, ap);
                   va_end(ap);
                 fprintf(stderr, "\n");                  fprintf(stderr, "\n");
         }          }
         va_end(ap);  
 }  }
   
 char *  
 colon(cp)  
         char *cp;  
 {  
         int flag = 0;  
   
         if (*cp == ':')         /* Leading colon is part of file name. */  
                 return (0);  
         if (*cp == '[')  
                 flag = 1;  
   
         for (; *cp; ++cp) {  
                 if (*cp == '@' && *(cp+1) == '[')  
                         flag = 1;  
                 if (*cp == ']' && *(cp+1) == ':' && flag)  
                         return (cp+1);  
                 if (*cp == ':' && !flag)  
                         return (cp);  
                 if (*cp == '/')  
                         return (0);  
         }  
         return (0);  
 }  
   
 void  void
 verifydir(cp)  verifydir(cp)
         char *cp;          char *cp;
Line 1019 
Line 981 
   
         cp = cp0;          cp = cp0;
         do {          do {
                 c = *cp;                  c = (int)*cp;
                 if (c & 0200)                  if (c & 0200)
                         goto bad;                          goto bad;
                 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-' && c != '.')                  if (!isalpha(c) && !isdigit(c) &&
                       c != '_' && c != '-' && c != '.' && c != '+')
                         goto bad;                          goto bad;
         } while (*++cp);          } while (*++cp);
         return (1);          return (1);
Line 1054 
Line 1017 
                 bp->buf = xmalloc(size);                  bp->buf = xmalloc(size);
         else          else
                 bp->buf = xrealloc(bp->buf, size);                  bp->buf = xrealloc(bp->buf, size);
           memset(bp->buf, 0, size);
         bp->cnt = size;          bp->cnt = size;
         return (bp);          return (bp);
 }  }
Line 1063 
Line 1027 
         int signo;          int signo;
 {  {
         if (!iamremote)          if (!iamremote)
                 fprintf(stderr, "lost connection\n");                  write(STDERR_FILENO, "lost connection\n", 16);
         exit(1);          if (signo)
                   _exit(1);
           else
                   exit(1);
 }  }
   
   static void
 void  
 alarmtimer(int wait)  
 {  
         struct itimerval itv;  
   
         itv.it_value.tv_sec = wait;  
         itv.it_value.tv_usec = 0;  
         itv.it_interval = itv.it_value;  
         setitimer(ITIMER_REAL, &itv, NULL);  
 }  
   
 void  
 updateprogressmeter(int ignore)  updateprogressmeter(int ignore)
 {  {
         int save_errno = errno;          int save_errno = errno;
   
         progressmeter(0);          progressmeter(0);
           signal(SIGALRM, updateprogressmeter);
           alarm(PROGRESSTIME);
         errno = save_errno;          errno = save_errno;
 }  }
   
 int  static int
 foregroundproc()  foregroundproc(void)
 {  {
         static pid_t pgrp = -1;          static pid_t pgrp = -1;
         int ctty_pgrp;          int ctty_pgrp;
Line 1137 
Line 1094 
                 i = barlength * ratio / 100;                  i = barlength * ratio / 100;
                 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),                  snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
                     "|%.*s%*s|", i,                      "|%.*s%*s|", i,
                     "*****************************************************************************"                      "***************************************"
                     "*****************************************************************************",                      "***************************************"
                       "***************************************"
                       "***************************************",
                     barlength - i, "");                      barlength - i, "");
         }          }
         i = 0;          i = 0;
Line 1147 
Line 1106 
                 i++;                  i++;
                 abbrevsize >>= 10;                  abbrevsize >>= 10;
         }          }
         snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5qd %c%c ",          snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5llu %c%c ",
              (quad_t) abbrevsize, prefixes[i], prefixes[i] == ' ' ? ' ' :              (unsigned long long) abbrevsize, prefixes[i],
                  'B');              prefixes[i] == ' ' ? ' ' : 'B');
   
         timersub(&now, &lastupdate, &wait);          timersub(&now, &lastupdate, &wait);
         if (cursize > lastsize) {          if (cursize > lastsize) {
Line 1164 
Line 1123 
         timersub(&now, &start, &td);          timersub(&now, &start, &td);
         elapsed = td.tv_sec + (td.tv_usec / 1000000.0);          elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
   
         if (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes) {          if (flag != 1 &&
               (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
                 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),                  snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
                          "   --:-- ETA");                      "   --:-- ETA");
         } else if (wait.tv_sec >= STALLTIME) {          } else if (wait.tv_sec >= STALLTIME) {
                 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),                  snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
                          " - stalled -");                      " - stalled -");
         } else {          } else {
                 if (flag != 1)                  if (flag != 1)
                         remaining =                          remaining = (int)(totalbytes / (statbytes / elapsed) -
                             (int)(totalbytes / (statbytes / elapsed) - elapsed);                              elapsed);
                 else                  else
                         remaining = elapsed;                          remaining = elapsed;
   
Line 1193 
Line 1153 
   
         if (flag == -1) {          if (flag == -1) {
                 signal(SIGALRM, updateprogressmeter);                  signal(SIGALRM, updateprogressmeter);
                 alarmtimer(1);                  alarm(PROGRESSTIME);
         } else if (flag == 1) {          } else if (flag == 1) {
                 alarmtimer(0);                  alarm(0);
                 atomicio(write, fileno(stdout), "\n", 1);                  atomicio(write, fileno(stdout), "\n", 1);
                 statbytes = 0;                  statbytes = 0;
         }          }
Line 1210 
Line 1170 
                 return (winsize.ws_col ? winsize.ws_col : 80);                  return (winsize.ws_col ? winsize.ws_col : 80);
         else          else
                 return (80);                  return (80);
 }  
   
 void  
 addargs(char *fmt, ...)  
 {  
         va_list ap;  
         char buf[1024];  
   
         va_start(ap, fmt);  
         vsnprintf(buf, sizeof(buf), fmt, ap);  
         va_end(ap);  
   
         if (args.list == NULL) {  
                 args.nalloc = 32;  
                 args.num = 0;  
                 args.list = xmalloc(args.nalloc * sizeof(char *));  
         } else if (args.num+2 >= args.nalloc) {  
                 args.nalloc *= 2;  
                 args.list = xrealloc(args.list, args.nalloc * sizeof(char *));  
         }  
         args.list[args.num++] = xstrdup(buf);  
         args.list[args.num] = NULL;  
 }  }

Legend:
Removed from v.1.43  
changed lines
  Added in v.1.43.2.5