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.40 2000/09/21 11:11:42 markus Exp $");
87 /* For progressmeter() -- number of seconds before xfer considered "stalled" */
90 /* Progress meter bar */
92 "************************************************************"\
93 "************************************************************"\
94 "************************************************************"\
95 "************************************************************"
96 #define MAX_BARLENGTH (sizeof(BAR) - 1)
98 /* Visual statistics about files as they are transferred. */
99 void progressmeter(int);
101 /* Returns width of the terminal (for progress meter calculations). */
102 int getttywidth(void);
103 int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc);
105 /* Time a transfer started. */
106 static struct timeval start;
108 /* Number of bytes of current file transferred so far. */
109 volatile unsigned long statbytes;
111 /* Total size of current file. */
112 off_t totalbytes = 0;
114 /* Name of current file being transferred. */
117 /* This is set to non-zero if IPv4 is desired. */
120 /* This is set to non-zero if IPv6 is desired. */
123 /* This is set to non-zero to enable verbose mode. */
124 int verbose_mode = 0;
126 /* This is set to non-zero if compression is desired. */
127 int compress_flag = 0;
129 /* This is set to zero if the progressmeter is not desired. */
130 int showprogress = 1;
132 /* This is set to non-zero if running in batch mode (that is, password
133 and passphrase queries are not allowed). */
136 /* This is set to the cipher type string if given on the command line. */
139 /* This is set to the RSA authentication identity file name if given on
141 char *identity = NULL;
143 /* This is the port to use in contacting the remote site (is non-NULL). */
146 /* This is the program to execute for the secured connection. ("ssh" or -S) */
147 char *ssh_program = SSH_PROGRAM;
150 * This function executes the given command as the specified user on the
151 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
152 * assigns the input and output file descriptors on success.
156 do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
158 int pin[2], pout[2], reserved[2];
161 fprintf(stderr, "Executing: host %s, user %s, command %s\n",
162 host, remuser ? remuser : "(unspecified)", cmd);
165 * Reserve two descriptors so that the real pipes won't get
166 * descriptors 0 and 1 because that will screw up dup2 below.
170 /* Create a socket pair for communicating with ssh. */
172 fatal("pipe: %s", strerror(errno));
174 fatal("pipe: %s", strerror(errno));
176 /* Free the reserved descriptors. */
180 /* For a child to execute the command on the remote host using ssh. */
182 char *args[100]; /* XXX careful */
194 args[i++] = ssh_program;
196 args[i++] = "-oFallBackToRsh no";
206 args[i++] = "-oBatchMode yes";
207 if (cipher != NULL) {
211 if (identity != NULL) {
213 args[i++] = identity;
219 if (remuser != NULL) {
227 execvp(ssh_program, args);
231 /* Parent. Close the other side, and return the local side. */
240 fatal(const char *fmt,...)
246 vsnprintf(buf, sizeof(buf), fmt, ap);
248 fprintf(stderr, "%s\n", buf);
257 extern int iamremote;
259 BUF *allocbuf(BUF *, int, int);
264 void run_err(const char *,...);
265 void verifydir(char *);
269 int errs, remin, remout;
270 int pflag, iamremote, iamrecursive, targetshouldbedirectory;
273 char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
276 void rsource(char *, struct stat *);
277 void sink(int, char *[]);
278 void source(int, char *[]);
279 void tolocal(int, char *[]);
280 void toremote(char *, int, char *[]);
288 int ch, fflag, tflag;
294 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:")) != EOF)
296 /* User-visible flags. */
313 ssh_program = optarg;
316 /* Server options. */
318 targetshouldbedirectory = 1;
320 case 'f': /* "from" */
353 if ((pwd = getpwuid(userid = getuid())) == NULL)
354 fatal("unknown user %d", (int) userid);
356 if (!isatty(STDERR_FILENO))
359 remin = STDIN_FILENO;
360 remout = STDOUT_FILENO;
363 /* Follow "protocol", send data. */
376 targetshouldbedirectory = 1;
379 /* Command to be executed on remote system using "ssh". */
380 (void) sprintf(cmd, "scp%s%s%s%s", verbose_mode ? " -v" : "",
381 iamrecursive ? " -r" : "", pflag ? " -p" : "",
382 targetshouldbedirectory ? " -d" : "");
384 (void) signal(SIGPIPE, lostconn);
386 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
387 toremote(targ, argc, argv);
389 tolocal(argc, argv); /* Dest is local host. */
390 if (targetshouldbedirectory)
391 verifydir(argv[argc - 1]);
400 if (*host == '[' && host[strlen(host) - 1] == ']') {
401 host[strlen(host) - 1] = '\0';
408 toremote(targ, argc, argv)
413 char *bp, *host, *src, *suser, *thost, *tuser;
419 if ((thost = strchr(argv[argc - 1], '@'))) {
422 tuser = argv[argc - 1];
425 else if (!okname(tuser))
428 thost = argv[argc - 1];
432 for (i = 0; i < argc - 1; i++) {
433 src = colon(argv[i]);
434 if (src) { /* remote to remote */
438 host = strchr(argv[i], '@');
439 len = strlen(ssh_program) + strlen(argv[i]) +
440 strlen(src) + (tuser ? strlen(tuser) : 0) +
441 strlen(thost) + strlen(targ) + CMDNEEDS + 32;
445 host = cleanhostname(host);
448 suser = pwd->pw_name;
449 else if (!okname(suser))
452 "%s%s -x -o'FallBackToRsh no' -n -l %s %s %s %s '%s%s%s:%s'",
453 ssh_program, verbose_mode ? " -v" : "",
454 suser, host, cmd, src,
455 tuser ? tuser : "", tuser ? "@" : "",
458 host = cleanhostname(argv[i]);
460 "exec %s%s -x -o'FallBackToRsh no' -n %s %s %s '%s%s%s:%s'",
461 ssh_program, verbose_mode ? " -v" : "",
463 tuser ? tuser : "", tuser ? "@" : "",
467 fprintf(stderr, "Executing: %s\n", bp);
470 } else { /* local to remote */
472 len = strlen(targ) + CMDNEEDS + 20;
474 (void) sprintf(bp, "%s -t %s", cmd, targ);
475 host = cleanhostname(thost);
476 if (do_cmd(host, tuser, bp, &remin,
494 char *bp, *host, *src, *suser;
496 for (i = 0; i < argc - 1; i++) {
497 if (!(src = colon(argv[i]))) { /* Local to local. */
498 len = strlen(_PATH_CP) + strlen(argv[i]) +
499 strlen(argv[argc - 1]) + 20;
501 (void) sprintf(bp, "exec %s%s%s %s %s", _PATH_CP,
502 iamrecursive ? " -r" : "", pflag ? " -p" : "",
503 argv[i], argv[argc - 1]);
505 fprintf(stderr, "Executing: %s\n", bp);
514 if ((host = strchr(argv[i], '@')) == NULL) {
521 suser = pwd->pw_name;
522 else if (!okname(suser))
525 host = cleanhostname(host);
526 len = strlen(src) + CMDNEEDS + 20;
528 (void) sprintf(bp, "%s -f %s", cmd, src);
529 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
535 sink(1, argv + argc - 1);
550 int amt, fd, haderr, indx, result;
551 char *last, *name, buf[2048];
553 for (indx = 0; indx < argc; ++indx) {
556 if ((fd = open(name, O_RDONLY, 0)) < 0)
558 if (fstat(fd, &stb) < 0) {
559 syserr: run_err("%s: %s", name, strerror(errno));
562 switch (stb.st_mode & S_IFMT) {
572 run_err("%s: not a regular file", name);
575 if ((last = strrchr(name, '/')) == NULL)
582 * Make it compatible with possible future
583 * versions expecting microseconds.
585 (void) sprintf(buf, "T%lu 0 %lu 0\n",
586 (unsigned long) stb.st_mtime,
587 (unsigned long) stb.st_atime);
588 (void) atomicio(write, remout, buf, strlen(buf));
592 #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
593 (void) sprintf(buf, "C%04o %lu %s\n",
594 (unsigned int) (stb.st_mode & FILEMODEMASK),
595 (unsigned long) stb.st_size,
598 fprintf(stderr, "Sending file modes: %s", buf);
601 (void) atomicio(write, remout, buf, strlen(buf));
604 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
605 next: (void) close(fd);
609 totalbytes = stb.st_size;
612 /* Keep writing after an error so that we stay sync'd up. */
613 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
615 if (i + amt > stb.st_size)
616 amt = stb.st_size - i;
618 result = atomicio(read, fd, bp->buf, amt);
620 haderr = result >= 0 ? EIO : errno;
623 (void) atomicio(write, remout, bp->buf, amt);
625 result = atomicio(write, remout, bp->buf, amt);
627 haderr = result >= 0 ? EIO : errno;
634 if (close(fd) < 0 && !haderr)
637 (void) atomicio(write, remout, "", 1);
639 run_err("%s: %s", name, strerror(haderr));
651 char *last, *vect[1], path[1100];
653 if (!(dirp = opendir(name))) {
654 run_err("%s: %s", name, strerror(errno));
657 last = strrchr(name, '/');
663 (void) sprintf(path, "T%lu 0 %lu 0\n",
664 (unsigned long) statp->st_mtime,
665 (unsigned long) statp->st_atime);
666 (void) atomicio(write, remout, path, strlen(path));
667 if (response() < 0) {
672 (void) sprintf(path, "D%04o %d %.1024s\n",
673 (unsigned int) (statp->st_mode & FILEMODEMASK), 0, last);
675 fprintf(stderr, "Entering directory: %s", path);
676 (void) atomicio(write, remout, path, strlen(path));
677 if (response() < 0) {
681 while ((dp = readdir(dirp))) {
684 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
686 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
687 run_err("%s/%s: name too long", name, dp->d_name);
690 (void) sprintf(path, "%s/%s", name, dp->d_name);
694 (void) closedir(dirp);
695 (void) atomicio(write, remout, "E\n", 2);
711 int amt, count, exists, first, mask, mode, ofd, omode;
713 int setimes, targisdir, wrerrno = 0;
714 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
716 struct timeval tv[2];
718 #define SCREWUP(str) { why = str; goto screwup; }
720 setimes = targisdir = 0;
725 run_err("ambiguous target");
729 if (targetshouldbedirectory)
732 (void) atomicio(write, remout, "", 1);
733 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
735 for (first = 1;; first = 0) {
737 if (atomicio(read, remin, cp, 1) <= 0)
740 SCREWUP("unexpected <newline>");
742 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
743 SCREWUP("lost connection");
745 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
748 if (buf[0] == '\01' || buf[0] == '\02') {
750 (void) atomicio(write, STDERR_FILENO,
751 buf + 1, strlen(buf + 1));
758 (void) atomicio(write, remout, "", 1);
764 #define getnum(t) (t) = 0; \
765 while (*cp >= '0' && *cp <= '9') (t) = (t) * 10 + (*cp++ - '0');
770 getnum(tv[1].tv_sec);
772 SCREWUP("mtime.sec not delimited");
776 SCREWUP("mtime.usec not delimited");
777 getnum(tv[0].tv_sec);
779 SCREWUP("atime.sec not delimited");
783 SCREWUP("atime.usec not delimited");
784 (void) atomicio(write, remout, "", 1);
787 if (*cp != 'C' && *cp != 'D') {
789 * Check for the case "rcp remote:foo\* local:bar".
790 * In this case, the line "No match." can be returned
791 * by the shell before the rcp command on the remote is
792 * executed so the ^Aerror_message convention isn't
799 SCREWUP("expected control record");
802 for (++cp; cp < buf + 5; cp++) {
803 if (*cp < '0' || *cp > '7')
805 mode = (mode << 3) | (*cp - '0');
808 SCREWUP("mode not delimited");
810 for (size = 0; *cp >= '0' && *cp <= '9';)
811 size = size * 10 + (*cp++ - '0');
813 SCREWUP("size not delimited");
815 static char *namebuf;
819 need = strlen(targ) + strlen(cp) + 250;
821 namebuf = xmalloc(need);
822 (void) sprintf(namebuf, "%s%s%s", targ,
823 *targ ? "/" : "", cp);
828 exists = stat(np, &stb) == 0;
830 int mod_flag = pflag;
832 if (!S_ISDIR(stb.st_mode)) {
837 (void) chmod(np, mode);
839 /* Handle copying from a read-only
842 if (mkdir(np, mode | S_IRWXU) < 0)
849 if (utimes(np, tv) < 0)
850 run_err("%s: set times: %s",
851 np, strerror(errno));
854 (void) chmod(np, mode);
859 if ((ofd = open(np, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
860 bad: run_err("%s: %s", np, strerror(errno));
863 (void) atomicio(write, remout, "", 1);
864 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
876 for (count = i = 0; i < size; i += 4096) {
882 j = atomicio(read, remin, cp, amt);
884 run_err("%s", j ? strerror(errno) :
885 "dropped connection");
892 if (count == bp->cnt) {
893 /* Keep reading so we stay sync'd up. */
895 j = atomicio(write, ofd, bp->buf, count);
898 wrerrno = j >= 0 ? EIO : errno;
907 if (count != 0 && wrerr == NO &&
908 (j = atomicio(write, ofd, bp->buf, count)) != count) {
910 wrerrno = j >= 0 ? EIO : errno;
913 if (ftruncate(ofd, size)) {
914 run_err("%s: truncate: %s", np, strerror(errno));
919 if (exists || omode != mode)
921 if (fchmod(ofd, omode))
922 #else /* HAVE_FCHMOD */
923 if (chmod(np, omode))
924 #endif /* HAVE_FCHMOD */
925 run_err("%s: set mode: %s",
926 np, strerror(errno));
928 if (!exists && omode != mode)
930 if (fchmod(ofd, omode & ~mask))
931 #else /* HAVE_FCHMOD */
932 if (chmod(np, omode & ~mask))
933 #endif /* HAVE_FCHMOD */
934 run_err("%s: set mode: %s",
935 np, strerror(errno));
937 if (close(ofd) == -1) {
942 if (setimes && wrerr == NO) {
944 if (utimes(np, tv) < 0) {
945 run_err("%s: set times: %s",
946 np, strerror(errno));
952 run_err("%s: %s", np, strerror(wrerrno));
955 (void) atomicio(write, remout, "", 1);
962 run_err("protocol error: %s", why);
969 char ch, *cp, resp, rbuf[2048];
971 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
981 case 1: /* error, followed by error msg */
982 case 2: /* fatal error, "" */
984 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
987 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
990 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
1002 (void) fprintf(stderr, "usage: scp "
1003 "[-pqrvC46] [-S ssh] [-P port] [-c cipher] [-i identity] f1 f2; or:\n"
1004 " scp [options] f1 ... fn directory\n");
1009 run_err(const char *fmt,...)
1015 if (fp == NULL && !(fp = fdopen(remout, "w")))
1017 (void) fprintf(fp, "%c", 0x01);
1018 (void) fprintf(fp, "scp: ");
1020 (void) vfprintf(fp, fmt, ap);
1022 (void) fprintf(fp, "\n");
1027 vfprintf(stderr, fmt, ap);
1029 fprintf(stderr, "\n");
1039 if (*cp == ':') /* Leading colon is part of file name. */
1045 if (*cp == '@' && *(cp+1) == '[')
1047 if (*cp == ']' && *(cp+1) == ':' && flag)
1049 if (*cp == ':' && !flag)
1063 if (!stat(cp, &stb)) {
1064 if (S_ISDIR(stb.st_mode))
1068 run_err("%s: %s", cp, strerror(errno));
1084 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
1089 bad: fprintf(stderr, "%s: invalid user name\n", cp0);
1094 allocbuf(bp, fd, blksize)
1099 #ifdef HAVE_ST_BLKSIZE
1102 if (fstat(fd, &stb) < 0) {
1103 run_err("fstat: %s", strerror(errno));
1106 if (stb.st_blksize == 0)
1109 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
1111 #else /* HAVE_ST_BLKSIZE */
1113 #endif /* HAVE_ST_BLKSIZE */
1114 if (bp->cnt >= size)
1116 if (bp->buf == NULL)
1117 bp->buf = xmalloc(size);
1119 bp->buf = xrealloc(bp->buf, size);
1129 fprintf(stderr, "lost connection\n");
1135 alarmtimer(int wait)
1137 struct itimerval itv;
1139 itv.it_value.tv_sec = wait;
1140 itv.it_value.tv_usec = 0;
1141 itv.it_interval = itv.it_value;
1142 setitimer(ITIMER_REAL, &itv, NULL);
1146 updateprogressmeter(int ignore)
1148 int save_errno = errno;
1157 static pid_t pgrp = -1;
1165 * Cygwin only supports tcgetpgrp() for getting the controlling tty
1168 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1171 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1172 ctty_pgrp == pgrp));
1177 progressmeter(int flag)
1179 static const char prefixes[] = " KMGTP";
1180 static struct timeval lastupdate;
1181 static off_t lastsize;
1182 struct timeval now, td, wait;
1183 off_t cursize, abbrevsize;
1185 int ratio, barlength, i, remaining;
1189 (void) gettimeofday(&start, (struct timezone *) 0);
1193 if (foregroundproc() == 0)
1196 (void) gettimeofday(&now, (struct timezone *) 0);
1197 cursize = statbytes;
1198 if (totalbytes != 0) {
1199 ratio = 100.0 * cursize / totalbytes;
1200 ratio = MAX(ratio, 0);
1201 ratio = MIN(ratio, 100);
1205 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
1207 barlength = getttywidth() - 51;
1208 barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH;
1209 if (barlength > 0) {
1210 i = barlength * ratio / 100;
1211 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1212 "|%.*s%*s|", i, BAR, barlength - i, "");
1215 abbrevsize = cursize;
1216 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1220 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5d %c%c ",
1221 (int) abbrevsize, prefixes[i], prefixes[i] == ' ' ? ' ' :
1224 timersub(&now, &lastupdate, &wait);
1225 if (cursize > lastsize) {
1228 if (wait.tv_sec >= STALLTIME) {
1229 start.tv_sec += wait.tv_sec;
1230 start.tv_usec += wait.tv_usec;
1234 timersub(&now, &start, &td);
1235 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1237 if (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes) {
1238 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1240 } else if (wait.tv_sec >= STALLTIME) {
1241 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1246 (int)(totalbytes / (statbytes / elapsed) - elapsed);
1248 remaining = elapsed;
1250 i = remaining / 3600;
1252 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1255 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1257 i = remaining % 3600;
1258 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1259 "%02d:%02d%s", i / 60, i % 60,
1260 (flag != 1) ? " ETA" : " ");
1262 atomicio(write, fileno(stdout), buf, strlen(buf));
1265 struct sigaction sa;
1266 sa.sa_handler = updateprogressmeter;
1267 sigemptyset((sigset_t *)&sa.sa_mask);
1269 sa.sa_flags = SA_RESTART;
1271 sigaction(SIGALRM, &sa, NULL);
1273 } else if (flag == 1) {
1275 atomicio(write, fileno(stdout), "\n", 1);
1283 struct winsize winsize;
1285 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
1286 return (winsize.ws_col ? winsize.ws_col : 80);