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.51 2001/01/21 19:05:55 markus Exp $");
82 #include "pathnames.h"
85 #ifdef HAVE___PROGNAME
86 extern char *__progname;
91 /* For progressmeter() -- number of seconds before xfer considered "stalled" */
94 /* Progress meter bar */
96 "************************************************************"\
97 "************************************************************"\
98 "************************************************************"\
99 "************************************************************"
100 #define MAX_BARLENGTH (sizeof(BAR) - 1)
102 /* Visual statistics about files as they are transferred. */
103 void progressmeter(int);
105 /* Returns width of the terminal (for progress meter calculations). */
106 int getttywidth(void);
107 int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc);
109 /* setup arguments for the call to ssh */
110 void addargs(char *fmt, ...) __attribute__((format(printf, 1, 2)));
112 /* Time a transfer started. */
113 static struct timeval start;
115 /* Number of bytes of current file transferred so far. */
116 volatile u_long statbytes;
118 /* Total size of current file. */
119 off_t totalbytes = 0;
121 /* Name of current file being transferred. */
124 /* This is set to non-zero to enable verbose mode. */
125 int verbose_mode = 0;
127 /* This is set to zero if the progressmeter is not desired. */
128 int showprogress = 1;
130 /* This is the program to execute for the secured connection. ("ssh" or -S) */
131 char *ssh_program = _PATH_SSH_PROGRAM;
133 /* This is the list of arguments that scp passes to ssh */
141 * This function executes the given command as the specified user on the
142 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
143 * assigns the input and output file descriptors on success.
147 do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
149 int pin[2], pout[2], reserved[2];
152 fprintf(stderr, "Executing: program %s host %s, user %s, command %s\n",
153 ssh_program, host, remuser ? remuser : "(unspecified)", cmd);
156 * Reserve two descriptors so that the real pipes won't get
157 * descriptors 0 and 1 because that will screw up dup2 below.
161 /* Create a socket pair for communicating with ssh. */
163 fatal("pipe: %s", strerror(errno));
165 fatal("pipe: %s", strerror(errno));
167 /* Free the reserved descriptors. */
171 /* For a child to execute the command on the remote host using ssh. */
181 args.list[0] = ssh_program;
183 addargs("-l%s", remuser);
187 execvp(ssh_program, args.list);
191 /* Parent. Close the other side, and return the local side. */
204 extern int iamremote;
206 BUF *allocbuf(BUF *, int, int);
211 void run_err(const char *,...);
212 void verifydir(char *);
216 int errs, remin, remout;
217 int pflag, iamremote, iamrecursive, targetshouldbedirectory;
220 char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
223 void rsource(char *, struct stat *);
224 void sink(int, char *[]);
225 void source(int, char *[]);
226 void tolocal(int, char *[]);
227 void toremote(char *, int, char *[]);
235 int ch, fflag, tflag;
240 __progname = get_progname(argv[0]);
243 addargs("ssh"); /* overwritten with ssh_program */
245 addargs("-oFallBackToRsh no");
248 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:")) != -1)
250 /* User-visible flags. */
259 addargs("-%c%s", ch, optarg);
262 addargs("-p%s", optarg);
265 addargs("-oBatchmode yes");
274 ssh_program = xstrdup(optarg);
283 /* Server options. */
285 targetshouldbedirectory = 1;
287 case 'f': /* "from" */
302 if ((pwd = getpwuid(userid = getuid())) == NULL)
303 fatal("unknown user %d", (int) userid);
305 if (!isatty(STDERR_FILENO))
308 remin = STDIN_FILENO;
309 remout = STDOUT_FILENO;
312 /* Follow "protocol", send data. */
325 targetshouldbedirectory = 1;
328 /* Command to be executed on remote system using "ssh". */
329 (void) sprintf(cmd, "scp%s%s%s%s", 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]);
349 if (*host == '[' && host[strlen(host) - 1] == ']') {
350 host[strlen(host) - 1] = '\0';
357 toremote(targ, argc, argv)
362 char *bp, *host, *src, *suser, *thost, *tuser;
368 if ((thost = strchr(argv[argc - 1], '@'))) {
371 tuser = argv[argc - 1];
374 else if (!okname(tuser))
377 thost = argv[argc - 1];
381 for (i = 0; i < argc - 1; i++) {
382 src = colon(argv[i]);
383 if (src) { /* remote to remote */
387 host = strchr(argv[i], '@');
388 len = strlen(ssh_program) + strlen(argv[i]) +
389 strlen(src) + (tuser ? strlen(tuser) : 0) +
390 strlen(thost) + strlen(targ) + CMDNEEDS + 32;
394 host = cleanhostname(host);
397 suser = pwd->pw_name;
398 else if (!okname(suser))
401 "%s%s -x -o'FallBackToRsh no' -n -l %s %s %s %s '%s%s%s:%s'",
402 ssh_program, verbose_mode ? " -v" : "",
403 suser, host, cmd, src,
404 tuser ? tuser : "", tuser ? "@" : "",
407 host = cleanhostname(argv[i]);
409 "exec %s%s -x -o'FallBackToRsh no' -n %s %s %s '%s%s%s:%s'",
410 ssh_program, verbose_mode ? " -v" : "",
412 tuser ? tuser : "", tuser ? "@" : "",
416 fprintf(stderr, "Executing: %s\n", bp);
419 } else { /* local to remote */
421 len = strlen(targ) + CMDNEEDS + 20;
423 (void) sprintf(bp, "%s -t %s", cmd, targ);
424 host = cleanhostname(thost);
425 if (do_cmd(host, tuser, bp, &remin,
443 char *bp, *host, *src, *suser;
445 for (i = 0; i < argc - 1; i++) {
446 if (!(src = colon(argv[i]))) { /* Local to local. */
447 len = strlen(_PATH_CP) + strlen(argv[i]) +
448 strlen(argv[argc - 1]) + 20;
450 (void) sprintf(bp, "exec %s%s%s %s %s", _PATH_CP,
451 iamrecursive ? " -r" : "", pflag ? " -p" : "",
452 argv[i], argv[argc - 1]);
454 fprintf(stderr, "Executing: %s\n", bp);
463 if ((host = strchr(argv[i], '@')) == NULL) {
470 suser = pwd->pw_name;
471 else if (!okname(suser))
474 host = cleanhostname(host);
475 len = strlen(src) + CMDNEEDS + 20;
477 (void) sprintf(bp, "%s -f %s", cmd, src);
478 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
484 sink(1, argv + argc - 1);
499 int amt, fd, haderr, indx, result;
500 char *last, *name, buf[2048];
502 for (indx = 0; indx < argc; ++indx) {
505 if ((fd = open(name, O_RDONLY, 0)) < 0)
507 if (fstat(fd, &stb) < 0) {
508 syserr: run_err("%s: %s", name, strerror(errno));
511 switch (stb.st_mode & S_IFMT) {
521 run_err("%s: not a regular file", name);
524 if ((last = strrchr(name, '/')) == NULL)
531 * Make it compatible with possible future
532 * versions expecting microseconds.
534 (void) sprintf(buf, "T%lu 0 %lu 0\n",
535 (u_long) stb.st_mtime,
536 (u_long) stb.st_atime);
537 (void) atomicio(write, remout, buf, strlen(buf));
541 #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
542 sprintf(buf, "C%04o %lu %s\n",
543 (u_int) (stb.st_mode & FILEMODEMASK),
544 (u_long) stb.st_size, last);
546 fprintf(stderr, "Sending file modes: %s", buf);
549 (void) atomicio(write, remout, buf, strlen(buf));
552 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
553 next: (void) close(fd);
557 totalbytes = stb.st_size;
560 /* Keep writing after an error so that we stay sync'd up. */
561 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
563 if (i + amt > stb.st_size)
564 amt = stb.st_size - i;
566 result = atomicio(read, fd, bp->buf, amt);
568 haderr = result >= 0 ? EIO : errno;
571 (void) atomicio(write, remout, bp->buf, amt);
573 result = atomicio(write, remout, bp->buf, amt);
575 haderr = result >= 0 ? EIO : errno;
582 if (close(fd) < 0 && !haderr)
585 (void) atomicio(write, remout, "", 1);
587 run_err("%s: %s", name, strerror(haderr));
599 char *last, *vect[1], path[1100];
601 if (!(dirp = opendir(name))) {
602 run_err("%s: %s", name, strerror(errno));
605 last = strrchr(name, '/');
611 (void) sprintf(path, "T%lu 0 %lu 0\n",
612 (u_long) statp->st_mtime,
613 (u_long) statp->st_atime);
614 (void) atomicio(write, remout, path, strlen(path));
615 if (response() < 0) {
620 (void) sprintf(path, "D%04o %d %.1024s\n",
621 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
623 fprintf(stderr, "Entering directory: %s", path);
624 (void) atomicio(write, remout, path, strlen(path));
625 if (response() < 0) {
629 while ((dp = readdir(dirp))) {
632 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
634 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
635 run_err("%s/%s: name too long", name, dp->d_name);
638 (void) sprintf(path, "%s/%s", name, dp->d_name);
642 (void) closedir(dirp);
643 (void) atomicio(write, remout, "E\n", 2);
659 int amt, count, exists, first, mask, mode, ofd, omode;
661 int setimes, targisdir, wrerrno = 0;
662 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
664 struct timeval tv[2];
666 #define SCREWUP(str) { why = str; goto screwup; }
668 setimes = targisdir = 0;
673 run_err("ambiguous target");
677 if (targetshouldbedirectory)
680 (void) atomicio(write, remout, "", 1);
681 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
683 for (first = 1;; first = 0) {
685 if (atomicio(read, remin, cp, 1) <= 0)
688 SCREWUP("unexpected <newline>");
690 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
691 SCREWUP("lost connection");
693 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
696 if (buf[0] == '\01' || buf[0] == '\02') {
698 (void) atomicio(write, STDERR_FILENO,
699 buf + 1, strlen(buf + 1));
706 (void) atomicio(write, remout, "", 1);
712 #define getnum(t) (t) = 0; \
713 while (*cp >= '0' && *cp <= '9') (t) = (t) * 10 + (*cp++ - '0');
718 getnum(tv[1].tv_sec);
720 SCREWUP("mtime.sec not delimited");
724 SCREWUP("mtime.usec not delimited");
725 getnum(tv[0].tv_sec);
727 SCREWUP("atime.sec not delimited");
731 SCREWUP("atime.usec not delimited");
732 (void) atomicio(write, remout, "", 1);
735 if (*cp != 'C' && *cp != 'D') {
737 * Check for the case "rcp remote:foo\* local:bar".
738 * In this case, the line "No match." can be returned
739 * by the shell before the rcp command on the remote is
740 * executed so the ^Aerror_message convention isn't
747 SCREWUP("expected control record");
750 for (++cp; cp < buf + 5; cp++) {
751 if (*cp < '0' || *cp > '7')
753 mode = (mode << 3) | (*cp - '0');
756 SCREWUP("mode not delimited");
758 for (size = 0; *cp >= '0' && *cp <= '9';)
759 size = size * 10 + (*cp++ - '0');
761 SCREWUP("size not delimited");
763 static char *namebuf;
767 need = strlen(targ) + strlen(cp) + 250;
769 namebuf = xmalloc(need);
770 (void) sprintf(namebuf, "%s%s%s", targ,
771 *targ ? "/" : "", cp);
776 exists = stat(np, &stb) == 0;
778 int mod_flag = pflag;
780 if (!S_ISDIR(stb.st_mode)) {
785 (void) chmod(np, mode);
787 /* Handle copying from a read-only
790 if (mkdir(np, mode | S_IRWXU) < 0)
797 if (utimes(np, tv) < 0)
798 run_err("%s: set times: %s",
799 np, strerror(errno));
802 (void) chmod(np, mode);
807 if ((ofd = open(np, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
808 bad: run_err("%s: %s", np, strerror(errno));
811 (void) atomicio(write, remout, "", 1);
812 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
824 for (count = i = 0; i < size; i += 4096) {
830 j = read(remin, cp, amt);
831 if (j == -1 && (errno == EINTR || errno == EAGAIN)) {
834 run_err("%s", j ? strerror(errno) :
835 "dropped connection");
842 if (count == bp->cnt) {
843 /* Keep reading so we stay sync'd up. */
845 j = atomicio(write, ofd, bp->buf, count);
848 wrerrno = j >= 0 ? EIO : errno;
857 if (count != 0 && wrerr == NO &&
858 (j = atomicio(write, ofd, bp->buf, count)) != count) {
860 wrerrno = j >= 0 ? EIO : errno;
863 if (ftruncate(ofd, size)) {
864 run_err("%s: truncate: %s", np, strerror(errno));
869 if (exists || omode != mode)
871 if (fchmod(ofd, omode))
872 #else /* HAVE_FCHMOD */
873 if (chmod(np, omode))
874 #endif /* HAVE_FCHMOD */
875 run_err("%s: set mode: %s",
876 np, strerror(errno));
878 if (!exists && omode != mode)
880 if (fchmod(ofd, omode & ~mask))
881 #else /* HAVE_FCHMOD */
882 if (chmod(np, omode & ~mask))
883 #endif /* HAVE_FCHMOD */
884 run_err("%s: set mode: %s",
885 np, strerror(errno));
887 if (close(ofd) == -1) {
892 if (setimes && wrerr == NO) {
894 if (utimes(np, tv) < 0) {
895 run_err("%s: set times: %s",
896 np, strerror(errno));
902 run_err("%s: %s", np, strerror(wrerrno));
905 (void) atomicio(write, remout, "", 1);
912 run_err("protocol error: %s", why);
919 char ch, *cp, resp, rbuf[2048];
921 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
931 case 1: /* error, followed by error msg */
932 case 2: /* fatal error, "" */
934 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
937 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
940 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
952 (void) fprintf(stderr, "usage: scp "
953 "[-pqrvC46] [-S ssh] [-P port] [-c cipher] [-i identity] f1 f2; or:\n"
954 " scp [options] f1 ... fn directory\n");
959 run_err(const char *fmt,...)
965 if (fp == NULL && !(fp = fdopen(remout, "w")))
967 (void) fprintf(fp, "%c", 0x01);
968 (void) fprintf(fp, "scp: ");
970 (void) vfprintf(fp, fmt, ap);
972 (void) fprintf(fp, "\n");
977 vfprintf(stderr, fmt, ap);
979 fprintf(stderr, "\n");
989 if (*cp == ':') /* Leading colon is part of file name. */
995 if (*cp == '@' && *(cp+1) == '[')
997 if (*cp == ']' && *(cp+1) == ':' && flag)
999 if (*cp == ':' && !flag)
1013 if (!stat(cp, &stb)) {
1014 if (S_ISDIR(stb.st_mode))
1018 run_err("%s: %s", cp, strerror(errno));
1034 if (!isalpha(c) && !isdigit(c) &&
1035 c != '_' && c != '-' && c != '.' && c != '+')
1040 bad: fprintf(stderr, "%s: invalid user name\n", cp0);
1045 allocbuf(bp, fd, blksize)
1050 #ifdef HAVE_ST_BLKSIZE
1053 if (fstat(fd, &stb) < 0) {
1054 run_err("fstat: %s", strerror(errno));
1057 if (stb.st_blksize == 0)
1060 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
1062 #else /* HAVE_ST_BLKSIZE */
1064 #endif /* HAVE_ST_BLKSIZE */
1065 if (bp->cnt >= size)
1067 if (bp->buf == NULL)
1068 bp->buf = xmalloc(size);
1070 bp->buf = xrealloc(bp->buf, size);
1080 fprintf(stderr, "lost connection\n");
1086 alarmtimer(int wait)
1088 struct itimerval itv;
1090 itv.it_value.tv_sec = wait;
1091 itv.it_value.tv_usec = 0;
1092 itv.it_interval = itv.it_value;
1093 setitimer(ITIMER_REAL, &itv, NULL);
1097 updateprogressmeter(int ignore)
1099 int save_errno = errno;
1108 static pid_t pgrp = -1;
1116 * Cygwin only supports tcgetpgrp() for getting the controlling tty
1119 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1122 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1123 ctty_pgrp == pgrp));
1128 progressmeter(int flag)
1130 static const char prefixes[] = " KMGTP";
1131 static struct timeval lastupdate;
1132 static off_t lastsize;
1133 struct timeval now, td, wait;
1134 off_t cursize, abbrevsize;
1136 int ratio, barlength, i, remaining;
1140 (void) gettimeofday(&start, (struct timezone *) 0);
1144 if (foregroundproc() == 0)
1147 (void) gettimeofday(&now, (struct timezone *) 0);
1148 cursize = statbytes;
1149 if (totalbytes != 0) {
1150 ratio = 100.0 * cursize / totalbytes;
1151 ratio = MAX(ratio, 0);
1152 ratio = MIN(ratio, 100);
1156 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
1158 barlength = getttywidth() - 51;
1159 barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH;
1160 if (barlength > 0) {
1161 i = barlength * ratio / 100;
1162 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1163 "|%.*s%*s|", i, BAR, barlength - i, "");
1166 abbrevsize = cursize;
1167 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1171 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5d %c%c ",
1172 (int) abbrevsize, prefixes[i], prefixes[i] == ' ' ? ' ' : 'B');
1174 timersub(&now, &lastupdate, &wait);
1175 if (cursize > lastsize) {
1178 if (wait.tv_sec >= STALLTIME) {
1179 start.tv_sec += wait.tv_sec;
1180 start.tv_usec += wait.tv_usec;
1184 timersub(&now, &start, &td);
1185 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1188 (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
1189 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1191 } else if (wait.tv_sec >= STALLTIME) {
1192 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1196 remaining = (int)(totalbytes / (statbytes / elapsed) -
1199 remaining = elapsed;
1201 i = remaining / 3600;
1203 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1206 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1208 i = remaining % 3600;
1209 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1210 "%02d:%02d%s", i / 60, i % 60,
1211 (flag != 1) ? " ETA" : " ");
1213 atomicio(write, fileno(stdout), buf, strlen(buf));
1216 struct sigaction sa;
1217 sa.sa_handler = updateprogressmeter;
1218 sigemptyset((sigset_t *)&sa.sa_mask);
1220 sa.sa_flags = SA_RESTART;
1222 sigaction(SIGALRM, &sa, NULL);
1224 } else if (flag == 1) {
1226 atomicio(write, fileno(stdout), "\n", 1);
1234 struct winsize winsize;
1236 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
1237 return (winsize.ws_col ? winsize.ws_col : 80);
1243 addargs(char *fmt, ...)
1249 vsnprintf(buf, sizeof(buf), fmt, ap);
1252 if (args.list == NULL) {
1255 args.list = xmalloc(args.nalloc * sizeof(char *));
1256 } else if (args.num+2 >= args.nalloc) {
1258 args.list = xrealloc(args.list, args.nalloc * sizeof(char *));
1260 args.list[args.num++] = xstrdup(buf);
1261 args.list[args.num] = NULL;