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 $");
85 /* For progressmeter() -- number of seconds before xfer considered "stalled" */
88 /* Progress meter bar */
90 "************************************************************"\
91 "************************************************************"\
92 "************************************************************"\
93 "************************************************************"
94 #define MAX_BARLENGTH (sizeof(BAR) - 1)
96 /* Visual statistics about files as they are transferred. */
97 void progressmeter(int);
99 /* Returns width of the terminal (for progress meter calculations). */
100 int getttywidth(void);
101 int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc);
103 /* Time a transfer started. */
104 static struct timeval start;
106 /* Number of bytes of current file transferred so far. */
107 volatile unsigned long statbytes;
109 /* Total size of current file. */
110 off_t totalbytes = 0;
112 /* Name of current file being transferred. */
115 /* This is set to non-zero if IPv4 is desired. */
118 /* This is set to non-zero if IPv6 is desired. */
121 /* This is set to non-zero to enable verbose mode. */
122 int verbose_mode = 0;
124 /* This is set to non-zero if compression is desired. */
125 int compress_flag = 0;
127 /* This is set to zero if the progressmeter is not desired. */
128 int showprogress = 1;
130 /* This is set to non-zero if running in batch mode (that is, password
131 and passphrase queries are not allowed). */
134 /* This is set to the cipher type string if given on the command line. */
137 /* This is set to the RSA authentication identity file name if given on
139 char *identity = NULL;
141 /* This is the port to use in contacting the remote site (is non-NULL). */
144 /* This is the program to execute for the secured connection. ("ssh" or -S) */
145 char *ssh_program = SSH_PROGRAM;
148 * This function executes the given command as the specified user on the
149 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
150 * assigns the input and output file descriptors on success.
154 do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
156 int pin[2], pout[2], reserved[2];
159 fprintf(stderr, "Executing: host %s, user %s, command %s\n",
160 host, remuser ? remuser : "(unspecified)", cmd);
163 * Reserve two descriptors so that the real pipes won't get
164 * descriptors 0 and 1 because that will screw up dup2 below.
168 /* Create a socket pair for communicating with ssh. */
170 fatal("pipe: %s", strerror(errno));
172 fatal("pipe: %s", strerror(errno));
174 /* Free the reserved descriptors. */
178 /* For a child to execute the command on the remote host using ssh. */
180 char *args[100]; /* XXX careful */
192 args[i++] = ssh_program;
194 args[i++] = "-oFallBackToRsh no";
204 args[i++] = "-oBatchMode yes";
205 if (cipher != NULL) {
209 if (identity != NULL) {
211 args[i++] = identity;
217 if (remuser != NULL) {
225 execvp(ssh_program, args);
229 /* Parent. Close the other side, and return the local side. */
238 fatal(const char *fmt,...)
244 vsnprintf(buf, sizeof(buf), fmt, ap);
246 fprintf(stderr, "%s\n", buf);
255 extern int iamremote;
257 BUF *allocbuf(BUF *, int, int);
262 void run_err(const char *,...);
263 void verifydir(char *);
267 int errs, remin, remout;
268 int pflag, iamremote, iamrecursive, targetshouldbedirectory;
271 char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
274 void rsource(char *, struct stat *);
275 void sink(int, char *[]);
276 void source(int, char *[]);
277 void tolocal(int, char *[]);
278 void toremote(char *, int, char *[]);
286 int ch, fflag, tflag;
292 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:")) != EOF)
294 /* User-visible flags. */
311 ssh_program = optarg;
314 /* Server options. */
316 targetshouldbedirectory = 1;
318 case 'f': /* "from" */
351 if ((pwd = getpwuid(userid = getuid())) == NULL)
352 fatal("unknown user %d", (int) userid);
354 if (!isatty(STDERR_FILENO))
357 remin = STDIN_FILENO;
358 remout = STDOUT_FILENO;
361 /* Follow "protocol", send data. */
374 targetshouldbedirectory = 1;
377 /* Command to be executed on remote system using "ssh". */
378 (void) sprintf(cmd, "scp%s%s%s%s", verbose_mode ? " -v" : "",
379 iamrecursive ? " -r" : "", pflag ? " -p" : "",
380 targetshouldbedirectory ? " -d" : "");
382 (void) signal(SIGPIPE, lostconn);
384 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
385 toremote(targ, argc, argv);
387 tolocal(argc, argv); /* Dest is local host. */
388 if (targetshouldbedirectory)
389 verifydir(argv[argc - 1]);
398 if (*host == '[' && host[strlen(host) - 1] == ']') {
399 host[strlen(host) - 1] = '\0';
406 toremote(targ, argc, argv)
411 char *bp, *host, *src, *suser, *thost, *tuser;
417 if ((thost = strchr(argv[argc - 1], '@'))) {
420 tuser = argv[argc - 1];
423 else if (!okname(tuser))
426 thost = argv[argc - 1];
430 for (i = 0; i < argc - 1; i++) {
431 src = colon(argv[i]);
432 if (src) { /* remote to remote */
436 host = strchr(argv[i], '@');
437 len = strlen(ssh_program) + strlen(argv[i]) +
438 strlen(src) + (tuser ? strlen(tuser) : 0) +
439 strlen(thost) + strlen(targ) + CMDNEEDS + 32;
443 host = cleanhostname(host);
446 suser = pwd->pw_name;
447 else if (!okname(suser))
450 "%s%s -x -o'FallBackToRsh no' -n -l %s %s %s %s '%s%s%s:%s'",
451 ssh_program, verbose_mode ? " -v" : "",
452 suser, host, cmd, src,
453 tuser ? tuser : "", tuser ? "@" : "",
456 host = cleanhostname(argv[i]);
458 "exec %s%s -x -o'FallBackToRsh no' -n %s %s %s '%s%s%s:%s'",
459 ssh_program, verbose_mode ? " -v" : "",
461 tuser ? tuser : "", tuser ? "@" : "",
465 fprintf(stderr, "Executing: %s\n", bp);
468 } else { /* local to remote */
470 len = strlen(targ) + CMDNEEDS + 20;
472 (void) sprintf(bp, "%s -t %s", cmd, targ);
473 host = cleanhostname(thost);
474 if (do_cmd(host, tuser, bp, &remin,
492 char *bp, *host, *src, *suser;
494 for (i = 0; i < argc - 1; i++) {
495 if (!(src = colon(argv[i]))) { /* Local to local. */
496 len = strlen(_PATH_CP) + strlen(argv[i]) +
497 strlen(argv[argc - 1]) + 20;
499 (void) sprintf(bp, "exec %s%s%s %s %s", _PATH_CP,
500 iamrecursive ? " -r" : "", pflag ? " -p" : "",
501 argv[i], argv[argc - 1]);
503 fprintf(stderr, "Executing: %s\n", bp);
512 if ((host = strchr(argv[i], '@')) == NULL) {
519 suser = pwd->pw_name;
520 else if (!okname(suser))
523 host = cleanhostname(host);
524 len = strlen(src) + CMDNEEDS + 20;
526 (void) sprintf(bp, "%s -f %s", cmd, src);
527 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
533 sink(1, argv + argc - 1);
548 int amt, fd, haderr, indx, result;
549 char *last, *name, buf[2048];
551 for (indx = 0; indx < argc; ++indx) {
554 if ((fd = open(name, O_RDONLY, 0)) < 0)
556 if (fstat(fd, &stb) < 0) {
557 syserr: run_err("%s: %s", name, strerror(errno));
560 switch (stb.st_mode & S_IFMT) {
570 run_err("%s: not a regular file", name);
573 if ((last = strrchr(name, '/')) == NULL)
580 * Make it compatible with possible future
581 * versions expecting microseconds.
583 (void) sprintf(buf, "T%lu 0 %lu 0\n",
584 (unsigned long) stb.st_mtime,
585 (unsigned long) stb.st_atime);
586 (void) atomicio(write, remout, buf, strlen(buf));
590 #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
591 (void) sprintf(buf, "C%04o %lu %s\n",
592 (unsigned int) (stb.st_mode & FILEMODEMASK),
593 (unsigned long) stb.st_size,
596 fprintf(stderr, "Sending file modes: %s", buf);
599 (void) atomicio(write, remout, buf, strlen(buf));
602 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
603 next: (void) close(fd);
607 totalbytes = stb.st_size;
610 /* Keep writing after an error so that we stay sync'd up. */
611 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
613 if (i + amt > stb.st_size)
614 amt = stb.st_size - i;
616 result = atomicio(read, fd, bp->buf, amt);
618 haderr = result >= 0 ? EIO : errno;
621 (void) atomicio(write, remout, bp->buf, amt);
623 result = atomicio(write, remout, bp->buf, amt);
625 haderr = result >= 0 ? EIO : errno;
632 if (close(fd) < 0 && !haderr)
635 (void) atomicio(write, remout, "", 1);
637 run_err("%s: %s", name, strerror(haderr));
649 char *last, *vect[1], path[1100];
651 if (!(dirp = opendir(name))) {
652 run_err("%s: %s", name, strerror(errno));
655 last = strrchr(name, '/');
661 (void) sprintf(path, "T%lu 0 %lu 0\n",
662 (unsigned long) statp->st_mtime,
663 (unsigned long) statp->st_atime);
664 (void) atomicio(write, remout, path, strlen(path));
665 if (response() < 0) {
670 (void) sprintf(path, "D%04o %d %.1024s\n",
671 (unsigned int) (statp->st_mode & FILEMODEMASK), 0, last);
673 fprintf(stderr, "Entering directory: %s", path);
674 (void) atomicio(write, remout, path, strlen(path));
675 if (response() < 0) {
679 while ((dp = readdir(dirp))) {
682 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
684 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
685 run_err("%s/%s: name too long", name, dp->d_name);
688 (void) sprintf(path, "%s/%s", name, dp->d_name);
692 (void) closedir(dirp);
693 (void) atomicio(write, remout, "E\n", 2);
709 int amt, count, exists, first, mask, mode, ofd, omode;
711 int setimes, targisdir, wrerrno = 0;
712 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
714 struct timeval tv[2];
716 #define SCREWUP(str) { why = str; goto screwup; }
718 setimes = targisdir = 0;
723 run_err("ambiguous target");
727 if (targetshouldbedirectory)
730 (void) atomicio(write, remout, "", 1);
731 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
733 for (first = 1;; first = 0) {
735 if (atomicio(read, remin, cp, 1) <= 0)
738 SCREWUP("unexpected <newline>");
740 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
741 SCREWUP("lost connection");
743 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
746 if (buf[0] == '\01' || buf[0] == '\02') {
748 (void) atomicio(write, STDERR_FILENO,
749 buf + 1, strlen(buf + 1));
756 (void) atomicio(write, remout, "", 1);
762 #define getnum(t) (t) = 0; \
763 while (*cp >= '0' && *cp <= '9') (t) = (t) * 10 + (*cp++ - '0');
768 getnum(tv[1].tv_sec);
770 SCREWUP("mtime.sec not delimited");
774 SCREWUP("mtime.usec not delimited");
775 getnum(tv[0].tv_sec);
777 SCREWUP("atime.sec not delimited");
781 SCREWUP("atime.usec not delimited");
782 (void) atomicio(write, remout, "", 1);
785 if (*cp != 'C' && *cp != 'D') {
787 * Check for the case "rcp remote:foo\* local:bar".
788 * In this case, the line "No match." can be returned
789 * by the shell before the rcp command on the remote is
790 * executed so the ^Aerror_message convention isn't
797 SCREWUP("expected control record");
800 for (++cp; cp < buf + 5; cp++) {
801 if (*cp < '0' || *cp > '7')
803 mode = (mode << 3) | (*cp - '0');
806 SCREWUP("mode not delimited");
808 for (size = 0; *cp >= '0' && *cp <= '9';)
809 size = size * 10 + (*cp++ - '0');
811 SCREWUP("size not delimited");
813 static char *namebuf;
817 need = strlen(targ) + strlen(cp) + 250;
819 namebuf = xmalloc(need);
820 (void) sprintf(namebuf, "%s%s%s", targ,
821 *targ ? "/" : "", cp);
826 exists = stat(np, &stb) == 0;
828 int mod_flag = pflag;
830 if (!S_ISDIR(stb.st_mode)) {
835 (void) chmod(np, mode);
837 /* Handle copying from a read-only
840 if (mkdir(np, mode | S_IRWXU) < 0)
847 if (utimes(np, tv) < 0)
848 run_err("%s: set times: %s",
849 np, strerror(errno));
852 (void) chmod(np, mode);
857 if ((ofd = open(np, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
858 bad: run_err("%s: %s", np, strerror(errno));
861 (void) atomicio(write, remout, "", 1);
862 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
874 for (count = i = 0; i < size; i += 4096) {
880 j = atomicio(read, remin, cp, amt);
882 run_err("%s", j ? strerror(errno) :
883 "dropped connection");
890 if (count == bp->cnt) {
891 /* Keep reading so we stay sync'd up. */
893 j = atomicio(write, ofd, bp->buf, count);
896 wrerrno = j >= 0 ? EIO : errno;
905 if (count != 0 && wrerr == NO &&
906 (j = atomicio(write, ofd, bp->buf, count)) != count) {
908 wrerrno = j >= 0 ? EIO : errno;
911 if (ftruncate(ofd, size)) {
912 run_err("%s: truncate: %s", np, strerror(errno));
917 if (exists || omode != mode)
918 if (fchmod(ofd, omode))
919 run_err("%s: set mode: %s",
920 np, strerror(errno));
922 if (!exists && omode != mode)
923 if (fchmod(ofd, omode & ~mask))
924 run_err("%s: set mode: %s",
925 np, strerror(errno));
927 if (close(ofd) == -1) {
932 if (setimes && wrerr == NO) {
934 if (utimes(np, tv) < 0) {
935 run_err("%s: set times: %s",
936 np, strerror(errno));
942 run_err("%s: %s", np, strerror(wrerrno));
945 (void) atomicio(write, remout, "", 1);
952 run_err("protocol error: %s", why);
959 char ch, *cp, resp, rbuf[2048];
961 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
971 case 1: /* error, followed by error msg */
972 case 2: /* fatal error, "" */
974 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
977 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
980 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
992 (void) fprintf(stderr, "usage: scp "
993 "[-pqrvC46] [-S ssh] [-P port] [-c cipher] [-i identity] f1 f2; or:\n"
994 " scp [options] f1 ... fn directory\n");
999 run_err(const char *fmt,...)
1005 if (fp == NULL && !(fp = fdopen(remout, "w")))
1007 (void) fprintf(fp, "%c", 0x01);
1008 (void) fprintf(fp, "scp: ");
1010 (void) vfprintf(fp, fmt, ap);
1012 (void) fprintf(fp, "\n");
1017 vfprintf(stderr, fmt, ap);
1019 fprintf(stderr, "\n");
1029 if (*cp == ':') /* Leading colon is part of file name. */
1035 if (*cp == '@' && *(cp+1) == '[')
1037 if (*cp == ']' && *(cp+1) == ':' && flag)
1039 if (*cp == ':' && !flag)
1053 if (!stat(cp, &stb)) {
1054 if (S_ISDIR(stb.st_mode))
1058 run_err("%s: %s", cp, strerror(errno));
1074 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
1079 bad: fprintf(stderr, "%s: invalid user name\n", cp0);
1084 allocbuf(bp, fd, blksize)
1091 if (fstat(fd, &stb) < 0) {
1092 run_err("fstat: %s", strerror(errno));
1095 if (stb.st_blksize == 0)
1098 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
1100 if (bp->cnt >= size)
1102 if (bp->buf == NULL)
1103 bp->buf = xmalloc(size);
1105 bp->buf = xrealloc(bp->buf, size);
1115 fprintf(stderr, "lost connection\n");
1121 alarmtimer(int wait)
1123 struct itimerval itv;
1125 itv.it_value.tv_sec = wait;
1126 itv.it_value.tv_usec = 0;
1127 itv.it_interval = itv.it_value;
1128 setitimer(ITIMER_REAL, &itv, NULL);
1132 updateprogressmeter(int ignore)
1134 int save_errno = errno;
1143 static pid_t pgrp = -1;
1151 * Cygwin only supports tcgetpgrp() for getting the controlling tty
1154 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1157 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1158 ctty_pgrp == pgrp));
1163 progressmeter(int flag)
1165 static const char prefixes[] = " KMGTP";
1166 static struct timeval lastupdate;
1167 static off_t lastsize;
1168 struct timeval now, td, wait;
1169 off_t cursize, abbrevsize;
1171 int ratio, barlength, i, remaining;
1175 (void) gettimeofday(&start, (struct timezone *) 0);
1179 if (foregroundproc() == 0)
1182 (void) gettimeofday(&now, (struct timezone *) 0);
1183 cursize = statbytes;
1184 if (totalbytes != 0) {
1185 ratio = 100.0 * cursize / totalbytes;
1186 ratio = MAX(ratio, 0);
1187 ratio = MIN(ratio, 100);
1191 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
1193 barlength = getttywidth() - 51;
1194 barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH;
1195 if (barlength > 0) {
1196 i = barlength * ratio / 100;
1197 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1198 "|%.*s%*s|", i, BAR, barlength - i, "");
1201 abbrevsize = cursize;
1202 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1206 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5d %c%c ",
1207 (int) abbrevsize, prefixes[i], prefixes[i] == ' ' ? ' ' :
1210 timersub(&now, &lastupdate, &wait);
1211 if (cursize > lastsize) {
1214 if (wait.tv_sec >= STALLTIME) {
1215 start.tv_sec += wait.tv_sec;
1216 start.tv_usec += wait.tv_usec;
1220 timersub(&now, &start, &td);
1221 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1223 if (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes) {
1224 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1226 } else if (wait.tv_sec >= STALLTIME) {
1227 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1232 (int)(totalbytes / (statbytes / elapsed) - elapsed);
1234 remaining = elapsed;
1236 i = remaining / 3600;
1238 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1241 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1243 i = remaining % 3600;
1244 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1245 "%02d:%02d%s", i / 60, i % 60,
1246 (flag != 1) ? " ETA" : " ");
1248 atomicio(write, fileno(stdout), buf, strlen(buf));
1251 struct sigaction sa;
1252 sa.sa_handler = updateprogressmeter;
1253 sigemptyset((sigset_t *)&sa.sa_mask);
1255 sa.sa_flags = SA_RESTART;
1257 sigaction(SIGALRM, &sa, NULL);
1259 } else if (flag == 1) {
1261 atomicio(write, fileno(stdout), "\n", 1);
1269 struct winsize winsize;
1271 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
1272 return (winsize.ws_col ? winsize.ws_col : 80);