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.85 2001/10/01 08:06:28 markus Exp $");
82 #include "pathnames.h"
86 #ifdef HAVE___PROGNAME
87 extern char *__progname;
92 /* For progressmeter() -- number of seconds before xfer considered "stalled" */
94 /* alarm() interval for updating progress meter */
95 #define PROGRESSTIME 1
97 /* Progress meter bar */
99 "************************************************************"\
100 "************************************************************"\
101 "************************************************************"\
102 "************************************************************"
103 #define MAX_BARLENGTH (sizeof(BAR) - 1)
105 /* Visual statistics about files as they are transferred. */
106 void progressmeter(int);
108 /* Returns width of the terminal (for progress meter calculations). */
109 int getttywidth(void);
110 int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc);
112 /* Struct for addargs */
115 /* Time a transfer started. */
116 static struct timeval start;
118 /* Number of bytes of current file transferred so far. */
119 volatile off_t statbytes;
121 /* Total size of current file. */
122 off_t totalbytes = 0;
124 /* Name of current file being transferred. */
127 /* This is set to non-zero to enable verbose mode. */
128 int verbose_mode = 0;
130 /* This is set to zero if the progressmeter is not desired. */
131 int showprogress = 1;
133 /* This is the program to execute for the secured connection. ("ssh" or -S) */
134 char *ssh_program = _PATH_SSH_PROGRAM;
137 * This function executes the given command as the specified user on the
138 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
139 * assigns the input and output file descriptors on success.
143 do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
145 int pin[2], pout[2], reserved[2];
149 "Executing: program %s host %s, user %s, command %s\n",
151 remuser ? remuser : "(unspecified)", cmd);
154 * Reserve two descriptors so that the real pipes won't get
155 * descriptors 0 and 1 because that will screw up dup2 below.
159 /* Create a socket pair for communicating with ssh. */
161 fatal("pipe: %s", strerror(errno));
163 fatal("pipe: %s", strerror(errno));
165 /* Free the reserved descriptors. */
169 /* For a child to execute the command on the remote host using ssh. */
179 args.list[0] = ssh_program;
181 addargs(&args, "-l%s", remuser);
182 addargs(&args, "%s", host);
183 addargs(&args, "%s", cmd);
185 execvp(ssh_program, args.list);
189 /* Parent. Close the other side, and return the local side. */
202 BUF *allocbuf(BUF *, int, int);
206 void run_err(const char *,...);
207 void verifydir(char *);
211 int errs, remin, remout;
212 int pflag, iamremote, iamrecursive, targetshouldbedirectory;
215 char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
218 void rsource(char *, struct stat *);
219 void sink(int, char *[]);
220 void source(int, char *[]);
221 void tolocal(int, char *[]);
222 void toremote(char *, int, char *[]);
230 int ch, fflag, tflag;
235 __progname = get_progname(argv[0]);
238 addargs(&args, "ssh"); /* overwritten with ssh_program */
239 addargs(&args, "-x");
240 addargs(&args, "-oForwardAgent no");
241 addargs(&args, "-oFallBackToRsh no");
242 addargs(&args, "-oClearAllForwardings yes");
245 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:F:")) != -1)
247 /* User-visible flags. */
251 addargs(&args, "-%c", ch);
257 addargs(&args, "-%c%s", ch, optarg);
260 addargs(&args, "-p%s", optarg);
263 addargs(&args, "-oBatchmode yes");
272 ssh_program = xstrdup(optarg);
275 addargs(&args, "-v");
282 /* Server options. */
284 targetshouldbedirectory = 1;
286 case 'f': /* "from" */
294 setmode(0, O_BINARY);
303 if ((pwd = getpwuid(userid = getuid())) == NULL)
304 fatal("unknown user %d", (int) userid);
306 if (!isatty(STDERR_FILENO))
309 remin = STDIN_FILENO;
310 remout = STDOUT_FILENO;
313 /* Follow "protocol", send data. */
326 targetshouldbedirectory = 1;
329 /* Command to be executed on remote system using "ssh". */
330 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
331 verbose_mode ? " -v" : "",
332 iamrecursive ? " -r" : "", pflag ? " -p" : "",
333 targetshouldbedirectory ? " -d" : "");
335 (void) signal(SIGPIPE, lostconn);
337 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
338 toremote(targ, argc, argv);
340 tolocal(argc, argv); /* Dest is local host. */
341 if (targetshouldbedirectory)
342 verifydir(argv[argc - 1]);
348 toremote(targ, argc, argv)
353 char *bp, *host, *src, *suser, *thost, *tuser;
359 if ((thost = strchr(argv[argc - 1], '@'))) {
362 tuser = argv[argc - 1];
365 else if (!okname(tuser))
368 thost = argv[argc - 1];
372 for (i = 0; i < argc - 1; i++) {
373 src = colon(argv[i]);
374 if (src) { /* remote to remote */
375 static char *ssh_options =
376 "-x -o'FallBackToRsh no' "
377 "-o'ClearAllForwardings yes'";
381 host = strchr(argv[i], '@');
382 len = strlen(ssh_program) + strlen(argv[i]) +
383 strlen(src) + (tuser ? strlen(tuser) : 0) +
384 strlen(thost) + strlen(targ) +
385 strlen(ssh_options) + CMDNEEDS + 20;
389 host = cleanhostname(host);
392 suser = pwd->pw_name;
393 else if (!okname(suser))
397 "-l %s %s %s %s '%s%s%s:%s'",
398 ssh_program, verbose_mode ? " -v" : "",
399 ssh_options, suser, host, cmd, src,
400 tuser ? tuser : "", tuser ? "@" : "",
403 host = cleanhostname(argv[i]);
405 "exec %s%s %s -n %s "
407 ssh_program, verbose_mode ? " -v" : "",
408 ssh_options, host, cmd, src,
409 tuser ? tuser : "", tuser ? "@" : "",
413 fprintf(stderr, "Executing: %s\n", bp);
416 } else { /* local to remote */
418 len = strlen(targ) + CMDNEEDS + 20;
420 (void) snprintf(bp, len, "%s -t %s", cmd, targ);
421 host = cleanhostname(thost);
422 if (do_cmd(host, tuser, bp, &remin,
440 char *bp, *host, *src, *suser;
442 for (i = 0; i < argc - 1; i++) {
443 if (!(src = colon(argv[i]))) { /* Local to local. */
444 len = strlen(_PATH_CP) + strlen(argv[i]) +
445 strlen(argv[argc - 1]) + 20;
447 (void) snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
448 iamrecursive ? " -r" : "", pflag ? " -p" : "",
449 argv[i], argv[argc - 1]);
451 fprintf(stderr, "Executing: %s\n", bp);
460 if ((host = strchr(argv[i], '@')) == NULL) {
467 suser = pwd->pw_name;
468 else if (!okname(suser))
471 host = cleanhostname(host);
472 len = strlen(src) + CMDNEEDS + 20;
474 (void) snprintf(bp, len, "%s -f %s", cmd, src);
475 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
481 sink(1, argv + argc - 1);
495 off_t i, amt, result;
496 int fd, haderr, indx;
497 char *last, *name, buf[2048];
500 for (indx = 0; indx < argc; ++indx) {
504 while (len > 1 && name[len-1] == '/')
506 if (strchr(name, '\n') != NULL) {
507 run_err("%s: skipping, filename contains a newline",
511 if ((fd = open(name, O_RDONLY, 0)) < 0)
513 if (fstat(fd, &stb) < 0) {
514 syserr: run_err("%s: %s", name, strerror(errno));
517 switch (stb.st_mode & S_IFMT) {
527 run_err("%s: not a regular file", name);
530 if ((last = strrchr(name, '/')) == NULL)
537 * Make it compatible with possible future
538 * versions expecting microseconds.
540 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
541 (u_long) stb.st_mtime,
542 (u_long) stb.st_atime);
543 (void) atomicio(write, remout, buf, strlen(buf));
547 #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
548 #ifdef HAVE_LONG_LONG_INT
549 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
550 (u_int) (stb.st_mode & FILEMODEMASK),
551 (long long) stb.st_size, last);
553 /* XXX: Handle integer overflow? */
554 snprintf(buf, sizeof buf, "C%04o %lu %s\n",
555 (u_int) (stb.st_mode & FILEMODEMASK),
556 (u_long) stb.st_size, last);
560 fprintf(stderr, "Sending file modes: %s", buf);
563 (void) atomicio(write, remout, buf, strlen(buf));
566 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
567 next: (void) close(fd);
571 totalbytes = stb.st_size;
574 /* Keep writing after an error so that we stay sync'd up. */
575 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
577 if (i + amt > stb.st_size)
578 amt = stb.st_size - i;
580 result = atomicio(read, fd, bp->buf, amt);
582 haderr = result >= 0 ? EIO : errno;
585 (void) atomicio(write, remout, bp->buf, amt);
587 result = atomicio(write, remout, bp->buf, amt);
589 haderr = result >= 0 ? EIO : errno;
596 if (close(fd) < 0 && !haderr)
599 (void) atomicio(write, remout, "", 1);
601 run_err("%s: %s", name, strerror(haderr));
613 char *last, *vect[1], path[1100];
615 if (!(dirp = opendir(name))) {
616 run_err("%s: %s", name, strerror(errno));
619 last = strrchr(name, '/');
625 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
626 (u_long) statp->st_mtime,
627 (u_long) statp->st_atime);
628 (void) atomicio(write, remout, path, strlen(path));
629 if (response() < 0) {
634 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
635 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
637 fprintf(stderr, "Entering directory: %s", path);
638 (void) atomicio(write, remout, path, strlen(path));
639 if (response() < 0) {
643 while ((dp = readdir(dirp)) != NULL) {
646 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
648 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
649 run_err("%s/%s: name too long", name, dp->d_name);
652 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
656 (void) closedir(dirp);
657 (void) atomicio(write, remout, "E\n", 2);
673 int amt, count, exists, first, mask, mode, ofd, omode;
675 int setimes, targisdir, wrerrno = 0;
676 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
677 struct timeval tv[2];
681 #define SCREWUP(str) do { why = str; goto screwup; } while (0)
683 setimes = targisdir = 0;
688 run_err("ambiguous target");
692 if (targetshouldbedirectory)
695 (void) atomicio(write, remout, "", 1);
696 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
698 for (first = 1;; first = 0) {
700 if (atomicio(read, remin, cp, 1) <= 0)
703 SCREWUP("unexpected <newline>");
705 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
706 SCREWUP("lost connection");
708 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
711 if (buf[0] == '\01' || buf[0] == '\02') {
713 (void) atomicio(write, STDERR_FILENO,
714 buf + 1, strlen(buf + 1));
721 (void) atomicio(write, remout, "", 1);
731 mtime.tv_sec = strtol(cp, &cp, 10);
732 if (!cp || *cp++ != ' ')
733 SCREWUP("mtime.sec not delimited");
734 mtime.tv_usec = strtol(cp, &cp, 10);
735 if (!cp || *cp++ != ' ')
736 SCREWUP("mtime.usec not delimited");
737 atime.tv_sec = strtol(cp, &cp, 10);
738 if (!cp || *cp++ != ' ')
739 SCREWUP("atime.sec not delimited");
740 atime.tv_usec = strtol(cp, &cp, 10);
741 if (!cp || *cp++ != '\0')
742 SCREWUP("atime.usec not delimited");
743 (void) atomicio(write, remout, "", 1);
746 if (*cp != 'C' && *cp != 'D') {
748 * Check for the case "rcp remote:foo\* local:bar".
749 * In this case, the line "No match." can be returned
750 * by the shell before the rcp command on the remote is
751 * executed so the ^Aerror_message convention isn't
758 SCREWUP("expected control record");
761 for (++cp; cp < buf + 5; cp++) {
762 if (*cp < '0' || *cp > '7')
764 mode = (mode << 3) | (*cp - '0');
767 SCREWUP("mode not delimited");
769 for (size = 0; isdigit(*cp);)
770 size = size * 10 + (*cp++ - '0');
772 SCREWUP("size not delimited");
774 static char *namebuf;
778 need = strlen(targ) + strlen(cp) + 250;
779 if (need > cursize) {
782 namebuf = xmalloc(need);
785 (void) snprintf(namebuf, need, "%s%s%s", targ,
786 *targ ? "/" : "", cp);
791 exists = stat(np, &stb) == 0;
793 int mod_flag = pflag;
795 if (!S_ISDIR(stb.st_mode)) {
800 (void) chmod(np, mode);
802 /* Handle copying from a read-only
805 if (mkdir(np, mode | S_IRWXU) < 0)
808 vect[0] = xstrdup(np);
812 if (utimes(vect[0], tv) < 0)
813 run_err("%s: set times: %s",
814 vect[0], strerror(errno));
817 (void) chmod(vect[0], mode);
824 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
825 bad: run_err("%s: %s", np, strerror(errno));
828 (void) atomicio(write, remout, "", 1);
829 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
841 for (count = i = 0; i < size; i += 4096) {
847 j = read(remin, cp, amt);
848 if (j == -1 && (errno == EINTR ||
852 run_err("%s", j ? strerror(errno) :
853 "dropped connection");
860 if (count == bp->cnt) {
861 /* Keep reading so we stay sync'd up. */
863 j = atomicio(write, ofd, bp->buf, count);
866 wrerrno = j >= 0 ? EIO : errno;
875 if (count != 0 && wrerr == NO &&
876 (j = atomicio(write, ofd, bp->buf, count)) != count) {
878 wrerrno = j >= 0 ? EIO : errno;
880 if (ftruncate(ofd, size)) {
881 run_err("%s: truncate: %s", np, strerror(errno));
885 if (exists || omode != mode)
887 if (fchmod(ofd, omode))
888 #else /* HAVE_FCHMOD */
889 if (chmod(np, omode))
890 #endif /* HAVE_FCHMOD */
891 run_err("%s: set mode: %s",
892 np, strerror(errno));
894 if (!exists && omode != mode)
896 if (fchmod(ofd, omode & ~mask))
897 #else /* HAVE_FCHMOD */
898 if (chmod(np, omode & ~mask))
899 #endif /* HAVE_FCHMOD */
900 run_err("%s: set mode: %s",
901 np, strerror(errno));
903 if (close(ofd) == -1) {
908 if (setimes && wrerr == NO) {
910 if (utimes(np, tv) < 0) {
911 run_err("%s: set times: %s",
912 np, strerror(errno));
918 run_err("%s: %s", np, strerror(wrerrno));
921 (void) atomicio(write, remout, "", 1);
928 run_err("protocol error: %s", why);
935 char ch, *cp, resp, rbuf[2048];
937 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
947 case 1: /* error, followed by error msg */
948 case 2: /* fatal error, "" */
950 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
953 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
956 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
968 (void) fprintf(stderr,
969 "usage: scp [-pqrvBC46] [-F config] [-S ssh] [-P port] [-c cipher] [-i identity]\n"
970 " [-o option] f1 f2\n"
971 " or: scp [options] f1 ... fn directory\n");
976 run_err(const char *fmt,...)
982 if (fp == NULL && !(fp = fdopen(remout, "w")))
984 (void) fprintf(fp, "%c", 0x01);
985 (void) fprintf(fp, "scp: ");
987 (void) vfprintf(fp, fmt, ap);
989 (void) fprintf(fp, "\n");
994 vfprintf(stderr, fmt, ap);
996 fprintf(stderr, "\n");
1006 if (!stat(cp, &stb)) {
1007 if (S_ISDIR(stb.st_mode))
1011 run_err("%s: %s", cp, strerror(errno));
1027 if (!isalpha(c) && !isdigit(c) &&
1028 c != '_' && c != '-' && c != '.' && c != '+')
1033 bad: fprintf(stderr, "%s: invalid user name\n", cp0);
1038 allocbuf(bp, fd, blksize)
1043 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1046 if (fstat(fd, &stb) < 0) {
1047 run_err("fstat: %s", strerror(errno));
1050 if (stb.st_blksize == 0)
1053 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
1055 #else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1057 #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1058 if (bp->cnt >= size)
1060 if (bp->buf == NULL)
1061 bp->buf = xmalloc(size);
1063 bp->buf = xrealloc(bp->buf, size);
1064 memset(bp->buf, 0, size);
1074 write(STDERR_FILENO, "lost connection\n", 16);
1082 updateprogressmeter(int ignore)
1084 int save_errno = errno;
1087 signal(SIGALRM, updateprogressmeter);
1088 alarm(PROGRESSTIME);
1093 foregroundproc(void)
1095 static pid_t pgrp = -1;
1101 #ifdef HAVE_TCGETPGRP
1102 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1105 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1106 ctty_pgrp == pgrp));
1111 progressmeter(int flag)
1113 static const char prefixes[] = " KMGTP";
1114 static struct timeval lastupdate;
1115 static off_t lastsize;
1116 struct timeval now, td, wait;
1117 off_t cursize, abbrevsize;
1119 int ratio, barlength, i, remaining;
1123 (void) gettimeofday(&start, (struct timezone *) 0);
1127 if (foregroundproc() == 0)
1130 (void) gettimeofday(&now, (struct timezone *) 0);
1131 cursize = statbytes;
1132 if (totalbytes != 0) {
1133 ratio = 100.0 * cursize / totalbytes;
1134 ratio = MAX(ratio, 0);
1135 ratio = MIN(ratio, 100);
1139 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
1141 barlength = getttywidth() - 51;
1142 barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH;
1143 if (barlength > 0) {
1144 i = barlength * ratio / 100;
1145 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1146 "|%.*s%*s|", i, BAR, barlength - i, "");
1149 abbrevsize = cursize;
1150 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1154 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5lu %c%c ",
1155 (unsigned long) abbrevsize, prefixes[i],
1156 prefixes[i] == ' ' ? ' ' : 'B');
1158 timersub(&now, &lastupdate, &wait);
1159 if (cursize > lastsize) {
1162 if (wait.tv_sec >= STALLTIME) {
1163 start.tv_sec += wait.tv_sec;
1164 start.tv_usec += wait.tv_usec;
1168 timersub(&now, &start, &td);
1169 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1172 (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
1173 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1175 } else if (wait.tv_sec >= STALLTIME) {
1176 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1180 remaining = (int)(totalbytes / (statbytes / elapsed) -
1183 remaining = elapsed;
1185 i = remaining / 3600;
1187 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1190 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1192 i = remaining % 3600;
1193 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1194 "%02d:%02d%s", i / 60, i % 60,
1195 (flag != 1) ? " ETA" : " ");
1197 atomicio(write, fileno(stdout), buf, strlen(buf));
1200 mysignal(SIGALRM, updateprogressmeter);
1201 alarm(PROGRESSTIME);
1202 } else if (flag == 1) {
1204 atomicio(write, fileno(stdout), "\n", 1);
1212 struct winsize winsize;
1214 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
1215 return (winsize.ws_col ? winsize.ws_col : 80);