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.94 2002/11/27 17:53:35 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 /* Visual statistics about files as they are transferred. */
98 void progressmeter(int);
100 /* Returns width of the terminal (for progress meter calculations). */
101 int getttywidth(void);
102 int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc);
104 /* Struct for addargs */
107 /* Time a transfer started. */
108 static struct timeval start;
110 /* Number of bytes of current file transferred so far. */
111 volatile off_t statbytes;
113 /* Total size of current file. */
114 off_t totalbytes = 0;
116 /* Name of current file being transferred. */
119 /* This is set to non-zero to enable verbose mode. */
120 int verbose_mode = 0;
122 /* This is set to zero if the progressmeter is not desired. */
123 int showprogress = 1;
125 /* This is the program to execute for the secured connection. ("ssh" or -S) */
126 char *ssh_program = _PATH_SSH_PROGRAM;
128 /* This is used to store the pid of ssh_program */
132 * This function executes the given command as the specified user on the
133 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
134 * assigns the input and output file descriptors on success.
138 do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
140 int pin[2], pout[2], reserved[2];
144 "Executing: program %s host %s, user %s, command %s\n",
146 remuser ? remuser : "(unspecified)", cmd);
149 * Reserve two descriptors so that the real pipes won't get
150 * descriptors 0 and 1 because that will screw up dup2 below.
154 /* Create a socket pair for communicating with ssh. */
156 fatal("pipe: %s", strerror(errno));
158 fatal("pipe: %s", strerror(errno));
160 /* Free the reserved descriptors. */
164 /* For a child to execute the command on the remote host using ssh. */
166 if (do_cmd_pid == 0) {
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);
184 } else if (do_cmd_pid == -1) {
185 fatal("fork: %s", strerror(errno));
187 /* Parent. Close the other side, and return the local side. */
200 BUF *allocbuf(BUF *, int, int);
204 void run_err(const char *,...);
205 void verifydir(char *);
209 int errs, remin, remout;
210 int pflag, iamremote, iamrecursive, targetshouldbedirectory;
213 char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
216 void rsource(char *, struct stat *);
217 void sink(int, char *[]);
218 void source(int, char *[]);
219 void tolocal(int, char *[]);
220 void toremote(char *, int, char *[]);
228 int ch, fflag, tflag, status;
233 __progname = get_progname(argv[0]);
236 addargs(&args, "ssh"); /* overwritten with ssh_program */
237 addargs(&args, "-x");
238 addargs(&args, "-oForwardAgent no");
239 addargs(&args, "-oClearAllForwardings yes");
242 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:F:")) != -1)
244 /* User-visible flags. */
248 addargs(&args, "-%c", ch);
254 addargs(&args, "-%c%s", ch, optarg);
257 addargs(&args, "-p%s", optarg);
260 addargs(&args, "-oBatchmode yes");
269 ssh_program = xstrdup(optarg);
272 addargs(&args, "-v");
279 /* Server options. */
281 targetshouldbedirectory = 1;
283 case 'f': /* "from" */
291 setmode(0, O_BINARY);
300 if ((pwd = getpwuid(userid = getuid())) == NULL)
301 fatal("unknown user %d", (int) userid);
303 if (!isatty(STDERR_FILENO))
306 remin = STDIN_FILENO;
307 remout = STDOUT_FILENO;
310 /* Follow "protocol", send data. */
323 targetshouldbedirectory = 1;
327 /* Command to be executed on remote system using "ssh". */
328 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
329 verbose_mode ? " -v" : "",
330 iamrecursive ? " -r" : "", pflag ? " -p" : "",
331 targetshouldbedirectory ? " -d" : "");
333 (void) signal(SIGPIPE, lostconn);
335 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
336 toremote(targ, argc, argv);
338 tolocal(argc, argv); /* Dest is local host. */
339 if (targetshouldbedirectory)
340 verifydir(argv[argc - 1]);
343 * Finally check the exit status of the ssh process, if one was forked
344 * and no error has occured yet
346 if (do_cmd_pid != -1 && errs == 0) {
350 (void) close(remout);
351 if (waitpid(do_cmd_pid, &status, 0) == -1)
354 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
362 toremote(targ, argc, argv)
367 char *bp, *host, *src, *suser, *thost, *tuser;
373 if ((thost = strrchr(argv[argc - 1], '@'))) {
376 tuser = argv[argc - 1];
379 else if (!okname(tuser))
382 thost = argv[argc - 1];
386 for (i = 0; i < argc - 1; i++) {
387 src = colon(argv[i]);
388 if (src) { /* remote to remote */
389 static char *ssh_options =
390 "-x -o'ClearAllForwardings yes'";
394 host = strrchr(argv[i], '@');
395 len = strlen(ssh_program) + strlen(argv[i]) +
396 strlen(src) + (tuser ? strlen(tuser) : 0) +
397 strlen(thost) + strlen(targ) +
398 strlen(ssh_options) + CMDNEEDS + 20;
402 host = cleanhostname(host);
405 suser = pwd->pw_name;
406 else if (!okname(suser))
410 "-l %s %s %s %s '%s%s%s:%s'",
411 ssh_program, verbose_mode ? " -v" : "",
412 ssh_options, suser, host, cmd, src,
413 tuser ? tuser : "", tuser ? "@" : "",
416 host = cleanhostname(argv[i]);
418 "exec %s%s %s -n %s "
420 ssh_program, verbose_mode ? " -v" : "",
421 ssh_options, host, cmd, src,
422 tuser ? tuser : "", tuser ? "@" : "",
426 fprintf(stderr, "Executing: %s\n", bp);
429 } else { /* local to remote */
431 len = strlen(targ) + CMDNEEDS + 20;
433 (void) snprintf(bp, len, "%s -t %s", cmd, targ);
434 host = cleanhostname(thost);
435 if (do_cmd(host, tuser, bp, &remin,
453 char *bp, *host, *src, *suser;
455 for (i = 0; i < argc - 1; i++) {
456 if (!(src = colon(argv[i]))) { /* Local to local. */
457 len = strlen(_PATH_CP) + strlen(argv[i]) +
458 strlen(argv[argc - 1]) + 20;
460 (void) snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
461 iamrecursive ? " -r" : "", pflag ? " -p" : "",
462 argv[i], argv[argc - 1]);
464 fprintf(stderr, "Executing: %s\n", bp);
473 if ((host = strrchr(argv[i], '@')) == NULL) {
480 suser = pwd->pw_name;
481 else if (!okname(suser))
484 host = cleanhostname(host);
485 len = strlen(src) + CMDNEEDS + 20;
487 (void) snprintf(bp, len, "%s -f %s", cmd, src);
488 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
494 sink(1, argv + argc - 1);
508 off_t i, amt, result;
509 int fd, haderr, indx;
510 char *last, *name, buf[2048];
513 for (indx = 0; indx < argc; ++indx) {
517 while (len > 1 && name[len-1] == '/')
519 if (strchr(name, '\n') != NULL) {
520 run_err("%s: skipping, filename contains a newline",
524 if ((fd = open(name, O_RDONLY, 0)) < 0)
526 if (fstat(fd, &stb) < 0) {
527 syserr: run_err("%s: %s", name, strerror(errno));
530 switch (stb.st_mode & S_IFMT) {
540 run_err("%s: not a regular file", name);
543 if ((last = strrchr(name, '/')) == NULL)
550 * Make it compatible with possible future
551 * versions expecting microseconds.
553 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
554 (u_long) stb.st_mtime,
555 (u_long) stb.st_atime);
556 (void) atomicio(write, remout, buf, strlen(buf));
560 #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
561 #ifdef HAVE_LONG_LONG_INT
562 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
563 (u_int) (stb.st_mode & FILEMODEMASK),
564 (long long)stb.st_size, last);
566 /* XXX: Handle integer overflow? */
567 snprintf(buf, sizeof buf, "C%04o %lu %s\n",
568 (u_int) (stb.st_mode & FILEMODEMASK),
569 (u_long) stb.st_size, last);
572 fprintf(stderr, "Sending file modes: %s", buf);
574 (void) atomicio(write, remout, buf, strlen(buf));
577 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
578 next: (void) close(fd);
582 totalbytes = stb.st_size;
585 /* Keep writing after an error so that we stay sync'd up. */
586 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
588 if (i + amt > stb.st_size)
589 amt = stb.st_size - i;
591 result = atomicio(read, fd, bp->buf, amt);
593 haderr = result >= 0 ? EIO : errno;
596 (void) atomicio(write, remout, bp->buf, amt);
598 result = atomicio(write, remout, bp->buf, amt);
600 haderr = result >= 0 ? EIO : errno;
607 if (close(fd) < 0 && !haderr)
610 (void) atomicio(write, remout, "", 1);
612 run_err("%s: %s", name, strerror(haderr));
624 char *last, *vect[1], path[1100];
626 if (!(dirp = opendir(name))) {
627 run_err("%s: %s", name, strerror(errno));
630 last = strrchr(name, '/');
636 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
637 (u_long) statp->st_mtime,
638 (u_long) statp->st_atime);
639 (void) atomicio(write, remout, path, strlen(path));
640 if (response() < 0) {
645 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
646 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
648 fprintf(stderr, "Entering directory: %s", path);
649 (void) atomicio(write, remout, path, strlen(path));
650 if (response() < 0) {
654 while ((dp = readdir(dirp)) != NULL) {
657 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
659 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
660 run_err("%s/%s: name too long", name, dp->d_name);
663 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
667 (void) closedir(dirp);
668 (void) atomicio(write, remout, "E\n", 2);
684 int amt, count, exists, first, mask, mode, ofd, omode;
686 int setimes, targisdir, wrerrno = 0;
687 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
688 struct timeval tv[2];
692 #define SCREWUP(str) do { why = str; goto screwup; } while (0)
694 setimes = targisdir = 0;
699 run_err("ambiguous target");
703 if (targetshouldbedirectory)
706 (void) atomicio(write, remout, "", 1);
707 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
709 for (first = 1;; first = 0) {
711 if (atomicio(read, remin, cp, 1) <= 0)
714 SCREWUP("unexpected <newline>");
716 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
717 SCREWUP("lost connection");
719 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
722 if (buf[0] == '\01' || buf[0] == '\02') {
724 (void) atomicio(write, STDERR_FILENO,
725 buf + 1, strlen(buf + 1));
732 (void) atomicio(write, remout, "", 1);
742 mtime.tv_sec = strtol(cp, &cp, 10);
743 if (!cp || *cp++ != ' ')
744 SCREWUP("mtime.sec not delimited");
745 mtime.tv_usec = strtol(cp, &cp, 10);
746 if (!cp || *cp++ != ' ')
747 SCREWUP("mtime.usec not delimited");
748 atime.tv_sec = strtol(cp, &cp, 10);
749 if (!cp || *cp++ != ' ')
750 SCREWUP("atime.sec not delimited");
751 atime.tv_usec = strtol(cp, &cp, 10);
752 if (!cp || *cp++ != '\0')
753 SCREWUP("atime.usec not delimited");
754 (void) atomicio(write, remout, "", 1);
757 if (*cp != 'C' && *cp != 'D') {
759 * Check for the case "rcp remote:foo\* local:bar".
760 * In this case, the line "No match." can be returned
761 * by the shell before the rcp command on the remote is
762 * executed so the ^Aerror_message convention isn't
769 SCREWUP("expected control record");
772 for (++cp; cp < buf + 5; cp++) {
773 if (*cp < '0' || *cp > '7')
775 mode = (mode << 3) | (*cp - '0');
778 SCREWUP("mode not delimited");
780 for (size = 0; isdigit(*cp);)
781 size = size * 10 + (*cp++ - '0');
783 SCREWUP("size not delimited");
785 static char *namebuf;
789 need = strlen(targ) + strlen(cp) + 250;
790 if (need > cursize) {
793 namebuf = xmalloc(need);
796 (void) snprintf(namebuf, need, "%s%s%s", targ,
797 strcmp(targ, "/") ? "/" : "", cp);
802 exists = stat(np, &stb) == 0;
804 int mod_flag = pflag;
806 if (!S_ISDIR(stb.st_mode)) {
811 (void) chmod(np, mode);
813 /* Handle copying from a read-only
816 if (mkdir(np, mode | S_IRWXU) < 0)
819 vect[0] = xstrdup(np);
823 if (utimes(vect[0], tv) < 0)
824 run_err("%s: set times: %s",
825 vect[0], strerror(errno));
828 (void) chmod(vect[0], mode);
835 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
836 bad: run_err("%s: %s", np, strerror(errno));
839 (void) atomicio(write, remout, "", 1);
840 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
852 for (count = i = 0; i < size; i += 4096) {
858 j = read(remin, cp, amt);
859 if (j == -1 && (errno == EINTR ||
863 run_err("%s", j ? strerror(errno) :
864 "dropped connection");
871 if (count == bp->cnt) {
872 /* Keep reading so we stay sync'd up. */
874 j = atomicio(write, ofd, bp->buf, count);
877 wrerrno = j >= 0 ? EIO : errno;
886 if (count != 0 && wrerr == NO &&
887 (j = atomicio(write, ofd, bp->buf, count)) != count) {
889 wrerrno = j >= 0 ? EIO : errno;
891 if (ftruncate(ofd, size)) {
892 run_err("%s: truncate: %s", np, strerror(errno));
896 if (exists || omode != mode)
898 if (fchmod(ofd, omode))
899 #else /* HAVE_FCHMOD */
900 if (chmod(np, omode))
901 #endif /* HAVE_FCHMOD */
902 run_err("%s: set mode: %s",
903 np, strerror(errno));
905 if (!exists && omode != mode)
907 if (fchmod(ofd, omode & ~mask))
908 #else /* HAVE_FCHMOD */
909 if (chmod(np, omode & ~mask))
910 #endif /* HAVE_FCHMOD */
911 run_err("%s: set mode: %s",
912 np, strerror(errno));
914 if (close(ofd) == -1) {
919 if (setimes && wrerr == NO) {
921 if (utimes(np, tv) < 0) {
922 run_err("%s: set times: %s",
923 np, strerror(errno));
929 run_err("%s: %s", np, strerror(wrerrno));
932 (void) atomicio(write, remout, "", 1);
939 run_err("protocol error: %s", why);
946 char ch, *cp, resp, rbuf[2048];
948 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
958 case 1: /* error, followed by error msg */
959 case 2: /* fatal error, "" */
961 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
964 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
967 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
979 (void) fprintf(stderr,
980 "usage: scp [-pqrvBC46] [-F config] [-S program] [-P port]\n"
981 " [-c cipher] [-i identity] [-o option]\n"
982 " [[user@]host1:]file1 [...] [[user@]host2:]file2\n");
987 run_err(const char *fmt,...)
993 if (fp == NULL && !(fp = fdopen(remout, "w")))
995 (void) fprintf(fp, "%c", 0x01);
996 (void) fprintf(fp, "scp: ");
998 (void) vfprintf(fp, fmt, ap);
1000 (void) fprintf(fp, "\n");
1005 vfprintf(stderr, fmt, ap);
1007 fprintf(stderr, "\n");
1017 if (!stat(cp, &stb)) {
1018 if (S_ISDIR(stb.st_mode))
1022 run_err("%s: %s", cp, strerror(errno));
1038 if (!isalpha(c) && !isdigit(c) &&
1039 c != '@' && c != '_' && c != '-' && c != '.' && c != '+')
1044 bad: fprintf(stderr, "%s: invalid user name\n", cp0);
1049 allocbuf(bp, fd, blksize)
1054 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1057 if (fstat(fd, &stb) < 0) {
1058 run_err("fstat: %s", strerror(errno));
1061 if (stb.st_blksize == 0)
1064 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
1066 #else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1068 #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1069 if (bp->cnt >= size)
1071 if (bp->buf == NULL)
1072 bp->buf = xmalloc(size);
1074 bp->buf = xrealloc(bp->buf, size);
1075 memset(bp->buf, 0, size);
1085 write(STDERR_FILENO, "lost connection\n", 16);
1093 updateprogressmeter(int ignore)
1095 int save_errno = errno;
1098 signal(SIGALRM, updateprogressmeter);
1099 alarm(PROGRESSTIME);
1104 foregroundproc(void)
1106 static pid_t pgrp = -1;
1112 #ifdef HAVE_TCGETPGRP
1113 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1116 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1117 ctty_pgrp == pgrp));
1122 progressmeter(int flag)
1124 static const char prefixes[] = " KMGTP";
1125 static struct timeval lastupdate;
1126 static off_t lastsize;
1127 struct timeval now, td, wait;
1128 off_t cursize, abbrevsize;
1130 int ratio, barlength, i, remaining;
1134 (void) gettimeofday(&start, (struct timezone *) 0);
1138 if (foregroundproc() == 0)
1141 (void) gettimeofday(&now, (struct timezone *) 0);
1142 cursize = statbytes;
1143 if (totalbytes != 0) {
1144 ratio = 100.0 * cursize / totalbytes;
1145 ratio = MAX(ratio, 0);
1146 ratio = MIN(ratio, 100);
1150 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
1152 barlength = getttywidth() - 51;
1153 if (barlength > 0) {
1154 i = barlength * ratio / 100;
1155 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1157 "*******************************************************"
1158 "*******************************************************"
1159 "*******************************************************"
1160 "*******************************************************"
1161 "*******************************************************"
1162 "*******************************************************"
1163 "*******************************************************",
1167 abbrevsize = cursize;
1168 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1172 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5lu %c%c ",
1173 (unsigned long) abbrevsize, prefixes[i],
1174 prefixes[i] == ' ' ? ' ' : 'B');
1176 timersub(&now, &lastupdate, &wait);
1177 if (cursize > lastsize) {
1180 if (wait.tv_sec >= STALLTIME) {
1181 start.tv_sec += wait.tv_sec;
1182 start.tv_usec += wait.tv_usec;
1186 timersub(&now, &start, &td);
1187 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1190 (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
1191 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1193 } else if (wait.tv_sec >= STALLTIME) {
1194 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1198 remaining = (int)(totalbytes / (statbytes / elapsed) -
1201 remaining = elapsed;
1203 i = remaining / 3600;
1205 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1208 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1210 i = remaining % 3600;
1211 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1212 "%02d:%02d%s", i / 60, i % 60,
1213 (flag != 1) ? " ETA" : " ");
1215 atomicio(write, fileno(stdout), buf, strlen(buf));
1218 mysignal(SIGALRM, updateprogressmeter);
1219 alarm(PROGRESSTIME);
1220 } else if (flag == 1) {
1222 atomicio(write, fileno(stdout), "\n", 1);
1230 struct winsize winsize;
1232 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
1233 return (winsize.ws_col ? winsize.ws_col : 80);