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.42 2000/10/14 10:07:21 markus Exp $");
87 /* For progressmeter() -- number of seconds before xfer considered "stalled" */
90 /* Progress meter bar */
92 "************************************************************"\
93 "************************************************************"\
94 "************************************************************"\
95 "************************************************************"
96 #define MAX_BARLENGTH (sizeof(BAR) - 1)
98 /* Visual statistics about files as they are transferred. */
99 void progressmeter(int);
101 /* Returns width of the terminal (for progress meter calculations). */
102 int getttywidth(void);
103 int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc);
105 /* setup arguments for the call to ssh */
106 void addargs(char *fmt, ...) __attribute__((format(printf, 1, 2)));
108 /* Time a transfer started. */
109 static struct timeval start;
111 /* Number of bytes of current file transferred so far. */
112 volatile unsigned long statbytes;
114 /* Total size of current file. */
115 off_t totalbytes = 0;
117 /* Name of current file being transferred. */
120 /* This is set to non-zero to enable verbose mode. */
121 int verbose_mode = 0;
123 /* This is set to non-zero if compression is desired. */
124 int compress_flag = 0;
126 /* This is set to zero if the progressmeter is not desired. */
127 int showprogress = 1;
129 /* This is the program to execute for the secured connection. ("ssh" or -S) */
130 char *ssh_program = SSH_PROGRAM;
132 /* This is the list of arguments that scp passes to ssh */
140 * This function executes the given command as the specified user on the
141 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
142 * assigns the input and output file descriptors on success.
146 do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
148 int pin[2], pout[2], reserved[2];
151 fprintf(stderr, "Executing: program %s host %s, user %s, command %s\n",
152 ssh_program, host, remuser ? remuser : "(unspecified)", cmd);
155 * Reserve two descriptors so that the real pipes won't get
156 * descriptors 0 and 1 because that will screw up dup2 below.
160 /* Create a socket pair for communicating with ssh. */
162 fatal("pipe: %s", strerror(errno));
164 fatal("pipe: %s", strerror(errno));
166 /* Free the reserved descriptors. */
170 /* For a child to execute the command on the remote host using ssh. */
180 args.list[0] = ssh_program;
182 addargs("-l%s", remuser);
186 execvp(ssh_program, args.list);
190 /* Parent. Close the other side, and return the local side. */
199 fatal(const char *fmt,...)
205 vsnprintf(buf, sizeof(buf), fmt, ap);
207 fprintf(stderr, "%s\n", buf);
216 extern int iamremote;
218 BUF *allocbuf(BUF *, int, int);
223 void run_err(const char *,...);
224 void verifydir(char *);
228 int errs, remin, remout;
229 int pflag, iamremote, iamrecursive, targetshouldbedirectory;
232 char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
235 void rsource(char *, struct stat *);
236 void sink(int, char *[]);
237 void source(int, char *[]);
238 void tolocal(int, char *[]);
239 void toremote(char *, int, char *[]);
247 int ch, fflag, tflag;
253 addargs("ssh"); /* overwritten with ssh_program */
255 addargs("-oFallBackToRsh no");
258 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:")) != EOF)
260 /* User-visible flags. */
269 addargs("-%c%s", ch, optarg);
272 addargs("-p%s", optarg);
275 addargs("-oBatchmode yes");
284 ssh_program = xstrdup(optarg);
293 /* Server options. */
295 targetshouldbedirectory = 1;
297 case 'f': /* "from" */
312 if ((pwd = getpwuid(userid = getuid())) == NULL)
313 fatal("unknown user %d", (int) userid);
315 if (!isatty(STDERR_FILENO))
318 remin = STDIN_FILENO;
319 remout = STDOUT_FILENO;
322 /* Follow "protocol", send data. */
335 targetshouldbedirectory = 1;
338 /* Command to be executed on remote system using "ssh". */
339 (void) sprintf(cmd, "scp%s%s%s%s", verbose_mode ? " -v" : "",
340 iamrecursive ? " -r" : "", pflag ? " -p" : "",
341 targetshouldbedirectory ? " -d" : "");
343 (void) signal(SIGPIPE, lostconn);
345 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
346 toremote(targ, argc, argv);
348 tolocal(argc, argv); /* Dest is local host. */
349 if (targetshouldbedirectory)
350 verifydir(argv[argc - 1]);
359 if (*host == '[' && host[strlen(host) - 1] == ']') {
360 host[strlen(host) - 1] = '\0';
367 toremote(targ, argc, argv)
372 char *bp, *host, *src, *suser, *thost, *tuser;
378 if ((thost = strchr(argv[argc - 1], '@'))) {
381 tuser = argv[argc - 1];
384 else if (!okname(tuser))
387 thost = argv[argc - 1];
391 for (i = 0; i < argc - 1; i++) {
392 src = colon(argv[i]);
393 if (src) { /* remote to remote */
397 host = strchr(argv[i], '@');
398 len = strlen(ssh_program) + strlen(argv[i]) +
399 strlen(src) + (tuser ? strlen(tuser) : 0) +
400 strlen(thost) + strlen(targ) + CMDNEEDS + 32;
404 host = cleanhostname(host);
407 suser = pwd->pw_name;
408 else if (!okname(suser))
411 "%s%s -x -o'FallBackToRsh no' -n -l %s %s %s %s '%s%s%s:%s'",
412 ssh_program, verbose_mode ? " -v" : "",
413 suser, host, cmd, src,
414 tuser ? tuser : "", tuser ? "@" : "",
417 host = cleanhostname(argv[i]);
419 "exec %s%s -x -o'FallBackToRsh no' -n %s %s %s '%s%s%s:%s'",
420 ssh_program, verbose_mode ? " -v" : "",
422 tuser ? tuser : "", tuser ? "@" : "",
426 fprintf(stderr, "Executing: %s\n", bp);
429 } else { /* local to remote */
431 len = strlen(targ) + CMDNEEDS + 20;
433 (void) sprintf(bp, "%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) sprintf(bp, "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 = strchr(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) sprintf(bp, "%s -f %s", cmd, src);
488 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
494 sink(1, argv + argc - 1);
509 int amt, fd, haderr, indx, result;
510 char *last, *name, buf[2048];
512 for (indx = 0; indx < argc; ++indx) {
515 if ((fd = open(name, O_RDONLY, 0)) < 0)
517 if (fstat(fd, &stb) < 0) {
518 syserr: run_err("%s: %s", name, strerror(errno));
521 switch (stb.st_mode & S_IFMT) {
531 run_err("%s: not a regular file", name);
534 if ((last = strrchr(name, '/')) == NULL)
541 * Make it compatible with possible future
542 * versions expecting microseconds.
544 (void) sprintf(buf, "T%lu 0 %lu 0\n",
545 (unsigned long) stb.st_mtime,
546 (unsigned long) stb.st_atime);
547 (void) atomicio(write, remout, buf, strlen(buf));
551 #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
552 (void) sprintf(buf, "C%04o %lu %s\n",
553 (unsigned int) (stb.st_mode & FILEMODEMASK),
554 (unsigned long) stb.st_size,
557 fprintf(stderr, "Sending file modes: %s", buf);
560 (void) atomicio(write, remout, buf, strlen(buf));
563 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
564 next: (void) close(fd);
568 totalbytes = stb.st_size;
571 /* Keep writing after an error so that we stay sync'd up. */
572 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
574 if (i + amt > stb.st_size)
575 amt = stb.st_size - i;
577 result = atomicio(read, fd, bp->buf, amt);
579 haderr = result >= 0 ? EIO : errno;
582 (void) atomicio(write, remout, bp->buf, amt);
584 result = atomicio(write, remout, bp->buf, amt);
586 haderr = result >= 0 ? EIO : errno;
593 if (close(fd) < 0 && !haderr)
596 (void) atomicio(write, remout, "", 1);
598 run_err("%s: %s", name, strerror(haderr));
610 char *last, *vect[1], path[1100];
612 if (!(dirp = opendir(name))) {
613 run_err("%s: %s", name, strerror(errno));
616 last = strrchr(name, '/');
622 (void) sprintf(path, "T%lu 0 %lu 0\n",
623 (unsigned long) statp->st_mtime,
624 (unsigned long) statp->st_atime);
625 (void) atomicio(write, remout, path, strlen(path));
626 if (response() < 0) {
631 (void) sprintf(path, "D%04o %d %.1024s\n",
632 (unsigned int) (statp->st_mode & FILEMODEMASK), 0, last);
634 fprintf(stderr, "Entering directory: %s", path);
635 (void) atomicio(write, remout, path, strlen(path));
636 if (response() < 0) {
640 while ((dp = readdir(dirp))) {
643 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
645 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
646 run_err("%s/%s: name too long", name, dp->d_name);
649 (void) sprintf(path, "%s/%s", name, dp->d_name);
653 (void) closedir(dirp);
654 (void) atomicio(write, remout, "E\n", 2);
670 int amt, count, exists, first, mask, mode, ofd, omode;
672 int setimes, targisdir, wrerrno = 0;
673 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
675 struct timeval tv[2];
677 #define SCREWUP(str) { why = str; goto screwup; }
679 setimes = targisdir = 0;
684 run_err("ambiguous target");
688 if (targetshouldbedirectory)
691 (void) atomicio(write, remout, "", 1);
692 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
694 for (first = 1;; first = 0) {
696 if (atomicio(read, remin, cp, 1) <= 0)
699 SCREWUP("unexpected <newline>");
701 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
702 SCREWUP("lost connection");
704 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
707 if (buf[0] == '\01' || buf[0] == '\02') {
709 (void) atomicio(write, STDERR_FILENO,
710 buf + 1, strlen(buf + 1));
717 (void) atomicio(write, remout, "", 1);
723 #define getnum(t) (t) = 0; \
724 while (*cp >= '0' && *cp <= '9') (t) = (t) * 10 + (*cp++ - '0');
729 getnum(tv[1].tv_sec);
731 SCREWUP("mtime.sec not delimited");
735 SCREWUP("mtime.usec not delimited");
736 getnum(tv[0].tv_sec);
738 SCREWUP("atime.sec not delimited");
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; *cp >= '0' && *cp <= '9';)
770 size = size * 10 + (*cp++ - '0');
772 SCREWUP("size not delimited");
774 static char *namebuf;
778 need = strlen(targ) + strlen(cp) + 250;
780 namebuf = xmalloc(need);
781 (void) sprintf(namebuf, "%s%s%s", targ,
782 *targ ? "/" : "", cp);
787 exists = stat(np, &stb) == 0;
789 int mod_flag = pflag;
791 if (!S_ISDIR(stb.st_mode)) {
796 (void) chmod(np, mode);
798 /* Handle copying from a read-only
801 if (mkdir(np, mode | S_IRWXU) < 0)
808 if (utimes(np, tv) < 0)
809 run_err("%s: set times: %s",
810 np, strerror(errno));
813 (void) chmod(np, mode);
818 if ((ofd = open(np, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
819 bad: run_err("%s: %s", np, strerror(errno));
822 (void) atomicio(write, remout, "", 1);
823 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
835 for (count = i = 0; i < size; i += 4096) {
841 j = atomicio(read, remin, cp, amt);
843 run_err("%s", j ? strerror(errno) :
844 "dropped connection");
851 if (count == bp->cnt) {
852 /* Keep reading so we stay sync'd up. */
854 j = atomicio(write, ofd, bp->buf, count);
857 wrerrno = j >= 0 ? EIO : errno;
866 if (count != 0 && wrerr == NO &&
867 (j = atomicio(write, ofd, bp->buf, count)) != count) {
869 wrerrno = j >= 0 ? EIO : errno;
872 if (ftruncate(ofd, size)) {
873 run_err("%s: truncate: %s", np, strerror(errno));
878 if (exists || omode != mode)
880 if (fchmod(ofd, omode))
881 #else /* HAVE_FCHMOD */
882 if (chmod(np, omode))
883 #endif /* HAVE_FCHMOD */
884 run_err("%s: set mode: %s",
885 np, strerror(errno));
887 if (!exists && omode != mode)
889 if (fchmod(ofd, omode & ~mask))
890 #else /* HAVE_FCHMOD */
891 if (chmod(np, omode & ~mask))
892 #endif /* HAVE_FCHMOD */
893 run_err("%s: set mode: %s",
894 np, strerror(errno));
896 if (close(ofd) == -1) {
901 if (setimes && wrerr == NO) {
903 if (utimes(np, tv) < 0) {
904 run_err("%s: set times: %s",
905 np, strerror(errno));
911 run_err("%s: %s", np, strerror(wrerrno));
914 (void) atomicio(write, remout, "", 1);
921 run_err("protocol error: %s", why);
928 char ch, *cp, resp, rbuf[2048];
930 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
940 case 1: /* error, followed by error msg */
941 case 2: /* fatal error, "" */
943 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
946 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
949 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
961 (void) fprintf(stderr, "usage: scp "
962 "[-pqrvC46] [-S ssh] [-P port] [-c cipher] [-i identity] f1 f2; or:\n"
963 " scp [options] f1 ... fn directory\n");
968 run_err(const char *fmt,...)
974 if (fp == NULL && !(fp = fdopen(remout, "w")))
976 (void) fprintf(fp, "%c", 0x01);
977 (void) fprintf(fp, "scp: ");
979 (void) vfprintf(fp, fmt, ap);
981 (void) fprintf(fp, "\n");
986 vfprintf(stderr, fmt, ap);
988 fprintf(stderr, "\n");
998 if (*cp == ':') /* Leading colon is part of file name. */
1004 if (*cp == '@' && *(cp+1) == '[')
1006 if (*cp == ']' && *(cp+1) == ':' && flag)
1008 if (*cp == ':' && !flag)
1022 if (!stat(cp, &stb)) {
1023 if (S_ISDIR(stb.st_mode))
1027 run_err("%s: %s", cp, strerror(errno));
1043 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
1048 bad: fprintf(stderr, "%s: invalid user name\n", cp0);
1053 allocbuf(bp, fd, blksize)
1058 #ifdef HAVE_ST_BLKSIZE
1061 if (fstat(fd, &stb) < 0) {
1062 run_err("fstat: %s", strerror(errno));
1065 if (stb.st_blksize == 0)
1068 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
1070 #else /* HAVE_ST_BLKSIZE */
1072 #endif /* HAVE_ST_BLKSIZE */
1073 if (bp->cnt >= size)
1075 if (bp->buf == NULL)
1076 bp->buf = xmalloc(size);
1078 bp->buf = xrealloc(bp->buf, size);
1088 fprintf(stderr, "lost connection\n");
1094 alarmtimer(int wait)
1096 struct itimerval itv;
1098 itv.it_value.tv_sec = wait;
1099 itv.it_value.tv_usec = 0;
1100 itv.it_interval = itv.it_value;
1101 setitimer(ITIMER_REAL, &itv, NULL);
1105 updateprogressmeter(int ignore)
1107 int save_errno = errno;
1116 static pid_t pgrp = -1;
1124 * Cygwin only supports tcgetpgrp() for getting the controlling tty
1127 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1130 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1131 ctty_pgrp == pgrp));
1136 progressmeter(int flag)
1138 static const char prefixes[] = " KMGTP";
1139 static struct timeval lastupdate;
1140 static off_t lastsize;
1141 struct timeval now, td, wait;
1142 off_t cursize, abbrevsize;
1144 int ratio, barlength, i, remaining;
1148 (void) gettimeofday(&start, (struct timezone *) 0);
1152 if (foregroundproc() == 0)
1155 (void) gettimeofday(&now, (struct timezone *) 0);
1156 cursize = statbytes;
1157 if (totalbytes != 0) {
1158 ratio = 100.0 * cursize / totalbytes;
1159 ratio = MAX(ratio, 0);
1160 ratio = MIN(ratio, 100);
1164 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
1166 barlength = getttywidth() - 51;
1167 barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH;
1168 if (barlength > 0) {
1169 i = barlength * ratio / 100;
1170 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1171 "|%.*s%*s|", i, BAR, barlength - i, "");
1174 abbrevsize = cursize;
1175 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1179 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5d %c%c ",
1180 (int) abbrevsize, prefixes[i], prefixes[i] == ' ' ? ' ' :
1183 timersub(&now, &lastupdate, &wait);
1184 if (cursize > lastsize) {
1187 if (wait.tv_sec >= STALLTIME) {
1188 start.tv_sec += wait.tv_sec;
1189 start.tv_usec += wait.tv_usec;
1193 timersub(&now, &start, &td);
1194 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1196 if (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes) {
1197 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1199 } else if (wait.tv_sec >= STALLTIME) {
1200 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1205 (int)(totalbytes / (statbytes / elapsed) - elapsed);
1207 remaining = elapsed;
1209 i = remaining / 3600;
1211 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1214 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1216 i = remaining % 3600;
1217 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1218 "%02d:%02d%s", i / 60, i % 60,
1219 (flag != 1) ? " ETA" : " ");
1221 atomicio(write, fileno(stdout), buf, strlen(buf));
1224 struct sigaction sa;
1225 sa.sa_handler = updateprogressmeter;
1226 sigemptyset((sigset_t *)&sa.sa_mask);
1228 sa.sa_flags = SA_RESTART;
1230 sigaction(SIGALRM, &sa, NULL);
1232 } else if (flag == 1) {
1234 atomicio(write, fileno(stdout), "\n", 1);
1242 struct winsize winsize;
1244 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
1245 return (winsize.ws_col ? winsize.ws_col : 80);
1251 addargs(char *fmt, ...)
1257 vsnprintf(buf, sizeof(buf), fmt, ap);
1260 if (args.list == NULL) {
1263 args.list = xmalloc(args.nalloc * sizeof(char *));
1264 } else if (args.num+2 >= args.nalloc) {
1266 args.list = xrealloc(args.list, args.nalloc * sizeof(char *));
1268 args.list[args.num++] = xstrdup(buf);
1269 args.list[args.num] = NULL;