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.44 2000/12/11 17:27:33 deraadt Exp $");
87 #ifdef HAVE___PROGNAME
88 extern char *__progname;
93 /* For progressmeter() -- number of seconds before xfer considered "stalled" */
96 /* Progress meter bar */
98 "************************************************************"\
99 "************************************************************"\
100 "************************************************************"\
101 "************************************************************"
102 #define MAX_BARLENGTH (sizeof(BAR) - 1)
104 /* Visual statistics about files as they are transferred. */
105 void progressmeter(int);
107 /* Returns width of the terminal (for progress meter calculations). */
108 int getttywidth(void);
109 int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc);
111 /* setup arguments for the call to ssh */
112 void addargs(char *fmt, ...) __attribute__((format(printf, 1, 2)));
114 /* Time a transfer started. */
115 static struct timeval start;
117 /* Number of bytes of current file transferred so far. */
118 volatile unsigned long statbytes;
120 /* Total size of current file. */
121 off_t totalbytes = 0;
123 /* Name of current file being transferred. */
126 /* This is set to non-zero to enable verbose mode. */
127 int verbose_mode = 0;
129 /* This is set to non-zero if compression is desired. */
130 int compress_flag = 0;
132 /* This is set to zero if the progressmeter is not desired. */
133 int showprogress = 1;
135 /* This is the program to execute for the secured connection. ("ssh" or -S) */
136 char *ssh_program = SSH_PROGRAM;
138 /* This is the list of arguments that scp passes to ssh */
146 * This function executes the given command as the specified user on the
147 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
148 * assigns the input and output file descriptors on success.
152 do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
154 int pin[2], pout[2], reserved[2];
157 fprintf(stderr, "Executing: program %s host %s, user %s, command %s\n",
158 ssh_program, host, remuser ? remuser : "(unspecified)", cmd);
161 * Reserve two descriptors so that the real pipes won't get
162 * descriptors 0 and 1 because that will screw up dup2 below.
166 /* Create a socket pair for communicating with ssh. */
168 fatal("pipe: %s", strerror(errno));
170 fatal("pipe: %s", strerror(errno));
172 /* Free the reserved descriptors. */
176 /* For a child to execute the command on the remote host using ssh. */
186 args.list[0] = ssh_program;
188 addargs("-l%s", remuser);
192 execvp(ssh_program, args.list);
196 /* Parent. Close the other side, and return the local side. */
205 fatal(const char *fmt,...)
211 vsnprintf(buf, sizeof(buf), fmt, ap);
213 fprintf(stderr, "%s\n", buf);
222 extern int iamremote;
224 BUF *allocbuf(BUF *, int, int);
229 void run_err(const char *,...);
230 void verifydir(char *);
234 int errs, remin, remout;
235 int pflag, iamremote, iamrecursive, targetshouldbedirectory;
238 char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
241 void rsource(char *, struct stat *);
242 void sink(int, char *[]);
243 void source(int, char *[]);
244 void tolocal(int, char *[]);
245 void toremote(char *, int, char *[]);
253 int ch, fflag, tflag;
258 __progname = get_progname(argv[0]);
261 addargs("ssh"); /* overwritten with ssh_program */
263 addargs("-oFallBackToRsh no");
266 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:")) != EOF)
268 /* User-visible flags. */
277 addargs("-%c%s", ch, optarg);
280 addargs("-p%s", optarg);
283 addargs("-oBatchmode yes");
292 ssh_program = xstrdup(optarg);
301 /* Server options. */
303 targetshouldbedirectory = 1;
305 case 'f': /* "from" */
320 if ((pwd = getpwuid(userid = getuid())) == NULL)
321 fatal("unknown user %d", (int) userid);
323 if (!isatty(STDERR_FILENO))
326 remin = STDIN_FILENO;
327 remout = STDOUT_FILENO;
330 /* Follow "protocol", send data. */
343 targetshouldbedirectory = 1;
346 /* Command to be executed on remote system using "ssh". */
347 (void) sprintf(cmd, "scp%s%s%s%s", verbose_mode ? " -v" : "",
348 iamrecursive ? " -r" : "", pflag ? " -p" : "",
349 targetshouldbedirectory ? " -d" : "");
351 (void) signal(SIGPIPE, lostconn);
353 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
354 toremote(targ, argc, argv);
356 tolocal(argc, argv); /* Dest is local host. */
357 if (targetshouldbedirectory)
358 verifydir(argv[argc - 1]);
367 if (*host == '[' && host[strlen(host) - 1] == ']') {
368 host[strlen(host) - 1] = '\0';
375 toremote(targ, argc, argv)
380 char *bp, *host, *src, *suser, *thost, *tuser;
386 if ((thost = strchr(argv[argc - 1], '@'))) {
389 tuser = argv[argc - 1];
392 else if (!okname(tuser))
395 thost = argv[argc - 1];
399 for (i = 0; i < argc - 1; i++) {
400 src = colon(argv[i]);
401 if (src) { /* remote to remote */
405 host = strchr(argv[i], '@');
406 len = strlen(ssh_program) + strlen(argv[i]) +
407 strlen(src) + (tuser ? strlen(tuser) : 0) +
408 strlen(thost) + strlen(targ) + CMDNEEDS + 32;
412 host = cleanhostname(host);
415 suser = pwd->pw_name;
416 else if (!okname(suser))
419 "%s%s -x -o'FallBackToRsh no' -n -l %s %s %s %s '%s%s%s:%s'",
420 ssh_program, verbose_mode ? " -v" : "",
421 suser, host, cmd, src,
422 tuser ? tuser : "", tuser ? "@" : "",
425 host = cleanhostname(argv[i]);
427 "exec %s%s -x -o'FallBackToRsh no' -n %s %s %s '%s%s%s:%s'",
428 ssh_program, verbose_mode ? " -v" : "",
430 tuser ? tuser : "", tuser ? "@" : "",
434 fprintf(stderr, "Executing: %s\n", bp);
437 } else { /* local to remote */
439 len = strlen(targ) + CMDNEEDS + 20;
441 (void) sprintf(bp, "%s -t %s", cmd, targ);
442 host = cleanhostname(thost);
443 if (do_cmd(host, tuser, bp, &remin,
461 char *bp, *host, *src, *suser;
463 for (i = 0; i < argc - 1; i++) {
464 if (!(src = colon(argv[i]))) { /* Local to local. */
465 len = strlen(_PATH_CP) + strlen(argv[i]) +
466 strlen(argv[argc - 1]) + 20;
468 (void) sprintf(bp, "exec %s%s%s %s %s", _PATH_CP,
469 iamrecursive ? " -r" : "", pflag ? " -p" : "",
470 argv[i], argv[argc - 1]);
472 fprintf(stderr, "Executing: %s\n", bp);
481 if ((host = strchr(argv[i], '@')) == NULL) {
488 suser = pwd->pw_name;
489 else if (!okname(suser))
492 host = cleanhostname(host);
493 len = strlen(src) + CMDNEEDS + 20;
495 (void) sprintf(bp, "%s -f %s", cmd, src);
496 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
502 sink(1, argv + argc - 1);
517 int amt, fd, haderr, indx, result;
518 char *last, *name, buf[2048];
520 for (indx = 0; indx < argc; ++indx) {
523 if ((fd = open(name, O_RDONLY, 0)) < 0)
525 if (fstat(fd, &stb) < 0) {
526 syserr: run_err("%s: %s", name, strerror(errno));
529 switch (stb.st_mode & S_IFMT) {
539 run_err("%s: not a regular file", name);
542 if ((last = strrchr(name, '/')) == NULL)
549 * Make it compatible with possible future
550 * versions expecting microseconds.
552 (void) sprintf(buf, "T%lu 0 %lu 0\n",
553 (unsigned long) stb.st_mtime,
554 (unsigned long) stb.st_atime);
555 (void) atomicio(write, remout, buf, strlen(buf));
559 #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
560 sprintf(buf, "C%04o %lu %s\n",
561 (unsigned int) (stb.st_mode & FILEMODEMASK),
562 (unsigned long) stb.st_size, last);
564 fprintf(stderr, "Sending file modes: %s", buf);
567 (void) atomicio(write, remout, buf, strlen(buf));
570 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
571 next: (void) close(fd);
575 totalbytes = stb.st_size;
578 /* Keep writing after an error so that we stay sync'd up. */
579 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
581 if (i + amt > stb.st_size)
582 amt = stb.st_size - i;
584 result = atomicio(read, fd, bp->buf, amt);
586 haderr = result >= 0 ? EIO : errno;
589 (void) atomicio(write, remout, bp->buf, amt);
591 result = atomicio(write, remout, bp->buf, amt);
593 haderr = result >= 0 ? EIO : errno;
600 if (close(fd) < 0 && !haderr)
603 (void) atomicio(write, remout, "", 1);
605 run_err("%s: %s", name, strerror(haderr));
617 char *last, *vect[1], path[1100];
619 if (!(dirp = opendir(name))) {
620 run_err("%s: %s", name, strerror(errno));
623 last = strrchr(name, '/');
629 (void) sprintf(path, "T%lu 0 %lu 0\n",
630 (unsigned long) statp->st_mtime,
631 (unsigned long) statp->st_atime);
632 (void) atomicio(write, remout, path, strlen(path));
633 if (response() < 0) {
638 (void) sprintf(path, "D%04o %d %.1024s\n",
639 (unsigned int) (statp->st_mode & FILEMODEMASK), 0, last);
641 fprintf(stderr, "Entering directory: %s", path);
642 (void) atomicio(write, remout, path, strlen(path));
643 if (response() < 0) {
647 while ((dp = readdir(dirp))) {
650 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
652 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
653 run_err("%s/%s: name too long", name, dp->d_name);
656 (void) sprintf(path, "%s/%s", name, dp->d_name);
660 (void) closedir(dirp);
661 (void) atomicio(write, remout, "E\n", 2);
677 int amt, count, exists, first, mask, mode, ofd, omode;
679 int setimes, targisdir, wrerrno = 0;
680 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
682 struct timeval tv[2];
684 #define SCREWUP(str) { why = str; goto screwup; }
686 setimes = targisdir = 0;
691 run_err("ambiguous target");
695 if (targetshouldbedirectory)
698 (void) atomicio(write, remout, "", 1);
699 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
701 for (first = 1;; first = 0) {
703 if (atomicio(read, remin, cp, 1) <= 0)
706 SCREWUP("unexpected <newline>");
708 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
709 SCREWUP("lost connection");
711 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
714 if (buf[0] == '\01' || buf[0] == '\02') {
716 (void) atomicio(write, STDERR_FILENO,
717 buf + 1, strlen(buf + 1));
724 (void) atomicio(write, remout, "", 1);
730 #define getnum(t) (t) = 0; \
731 while (*cp >= '0' && *cp <= '9') (t) = (t) * 10 + (*cp++ - '0');
736 getnum(tv[1].tv_sec);
738 SCREWUP("mtime.sec not delimited");
742 SCREWUP("mtime.usec not delimited");
743 getnum(tv[0].tv_sec);
745 SCREWUP("atime.sec not delimited");
749 SCREWUP("atime.usec not delimited");
750 (void) atomicio(write, remout, "", 1);
753 if (*cp != 'C' && *cp != 'D') {
755 * Check for the case "rcp remote:foo\* local:bar".
756 * In this case, the line "No match." can be returned
757 * by the shell before the rcp command on the remote is
758 * executed so the ^Aerror_message convention isn't
765 SCREWUP("expected control record");
768 for (++cp; cp < buf + 5; cp++) {
769 if (*cp < '0' || *cp > '7')
771 mode = (mode << 3) | (*cp - '0');
774 SCREWUP("mode not delimited");
776 for (size = 0; *cp >= '0' && *cp <= '9';)
777 size = size * 10 + (*cp++ - '0');
779 SCREWUP("size not delimited");
781 static char *namebuf;
785 need = strlen(targ) + strlen(cp) + 250;
787 namebuf = xmalloc(need);
788 (void) sprintf(namebuf, "%s%s%s", targ,
789 *targ ? "/" : "", cp);
794 exists = stat(np, &stb) == 0;
796 int mod_flag = pflag;
798 if (!S_ISDIR(stb.st_mode)) {
803 (void) chmod(np, mode);
805 /* Handle copying from a read-only
808 if (mkdir(np, mode | S_IRWXU) < 0)
815 if (utimes(np, tv) < 0)
816 run_err("%s: set times: %s",
817 np, strerror(errno));
820 (void) chmod(np, mode);
825 if ((ofd = open(np, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
826 bad: run_err("%s: %s", np, strerror(errno));
829 (void) atomicio(write, remout, "", 1);
830 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
842 for (count = i = 0; i < size; i += 4096) {
848 j = read(remin, cp, amt);
849 if (j == -1 && (errno == EINTR || errno == EAGAIN)) {
852 run_err("%s", j ? strerror(errno) :
853 "dropped connection");
860 if (count == bp->cnt) {
861 /* Keep reading so we stay sync'd up. */
863 j = atomicio(write, ofd, bp->buf, count);
866 wrerrno = j >= 0 ? EIO : errno;
875 if (count != 0 && wrerr == NO &&
876 (j = atomicio(write, ofd, bp->buf, count)) != count) {
878 wrerrno = j >= 0 ? EIO : errno;
881 if (ftruncate(ofd, size)) {
882 run_err("%s: truncate: %s", np, strerror(errno));
887 if (exists || omode != mode)
889 if (fchmod(ofd, omode))
890 #else /* HAVE_FCHMOD */
891 if (chmod(np, omode))
892 #endif /* HAVE_FCHMOD */
893 run_err("%s: set mode: %s",
894 np, strerror(errno));
896 if (!exists && omode != mode)
898 if (fchmod(ofd, omode & ~mask))
899 #else /* HAVE_FCHMOD */
900 if (chmod(np, omode & ~mask))
901 #endif /* HAVE_FCHMOD */
902 run_err("%s: set mode: %s",
903 np, strerror(errno));
905 if (close(ofd) == -1) {
910 if (setimes && wrerr == NO) {
912 if (utimes(np, tv) < 0) {
913 run_err("%s: set times: %s",
914 np, strerror(errno));
920 run_err("%s: %s", np, strerror(wrerrno));
923 (void) atomicio(write, remout, "", 1);
930 run_err("protocol error: %s", why);
937 char ch, *cp, resp, rbuf[2048];
939 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
949 case 1: /* error, followed by error msg */
950 case 2: /* fatal error, "" */
952 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
955 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
958 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
970 (void) fprintf(stderr, "usage: scp "
971 "[-pqrvC46] [-S ssh] [-P port] [-c cipher] [-i identity] f1 f2; or:\n"
972 " scp [options] f1 ... fn directory\n");
977 run_err(const char *fmt,...)
983 if (fp == NULL && !(fp = fdopen(remout, "w")))
985 (void) fprintf(fp, "%c", 0x01);
986 (void) fprintf(fp, "scp: ");
988 (void) vfprintf(fp, fmt, ap);
990 (void) fprintf(fp, "\n");
995 vfprintf(stderr, fmt, ap);
997 fprintf(stderr, "\n");
1007 if (*cp == ':') /* Leading colon is part of file name. */
1013 if (*cp == '@' && *(cp+1) == '[')
1015 if (*cp == ']' && *(cp+1) == ':' && flag)
1017 if (*cp == ':' && !flag)
1031 if (!stat(cp, &stb)) {
1032 if (S_ISDIR(stb.st_mode))
1036 run_err("%s: %s", cp, strerror(errno));
1052 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
1057 bad: fprintf(stderr, "%s: invalid user name\n", cp0);
1062 allocbuf(bp, fd, blksize)
1067 #ifdef HAVE_ST_BLKSIZE
1070 if (fstat(fd, &stb) < 0) {
1071 run_err("fstat: %s", strerror(errno));
1074 if (stb.st_blksize == 0)
1077 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
1079 #else /* HAVE_ST_BLKSIZE */
1081 #endif /* HAVE_ST_BLKSIZE */
1082 if (bp->cnt >= size)
1084 if (bp->buf == NULL)
1085 bp->buf = xmalloc(size);
1087 bp->buf = xrealloc(bp->buf, size);
1097 fprintf(stderr, "lost connection\n");
1103 alarmtimer(int wait)
1105 struct itimerval itv;
1107 itv.it_value.tv_sec = wait;
1108 itv.it_value.tv_usec = 0;
1109 itv.it_interval = itv.it_value;
1110 setitimer(ITIMER_REAL, &itv, NULL);
1114 updateprogressmeter(int ignore)
1116 int save_errno = errno;
1125 static pid_t pgrp = -1;
1133 * Cygwin only supports tcgetpgrp() for getting the controlling tty
1136 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1139 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1140 ctty_pgrp == pgrp));
1145 progressmeter(int flag)
1147 static const char prefixes[] = " KMGTP";
1148 static struct timeval lastupdate;
1149 static off_t lastsize;
1150 struct timeval now, td, wait;
1151 off_t cursize, abbrevsize;
1153 int ratio, barlength, i, remaining;
1157 (void) gettimeofday(&start, (struct timezone *) 0);
1161 if (foregroundproc() == 0)
1164 (void) gettimeofday(&now, (struct timezone *) 0);
1165 cursize = statbytes;
1166 if (totalbytes != 0) {
1167 ratio = 100.0 * cursize / totalbytes;
1168 ratio = MAX(ratio, 0);
1169 ratio = MIN(ratio, 100);
1173 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
1175 barlength = getttywidth() - 51;
1176 barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH;
1177 if (barlength > 0) {
1178 i = barlength * ratio / 100;
1179 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1180 "|%.*s%*s|", i, BAR, barlength - i, "");
1183 abbrevsize = cursize;
1184 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1188 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5d %c%c ",
1189 (int) abbrevsize, prefixes[i], prefixes[i] == ' ' ? ' ' : 'B');
1191 timersub(&now, &lastupdate, &wait);
1192 if (cursize > lastsize) {
1195 if (wait.tv_sec >= STALLTIME) {
1196 start.tv_sec += wait.tv_sec;
1197 start.tv_usec += wait.tv_usec;
1201 timersub(&now, &start, &td);
1202 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1205 (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
1206 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1208 } else if (wait.tv_sec >= STALLTIME) {
1209 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1213 remaining = (int)(totalbytes / (statbytes / elapsed) -
1216 remaining = elapsed;
1218 i = remaining / 3600;
1220 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1223 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1225 i = remaining % 3600;
1226 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1227 "%02d:%02d%s", i / 60, i % 60,
1228 (flag != 1) ? " ETA" : " ");
1230 atomicio(write, fileno(stdout), buf, strlen(buf));
1233 struct sigaction sa;
1234 sa.sa_handler = updateprogressmeter;
1235 sigemptyset((sigset_t *)&sa.sa_mask);
1237 sa.sa_flags = SA_RESTART;
1239 sigaction(SIGALRM, &sa, NULL);
1241 } else if (flag == 1) {
1243 atomicio(write, fileno(stdout), "\n", 1);
1251 struct winsize winsize;
1253 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
1254 return (winsize.ws_col ? winsize.ws_col : 80);
1260 addargs(char *fmt, ...)
1266 vsnprintf(buf, sizeof(buf), fmt, ap);
1269 if (args.list == NULL) {
1272 args.list = xmalloc(args.nalloc * sizeof(char *));
1273 } else if (args.num+2 >= args.nalloc) {
1275 args.list = xrealloc(args.list, args.nalloc * sizeof(char *));
1277 args.list[args.num++] = xstrdup(buf);
1278 args.list[args.num] = NULL;