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.39 2000/09/07 20:53:00 markus Exp $");
86 /* For progressmeter() -- number of seconds before xfer considered "stalled" */
89 /* Progress meter bar */
91 "************************************************************"\
92 "************************************************************"\
93 "************************************************************"\
94 "************************************************************"
95 #define MAX_BARLENGTH (sizeof(BAR) - 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 /* Time a transfer started. */
105 static struct timeval start;
107 /* Number of bytes of current file transferred so far. */
108 volatile unsigned long statbytes;
110 /* Total size of current file. */
111 off_t totalbytes = 0;
113 /* Name of current file being transferred. */
116 /* This is set to non-zero if IPv4 is desired. */
119 /* This is set to non-zero if IPv6 is desired. */
122 /* This is set to non-zero to enable verbose mode. */
123 int verbose_mode = 0;
125 /* This is set to non-zero if compression is desired. */
126 int compress_flag = 0;
128 /* This is set to zero if the progressmeter is not desired. */
129 int showprogress = 1;
131 /* This is set to non-zero if running in batch mode (that is, password
132 and passphrase queries are not allowed). */
135 /* This is set to the cipher type string if given on the command line. */
138 /* This is set to the RSA authentication identity file name if given on
140 char *identity = NULL;
142 /* This is the port to use in contacting the remote site (is non-NULL). */
145 /* This is the program to execute for the secured connection. ("ssh" or -S) */
146 char *ssh_program = SSH_PROGRAM;
149 * This function executes the given command as the specified user on the
150 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
151 * assigns the input and output file descriptors on success.
155 do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
157 int pin[2], pout[2], reserved[2];
160 fprintf(stderr, "Executing: host %s, user %s, command %s\n",
161 host, remuser ? remuser : "(unspecified)", cmd);
164 * Reserve two descriptors so that the real pipes won't get
165 * descriptors 0 and 1 because that will screw up dup2 below.
169 /* Create a socket pair for communicating with ssh. */
171 fatal("pipe: %s", strerror(errno));
173 fatal("pipe: %s", strerror(errno));
175 /* Free the reserved descriptors. */
179 /* For a child to execute the command on the remote host using ssh. */
181 char *args[100]; /* XXX careful */
193 args[i++] = ssh_program;
195 args[i++] = "-oFallBackToRsh no";
205 args[i++] = "-oBatchMode yes";
206 if (cipher != NULL) {
210 if (identity != NULL) {
212 args[i++] = identity;
218 if (remuser != NULL) {
226 execvp(ssh_program, args);
230 /* Parent. Close the other side, and return the local side. */
239 fatal(const char *fmt,...)
245 vsnprintf(buf, sizeof(buf), fmt, ap);
247 fprintf(stderr, "%s\n", buf);
256 extern int iamremote;
258 BUF *allocbuf(BUF *, int, int);
263 void run_err(const char *,...);
264 void verifydir(char *);
268 int errs, remin, remout;
269 int pflag, iamremote, iamrecursive, targetshouldbedirectory;
272 char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
275 void rsource(char *, struct stat *);
276 void sink(int, char *[]);
277 void source(int, char *[]);
278 void tolocal(int, char *[]);
279 void toremote(char *, int, char *[]);
287 int ch, fflag, tflag;
293 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:")) != EOF)
295 /* User-visible flags. */
312 ssh_program = optarg;
315 /* Server options. */
317 targetshouldbedirectory = 1;
319 case 'f': /* "from" */
352 if ((pwd = getpwuid(userid = getuid())) == NULL)
353 fatal("unknown user %d", (int) userid);
355 if (!isatty(STDERR_FILENO))
358 remin = STDIN_FILENO;
359 remout = STDOUT_FILENO;
362 /* Follow "protocol", send data. */
375 targetshouldbedirectory = 1;
378 /* Command to be executed on remote system using "ssh". */
379 (void) sprintf(cmd, "scp%s%s%s%s", verbose_mode ? " -v" : "",
380 iamrecursive ? " -r" : "", pflag ? " -p" : "",
381 targetshouldbedirectory ? " -d" : "");
383 (void) signal(SIGPIPE, lostconn);
385 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
386 toremote(targ, argc, argv);
388 tolocal(argc, argv); /* Dest is local host. */
389 if (targetshouldbedirectory)
390 verifydir(argv[argc - 1]);
399 if (*host == '[' && host[strlen(host) - 1] == ']') {
400 host[strlen(host) - 1] = '\0';
407 toremote(targ, argc, argv)
412 char *bp, *host, *src, *suser, *thost, *tuser;
418 if ((thost = strchr(argv[argc - 1], '@'))) {
421 tuser = argv[argc - 1];
424 else if (!okname(tuser))
427 thost = argv[argc - 1];
431 for (i = 0; i < argc - 1; i++) {
432 src = colon(argv[i]);
433 if (src) { /* remote to remote */
437 host = strchr(argv[i], '@');
438 len = strlen(ssh_program) + strlen(argv[i]) +
439 strlen(src) + (tuser ? strlen(tuser) : 0) +
440 strlen(thost) + strlen(targ) + CMDNEEDS + 32;
444 host = cleanhostname(host);
447 suser = pwd->pw_name;
448 else if (!okname(suser))
451 "%s%s -x -o'FallBackToRsh no' -n -l %s %s %s %s '%s%s%s:%s'",
452 ssh_program, verbose_mode ? " -v" : "",
453 suser, host, cmd, src,
454 tuser ? tuser : "", tuser ? "@" : "",
457 host = cleanhostname(argv[i]);
459 "exec %s%s -x -o'FallBackToRsh no' -n %s %s %s '%s%s%s:%s'",
460 ssh_program, verbose_mode ? " -v" : "",
462 tuser ? tuser : "", tuser ? "@" : "",
466 fprintf(stderr, "Executing: %s\n", bp);
469 } else { /* local to remote */
471 len = strlen(targ) + CMDNEEDS + 20;
473 (void) sprintf(bp, "%s -t %s", cmd, targ);
474 host = cleanhostname(thost);
475 if (do_cmd(host, tuser, bp, &remin,
493 char *bp, *host, *src, *suser;
495 for (i = 0; i < argc - 1; i++) {
496 if (!(src = colon(argv[i]))) { /* Local to local. */
497 len = strlen(_PATH_CP) + strlen(argv[i]) +
498 strlen(argv[argc - 1]) + 20;
500 (void) sprintf(bp, "exec %s%s%s %s %s", _PATH_CP,
501 iamrecursive ? " -r" : "", pflag ? " -p" : "",
502 argv[i], argv[argc - 1]);
504 fprintf(stderr, "Executing: %s\n", bp);
513 if ((host = strchr(argv[i], '@')) == NULL) {
520 suser = pwd->pw_name;
521 else if (!okname(suser))
524 host = cleanhostname(host);
525 len = strlen(src) + CMDNEEDS + 20;
527 (void) sprintf(bp, "%s -f %s", cmd, src);
528 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
534 sink(1, argv + argc - 1);
549 int amt, fd, haderr, indx, result;
550 char *last, *name, buf[2048];
552 for (indx = 0; indx < argc; ++indx) {
555 if ((fd = open(name, O_RDONLY, 0)) < 0)
557 if (fstat(fd, &stb) < 0) {
558 syserr: run_err("%s: %s", name, strerror(errno));
561 switch (stb.st_mode & S_IFMT) {
571 run_err("%s: not a regular file", name);
574 if ((last = strrchr(name, '/')) == NULL)
581 * Make it compatible with possible future
582 * versions expecting microseconds.
584 (void) sprintf(buf, "T%lu 0 %lu 0\n",
585 (unsigned long) stb.st_mtime,
586 (unsigned long) stb.st_atime);
587 (void) atomicio(write, remout, buf, strlen(buf));
591 #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
592 (void) sprintf(buf, "C%04o %lu %s\n",
593 (unsigned int) (stb.st_mode & FILEMODEMASK),
594 (unsigned long) stb.st_size,
597 fprintf(stderr, "Sending file modes: %s", buf);
600 (void) atomicio(write, remout, buf, strlen(buf));
603 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
604 next: (void) close(fd);
608 totalbytes = stb.st_size;
611 /* Keep writing after an error so that we stay sync'd up. */
612 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
614 if (i + amt > stb.st_size)
615 amt = stb.st_size - i;
617 result = atomicio(read, fd, bp->buf, amt);
619 haderr = result >= 0 ? EIO : errno;
622 (void) atomicio(write, remout, bp->buf, amt);
624 result = atomicio(write, remout, bp->buf, amt);
626 haderr = result >= 0 ? EIO : errno;
633 if (close(fd) < 0 && !haderr)
636 (void) atomicio(write, remout, "", 1);
638 run_err("%s: %s", name, strerror(haderr));
650 char *last, *vect[1], path[1100];
652 if (!(dirp = opendir(name))) {
653 run_err("%s: %s", name, strerror(errno));
656 last = strrchr(name, '/');
662 (void) sprintf(path, "T%lu 0 %lu 0\n",
663 (unsigned long) statp->st_mtime,
664 (unsigned long) statp->st_atime);
665 (void) atomicio(write, remout, path, strlen(path));
666 if (response() < 0) {
671 (void) sprintf(path, "D%04o %d %.1024s\n",
672 (unsigned int) (statp->st_mode & FILEMODEMASK), 0, last);
674 fprintf(stderr, "Entering directory: %s", path);
675 (void) atomicio(write, remout, path, strlen(path));
676 if (response() < 0) {
680 while ((dp = readdir(dirp))) {
683 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
685 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
686 run_err("%s/%s: name too long", name, dp->d_name);
689 (void) sprintf(path, "%s/%s", name, dp->d_name);
693 (void) closedir(dirp);
694 (void) atomicio(write, remout, "E\n", 2);
710 int amt, count, exists, first, mask, mode, ofd, omode;
712 int setimes, targisdir, wrerrno = 0;
713 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
717 #define SCREWUP(str) { why = str; goto screwup; }
719 setimes = targisdir = 0;
724 run_err("ambiguous target");
728 if (targetshouldbedirectory)
731 (void) atomicio(write, remout, "", 1);
732 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
734 for (first = 1;; first = 0) {
736 if (atomicio(read, remin, cp, 1) <= 0)
739 SCREWUP("unexpected <newline>");
741 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
742 SCREWUP("lost connection");
744 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
747 if (buf[0] == '\01' || buf[0] == '\02') {
749 (void) atomicio(write, STDERR_FILENO,
750 buf + 1, strlen(buf + 1));
757 (void) atomicio(write, remout, "", 1);
763 #define getnum(t) (t) = 0; \
764 while (*cp >= '0' && *cp <= '9') (t) = (t) * 10 + (*cp++ - '0');
771 SCREWUP("mtime.sec not delimited");
774 SCREWUP("mtime.usec not delimited");
777 SCREWUP("atime.sec not delimited");
780 SCREWUP("atime.usec not delimited");
781 (void) atomicio(write, remout, "", 1);
784 if (*cp != 'C' && *cp != 'D') {
786 * Check for the case "rcp remote:foo\* local:bar".
787 * In this case, the line "No match." can be returned
788 * by the shell before the rcp command on the remote is
789 * executed so the ^Aerror_message convention isn't
796 SCREWUP("expected control record");
799 for (++cp; cp < buf + 5; cp++) {
800 if (*cp < '0' || *cp > '7')
802 mode = (mode << 3) | (*cp - '0');
805 SCREWUP("mode not delimited");
807 for (size = 0; *cp >= '0' && *cp <= '9';)
808 size = size * 10 + (*cp++ - '0');
810 SCREWUP("size not delimited");
812 static char *namebuf;
816 need = strlen(targ) + strlen(cp) + 250;
818 namebuf = xmalloc(need);
819 (void) sprintf(namebuf, "%s%s%s", targ,
820 *targ ? "/" : "", cp);
825 exists = stat(np, &stb) == 0;
827 int mod_flag = pflag;
829 if (!S_ISDIR(stb.st_mode)) {
834 (void) chmod(np, mode);
836 /* Handle copying from a read-only
839 if (mkdir(np, mode | S_IRWXU) < 0)
846 if (utime(np, &ut) < 0)
847 run_err("%s: set times: %s",
848 np, strerror(errno));
851 (void) chmod(np, mode);
856 if ((ofd = open(np, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
857 bad: run_err("%s: %s", np, strerror(errno));
860 (void) atomicio(write, remout, "", 1);
861 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
873 for (count = i = 0; i < size; i += 4096) {
879 j = atomicio(read, remin, cp, amt);
881 run_err("%s", j ? strerror(errno) :
882 "dropped connection");
889 if (count == bp->cnt) {
890 /* Keep reading so we stay sync'd up. */
892 j = atomicio(write, ofd, bp->buf, count);
895 wrerrno = j >= 0 ? EIO : errno;
904 if (count != 0 && wrerr == NO &&
905 (j = atomicio(write, ofd, bp->buf, count)) != count) {
907 wrerrno = j >= 0 ? EIO : errno;
910 if (ftruncate(ofd, size)) {
911 run_err("%s: truncate: %s", np, strerror(errno));
916 if (exists || omode != mode)
917 if (fchmod(ofd, omode))
918 run_err("%s: set mode: %s",
919 np, strerror(errno));
921 if (!exists && omode != mode)
922 if (fchmod(ofd, omode & ~mask))
923 run_err("%s: set mode: %s",
924 np, strerror(errno));
926 if (close(ofd) == -1) {
931 if (setimes && wrerr == NO) {
933 if (utime(np, &ut) < 0) {
934 run_err("%s: set times: %s",
935 np, strerror(errno));
941 run_err("%s: %s", np, strerror(wrerrno));
944 (void) atomicio(write, remout, "", 1);
951 run_err("protocol error: %s", why);
958 char ch, *cp, resp, rbuf[2048];
960 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
970 case 1: /* error, followed by error msg */
971 case 2: /* fatal error, "" */
973 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
976 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
979 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
991 (void) fprintf(stderr, "usage: scp "
992 "[-pqrvC46] [-S ssh] [-P port] [-c cipher] [-i identity] f1 f2; or:\n"
993 " scp [options] f1 ... fn directory\n");
998 run_err(const char *fmt,...)
1004 if (fp == NULL && !(fp = fdopen(remout, "w")))
1006 (void) fprintf(fp, "%c", 0x01);
1007 (void) fprintf(fp, "scp: ");
1009 (void) vfprintf(fp, fmt, ap);
1011 (void) fprintf(fp, "\n");
1016 vfprintf(stderr, fmt, ap);
1018 fprintf(stderr, "\n");
1028 if (*cp == ':') /* Leading colon is part of file name. */
1034 if (*cp == '@' && *(cp+1) == '[')
1036 if (*cp == ']' && *(cp+1) == ':' && flag)
1038 if (*cp == ':' && !flag)
1052 if (!stat(cp, &stb)) {
1053 if (S_ISDIR(stb.st_mode))
1057 run_err("%s: %s", cp, strerror(errno));
1073 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
1078 bad: fprintf(stderr, "%s: invalid user name\n", cp0);
1083 allocbuf(bp, fd, blksize)
1090 if (fstat(fd, &stb) < 0) {
1091 run_err("fstat: %s", strerror(errno));
1094 if (stb.st_blksize == 0)
1097 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
1099 if (bp->cnt >= size)
1101 if (bp->buf == NULL)
1102 bp->buf = xmalloc(size);
1104 bp->buf = xrealloc(bp->buf, size);
1114 fprintf(stderr, "lost connection\n");
1120 alarmtimer(int wait)
1122 struct itimerval itv;
1124 itv.it_value.tv_sec = wait;
1125 itv.it_value.tv_usec = 0;
1126 itv.it_interval = itv.it_value;
1127 setitimer(ITIMER_REAL, &itv, NULL);
1131 updateprogressmeter(int ignore)
1133 int save_errno = errno;
1142 static pid_t pgrp = -1;
1150 * Cygwin only supports tcgetpgrp() for getting the controlling tty
1153 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1156 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1157 ctty_pgrp == pgrp));
1162 progressmeter(int flag)
1164 static const char prefixes[] = " KMGTP";
1165 static struct timeval lastupdate;
1166 static off_t lastsize;
1167 struct timeval now, td, wait;
1168 off_t cursize, abbrevsize;
1170 int ratio, barlength, i, remaining;
1174 (void) gettimeofday(&start, (struct timezone *) 0);
1178 if (foregroundproc() == 0)
1181 (void) gettimeofday(&now, (struct timezone *) 0);
1182 cursize = statbytes;
1183 if (totalbytes != 0) {
1184 ratio = 100.0 * cursize / totalbytes;
1185 ratio = MAX(ratio, 0);
1186 ratio = MIN(ratio, 100);
1190 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
1192 barlength = getttywidth() - 51;
1193 barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH;
1194 if (barlength > 0) {
1195 i = barlength * ratio / 100;
1196 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1197 "|%.*s%*s|", i, BAR, barlength - i, "");
1200 abbrevsize = cursize;
1201 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1205 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5d %c%c ",
1206 (int) abbrevsize, prefixes[i], prefixes[i] == ' ' ? ' ' :
1209 timersub(&now, &lastupdate, &wait);
1210 if (cursize > lastsize) {
1213 if (wait.tv_sec >= STALLTIME) {
1214 start.tv_sec += wait.tv_sec;
1215 start.tv_usec += wait.tv_usec;
1219 timersub(&now, &start, &td);
1220 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1222 if (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes) {
1223 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1225 } else if (wait.tv_sec >= STALLTIME) {
1226 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1231 (int)(totalbytes / (statbytes / elapsed) - elapsed);
1233 remaining = elapsed;
1235 i = remaining / 3600;
1237 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1240 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1242 i = remaining % 3600;
1243 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1244 "%02d:%02d%s", i / 60, i % 60,
1245 (flag != 1) ? " ETA" : " ");
1247 atomicio(write, fileno(stdout), buf, strlen(buf));
1250 struct sigaction sa;
1251 sa.sa_handler = updateprogressmeter;
1252 sigemptyset((sigset_t *)&sa.sa_mask);
1254 sa.sa_flags = SA_RESTART;
1256 sigaction(SIGALRM, &sa, NULL);
1258 } else if (flag == 1) {
1260 atomicio(write, fileno(stdout), "\n", 1);
1268 struct winsize winsize;
1270 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
1271 return (winsize.ws_col ? winsize.ws_col : 80);