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.43 2000/10/18 18:23:02 markus Exp $");
87 #ifdef HAVE___PROGNAME
88 extern char *__progname;
93 /* For progressmeter() -- number of seconds before xfer considered "stalled" */
96 /* Progress meter bar */
98 "************************************************************"\
99 "************************************************************"\
100 "************************************************************"\
101 "************************************************************"
102 #define MAX_BARLENGTH (sizeof(BAR) - 1)
104 /* Visual statistics about files as they are transferred. */
105 void progressmeter(int);
107 /* Returns width of the terminal (for progress meter calculations). */
108 int getttywidth(void);
109 int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc);
111 /* setup arguments for the call to ssh */
112 void addargs(char *fmt, ...) __attribute__((format(printf, 1, 2)));
114 /* Time a transfer started. */
115 static struct timeval start;
117 /* Number of bytes of current file transferred so far. */
118 volatile unsigned long statbytes;
120 /* Total size of current file. */
121 off_t totalbytes = 0;
123 /* Name of current file being transferred. */
126 /* This is set to non-zero to enable verbose mode. */
127 int verbose_mode = 0;
129 /* This is set to non-zero if compression is desired. */
130 int compress_flag = 0;
132 /* This is set to zero if the progressmeter is not desired. */
133 int showprogress = 1;
135 /* This is the program to execute for the secured connection. ("ssh" or -S) */
136 char *ssh_program = SSH_PROGRAM;
138 /* This is the list of arguments that scp passes to ssh */
146 * This function executes the given command as the specified user on the
147 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
148 * assigns the input and output file descriptors on success.
152 do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
154 int pin[2], pout[2], reserved[2];
157 fprintf(stderr, "Executing: program %s host %s, user %s, command %s\n",
158 ssh_program, host, remuser ? remuser : "(unspecified)", cmd);
161 * Reserve two descriptors so that the real pipes won't get
162 * descriptors 0 and 1 because that will screw up dup2 below.
166 /* Create a socket pair for communicating with ssh. */
168 fatal("pipe: %s", strerror(errno));
170 fatal("pipe: %s", strerror(errno));
172 /* Free the reserved descriptors. */
176 /* For a child to execute the command on the remote host using ssh. */
186 args.list[0] = ssh_program;
188 addargs("-l%s", remuser);
192 execvp(ssh_program, args.list);
196 /* Parent. Close the other side, and return the local side. */
205 fatal(const char *fmt,...)
211 vsnprintf(buf, sizeof(buf), fmt, ap);
213 fprintf(stderr, "%s\n", buf);
222 extern int iamremote;
224 BUF *allocbuf(BUF *, int, int);
229 void run_err(const char *,...);
230 void verifydir(char *);
234 int errs, remin, remout;
235 int pflag, iamremote, iamrecursive, targetshouldbedirectory;
238 char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
241 void rsource(char *, struct stat *);
242 void sink(int, char *[]);
243 void source(int, char *[]);
244 void tolocal(int, char *[]);
245 void toremote(char *, int, char *[]);
253 int ch, fflag, tflag;
258 __progname = get_progname(argv[0]);
261 addargs("ssh"); /* overwritten with ssh_program */
263 addargs("-oFallBackToRsh no");
266 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:")) != EOF)
268 /* User-visible flags. */
277 addargs("-%c%s", ch, optarg);
280 addargs("-p%s", optarg);
283 addargs("-oBatchmode yes");
292 ssh_program = xstrdup(optarg);
301 /* Server options. */
303 targetshouldbedirectory = 1;
305 case 'f': /* "from" */
320 if ((pwd = getpwuid(userid = getuid())) == NULL)
321 fatal("unknown user %d", (int) userid);
323 if (!isatty(STDERR_FILENO))
326 remin = STDIN_FILENO;
327 remout = STDOUT_FILENO;
330 /* Follow "protocol", send data. */
343 targetshouldbedirectory = 1;
346 /* Command to be executed on remote system using "ssh". */
347 (void) sprintf(cmd, "scp%s%s%s%s", verbose_mode ? " -v" : "",
348 iamrecursive ? " -r" : "", pflag ? " -p" : "",
349 targetshouldbedirectory ? " -d" : "");
351 (void) signal(SIGPIPE, lostconn);
353 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
354 toremote(targ, argc, argv);
356 tolocal(argc, argv); /* Dest is local host. */
357 if (targetshouldbedirectory)
358 verifydir(argv[argc - 1]);
367 if (*host == '[' && host[strlen(host) - 1] == ']') {
368 host[strlen(host) - 1] = '\0';
375 toremote(targ, argc, argv)
380 char *bp, *host, *src, *suser, *thost, *tuser;
386 if ((thost = strchr(argv[argc - 1], '@'))) {
389 tuser = argv[argc - 1];
392 else if (!okname(tuser))
395 thost = argv[argc - 1];
399 for (i = 0; i < argc - 1; i++) {
400 src = colon(argv[i]);
401 if (src) { /* remote to remote */
405 host = strchr(argv[i], '@');
406 len = strlen(ssh_program) + strlen(argv[i]) +
407 strlen(src) + (tuser ? strlen(tuser) : 0) +
408 strlen(thost) + strlen(targ) + CMDNEEDS + 32;
412 host = cleanhostname(host);
415 suser = pwd->pw_name;
416 else if (!okname(suser))
419 "%s%s -x -o'FallBackToRsh no' -n -l %s %s %s %s '%s%s%s:%s'",
420 ssh_program, verbose_mode ? " -v" : "",
421 suser, host, cmd, src,
422 tuser ? tuser : "", tuser ? "@" : "",
425 host = cleanhostname(argv[i]);
427 "exec %s%s -x -o'FallBackToRsh no' -n %s %s %s '%s%s%s:%s'",
428 ssh_program, verbose_mode ? " -v" : "",
430 tuser ? tuser : "", tuser ? "@" : "",
434 fprintf(stderr, "Executing: %s\n", bp);
437 } else { /* local to remote */
439 len = strlen(targ) + CMDNEEDS + 20;
441 (void) sprintf(bp, "%s -t %s", cmd, targ);
442 host = cleanhostname(thost);
443 if (do_cmd(host, tuser, bp, &remin,
461 char *bp, *host, *src, *suser;
463 for (i = 0; i < argc - 1; i++) {
464 if (!(src = colon(argv[i]))) { /* Local to local. */
465 len = strlen(_PATH_CP) + strlen(argv[i]) +
466 strlen(argv[argc - 1]) + 20;
468 (void) sprintf(bp, "exec %s%s%s %s %s", _PATH_CP,
469 iamrecursive ? " -r" : "", pflag ? " -p" : "",
470 argv[i], argv[argc - 1]);
472 fprintf(stderr, "Executing: %s\n", bp);
481 if ((host = strchr(argv[i], '@')) == NULL) {
488 suser = pwd->pw_name;
489 else if (!okname(suser))
492 host = cleanhostname(host);
493 len = strlen(src) + CMDNEEDS + 20;
495 (void) sprintf(bp, "%s -f %s", cmd, src);
496 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
502 sink(1, argv + argc - 1);
517 int amt, fd, haderr, indx, result;
518 char *last, *name, buf[2048];
520 for (indx = 0; indx < argc; ++indx) {
523 if ((fd = open(name, O_RDONLY, 0)) < 0)
525 if (fstat(fd, &stb) < 0) {
526 syserr: run_err("%s: %s", name, strerror(errno));
529 switch (stb.st_mode & S_IFMT) {
539 run_err("%s: not a regular file", name);
542 if ((last = strrchr(name, '/')) == NULL)
549 * Make it compatible with possible future
550 * versions expecting microseconds.
552 (void) sprintf(buf, "T%lu 0 %lu 0\n",
553 (unsigned long) stb.st_mtime,
554 (unsigned long) stb.st_atime);
555 (void) atomicio(write, remout, buf, strlen(buf));
559 #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
560 (void) sprintf(buf, "C%04o %lu %s\n",
561 (unsigned int) (stb.st_mode & FILEMODEMASK),
562 (unsigned long) stb.st_size,
565 fprintf(stderr, "Sending file modes: %s", buf);
568 (void) atomicio(write, remout, buf, strlen(buf));
571 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
572 next: (void) close(fd);
576 totalbytes = stb.st_size;
579 /* Keep writing after an error so that we stay sync'd up. */
580 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
582 if (i + amt > stb.st_size)
583 amt = stb.st_size - i;
585 result = atomicio(read, fd, bp->buf, amt);
587 haderr = result >= 0 ? EIO : errno;
590 (void) atomicio(write, remout, bp->buf, amt);
592 result = atomicio(write, remout, bp->buf, amt);
594 haderr = result >= 0 ? EIO : errno;
601 if (close(fd) < 0 && !haderr)
604 (void) atomicio(write, remout, "", 1);
606 run_err("%s: %s", name, strerror(haderr));
618 char *last, *vect[1], path[1100];
620 if (!(dirp = opendir(name))) {
621 run_err("%s: %s", name, strerror(errno));
624 last = strrchr(name, '/');
630 (void) sprintf(path, "T%lu 0 %lu 0\n",
631 (unsigned long) statp->st_mtime,
632 (unsigned long) statp->st_atime);
633 (void) atomicio(write, remout, path, strlen(path));
634 if (response() < 0) {
639 (void) sprintf(path, "D%04o %d %.1024s\n",
640 (unsigned int) (statp->st_mode & FILEMODEMASK), 0, last);
642 fprintf(stderr, "Entering directory: %s", path);
643 (void) atomicio(write, remout, path, strlen(path));
644 if (response() < 0) {
648 while ((dp = readdir(dirp))) {
651 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
653 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
654 run_err("%s/%s: name too long", name, dp->d_name);
657 (void) sprintf(path, "%s/%s", name, dp->d_name);
661 (void) closedir(dirp);
662 (void) atomicio(write, remout, "E\n", 2);
678 int amt, count, exists, first, mask, mode, ofd, omode;
680 int setimes, targisdir, wrerrno = 0;
681 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
683 struct timeval tv[2];
685 #define SCREWUP(str) { why = str; goto screwup; }
687 setimes = targisdir = 0;
692 run_err("ambiguous target");
696 if (targetshouldbedirectory)
699 (void) atomicio(write, remout, "", 1);
700 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
702 for (first = 1;; first = 0) {
704 if (atomicio(read, remin, cp, 1) <= 0)
707 SCREWUP("unexpected <newline>");
709 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
710 SCREWUP("lost connection");
712 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
715 if (buf[0] == '\01' || buf[0] == '\02') {
717 (void) atomicio(write, STDERR_FILENO,
718 buf + 1, strlen(buf + 1));
725 (void) atomicio(write, remout, "", 1);
731 #define getnum(t) (t) = 0; \
732 while (*cp >= '0' && *cp <= '9') (t) = (t) * 10 + (*cp++ - '0');
737 getnum(tv[1].tv_sec);
739 SCREWUP("mtime.sec not delimited");
743 SCREWUP("mtime.usec not delimited");
744 getnum(tv[0].tv_sec);
746 SCREWUP("atime.sec not delimited");
750 SCREWUP("atime.usec not delimited");
751 (void) atomicio(write, remout, "", 1);
754 if (*cp != 'C' && *cp != 'D') {
756 * Check for the case "rcp remote:foo\* local:bar".
757 * In this case, the line "No match." can be returned
758 * by the shell before the rcp command on the remote is
759 * executed so the ^Aerror_message convention isn't
766 SCREWUP("expected control record");
769 for (++cp; cp < buf + 5; cp++) {
770 if (*cp < '0' || *cp > '7')
772 mode = (mode << 3) | (*cp - '0');
775 SCREWUP("mode not delimited");
777 for (size = 0; *cp >= '0' && *cp <= '9';)
778 size = size * 10 + (*cp++ - '0');
780 SCREWUP("size not delimited");
782 static char *namebuf;
786 need = strlen(targ) + strlen(cp) + 250;
788 namebuf = xmalloc(need);
789 (void) sprintf(namebuf, "%s%s%s", targ,
790 *targ ? "/" : "", cp);
795 exists = stat(np, &stb) == 0;
797 int mod_flag = pflag;
799 if (!S_ISDIR(stb.st_mode)) {
804 (void) chmod(np, mode);
806 /* Handle copying from a read-only
809 if (mkdir(np, mode | S_IRWXU) < 0)
816 if (utimes(np, tv) < 0)
817 run_err("%s: set times: %s",
818 np, strerror(errno));
821 (void) chmod(np, mode);
826 if ((ofd = open(np, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
827 bad: run_err("%s: %s", np, strerror(errno));
830 (void) atomicio(write, remout, "", 1);
831 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
843 for (count = i = 0; i < size; i += 4096) {
849 j = read(remin, cp, amt);
850 if (j == -1 && (errno == EINTR || errno == EAGAIN)) {
853 run_err("%s", j ? strerror(errno) :
854 "dropped connection");
861 if (count == bp->cnt) {
862 /* Keep reading so we stay sync'd up. */
864 j = atomicio(write, ofd, bp->buf, count);
867 wrerrno = j >= 0 ? EIO : errno;
876 if (count != 0 && wrerr == NO &&
877 (j = atomicio(write, ofd, bp->buf, count)) != count) {
879 wrerrno = j >= 0 ? EIO : errno;
882 if (ftruncate(ofd, size)) {
883 run_err("%s: truncate: %s", np, strerror(errno));
888 if (exists || omode != mode)
890 if (fchmod(ofd, omode))
891 #else /* HAVE_FCHMOD */
892 if (chmod(np, omode))
893 #endif /* HAVE_FCHMOD */
894 run_err("%s: set mode: %s",
895 np, strerror(errno));
897 if (!exists && omode != mode)
899 if (fchmod(ofd, omode & ~mask))
900 #else /* HAVE_FCHMOD */
901 if (chmod(np, omode & ~mask))
902 #endif /* HAVE_FCHMOD */
903 run_err("%s: set mode: %s",
904 np, strerror(errno));
906 if (close(ofd) == -1) {
911 if (setimes && wrerr == NO) {
913 if (utimes(np, tv) < 0) {
914 run_err("%s: set times: %s",
915 np, strerror(errno));
921 run_err("%s: %s", np, strerror(wrerrno));
924 (void) atomicio(write, remout, "", 1);
931 run_err("protocol error: %s", why);
938 char ch, *cp, resp, rbuf[2048];
940 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
950 case 1: /* error, followed by error msg */
951 case 2: /* fatal error, "" */
953 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
956 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
959 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
971 (void) fprintf(stderr, "usage: scp "
972 "[-pqrvC46] [-S ssh] [-P port] [-c cipher] [-i identity] f1 f2; or:\n"
973 " scp [options] f1 ... fn directory\n");
978 run_err(const char *fmt,...)
984 if (fp == NULL && !(fp = fdopen(remout, "w")))
986 (void) fprintf(fp, "%c", 0x01);
987 (void) fprintf(fp, "scp: ");
989 (void) vfprintf(fp, fmt, ap);
991 (void) fprintf(fp, "\n");
996 vfprintf(stderr, fmt, ap);
998 fprintf(stderr, "\n");
1008 if (*cp == ':') /* Leading colon is part of file name. */
1014 if (*cp == '@' && *(cp+1) == '[')
1016 if (*cp == ']' && *(cp+1) == ':' && flag)
1018 if (*cp == ':' && !flag)
1032 if (!stat(cp, &stb)) {
1033 if (S_ISDIR(stb.st_mode))
1037 run_err("%s: %s", cp, strerror(errno));
1053 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
1058 bad: fprintf(stderr, "%s: invalid user name\n", cp0);
1063 allocbuf(bp, fd, blksize)
1068 #ifdef HAVE_ST_BLKSIZE
1071 if (fstat(fd, &stb) < 0) {
1072 run_err("fstat: %s", strerror(errno));
1075 if (stb.st_blksize == 0)
1078 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
1080 #else /* HAVE_ST_BLKSIZE */
1082 #endif /* HAVE_ST_BLKSIZE */
1083 if (bp->cnt >= size)
1085 if (bp->buf == NULL)
1086 bp->buf = xmalloc(size);
1088 bp->buf = xrealloc(bp->buf, size);
1098 fprintf(stderr, "lost connection\n");
1104 alarmtimer(int wait)
1106 struct itimerval itv;
1108 itv.it_value.tv_sec = wait;
1109 itv.it_value.tv_usec = 0;
1110 itv.it_interval = itv.it_value;
1111 setitimer(ITIMER_REAL, &itv, NULL);
1115 updateprogressmeter(int ignore)
1117 int save_errno = errno;
1126 static pid_t pgrp = -1;
1134 * Cygwin only supports tcgetpgrp() for getting the controlling tty
1137 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1140 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1141 ctty_pgrp == pgrp));
1146 progressmeter(int flag)
1148 static const char prefixes[] = " KMGTP";
1149 static struct timeval lastupdate;
1150 static off_t lastsize;
1151 struct timeval now, td, wait;
1152 off_t cursize, abbrevsize;
1154 int ratio, barlength, i, remaining;
1158 (void) gettimeofday(&start, (struct timezone *) 0);
1162 if (foregroundproc() == 0)
1165 (void) gettimeofday(&now, (struct timezone *) 0);
1166 cursize = statbytes;
1167 if (totalbytes != 0) {
1168 ratio = 100.0 * cursize / totalbytes;
1169 ratio = MAX(ratio, 0);
1170 ratio = MIN(ratio, 100);
1174 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
1176 barlength = getttywidth() - 51;
1177 barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH;
1178 if (barlength > 0) {
1179 i = barlength * ratio / 100;
1180 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1181 "|%.*s%*s|", i, BAR, barlength - i, "");
1184 abbrevsize = cursize;
1185 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1189 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5d %c%c ",
1190 (int) abbrevsize, prefixes[i], prefixes[i] == ' ' ? ' ' :
1193 timersub(&now, &lastupdate, &wait);
1194 if (cursize > lastsize) {
1197 if (wait.tv_sec >= STALLTIME) {
1198 start.tv_sec += wait.tv_sec;
1199 start.tv_usec += wait.tv_usec;
1203 timersub(&now, &start, &td);
1204 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1206 if (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes) {
1207 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1209 } else if (wait.tv_sec >= STALLTIME) {
1210 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1215 (int)(totalbytes / (statbytes / elapsed) - elapsed);
1217 remaining = elapsed;
1219 i = remaining / 3600;
1221 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1224 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1226 i = remaining % 3600;
1227 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1228 "%02d:%02d%s", i / 60, i % 60,
1229 (flag != 1) ? " ETA" : " ");
1231 atomicio(write, fileno(stdout), buf, strlen(buf));
1234 struct sigaction sa;
1235 sa.sa_handler = updateprogressmeter;
1236 sigemptyset((sigset_t *)&sa.sa_mask);
1238 sa.sa_flags = SA_RESTART;
1240 sigaction(SIGALRM, &sa, NULL);
1242 } else if (flag == 1) {
1244 atomicio(write, fileno(stdout), "\n", 1);
1252 struct winsize winsize;
1254 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
1255 return (winsize.ws_col ? winsize.ws_col : 80);
1261 addargs(char *fmt, ...)
1267 vsnprintf(buf, sizeof(buf), fmt, ap);
1270 if (args.list == NULL) {
1273 args.list = xmalloc(args.nalloc * sizeof(char *));
1274 } else if (args.num+2 >= args.nalloc) {
1276 args.list = xrealloc(args.list, args.nalloc * sizeof(char *));
1278 args.list[args.num++] = xstrdup(buf);
1279 args.list[args.num] = NULL;