2 * scp - secure remote copy. This is basically patched BSD rcp which
3 * uses ssh to do the data transfer (instead of using rcmd).
5 * NOTE: This version should NOT be suid root. (This uses ssh to
6 * do the transfer and ssh has the necessary privileges.)
8 * 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi>
10 * As far as I am concerned, the code I have written for this software
11 * can be used freely for any purpose. Any derived versions of this
12 * software must be clearly marked as such, and if the derived work is
13 * incompatible with the protocol description in the RFC file, it must be
14 * called by a name other than "ssh" or "Secure Shell".
17 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
18 * Copyright (c) 1999 Aaron Campbell. All rights reserved.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
30 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
31 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
33 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
34 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
38 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 * Copyright (c) 1983, 1990, 1992, 1993, 1995
45 * The Regents of the University of California. All rights reserved.
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
55 * 3. All advertising materials mentioning features or use of this software
56 * must display the following acknowledgement:
57 * This product includes software developed by the University of
58 * California, Berkeley and its contributors.
59 * 4. Neither the name of the University nor the names of its contributors
60 * may be used to endorse or promote products derived from this software
61 * without specific prior written permission.
63 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
78 RCSID("$OpenBSD: scp.c,v 1.61 2001/03/15 15:05:59 markus Exp $");
82 #include "pathnames.h"
86 #ifdef HAVE___PROGNAME
87 extern char *__progname;
92 /* For progressmeter() -- number of seconds before xfer considered "stalled" */
95 /* Progress meter bar */
97 "************************************************************"\
98 "************************************************************"\
99 "************************************************************"\
100 "************************************************************"
101 #define MAX_BARLENGTH (sizeof(BAR) - 1)
103 /* Visual statistics about files as they are transferred. */
104 void progressmeter(int);
106 /* Returns width of the terminal (for progress meter calculations). */
107 int getttywidth(void);
108 int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc);
110 /* setup arguments for the call to ssh */
111 void addargs(char *fmt, ...) __attribute__((format(printf, 1, 2)));
113 /* Time a transfer started. */
114 static struct timeval start;
116 /* Number of bytes of current file transferred so far. */
117 volatile u_long statbytes;
119 /* Total size of current file. */
120 off_t totalbytes = 0;
122 /* Name of current file being transferred. */
125 /* This is set to non-zero to enable verbose mode. */
126 int verbose_mode = 0;
128 /* This is set to zero if the progressmeter is not desired. */
129 int showprogress = 1;
131 /* This is the program to execute for the secured connection. ("ssh" or -S) */
132 char *ssh_program = _PATH_SSH_PROGRAM;
134 /* This is the list of arguments that scp passes to ssh */
142 * This function executes the given command as the specified user on the
143 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
144 * assigns the input and output file descriptors on success.
148 do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
150 int pin[2], pout[2], reserved[2];
153 fprintf(stderr, "Executing: program %s host %s, user %s, command %s\n",
154 ssh_program, host, remuser ? remuser : "(unspecified)", cmd);
157 * Reserve two descriptors so that the real pipes won't get
158 * descriptors 0 and 1 because that will screw up dup2 below.
162 /* Create a socket pair for communicating with ssh. */
164 fatal("pipe: %s", strerror(errno));
166 fatal("pipe: %s", strerror(errno));
168 /* Free the reserved descriptors. */
172 /* For a child to execute the command on the remote host using ssh. */
182 args.list[0] = ssh_program;
184 addargs("-l%s", remuser);
188 execvp(ssh_program, args.list);
192 /* Parent. Close the other side, and return the local side. */
205 extern int iamremote;
207 BUF *allocbuf(BUF *, int, int);
212 void run_err(const char *,...);
213 void verifydir(char *);
217 int errs, remin, remout;
218 int pflag, iamremote, iamrecursive, targetshouldbedirectory;
221 char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
223 int main(int, char *[]);
225 void rsource(char *, struct stat *);
226 void sink(int, char *[]);
227 void source(int, char *[]);
228 void tolocal(int, char *[]);
229 char *cleanhostname(char *);
230 void toremote(char *, int, char *[]);
238 int ch, fflag, tflag;
243 __progname = get_progname(argv[0]);
246 addargs("ssh"); /* overwritten with ssh_program */
248 addargs("-oFallBackToRsh no");
251 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:")) != -1)
253 /* User-visible flags. */
262 addargs("-%c%s", ch, optarg);
265 addargs("-p%s", optarg);
268 addargs("-oBatchmode yes");
277 ssh_program = xstrdup(optarg);
286 /* Server options. */
288 targetshouldbedirectory = 1;
290 case 'f': /* "from" */
305 if ((pwd = getpwuid(userid = getuid())) == NULL)
306 fatal("unknown user %d", (int) userid);
308 if (!isatty(STDERR_FILENO))
311 remin = STDIN_FILENO;
312 remout = STDOUT_FILENO;
315 /* Follow "protocol", send data. */
328 targetshouldbedirectory = 1;
331 /* Command to be executed on remote system using "ssh". */
332 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
333 verbose_mode ? " -v" : "",
334 iamrecursive ? " -r" : "", pflag ? " -p" : "",
335 targetshouldbedirectory ? " -d" : "");
337 (void) signal(SIGPIPE, lostconn);
339 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
340 toremote(targ, argc, argv);
342 tolocal(argc, argv); /* Dest is local host. */
343 if (targetshouldbedirectory)
344 verifydir(argv[argc - 1]);
353 if (*host == '[' && host[strlen(host) - 1] == ']') {
354 host[strlen(host) - 1] = '\0';
361 toremote(targ, argc, argv)
366 char *bp, *host, *src, *suser, *thost, *tuser;
372 if ((thost = strchr(argv[argc - 1], '@'))) {
375 tuser = argv[argc - 1];
378 else if (!okname(tuser))
381 thost = argv[argc - 1];
385 for (i = 0; i < argc - 1; i++) {
386 src = colon(argv[i]);
387 if (src) { /* remote to remote */
391 host = strchr(argv[i], '@');
392 len = strlen(ssh_program) + strlen(argv[i]) +
393 strlen(src) + (tuser ? strlen(tuser) : 0) +
394 strlen(thost) + strlen(targ) + CMDNEEDS + 32;
398 host = cleanhostname(host);
401 suser = pwd->pw_name;
402 else if (!okname(suser))
405 "%s%s -x -o'FallBackToRsh no' -n "
406 "-l %s %s %s %s '%s%s%s:%s'",
407 ssh_program, verbose_mode ? " -v" : "",
408 suser, host, cmd, src,
409 tuser ? tuser : "", tuser ? "@" : "",
412 host = cleanhostname(argv[i]);
414 "exec %s%s -x -o'FallBackToRsh no' -n %s "
416 ssh_program, verbose_mode ? " -v" : "",
418 tuser ? tuser : "", tuser ? "@" : "",
422 fprintf(stderr, "Executing: %s\n", bp);
425 } else { /* local to remote */
427 len = strlen(targ) + CMDNEEDS + 20;
429 (void) snprintf(bp, len, "%s -t %s", cmd, targ);
430 host = cleanhostname(thost);
431 if (do_cmd(host, tuser, bp, &remin,
449 char *bp, *host, *src, *suser;
451 for (i = 0; i < argc - 1; i++) {
452 if (!(src = colon(argv[i]))) { /* Local to local. */
453 len = strlen(_PATH_CP) + strlen(argv[i]) +
454 strlen(argv[argc - 1]) + 20;
456 (void) snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
457 iamrecursive ? " -r" : "", pflag ? " -p" : "",
458 argv[i], argv[argc - 1]);
460 fprintf(stderr, "Executing: %s\n", bp);
469 if ((host = strchr(argv[i], '@')) == NULL) {
476 suser = pwd->pw_name;
477 else if (!okname(suser))
480 host = cleanhostname(host);
481 len = strlen(src) + CMDNEEDS + 20;
483 (void) snprintf(bp, len, "%s -f %s", cmd, src);
484 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
490 sink(1, argv + argc - 1);
505 int amt, fd, haderr, indx, result;
506 char *last, *name, buf[2048];
508 for (indx = 0; indx < argc; ++indx) {
511 if ((fd = open(name, O_RDONLY, 0)) < 0)
513 if (fstat(fd, &stb) < 0) {
514 syserr: run_err("%s: %s", name, strerror(errno));
517 switch (stb.st_mode & S_IFMT) {
527 run_err("%s: not a regular file", name);
530 if ((last = strrchr(name, '/')) == NULL)
537 * Make it compatible with possible future
538 * versions expecting microseconds.
540 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
541 (u_long) stb.st_mtime,
542 (u_long) stb.st_atime);
543 (void) atomicio(write, remout, buf, strlen(buf));
547 #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
548 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
549 (u_int) (stb.st_mode & FILEMODEMASK),
552 fprintf(stderr, "Sending file modes: %s", buf);
555 (void) atomicio(write, remout, buf, strlen(buf));
558 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
559 next: (void) close(fd);
563 totalbytes = stb.st_size;
566 /* Keep writing after an error so that we stay sync'd up. */
567 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
569 if (i + amt > stb.st_size)
570 amt = stb.st_size - i;
572 result = atomicio(read, fd, bp->buf, amt);
574 haderr = result >= 0 ? EIO : errno;
577 (void) atomicio(write, remout, bp->buf, amt);
579 result = atomicio(write, remout, bp->buf, amt);
581 haderr = result >= 0 ? EIO : errno;
588 if (close(fd) < 0 && !haderr)
591 (void) atomicio(write, remout, "", 1);
593 run_err("%s: %s", name, strerror(haderr));
605 char *last, *vect[1], path[1100];
607 if (!(dirp = opendir(name))) {
608 run_err("%s: %s", name, strerror(errno));
611 last = strrchr(name, '/');
617 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
618 (u_long) statp->st_mtime,
619 (u_long) statp->st_atime);
620 (void) atomicio(write, remout, path, strlen(path));
621 if (response() < 0) {
626 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
627 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
629 fprintf(stderr, "Entering directory: %s", path);
630 (void) atomicio(write, remout, path, strlen(path));
631 if (response() < 0) {
635 while ((dp = readdir(dirp))) {
638 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
640 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
641 run_err("%s/%s: name too long", name, dp->d_name);
644 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
648 (void) closedir(dirp);
649 (void) atomicio(write, remout, "E\n", 2);
665 int amt, count, exists, first, mask, mode, ofd, omode;
667 int setimes, targisdir, wrerrno = 0;
668 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
670 struct timeval tv[2];
672 #define SCREWUP(str) { why = str; goto screwup; }
674 setimes = targisdir = 0;
679 run_err("ambiguous target");
683 if (targetshouldbedirectory)
686 (void) atomicio(write, remout, "", 1);
687 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
689 for (first = 1;; first = 0) {
691 if (atomicio(read, remin, cp, 1) <= 0)
694 SCREWUP("unexpected <newline>");
696 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
697 SCREWUP("lost connection");
699 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
702 if (buf[0] == '\01' || buf[0] == '\02') {
704 (void) atomicio(write, STDERR_FILENO,
705 buf + 1, strlen(buf + 1));
712 (void) atomicio(write, remout, "", 1);
718 #define getnum(t) (t) = 0; \
719 while (*cp >= '0' && *cp <= '9') (t) = (t) * 10 + (*cp++ - '0');
724 getnum(tv[1].tv_sec);
726 SCREWUP("mtime.sec not delimited");
730 SCREWUP("mtime.usec not delimited");
731 getnum(tv[0].tv_sec);
733 SCREWUP("atime.sec not delimited");
737 SCREWUP("atime.usec not delimited");
738 (void) atomicio(write, remout, "", 1);
741 if (*cp != 'C' && *cp != 'D') {
743 * Check for the case "rcp remote:foo\* local:bar".
744 * In this case, the line "No match." can be returned
745 * by the shell before the rcp command on the remote is
746 * executed so the ^Aerror_message convention isn't
753 SCREWUP("expected control record");
756 for (++cp; cp < buf + 5; cp++) {
757 if (*cp < '0' || *cp > '7')
759 mode = (mode << 3) | (*cp - '0');
762 SCREWUP("mode not delimited");
764 for (size = 0; *cp >= '0' && *cp <= '9';)
765 size = size * 10 + (*cp++ - '0');
767 SCREWUP("size not delimited");
769 static char *namebuf;
773 need = strlen(targ) + strlen(cp) + 250;
774 if (need > cursize) {
777 namebuf = xmalloc(need);
780 (void) snprintf(namebuf, need, "%s%s%s", targ,
781 *targ ? "/" : "", cp);
786 exists = stat(np, &stb) == 0;
788 int mod_flag = pflag;
790 if (!S_ISDIR(stb.st_mode)) {
795 (void) chmod(np, mode);
797 /* Handle copying from a read-only
800 if (mkdir(np, mode | S_IRWXU) < 0)
803 vect[0] = xstrdup(np);
807 if (utimes(vect[0], tv) < 0)
808 run_err("%s: set times: %s",
809 vect[0], strerror(errno));
812 (void) chmod(vect[0], mode);
819 if ((ofd = open(np, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
820 bad: run_err("%s: %s", np, strerror(errno));
823 (void) atomicio(write, remout, "", 1);
824 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
836 for (count = i = 0; i < size; i += 4096) {
842 j = read(remin, cp, amt);
843 if (j == -1 && (errno == EINTR || errno == EAGAIN)) {
846 run_err("%s", j ? strerror(errno) :
847 "dropped connection");
854 if (count == bp->cnt) {
855 /* Keep reading so we stay sync'd up. */
857 j = atomicio(write, ofd, bp->buf, count);
860 wrerrno = j >= 0 ? EIO : errno;
869 if (count != 0 && wrerr == NO &&
870 (j = atomicio(write, ofd, bp->buf, count)) != count) {
872 wrerrno = j >= 0 ? EIO : errno;
875 if (ftruncate(ofd, size)) {
876 run_err("%s: truncate: %s", np, strerror(errno));
881 if (exists || omode != mode)
883 if (fchmod(ofd, omode))
884 #else /* HAVE_FCHMOD */
885 if (chmod(np, omode))
886 #endif /* HAVE_FCHMOD */
887 run_err("%s: set mode: %s",
888 np, strerror(errno));
890 if (!exists && omode != mode)
892 if (fchmod(ofd, omode & ~mask))
893 #else /* HAVE_FCHMOD */
894 if (chmod(np, omode & ~mask))
895 #endif /* HAVE_FCHMOD */
896 run_err("%s: set mode: %s",
897 np, strerror(errno));
899 if (close(ofd) == -1) {
904 if (setimes && wrerr == NO) {
906 if (utimes(np, tv) < 0) {
907 run_err("%s: set times: %s",
908 np, strerror(errno));
914 run_err("%s: %s", np, strerror(wrerrno));
917 (void) atomicio(write, remout, "", 1);
924 run_err("protocol error: %s", why);
931 char ch, *cp, resp, rbuf[2048];
933 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
943 case 1: /* error, followed by error msg */
944 case 2: /* fatal error, "" */
946 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
949 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
952 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
964 (void) fprintf(stderr, "usage: scp "
965 "[-pqrvC46] [-S ssh] [-P port] [-c cipher] [-i identity] f1 f2; or:\n"
966 " scp [options] f1 ... fn directory\n");
971 run_err(const char *fmt,...)
977 if (fp == NULL && !(fp = fdopen(remout, "w")))
979 (void) fprintf(fp, "%c", 0x01);
980 (void) fprintf(fp, "scp: ");
982 (void) vfprintf(fp, fmt, ap);
984 (void) fprintf(fp, "\n");
989 vfprintf(stderr, fmt, ap);
991 fprintf(stderr, "\n");
1001 if (*cp == ':') /* Leading colon is part of file name. */
1007 if (*cp == '@' && *(cp+1) == '[')
1009 if (*cp == ']' && *(cp+1) == ':' && flag)
1011 if (*cp == ':' && !flag)
1025 if (!stat(cp, &stb)) {
1026 if (S_ISDIR(stb.st_mode))
1030 run_err("%s: %s", cp, strerror(errno));
1046 if (!isalpha(c) && !isdigit(c) &&
1047 c != '_' && c != '-' && c != '.' && c != '+')
1052 bad: fprintf(stderr, "%s: invalid user name\n", cp0);
1057 allocbuf(bp, fd, blksize)
1062 #ifdef HAVE_ST_BLKSIZE
1065 if (fstat(fd, &stb) < 0) {
1066 run_err("fstat: %s", strerror(errno));
1069 if (stb.st_blksize == 0)
1072 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
1074 #else /* HAVE_ST_BLKSIZE */
1076 #endif /* HAVE_ST_BLKSIZE */
1077 if (bp->cnt >= size)
1079 if (bp->buf == NULL)
1080 bp->buf = xmalloc(size);
1082 bp->buf = xrealloc(bp->buf, size);
1092 fprintf(stderr, "lost connection\n");
1098 alarmtimer(int wait)
1100 struct itimerval itv;
1102 itv.it_value.tv_sec = wait;
1103 itv.it_value.tv_usec = 0;
1104 itv.it_interval = itv.it_value;
1105 setitimer(ITIMER_REAL, &itv, NULL);
1109 updateprogressmeter(int ignore)
1111 int save_errno = errno;
1118 foregroundproc(void)
1120 static pid_t pgrp = -1;
1126 #ifdef HAVE_TCGETPGRP
1127 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1130 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1131 ctty_pgrp == pgrp));
1136 progressmeter(int flag)
1138 static const char prefixes[] = " KMGTP";
1139 static struct timeval lastupdate;
1140 static off_t lastsize;
1141 struct timeval now, td, wait;
1142 off_t cursize, abbrevsize;
1144 int ratio, barlength, i, remaining;
1148 (void) gettimeofday(&start, (struct timezone *) 0);
1152 if (foregroundproc() == 0)
1155 (void) gettimeofday(&now, (struct timezone *) 0);
1156 cursize = statbytes;
1157 if (totalbytes != 0) {
1158 ratio = 100.0 * cursize / totalbytes;
1159 ratio = MAX(ratio, 0);
1160 ratio = MIN(ratio, 100);
1164 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
1166 barlength = getttywidth() - 51;
1167 barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH;
1168 if (barlength > 0) {
1169 i = barlength * ratio / 100;
1170 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1171 "|%.*s%*s|", i, BAR, barlength - i, "");
1174 abbrevsize = cursize;
1175 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1179 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5lu %c%c ",
1180 (unsigned long) abbrevsize, prefixes[i],
1181 prefixes[i] == ' ' ? ' ' : 'B');
1183 timersub(&now, &lastupdate, &wait);
1184 if (cursize > lastsize) {
1187 if (wait.tv_sec >= STALLTIME) {
1188 start.tv_sec += wait.tv_sec;
1189 start.tv_usec += wait.tv_usec;
1193 timersub(&now, &start, &td);
1194 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1197 (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
1198 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1200 } else if (wait.tv_sec >= STALLTIME) {
1201 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1205 remaining = (int)(totalbytes / (statbytes / elapsed) -
1208 remaining = elapsed;
1210 i = remaining / 3600;
1212 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1215 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1217 i = remaining % 3600;
1218 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1219 "%02d:%02d%s", i / 60, i % 60,
1220 (flag != 1) ? " ETA" : " ");
1222 atomicio(write, fileno(stdout), buf, strlen(buf));
1225 mysignal(SIGALRM, updateprogressmeter);
1227 } else if (flag == 1) {
1229 atomicio(write, fileno(stdout), "\n", 1);
1237 struct winsize winsize;
1239 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
1240 return (winsize.ws_col ? winsize.ws_col : 80);
1246 addargs(char *fmt, ...)
1252 vsnprintf(buf, sizeof(buf), fmt, ap);
1255 if (args.list == NULL) {
1258 args.list = xmalloc(args.nalloc * sizeof(char *));
1259 } else if (args.num+2 >= args.nalloc) {
1261 args.list = xrealloc(args.list, args.nalloc * sizeof(char *));
1263 args.list[args.num++] = xstrdup(buf);
1264 args.list[args.num] = NULL;