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.71 2001/05/19 16:05:41 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 /* Struct for addargs */
113 /* Time a transfer started. */
114 static struct timeval start;
116 /* Number of bytes of current file transferred so far. */
117 volatile off_t 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;
135 * This function executes the given command as the specified user on the
136 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
137 * assigns the input and output file descriptors on success.
141 do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
143 int pin[2], pout[2], reserved[2];
146 fprintf(stderr, "Executing: program %s host %s, user %s, command %s\n",
147 ssh_program, host, remuser ? remuser : "(unspecified)", cmd);
150 * Reserve two descriptors so that the real pipes won't get
151 * descriptors 0 and 1 because that will screw up dup2 below.
155 /* Create a socket pair for communicating with ssh. */
157 fatal("pipe: %s", strerror(errno));
159 fatal("pipe: %s", strerror(errno));
161 /* Free the reserved descriptors. */
165 /* For a child to execute the command on the remote host using ssh. */
175 args.list[0] = ssh_program;
177 addargs(&args, "-l%s", remuser);
178 addargs(&args, "%s", host);
179 addargs(&args, "%s", cmd);
181 execvp(ssh_program, args.list);
185 /* Parent. Close the other side, and return the local side. */
198 BUF *allocbuf(BUF *, int, int);
202 void run_err(const char *,...);
203 void verifydir(char *);
207 int errs, remin, remout;
208 int pflag, iamremote, iamrecursive, targetshouldbedirectory;
211 char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
214 void rsource(char *, struct stat *);
215 void sink(int, char *[]);
216 void source(int, char *[]);
217 void tolocal(int, char *[]);
218 void toremote(char *, int, char *[]);
226 int ch, fflag, tflag;
231 __progname = get_progname(argv[0]);
234 addargs(&args, "ssh"); /* overwritten with ssh_program */
235 addargs(&args, "-x");
236 addargs(&args, "-oFallBackToRsh no");
239 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:")) != -1)
241 /* User-visible flags. */
245 addargs(&args, "-%c", ch);
250 addargs(&args, "-%c%s", ch, optarg);
253 addargs(&args, "-p%s", optarg);
256 addargs(&args, "-oBatchmode yes");
265 ssh_program = xstrdup(optarg);
274 /* Server options. */
276 targetshouldbedirectory = 1;
278 case 'f': /* "from" */
286 setmode(0, O_BINARY);
295 if ((pwd = getpwuid(userid = getuid())) == NULL)
296 fatal("unknown user %d", (int) userid);
298 if (!isatty(STDERR_FILENO))
301 remin = STDIN_FILENO;
302 remout = STDOUT_FILENO;
305 /* Follow "protocol", send data. */
318 targetshouldbedirectory = 1;
321 /* Command to be executed on remote system using "ssh". */
322 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
323 verbose_mode ? " -v" : "",
324 iamrecursive ? " -r" : "", pflag ? " -p" : "",
325 targetshouldbedirectory ? " -d" : "");
327 (void) signal(SIGPIPE, lostconn);
329 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
330 toremote(targ, argc, argv);
332 tolocal(argc, argv); /* Dest is local host. */
333 if (targetshouldbedirectory)
334 verifydir(argv[argc - 1]);
340 toremote(targ, argc, argv)
345 char *bp, *host, *src, *suser, *thost, *tuser;
351 if ((thost = strchr(argv[argc - 1], '@'))) {
354 tuser = argv[argc - 1];
357 else if (!okname(tuser))
360 thost = argv[argc - 1];
364 for (i = 0; i < argc - 1; i++) {
365 src = colon(argv[i]);
366 if (src) { /* remote to remote */
370 host = strchr(argv[i], '@');
371 len = strlen(ssh_program) + strlen(argv[i]) +
372 strlen(src) + (tuser ? strlen(tuser) : 0) +
373 strlen(thost) + strlen(targ) + CMDNEEDS + 32;
377 host = cleanhostname(host);
380 suser = pwd->pw_name;
381 else if (!okname(suser))
384 "%s%s -x -o'FallBackToRsh no' -n "
385 "-l %s %s %s %s '%s%s%s:%s'",
386 ssh_program, verbose_mode ? " -v" : "",
387 suser, host, cmd, src,
388 tuser ? tuser : "", tuser ? "@" : "",
391 host = cleanhostname(argv[i]);
393 "exec %s%s -x -o'FallBackToRsh no' -n %s "
395 ssh_program, verbose_mode ? " -v" : "",
397 tuser ? tuser : "", tuser ? "@" : "",
401 fprintf(stderr, "Executing: %s\n", bp);
404 } else { /* local to remote */
406 len = strlen(targ) + CMDNEEDS + 20;
408 (void) snprintf(bp, len, "%s -t %s", cmd, targ);
409 host = cleanhostname(thost);
410 if (do_cmd(host, tuser, bp, &remin,
428 char *bp, *host, *src, *suser;
430 for (i = 0; i < argc - 1; i++) {
431 if (!(src = colon(argv[i]))) { /* Local to local. */
432 len = strlen(_PATH_CP) + strlen(argv[i]) +
433 strlen(argv[argc - 1]) + 20;
435 (void) snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
436 iamrecursive ? " -r" : "", pflag ? " -p" : "",
437 argv[i], argv[argc - 1]);
439 fprintf(stderr, "Executing: %s\n", bp);
448 if ((host = strchr(argv[i], '@')) == NULL) {
455 suser = pwd->pw_name;
456 else if (!okname(suser))
459 host = cleanhostname(host);
460 len = strlen(src) + CMDNEEDS + 20;
462 (void) snprintf(bp, len, "%s -f %s", cmd, src);
463 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
469 sink(1, argv + argc - 1);
483 off_t i, amt, result;
484 int fd, haderr, indx;
485 char *last, *name, buf[2048];
488 for (indx = 0; indx < argc; ++indx) {
492 while (len > 1 && name[len-1] == '/')
494 if ((fd = open(name, O_RDONLY, 0)) < 0)
496 if (fstat(fd, &stb) < 0) {
497 syserr: run_err("%s: %s", name, strerror(errno));
500 switch (stb.st_mode & S_IFMT) {
510 run_err("%s: not a regular file", name);
513 if ((last = strrchr(name, '/')) == NULL)
520 * Make it compatible with possible future
521 * versions expecting microseconds.
523 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
524 (u_long) stb.st_mtime,
525 (u_long) stb.st_atime);
526 (void) atomicio(write, remout, buf, strlen(buf));
530 #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
531 #ifdef HAVE_LONG_LONG_INT
532 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
533 (u_int) (stb.st_mode & FILEMODEMASK),
534 (long long) stb.st_size, last);
536 /* XXX: Handle integer overflow? */
537 snprintf(buf, sizeof buf, "C%04o %lu %s\n",
538 (u_int) (stb.st_mode & FILEMODEMASK),
539 (u_long) stb.st_size, last);
543 fprintf(stderr, "Sending file modes: %s", buf);
546 (void) atomicio(write, remout, buf, strlen(buf));
549 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
550 next: (void) close(fd);
554 totalbytes = stb.st_size;
557 /* Keep writing after an error so that we stay sync'd up. */
558 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
560 if (i + amt > stb.st_size)
561 amt = stb.st_size - i;
563 result = atomicio(read, fd, bp->buf, amt);
565 haderr = result >= 0 ? EIO : errno;
568 (void) atomicio(write, remout, bp->buf, amt);
570 result = atomicio(write, remout, bp->buf, amt);
572 haderr = result >= 0 ? EIO : errno;
579 if (close(fd) < 0 && !haderr)
582 (void) atomicio(write, remout, "", 1);
584 run_err("%s: %s", name, strerror(haderr));
596 char *last, *vect[1], path[1100];
598 if (!(dirp = opendir(name))) {
599 run_err("%s: %s", name, strerror(errno));
602 last = strrchr(name, '/');
608 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
609 (u_long) statp->st_mtime,
610 (u_long) statp->st_atime);
611 (void) atomicio(write, remout, path, strlen(path));
612 if (response() < 0) {
617 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
618 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
620 fprintf(stderr, "Entering directory: %s", path);
621 (void) atomicio(write, remout, path, strlen(path));
622 if (response() < 0) {
626 while ((dp = readdir(dirp)) != NULL) {
629 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
631 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
632 run_err("%s/%s: name too long", name, dp->d_name);
635 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
639 (void) closedir(dirp);
640 (void) atomicio(write, remout, "E\n", 2);
656 int amt, count, exists, first, mask, mode, ofd, omode;
658 int setimes, targisdir, wrerrno = 0;
659 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
660 struct timeval tv[2];
664 #define SCREWUP(str) { why = str; goto screwup; }
666 setimes = targisdir = 0;
671 run_err("ambiguous target");
675 if (targetshouldbedirectory)
678 (void) atomicio(write, remout, "", 1);
679 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
681 for (first = 1;; first = 0) {
683 if (atomicio(read, remin, cp, 1) <= 0)
686 SCREWUP("unexpected <newline>");
688 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
689 SCREWUP("lost connection");
691 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
694 if (buf[0] == '\01' || buf[0] == '\02') {
696 (void) atomicio(write, STDERR_FILENO,
697 buf + 1, strlen(buf + 1));
704 (void) atomicio(write, remout, "", 1);
714 mtime.tv_sec = strtol(cp, &cp, 10);
715 if (!cp || *cp++ != ' ')
716 SCREWUP("mtime.sec not delimited");
717 mtime.tv_usec = strtol(cp, &cp, 10);
718 if (!cp || *cp++ != ' ')
719 SCREWUP("mtime.usec not delimited");
720 atime.tv_sec = strtol(cp, &cp, 10);
721 if (!cp || *cp++ != ' ')
722 SCREWUP("atime.sec not delimited");
723 atime.tv_usec = strtol(cp, &cp, 10);
724 if (!cp || *cp++ != '\0')
725 SCREWUP("atime.usec not delimited");
726 (void) atomicio(write, remout, "", 1);
729 if (*cp != 'C' && *cp != 'D') {
731 * Check for the case "rcp remote:foo\* local:bar".
732 * In this case, the line "No match." can be returned
733 * by the shell before the rcp command on the remote is
734 * executed so the ^Aerror_message convention isn't
741 SCREWUP("expected control record");
744 for (++cp; cp < buf + 5; cp++) {
745 if (*cp < '0' || *cp > '7')
747 mode = (mode << 3) | (*cp - '0');
750 SCREWUP("mode not delimited");
752 for (size = 0; isdigit(*cp);)
753 size = size * 10 + (*cp++ - '0');
755 SCREWUP("size not delimited");
757 static char *namebuf;
761 need = strlen(targ) + strlen(cp) + 250;
762 if (need > cursize) {
765 namebuf = xmalloc(need);
768 (void) snprintf(namebuf, need, "%s%s%s", targ,
769 *targ ? "/" : "", cp);
774 exists = stat(np, &stb) == 0;
776 int mod_flag = pflag;
778 if (!S_ISDIR(stb.st_mode)) {
783 (void) chmod(np, mode);
785 /* Handle copying from a read-only
788 if (mkdir(np, mode | S_IRWXU) < 0)
791 vect[0] = xstrdup(np);
795 if (utimes(vect[0], tv) < 0)
796 run_err("%s: set times: %s",
797 vect[0], strerror(errno));
800 (void) chmod(vect[0], mode);
807 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
808 bad: run_err("%s: %s", np, strerror(errno));
811 (void) atomicio(write, remout, "", 1);
812 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
824 for (count = i = 0; i < size; i += 4096) {
830 j = read(remin, cp, amt);
831 if (j == -1 && (errno == EINTR || errno == EAGAIN)) {
834 run_err("%s", j ? strerror(errno) :
835 "dropped connection");
842 if (count == bp->cnt) {
843 /* Keep reading so we stay sync'd up. */
845 j = atomicio(write, ofd, bp->buf, count);
848 wrerrno = j >= 0 ? EIO : errno;
857 if (count != 0 && wrerr == NO &&
858 (j = atomicio(write, ofd, bp->buf, count)) != count) {
860 wrerrno = j >= 0 ? EIO : errno;
862 if (ftruncate(ofd, size)) {
863 run_err("%s: truncate: %s", np, strerror(errno));
867 if (exists || omode != mode)
869 if (fchmod(ofd, omode))
870 #else /* HAVE_FCHMOD */
871 if (chmod(np, omode))
872 #endif /* HAVE_FCHMOD */
873 run_err("%s: set mode: %s",
874 np, strerror(errno));
876 if (!exists && omode != mode)
878 if (fchmod(ofd, omode & ~mask))
879 #else /* HAVE_FCHMOD */
880 if (chmod(np, omode & ~mask))
881 #endif /* HAVE_FCHMOD */
882 run_err("%s: set mode: %s",
883 np, strerror(errno));
885 if (close(ofd) == -1) {
890 if (setimes && wrerr == NO) {
892 if (utimes(np, tv) < 0) {
893 run_err("%s: set times: %s",
894 np, strerror(errno));
900 run_err("%s: %s", np, strerror(wrerrno));
903 (void) atomicio(write, remout, "", 1);
910 run_err("protocol error: %s", why);
917 char ch, *cp, resp, rbuf[2048];
919 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
929 case 1: /* error, followed by error msg */
930 case 2: /* fatal error, "" */
932 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
935 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
938 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
950 (void) fprintf(stderr, "usage: scp "
951 "[-pqrvBC46] [-S ssh] [-P port] [-c cipher] [-i identity] f1 f2\n"
952 " or: scp [options] f1 ... fn directory\n");
957 run_err(const char *fmt,...)
963 if (fp == NULL && !(fp = fdopen(remout, "w")))
965 (void) fprintf(fp, "%c", 0x01);
966 (void) fprintf(fp, "scp: ");
968 (void) vfprintf(fp, fmt, ap);
970 (void) fprintf(fp, "\n");
975 vfprintf(stderr, fmt, ap);
977 fprintf(stderr, "\n");
987 if (!stat(cp, &stb)) {
988 if (S_ISDIR(stb.st_mode))
992 run_err("%s: %s", cp, strerror(errno));
1008 if (!isalpha(c) && !isdigit(c) &&
1009 c != '_' && c != '-' && c != '.' && c != '+')
1014 bad: fprintf(stderr, "%s: invalid user name\n", cp0);
1019 allocbuf(bp, fd, blksize)
1024 #ifdef HAVE_ST_BLKSIZE
1027 if (fstat(fd, &stb) < 0) {
1028 run_err("fstat: %s", strerror(errno));
1031 if (stb.st_blksize == 0)
1034 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
1036 #else /* HAVE_ST_BLKSIZE */
1038 #endif /* HAVE_ST_BLKSIZE */
1039 if (bp->cnt >= size)
1041 if (bp->buf == NULL)
1042 bp->buf = xmalloc(size);
1044 bp->buf = xrealloc(bp->buf, size);
1054 fprintf(stderr, "lost connection\n");
1060 alarmtimer(int wait)
1062 struct itimerval itv;
1064 itv.it_value.tv_sec = wait;
1065 itv.it_value.tv_usec = 0;
1066 itv.it_interval = itv.it_value;
1067 setitimer(ITIMER_REAL, &itv, NULL);
1071 updateprogressmeter(int ignore)
1073 int save_errno = errno;
1080 foregroundproc(void)
1082 static pid_t pgrp = -1;
1088 #ifdef HAVE_TCGETPGRP
1089 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1092 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1093 ctty_pgrp == pgrp));
1098 progressmeter(int flag)
1100 static const char prefixes[] = " KMGTP";
1101 static struct timeval lastupdate;
1102 static off_t lastsize;
1103 struct timeval now, td, wait;
1104 off_t cursize, abbrevsize;
1106 int ratio, barlength, i, remaining;
1110 (void) gettimeofday(&start, (struct timezone *) 0);
1114 if (foregroundproc() == 0)
1117 (void) gettimeofday(&now, (struct timezone *) 0);
1118 cursize = statbytes;
1119 if (totalbytes != 0) {
1120 ratio = 100.0 * cursize / totalbytes;
1121 ratio = MAX(ratio, 0);
1122 ratio = MIN(ratio, 100);
1126 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
1128 barlength = getttywidth() - 51;
1129 barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH;
1130 if (barlength > 0) {
1131 i = barlength * ratio / 100;
1132 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1133 "|%.*s%*s|", i, BAR, barlength - i, "");
1136 abbrevsize = cursize;
1137 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1141 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5lu %c%c ",
1142 (unsigned long) abbrevsize, prefixes[i],
1143 prefixes[i] == ' ' ? ' ' : 'B');
1145 timersub(&now, &lastupdate, &wait);
1146 if (cursize > lastsize) {
1149 if (wait.tv_sec >= STALLTIME) {
1150 start.tv_sec += wait.tv_sec;
1151 start.tv_usec += wait.tv_usec;
1155 timersub(&now, &start, &td);
1156 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1159 (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
1160 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1162 } else if (wait.tv_sec >= STALLTIME) {
1163 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1167 remaining = (int)(totalbytes / (statbytes / elapsed) -
1170 remaining = elapsed;
1172 i = remaining / 3600;
1174 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1177 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1179 i = remaining % 3600;
1180 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1181 "%02d:%02d%s", i / 60, i % 60,
1182 (flag != 1) ? " ETA" : " ");
1184 atomicio(write, fileno(stdout), buf, strlen(buf));
1187 mysignal(SIGALRM, updateprogressmeter);
1189 } else if (flag == 1) {
1191 atomicio(write, fileno(stdout), "\n", 1);
1199 struct winsize winsize;
1201 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
1202 return (winsize.ws_col ? winsize.ws_col : 80);