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 $");
80 RCSID("$DragonFly: src/crypto/openssh/Attic/scp.c,v 1.2 2003/06/17 04:24:36 dillon Exp $");
84 #include "pathnames.h"
88 #ifdef HAVE___PROGNAME
89 extern char *__progname;
94 /* For progressmeter() -- number of seconds before xfer considered "stalled" */
96 /* alarm() interval for updating progress meter */
97 #define PROGRESSTIME 1
99 /* Visual statistics about files as they are transferred. */
100 void progressmeter(int);
102 /* Returns width of the terminal (for progress meter calculations). */
103 int getttywidth(void);
104 int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc);
106 /* Struct for addargs */
109 /* Time a transfer started. */
110 static struct timeval start;
112 /* Number of bytes of current file transferred so far. */
113 volatile off_t statbytes;
115 /* Total size of current file. */
116 off_t totalbytes = 0;
118 /* Name of current file being transferred. */
121 /* This is set to non-zero to enable verbose mode. */
122 int verbose_mode = 0;
124 /* This is set to zero if the progressmeter is not desired. */
125 int showprogress = 1;
127 /* This is the program to execute for the secured connection. ("ssh" or -S) */
128 char *ssh_program = _PATH_SSH_PROGRAM;
131 * This function executes the given command as the specified user on the
132 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
133 * assigns the input and output file descriptors on success.
137 do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
139 int pin[2], pout[2], reserved[2];
143 "Executing: program %s host %s, user %s, command %s\n",
145 remuser ? remuser : "(unspecified)", cmd);
148 * Reserve two descriptors so that the real pipes won't get
149 * descriptors 0 and 1 because that will screw up dup2 below.
153 /* Create a socket pair for communicating with ssh. */
155 fatal("pipe: %s", strerror(errno));
157 fatal("pipe: %s", strerror(errno));
159 /* Free the reserved descriptors. */
163 /* For a child to execute the command on the remote host using ssh. */
173 args.list[0] = ssh_program;
175 addargs(&args, "-l%s", remuser);
176 addargs(&args, "%s", host);
177 addargs(&args, "%s", cmd);
179 execvp(ssh_program, args.list);
183 /* Parent. Close the other side, and return the local side. */
196 BUF *allocbuf(BUF *, int, int);
200 void run_err(const char *,...);
201 void verifydir(char *);
205 int errs, remin, remout;
206 int pflag, iamremote, iamrecursive, targetshouldbedirectory;
209 char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
212 void rsource(char *, struct stat *);
213 void sink(int, char *[]);
214 void source(int, char *[]);
215 void tolocal(int, char *[]);
216 void toremote(char *, int, char *[]);
224 int ch, fflag, tflag;
229 __progname = get_progname(argv[0]);
232 addargs(&args, "ssh"); /* overwritten with ssh_program */
233 addargs(&args, "-x");
234 addargs(&args, "-oForwardAgent no");
235 addargs(&args, "-oClearAllForwardings yes");
238 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:F:")) != -1)
240 /* User-visible flags. */
244 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);
268 addargs(&args, "-v");
275 /* Server options. */
277 targetshouldbedirectory = 1;
279 case 'f': /* "from" */
287 setmode(0, O_BINARY);
296 if ((pwd = getpwuid(userid = getuid())) == NULL)
297 fatal("unknown user %d", (int) userid);
299 if (!isatty(STDERR_FILENO))
302 remin = STDIN_FILENO;
303 remout = STDOUT_FILENO;
306 /* Follow "protocol", send data. */
319 targetshouldbedirectory = 1;
322 /* Command to be executed on remote system using "ssh". */
323 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
324 verbose_mode ? " -v" : "",
325 iamrecursive ? " -r" : "", pflag ? " -p" : "",
326 targetshouldbedirectory ? " -d" : "");
328 (void) signal(SIGPIPE, lostconn);
330 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
331 toremote(targ, argc, argv);
333 tolocal(argc, argv); /* Dest is local host. */
334 if (targetshouldbedirectory)
335 verifydir(argv[argc - 1]);
341 toremote(targ, argc, argv)
346 char *bp, *host, *src, *suser, *thost, *tuser;
352 if ((thost = strchr(argv[argc - 1], '@'))) {
355 tuser = argv[argc - 1];
358 else if (!okname(tuser))
361 thost = argv[argc - 1];
365 for (i = 0; i < argc - 1; i++) {
366 src = colon(argv[i]);
367 if (src) { /* remote to remote */
368 static char *ssh_options =
369 "-x -o'ClearAllForwardings yes'";
373 host = strchr(argv[i], '@');
374 len = strlen(ssh_program) + strlen(argv[i]) +
375 strlen(src) + (tuser ? strlen(tuser) : 0) +
376 strlen(thost) + strlen(targ) +
377 strlen(ssh_options) + CMDNEEDS + 20;
381 host = cleanhostname(host);
384 suser = pwd->pw_name;
385 else if (!okname(suser))
389 "-l %s %s %s %s '%s%s%s:%s'",
390 ssh_program, verbose_mode ? " -v" : "",
391 ssh_options, suser, host, cmd, src,
392 tuser ? tuser : "", tuser ? "@" : "",
395 host = cleanhostname(argv[i]);
397 "exec %s%s %s -n %s "
399 ssh_program, verbose_mode ? " -v" : "",
400 ssh_options, host, cmd, src,
401 tuser ? tuser : "", tuser ? "@" : "",
405 fprintf(stderr, "Executing: %s\n", bp);
408 } else { /* local to remote */
410 len = strlen(targ) + CMDNEEDS + 20;
412 (void) snprintf(bp, len, "%s -t %s", cmd, targ);
413 host = cleanhostname(thost);
414 if (do_cmd(host, tuser, bp, &remin,
432 char *bp, *host, *src, *suser;
434 for (i = 0; i < argc - 1; i++) {
435 if (!(src = colon(argv[i]))) { /* Local to local. */
436 len = strlen(_PATH_CP) + strlen(argv[i]) +
437 strlen(argv[argc - 1]) + 20;
439 (void) snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
440 iamrecursive ? " -r" : "", pflag ? " -p" : "",
441 argv[i], argv[argc - 1]);
443 fprintf(stderr, "Executing: %s\n", bp);
452 if ((host = strchr(argv[i], '@')) == NULL) {
459 suser = pwd->pw_name;
460 else if (!okname(suser))
463 host = cleanhostname(host);
464 len = strlen(src) + CMDNEEDS + 20;
466 (void) snprintf(bp, len, "%s -f %s", cmd, src);
467 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
473 sink(1, argv + argc - 1);
487 off_t i, amt, result;
488 int fd, haderr, indx;
489 char *last, *name, buf[2048];
492 for (indx = 0; indx < argc; ++indx) {
496 while (len > 1 && name[len-1] == '/')
498 if (strchr(name, '\n') != NULL) {
499 run_err("%s: skipping, filename contains a newline",
503 if ((fd = open(name, O_RDONLY, 0)) < 0)
505 if (fstat(fd, &stb) < 0) {
506 syserr: run_err("%s: %s", name, strerror(errno));
509 switch (stb.st_mode & S_IFMT) {
519 run_err("%s: not a regular file", name);
522 if ((last = strrchr(name, '/')) == NULL)
529 * Make it compatible with possible future
530 * versions expecting microseconds.
532 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
533 (u_long) stb.st_mtime,
534 (u_long) stb.st_atime);
535 (void) atomicio(write, remout, buf, strlen(buf));
539 #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
540 #ifdef HAVE_LONG_LONG_INT
541 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
542 (u_int) (stb.st_mode & FILEMODEMASK),
543 (long long)stb.st_size, last);
545 /* XXX: Handle integer overflow? */
546 snprintf(buf, sizeof buf, "C%04o %lu %s\n",
547 (u_int) (stb.st_mode & FILEMODEMASK),
548 (u_long) stb.st_size, last);
551 fprintf(stderr, "Sending file modes: %s", buf);
554 (void) atomicio(write, remout, buf, strlen(buf));
557 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
558 next: (void) close(fd);
562 totalbytes = stb.st_size;
565 /* Keep writing after an error so that we stay sync'd up. */
566 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
568 if (i + amt > stb.st_size)
569 amt = stb.st_size - i;
571 result = atomicio(read, fd, bp->buf, amt);
573 haderr = result >= 0 ? EIO : errno;
576 (void) atomicio(write, remout, bp->buf, amt);
578 result = atomicio(write, remout, bp->buf, amt);
580 haderr = result >= 0 ? EIO : errno;
587 if (close(fd) < 0 && !haderr)
590 (void) atomicio(write, remout, "", 1);
592 run_err("%s: %s", name, strerror(haderr));
604 char *last, *vect[1], path[1100];
606 if (!(dirp = opendir(name))) {
607 run_err("%s: %s", name, strerror(errno));
610 last = strrchr(name, '/');
616 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
617 (u_long) statp->st_mtime,
618 (u_long) statp->st_atime);
619 (void) atomicio(write, remout, path, strlen(path));
620 if (response() < 0) {
625 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
626 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
628 fprintf(stderr, "Entering directory: %s", path);
629 (void) atomicio(write, remout, path, strlen(path));
630 if (response() < 0) {
634 while ((dp = readdir(dirp)) != NULL) {
637 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
639 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
640 run_err("%s/%s: name too long", name, dp->d_name);
643 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
647 (void) closedir(dirp);
648 (void) atomicio(write, remout, "E\n", 2);
664 int amt, count, exists, first, mask, mode, ofd, omode;
666 int setimes, targisdir, wrerrno = 0;
667 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
668 struct timeval tv[2];
672 #define SCREWUP(str) do { why = str; goto screwup; } while (0)
674 setimes = targisdir = 0;
679 run_err("ambiguous target");
683 if (targetshouldbedirectory)
686 (void) atomicio(write, remout, "", 1);
687 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
689 for (first = 1;; first = 0) {
691 if (atomicio(read, remin, cp, 1) <= 0)
694 SCREWUP("unexpected <newline>");
696 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
697 SCREWUP("lost connection");
699 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
702 if (buf[0] == '\01' || buf[0] == '\02') {
704 (void) atomicio(write, STDERR_FILENO,
705 buf + 1, strlen(buf + 1));
712 (void) atomicio(write, remout, "", 1);
722 mtime.tv_sec = strtol(cp, &cp, 10);
723 if (!cp || *cp++ != ' ')
724 SCREWUP("mtime.sec not delimited");
725 mtime.tv_usec = strtol(cp, &cp, 10);
726 if (!cp || *cp++ != ' ')
727 SCREWUP("mtime.usec not delimited");
728 atime.tv_sec = strtol(cp, &cp, 10);
729 if (!cp || *cp++ != ' ')
730 SCREWUP("atime.sec not delimited");
731 atime.tv_usec = strtol(cp, &cp, 10);
732 if (!cp || *cp++ != '\0')
733 SCREWUP("atime.usec not delimited");
734 (void) atomicio(write, remout, "", 1);
737 if (*cp != 'C' && *cp != 'D') {
739 * Check for the case "rcp remote:foo\* local:bar".
740 * In this case, the line "No match." can be returned
741 * by the shell before the rcp command on the remote is
742 * executed so the ^Aerror_message convention isn't
749 SCREWUP("expected control record");
752 for (++cp; cp < buf + 5; cp++) {
753 if (*cp < '0' || *cp > '7')
755 mode = (mode << 3) | (*cp - '0');
758 SCREWUP("mode not delimited");
760 for (size = 0; isdigit(*cp);)
761 size = size * 10 + (*cp++ - '0');
763 SCREWUP("size not delimited");
765 static char *namebuf;
769 need = strlen(targ) + strlen(cp) + 250;
770 if (need > cursize) {
773 namebuf = xmalloc(need);
776 (void) snprintf(namebuf, need, "%s%s%s", targ,
777 strcmp(targ, "/") ? "/" : "", cp);
782 exists = stat(np, &stb) == 0;
784 int mod_flag = pflag;
786 if (!S_ISDIR(stb.st_mode)) {
791 (void) chmod(np, mode);
793 /* Handle copying from a read-only
796 if (mkdir(np, mode | S_IRWXU) < 0)
799 vect[0] = xstrdup(np);
803 if (utimes(vect[0], tv) < 0)
804 run_err("%s: set times: %s",
805 vect[0], strerror(errno));
808 (void) chmod(vect[0], mode);
815 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
816 bad: run_err("%s: %s", np, strerror(errno));
819 (void) atomicio(write, remout, "", 1);
820 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
832 for (count = i = 0; i < size; i += 4096) {
838 j = read(remin, cp, amt);
839 if (j == -1 && (errno == EINTR ||
843 run_err("%s", j ? strerror(errno) :
844 "dropped connection");
851 if (count == bp->cnt) {
852 /* Keep reading so we stay sync'd up. */
854 j = atomicio(write, ofd, bp->buf, count);
857 wrerrno = j >= 0 ? EIO : errno;
866 if (count != 0 && wrerr == NO &&
867 (j = atomicio(write, ofd, bp->buf, count)) != count) {
869 wrerrno = j >= 0 ? EIO : errno;
871 if (ftruncate(ofd, size)) {
872 run_err("%s: truncate: %s", np, strerror(errno));
876 if (exists || omode != mode)
878 if (fchmod(ofd, omode))
879 #else /* HAVE_FCHMOD */
880 if (chmod(np, omode))
881 #endif /* HAVE_FCHMOD */
882 run_err("%s: set mode: %s",
883 np, strerror(errno));
885 if (!exists && omode != mode)
887 if (fchmod(ofd, omode & ~mask))
888 #else /* HAVE_FCHMOD */
889 if (chmod(np, omode & ~mask))
890 #endif /* HAVE_FCHMOD */
891 run_err("%s: set mode: %s",
892 np, strerror(errno));
894 if (close(ofd) == -1) {
899 if (setimes && wrerr == NO) {
901 if (utimes(np, tv) < 0) {
902 run_err("%s: set times: %s",
903 np, strerror(errno));
909 run_err("%s: %s", np, strerror(wrerrno));
912 (void) atomicio(write, remout, "", 1);
919 run_err("protocol error: %s", why);
926 char ch, *cp, resp, rbuf[2048];
928 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
938 case 1: /* error, followed by error msg */
939 case 2: /* fatal error, "" */
941 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
944 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
947 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
959 (void) fprintf(stderr,
960 "usage: scp [-pqrvBC46] [-F config] [-S program] [-P port]\n"
961 " [-c cipher] [-i identity] [-o option]\n"
962 " [[user@]host1:]file1 [...] [[user@]host2:]file2\n");
967 run_err(const char *fmt,...)
973 if (fp == NULL && !(fp = fdopen(remout, "w")))
975 (void) fprintf(fp, "%c", 0x01);
976 (void) fprintf(fp, "scp: ");
978 (void) vfprintf(fp, fmt, ap);
980 (void) fprintf(fp, "\n");
985 vfprintf(stderr, fmt, ap);
987 fprintf(stderr, "\n");
997 if (!stat(cp, &stb)) {
998 if (S_ISDIR(stb.st_mode))
1002 run_err("%s: %s", cp, strerror(errno));
1018 if (!isalpha(c) && !isdigit(c) &&
1019 c != '_' && c != '-' && c != '.' && c != '+')
1024 bad: fprintf(stderr, "%s: invalid user name\n", cp0);
1029 allocbuf(bp, fd, blksize)
1034 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1037 if (fstat(fd, &stb) < 0) {
1038 run_err("fstat: %s", strerror(errno));
1041 if (stb.st_blksize == 0)
1044 size = roundup(stb.st_blksize, blksize);
1045 #else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1047 #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1048 if (bp->cnt >= size)
1050 if (bp->buf == NULL)
1051 bp->buf = xmalloc(size);
1053 bp->buf = xrealloc(bp->buf, size);
1054 memset(bp->buf, 0, size);
1064 write(STDERR_FILENO, "lost connection\n", 16);
1072 updateprogressmeter(int ignore)
1074 int save_errno = errno;
1077 signal(SIGALRM, updateprogressmeter);
1078 alarm(PROGRESSTIME);
1083 foregroundproc(void)
1085 static pid_t pgrp = -1;
1091 #ifdef HAVE_TCGETPGRP
1092 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1095 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1096 ctty_pgrp == pgrp));
1101 progressmeter(int flag)
1103 static const char prefixes[] = " KMGTP";
1104 static struct timeval lastupdate;
1105 static off_t lastsize;
1106 struct timeval now, td, wait;
1107 off_t cursize, abbrevsize;
1109 int ratio, barlength, i, remaining;
1113 (void) gettimeofday(&start, (struct timezone *) 0);
1117 if (foregroundproc() == 0)
1120 (void) gettimeofday(&now, (struct timezone *) 0);
1121 cursize = statbytes;
1122 if (totalbytes != 0) {
1123 ratio = 100.0 * cursize / totalbytes;
1124 ratio = MAX(ratio, 0);
1125 ratio = MIN(ratio, 100);
1129 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
1131 barlength = getttywidth() - 51;
1132 if (barlength > 0) {
1133 i = barlength * ratio / 100;
1134 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1136 "*******************************************************"
1137 "*******************************************************"
1138 "*******************************************************"
1139 "*******************************************************"
1140 "*******************************************************"
1141 "*******************************************************"
1142 "*******************************************************",
1146 abbrevsize = cursize;
1147 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1151 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5lu %c%c ",
1152 (unsigned long) abbrevsize, prefixes[i],
1153 prefixes[i] == ' ' ? ' ' : 'B');
1155 timersub(&now, &lastupdate, &wait);
1156 if (cursize > lastsize) {
1159 if (wait.tv_sec >= STALLTIME) {
1160 start.tv_sec += wait.tv_sec;
1161 start.tv_usec += wait.tv_usec;
1165 timersub(&now, &start, &td);
1166 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1169 (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
1170 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1172 } else if (wait.tv_sec >= STALLTIME) {
1173 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1177 remaining = (int)(totalbytes / (statbytes / elapsed) -
1180 remaining = elapsed;
1182 i = remaining / 3600;
1184 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1187 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1189 i = remaining % 3600;
1190 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1191 "%02d:%02d%s", i / 60, i % 60,
1192 (flag != 1) ? " ETA" : " ");
1194 atomicio(write, fileno(stdout), buf, strlen(buf));
1197 mysignal(SIGALRM, updateprogressmeter);
1198 alarm(PROGRESSTIME);
1199 } else if (flag == 1) {
1201 atomicio(write, fileno(stdout), "\n", 1);
1209 struct winsize winsize;
1211 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
1212 return (winsize.ws_col ? winsize.ws_col : 80);