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.91 2002/06/19 00:27:55 deraadt Exp $");
79 RCSID("$FreeBSD: src/crypto/openssh/scp.c,v 1.1.1.1.2.6 2003/02/03 17:31:07 des Exp $");
83 #include "pathnames.h"
87 #ifdef HAVE___PROGNAME
88 extern char *__progname;
93 /* For progressmeter() -- number of seconds before xfer considered "stalled" */
95 /* alarm() interval for updating progress meter */
96 #define PROGRESSTIME 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 /* Struct for addargs */
108 /* Time a transfer started. */
109 static struct timeval start;
111 /* Number of bytes of current file transferred so far. */
112 volatile off_t statbytes;
114 /* Total size of current file. */
115 off_t totalbytes = 0;
117 /* Name of current file being transferred. */
120 /* This is set to non-zero to enable verbose mode. */
121 int verbose_mode = 0;
123 /* This is set to zero if the progressmeter is not desired. */
124 int showprogress = 1;
126 /* This is the program to execute for the secured connection. ("ssh" or -S) */
127 char *ssh_program = _PATH_SSH_PROGRAM;
130 * This function executes the given command as the specified user on the
131 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
132 * assigns the input and output file descriptors on success.
136 do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
138 int pin[2], pout[2], reserved[2];
142 "Executing: program %s host %s, user %s, command %s\n",
144 remuser ? remuser : "(unspecified)", cmd);
147 * Reserve two descriptors so that the real pipes won't get
148 * descriptors 0 and 1 because that will screw up dup2 below.
152 /* Create a socket pair for communicating with ssh. */
154 fatal("pipe: %s", strerror(errno));
156 fatal("pipe: %s", strerror(errno));
158 /* Free the reserved descriptors. */
162 /* For a child to execute the command on the remote host using ssh. */
172 args.list[0] = ssh_program;
174 addargs(&args, "-l%s", remuser);
175 addargs(&args, "%s", host);
176 addargs(&args, "%s", cmd);
178 execvp(ssh_program, args.list);
182 /* Parent. Close the other side, and return the local side. */
195 BUF *allocbuf(BUF *, int, int);
199 void run_err(const char *,...);
200 void verifydir(char *);
204 int errs, remin, remout;
205 int pflag, iamremote, iamrecursive, targetshouldbedirectory;
208 char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
211 void rsource(char *, struct stat *);
212 void sink(int, char *[]);
213 void source(int, char *[]);
214 void tolocal(int, char *[]);
215 void toremote(char *, int, char *[]);
223 int ch, fflag, tflag;
228 __progname = get_progname(argv[0]);
231 addargs(&args, "ssh"); /* overwritten with ssh_program */
232 addargs(&args, "-x");
233 addargs(&args, "-oForwardAgent no");
234 addargs(&args, "-oClearAllForwardings yes");
237 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:F:")) != -1)
239 /* User-visible flags. */
243 addargs(&args, "-%c", ch);
249 addargs(&args, "-%c%s", ch, optarg);
252 addargs(&args, "-p%s", optarg);
255 addargs(&args, "-oBatchmode yes");
264 ssh_program = xstrdup(optarg);
267 addargs(&args, "-v");
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 */
367 static char *ssh_options =
368 "-x -o'ClearAllForwardings yes'";
372 host = strchr(argv[i], '@');
373 len = strlen(ssh_program) + strlen(argv[i]) +
374 strlen(src) + (tuser ? strlen(tuser) : 0) +
375 strlen(thost) + strlen(targ) +
376 strlen(ssh_options) + CMDNEEDS + 20;
380 host = cleanhostname(host);
383 suser = pwd->pw_name;
384 else if (!okname(suser))
388 "-l %s %s %s %s '%s%s%s:%s'",
389 ssh_program, verbose_mode ? " -v" : "",
390 ssh_options, suser, host, cmd, src,
391 tuser ? tuser : "", tuser ? "@" : "",
394 host = cleanhostname(argv[i]);
396 "exec %s%s %s -n %s "
398 ssh_program, verbose_mode ? " -v" : "",
399 ssh_options, host, cmd, src,
400 tuser ? tuser : "", tuser ? "@" : "",
404 fprintf(stderr, "Executing: %s\n", bp);
407 } else { /* local to remote */
409 len = strlen(targ) + CMDNEEDS + 20;
411 (void) snprintf(bp, len, "%s -t %s", cmd, targ);
412 host = cleanhostname(thost);
413 if (do_cmd(host, tuser, bp, &remin,
431 char *bp, *host, *src, *suser;
433 for (i = 0; i < argc - 1; i++) {
434 if (!(src = colon(argv[i]))) { /* Local to local. */
435 len = strlen(_PATH_CP) + strlen(argv[i]) +
436 strlen(argv[argc - 1]) + 20;
438 (void) snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
439 iamrecursive ? " -r" : "", pflag ? " -p" : "",
440 argv[i], argv[argc - 1]);
442 fprintf(stderr, "Executing: %s\n", bp);
451 if ((host = strchr(argv[i], '@')) == NULL) {
458 suser = pwd->pw_name;
459 else if (!okname(suser))
462 host = cleanhostname(host);
463 len = strlen(src) + CMDNEEDS + 20;
465 (void) snprintf(bp, len, "%s -f %s", cmd, src);
466 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
472 sink(1, argv + argc - 1);
486 off_t i, amt, result;
487 int fd, haderr, indx;
488 char *last, *name, buf[2048];
491 for (indx = 0; indx < argc; ++indx) {
495 while (len > 1 && name[len-1] == '/')
497 if (strchr(name, '\n') != NULL) {
498 run_err("%s: skipping, filename contains a newline",
502 if ((fd = open(name, O_RDONLY, 0)) < 0)
504 if (fstat(fd, &stb) < 0) {
505 syserr: run_err("%s: %s", name, strerror(errno));
508 switch (stb.st_mode & S_IFMT) {
518 run_err("%s: not a regular file", name);
521 if ((last = strrchr(name, '/')) == NULL)
528 * Make it compatible with possible future
529 * versions expecting microseconds.
531 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
532 (u_long) stb.st_mtime,
533 (u_long) stb.st_atime);
534 (void) atomicio(write, remout, buf, strlen(buf));
538 #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
539 #ifdef HAVE_LONG_LONG_INT
540 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
541 (u_int) (stb.st_mode & FILEMODEMASK),
542 (long long)stb.st_size, last);
544 /* XXX: Handle integer overflow? */
545 snprintf(buf, sizeof buf, "C%04o %lu %s\n",
546 (u_int) (stb.st_mode & FILEMODEMASK),
547 (u_long) stb.st_size, last);
550 fprintf(stderr, "Sending file modes: %s", buf);
553 (void) atomicio(write, remout, buf, strlen(buf));
556 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
557 next: (void) close(fd);
561 totalbytes = stb.st_size;
564 /* Keep writing after an error so that we stay sync'd up. */
565 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
567 if (i + amt > stb.st_size)
568 amt = stb.st_size - i;
570 result = atomicio(read, fd, bp->buf, amt);
572 haderr = result >= 0 ? EIO : errno;
575 (void) atomicio(write, remout, bp->buf, amt);
577 result = atomicio(write, remout, bp->buf, amt);
579 haderr = result >= 0 ? EIO : errno;
586 if (close(fd) < 0 && !haderr)
589 (void) atomicio(write, remout, "", 1);
591 run_err("%s: %s", name, strerror(haderr));
603 char *last, *vect[1], path[1100];
605 if (!(dirp = opendir(name))) {
606 run_err("%s: %s", name, strerror(errno));
609 last = strrchr(name, '/');
615 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
616 (u_long) statp->st_mtime,
617 (u_long) statp->st_atime);
618 (void) atomicio(write, remout, path, strlen(path));
619 if (response() < 0) {
624 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
625 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
627 fprintf(stderr, "Entering directory: %s", path);
628 (void) atomicio(write, remout, path, strlen(path));
629 if (response() < 0) {
633 while ((dp = readdir(dirp)) != NULL) {
636 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
638 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
639 run_err("%s/%s: name too long", name, dp->d_name);
642 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
646 (void) closedir(dirp);
647 (void) atomicio(write, remout, "E\n", 2);
663 int amt, count, exists, first, mask, mode, ofd, omode;
665 int setimes, targisdir, wrerrno = 0;
666 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
667 struct timeval tv[2];
671 #define SCREWUP(str) do { why = str; goto screwup; } while (0)
673 setimes = targisdir = 0;
678 run_err("ambiguous target");
682 if (targetshouldbedirectory)
685 (void) atomicio(write, remout, "", 1);
686 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
688 for (first = 1;; first = 0) {
690 if (atomicio(read, remin, cp, 1) <= 0)
693 SCREWUP("unexpected <newline>");
695 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
696 SCREWUP("lost connection");
698 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
701 if (buf[0] == '\01' || buf[0] == '\02') {
703 (void) atomicio(write, STDERR_FILENO,
704 buf + 1, strlen(buf + 1));
711 (void) atomicio(write, remout, "", 1);
721 mtime.tv_sec = strtol(cp, &cp, 10);
722 if (!cp || *cp++ != ' ')
723 SCREWUP("mtime.sec not delimited");
724 mtime.tv_usec = strtol(cp, &cp, 10);
725 if (!cp || *cp++ != ' ')
726 SCREWUP("mtime.usec not delimited");
727 atime.tv_sec = strtol(cp, &cp, 10);
728 if (!cp || *cp++ != ' ')
729 SCREWUP("atime.sec not delimited");
730 atime.tv_usec = strtol(cp, &cp, 10);
731 if (!cp || *cp++ != '\0')
732 SCREWUP("atime.usec not delimited");
733 (void) atomicio(write, remout, "", 1);
736 if (*cp != 'C' && *cp != 'D') {
738 * Check for the case "rcp remote:foo\* local:bar".
739 * In this case, the line "No match." can be returned
740 * by the shell before the rcp command on the remote is
741 * executed so the ^Aerror_message convention isn't
748 SCREWUP("expected control record");
751 for (++cp; cp < buf + 5; cp++) {
752 if (*cp < '0' || *cp > '7')
754 mode = (mode << 3) | (*cp - '0');
757 SCREWUP("mode not delimited");
759 for (size = 0; isdigit(*cp);)
760 size = size * 10 + (*cp++ - '0');
762 SCREWUP("size not delimited");
764 static char *namebuf;
768 need = strlen(targ) + strlen(cp) + 250;
769 if (need > cursize) {
772 namebuf = xmalloc(need);
775 (void) snprintf(namebuf, need, "%s%s%s", targ,
776 strcmp(targ, "/") ? "/" : "", cp);
781 exists = stat(np, &stb) == 0;
783 int mod_flag = pflag;
785 if (!S_ISDIR(stb.st_mode)) {
790 (void) chmod(np, mode);
792 /* Handle copying from a read-only
795 if (mkdir(np, mode | S_IRWXU) < 0)
798 vect[0] = xstrdup(np);
802 if (utimes(vect[0], tv) < 0)
803 run_err("%s: set times: %s",
804 vect[0], strerror(errno));
807 (void) chmod(vect[0], mode);
814 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
815 bad: run_err("%s: %s", np, strerror(errno));
818 (void) atomicio(write, remout, "", 1);
819 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
831 for (count = i = 0; i < size; i += 4096) {
837 j = read(remin, cp, amt);
838 if (j == -1 && (errno == EINTR ||
842 run_err("%s", j ? strerror(errno) :
843 "dropped connection");
850 if (count == bp->cnt) {
851 /* Keep reading so we stay sync'd up. */
853 j = atomicio(write, ofd, bp->buf, count);
856 wrerrno = j >= 0 ? EIO : errno;
865 if (count != 0 && wrerr == NO &&
866 (j = atomicio(write, ofd, bp->buf, count)) != count) {
868 wrerrno = j >= 0 ? EIO : errno;
870 if (ftruncate(ofd, size)) {
871 run_err("%s: truncate: %s", np, strerror(errno));
875 if (exists || omode != mode)
877 if (fchmod(ofd, omode))
878 #else /* HAVE_FCHMOD */
879 if (chmod(np, omode))
880 #endif /* HAVE_FCHMOD */
881 run_err("%s: set mode: %s",
882 np, strerror(errno));
884 if (!exists && omode != mode)
886 if (fchmod(ofd, omode & ~mask))
887 #else /* HAVE_FCHMOD */
888 if (chmod(np, omode & ~mask))
889 #endif /* HAVE_FCHMOD */
890 run_err("%s: set mode: %s",
891 np, strerror(errno));
893 if (close(ofd) == -1) {
898 if (setimes && wrerr == NO) {
900 if (utimes(np, tv) < 0) {
901 run_err("%s: set times: %s",
902 np, strerror(errno));
908 run_err("%s: %s", np, strerror(wrerrno));
911 (void) atomicio(write, remout, "", 1);
918 run_err("protocol error: %s", why);
925 char ch, *cp, resp, rbuf[2048];
927 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
937 case 1: /* error, followed by error msg */
938 case 2: /* fatal error, "" */
940 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
943 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
946 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
958 (void) fprintf(stderr,
959 "usage: scp [-pqrvBC46] [-F config] [-S program] [-P port]\n"
960 " [-c cipher] [-i identity] [-o option]\n"
961 " [[user@]host1:]file1 [...] [[user@]host2:]file2\n");
966 run_err(const char *fmt,...)
972 if (fp == NULL && !(fp = fdopen(remout, "w")))
974 (void) fprintf(fp, "%c", 0x01);
975 (void) fprintf(fp, "scp: ");
977 (void) vfprintf(fp, fmt, ap);
979 (void) fprintf(fp, "\n");
984 vfprintf(stderr, fmt, ap);
986 fprintf(stderr, "\n");
996 if (!stat(cp, &stb)) {
997 if (S_ISDIR(stb.st_mode))
1001 run_err("%s: %s", cp, strerror(errno));
1017 if (!isalpha(c) && !isdigit(c) &&
1018 c != '_' && c != '-' && c != '.' && c != '+')
1023 bad: fprintf(stderr, "%s: invalid user name\n", cp0);
1028 allocbuf(bp, fd, blksize)
1033 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1036 if (fstat(fd, &stb) < 0) {
1037 run_err("fstat: %s", strerror(errno));
1040 if (stb.st_blksize == 0)
1043 size = roundup(stb.st_blksize, blksize);
1044 #else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1046 #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1047 if (bp->cnt >= size)
1049 if (bp->buf == NULL)
1050 bp->buf = xmalloc(size);
1052 bp->buf = xrealloc(bp->buf, size);
1053 memset(bp->buf, 0, size);
1063 write(STDERR_FILENO, "lost connection\n", 16);
1071 updateprogressmeter(int ignore)
1073 int save_errno = errno;
1076 signal(SIGALRM, updateprogressmeter);
1077 alarm(PROGRESSTIME);
1082 foregroundproc(void)
1084 static pid_t pgrp = -1;
1090 #ifdef HAVE_TCGETPGRP
1091 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1094 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1095 ctty_pgrp == pgrp));
1100 progressmeter(int flag)
1102 static const char prefixes[] = " KMGTP";
1103 static struct timeval lastupdate;
1104 static off_t lastsize;
1105 struct timeval now, td, wait;
1106 off_t cursize, abbrevsize;
1108 int ratio, barlength, i, remaining;
1112 (void) gettimeofday(&start, (struct timezone *) 0);
1116 if (foregroundproc() == 0)
1119 (void) gettimeofday(&now, (struct timezone *) 0);
1120 cursize = statbytes;
1121 if (totalbytes != 0) {
1122 ratio = 100.0 * cursize / totalbytes;
1123 ratio = MAX(ratio, 0);
1124 ratio = MIN(ratio, 100);
1128 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
1130 barlength = getttywidth() - 51;
1131 if (barlength > 0) {
1132 i = barlength * ratio / 100;
1133 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1135 "*******************************************************"
1136 "*******************************************************"
1137 "*******************************************************"
1138 "*******************************************************"
1139 "*******************************************************"
1140 "*******************************************************"
1141 "*******************************************************",
1145 abbrevsize = cursize;
1146 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1150 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5lu %c%c ",
1151 (unsigned long) abbrevsize, prefixes[i],
1152 prefixes[i] == ' ' ? ' ' : 'B');
1154 timersub(&now, &lastupdate, &wait);
1155 if (cursize > lastsize) {
1158 if (wait.tv_sec >= STALLTIME) {
1159 start.tv_sec += wait.tv_sec;
1160 start.tv_usec += wait.tv_usec;
1164 timersub(&now, &start, &td);
1165 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1168 (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
1169 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1171 } else if (wait.tv_sec >= STALLTIME) {
1172 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1176 remaining = (int)(totalbytes / (statbytes / elapsed) -
1179 remaining = elapsed;
1181 i = remaining / 3600;
1183 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1186 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1188 i = remaining % 3600;
1189 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1190 "%02d:%02d%s", i / 60, i % 60,
1191 (flag != 1) ? " ETA" : " ");
1193 atomicio(write, fileno(stdout), buf, strlen(buf));
1196 mysignal(SIGALRM, updateprogressmeter);
1197 alarm(PROGRESSTIME);
1198 } else if (flag == 1) {
1200 atomicio(write, fileno(stdout), "\n", 1);
1208 struct winsize winsize;
1210 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
1211 return (winsize.ws_col ? winsize.ws_col : 80);