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.48 2001/01/01 14:52:49 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 u_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 zero if the progressmeter is not desired. */
130 int showprogress = 1;
132 /* This is the program to execute for the secured connection. ("ssh" or -S) */
133 char *ssh_program = SSH_PROGRAM;
135 /* This is the list of arguments that scp passes to ssh */
143 * This function executes the given command as the specified user on the
144 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
145 * assigns the input and output file descriptors on success.
149 do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
151 int pin[2], pout[2], reserved[2];
154 fprintf(stderr, "Executing: program %s host %s, user %s, command %s\n",
155 ssh_program, host, remuser ? remuser : "(unspecified)", cmd);
158 * Reserve two descriptors so that the real pipes won't get
159 * descriptors 0 and 1 because that will screw up dup2 below.
163 /* Create a socket pair for communicating with ssh. */
165 fatal("pipe: %s", strerror(errno));
167 fatal("pipe: %s", strerror(errno));
169 /* Free the reserved descriptors. */
173 /* For a child to execute the command on the remote host using ssh. */
183 args.list[0] = ssh_program;
185 addargs("-l%s", remuser);
189 execvp(ssh_program, args.list);
193 /* Parent. Close the other side, and return the local side. */
206 extern int iamremote;
208 BUF *allocbuf(BUF *, int, int);
213 void run_err(const char *,...);
214 void verifydir(char *);
218 int errs, remin, remout;
219 int pflag, iamremote, iamrecursive, targetshouldbedirectory;
222 char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
225 void rsource(char *, struct stat *);
226 void sink(int, char *[]);
227 void source(int, char *[]);
228 void tolocal(int, char *[]);
229 void toremote(char *, int, char *[]);
237 int ch, fflag, tflag;
242 __progname = get_progname(argv[0]);
245 addargs("ssh"); /* overwritten with ssh_program */
247 addargs("-oFallBackToRsh no");
250 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:")) != EOF)
252 /* User-visible flags. */
261 addargs("-%c%s", ch, optarg);
264 addargs("-p%s", optarg);
267 addargs("-oBatchmode yes");
276 ssh_program = xstrdup(optarg);
285 /* Server options. */
287 targetshouldbedirectory = 1;
289 case 'f': /* "from" */
304 if ((pwd = getpwuid(userid = getuid())) == NULL)
305 fatal("unknown user %d", (int) userid);
307 if (!isatty(STDERR_FILENO))
310 remin = STDIN_FILENO;
311 remout = STDOUT_FILENO;
314 /* Follow "protocol", send data. */
327 targetshouldbedirectory = 1;
330 /* Command to be executed on remote system using "ssh". */
331 (void) sprintf(cmd, "scp%s%s%s%s", verbose_mode ? " -v" : "",
332 iamrecursive ? " -r" : "", pflag ? " -p" : "",
333 targetshouldbedirectory ? " -d" : "");
335 (void) signal(SIGPIPE, lostconn);
337 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
338 toremote(targ, argc, argv);
340 tolocal(argc, argv); /* Dest is local host. */
341 if (targetshouldbedirectory)
342 verifydir(argv[argc - 1]);
351 if (*host == '[' && host[strlen(host) - 1] == ']') {
352 host[strlen(host) - 1] = '\0';
359 toremote(targ, argc, argv)
364 char *bp, *host, *src, *suser, *thost, *tuser;
370 if ((thost = strchr(argv[argc - 1], '@'))) {
373 tuser = argv[argc - 1];
376 else if (!okname(tuser))
379 thost = argv[argc - 1];
383 for (i = 0; i < argc - 1; i++) {
384 src = colon(argv[i]);
385 if (src) { /* remote to remote */
389 host = strchr(argv[i], '@');
390 len = strlen(ssh_program) + strlen(argv[i]) +
391 strlen(src) + (tuser ? strlen(tuser) : 0) +
392 strlen(thost) + strlen(targ) + CMDNEEDS + 32;
396 host = cleanhostname(host);
399 suser = pwd->pw_name;
400 else if (!okname(suser))
403 "%s%s -x -o'FallBackToRsh no' -n -l %s %s %s %s '%s%s%s:%s'",
404 ssh_program, verbose_mode ? " -v" : "",
405 suser, host, cmd, src,
406 tuser ? tuser : "", tuser ? "@" : "",
409 host = cleanhostname(argv[i]);
411 "exec %s%s -x -o'FallBackToRsh no' -n %s %s %s '%s%s%s:%s'",
412 ssh_program, verbose_mode ? " -v" : "",
414 tuser ? tuser : "", tuser ? "@" : "",
418 fprintf(stderr, "Executing: %s\n", bp);
421 } else { /* local to remote */
423 len = strlen(targ) + CMDNEEDS + 20;
425 (void) sprintf(bp, "%s -t %s", cmd, targ);
426 host = cleanhostname(thost);
427 if (do_cmd(host, tuser, bp, &remin,
445 char *bp, *host, *src, *suser;
447 for (i = 0; i < argc - 1; i++) {
448 if (!(src = colon(argv[i]))) { /* Local to local. */
449 len = strlen(_PATH_CP) + strlen(argv[i]) +
450 strlen(argv[argc - 1]) + 20;
452 (void) sprintf(bp, "exec %s%s%s %s %s", _PATH_CP,
453 iamrecursive ? " -r" : "", pflag ? " -p" : "",
454 argv[i], argv[argc - 1]);
456 fprintf(stderr, "Executing: %s\n", bp);
465 if ((host = strchr(argv[i], '@')) == NULL) {
472 suser = pwd->pw_name;
473 else if (!okname(suser))
476 host = cleanhostname(host);
477 len = strlen(src) + CMDNEEDS + 20;
479 (void) sprintf(bp, "%s -f %s", cmd, src);
480 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
486 sink(1, argv + argc - 1);
501 int amt, fd, haderr, indx, result;
502 char *last, *name, buf[2048];
504 for (indx = 0; indx < argc; ++indx) {
507 if ((fd = open(name, O_RDONLY, 0)) < 0)
509 if (fstat(fd, &stb) < 0) {
510 syserr: run_err("%s: %s", name, strerror(errno));
513 switch (stb.st_mode & S_IFMT) {
523 run_err("%s: not a regular file", name);
526 if ((last = strrchr(name, '/')) == NULL)
533 * Make it compatible with possible future
534 * versions expecting microseconds.
536 (void) sprintf(buf, "T%lu 0 %lu 0\n",
537 (u_long) stb.st_mtime,
538 (u_long) stb.st_atime);
539 (void) atomicio(write, remout, buf, strlen(buf));
543 #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
544 sprintf(buf, "C%04o %lu %s\n",
545 (u_int) (stb.st_mode & FILEMODEMASK),
546 (u_long) stb.st_size, last);
548 fprintf(stderr, "Sending file modes: %s", buf);
551 (void) atomicio(write, remout, buf, strlen(buf));
554 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
555 next: (void) close(fd);
559 totalbytes = stb.st_size;
562 /* Keep writing after an error so that we stay sync'd up. */
563 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
565 if (i + amt > stb.st_size)
566 amt = stb.st_size - i;
568 result = atomicio(read, fd, bp->buf, amt);
570 haderr = result >= 0 ? EIO : errno;
573 (void) atomicio(write, remout, bp->buf, amt);
575 result = atomicio(write, remout, bp->buf, amt);
577 haderr = result >= 0 ? EIO : errno;
584 if (close(fd) < 0 && !haderr)
587 (void) atomicio(write, remout, "", 1);
589 run_err("%s: %s", name, strerror(haderr));
601 char *last, *vect[1], path[1100];
603 if (!(dirp = opendir(name))) {
604 run_err("%s: %s", name, strerror(errno));
607 last = strrchr(name, '/');
613 (void) sprintf(path, "T%lu 0 %lu 0\n",
614 (u_long) statp->st_mtime,
615 (u_long) statp->st_atime);
616 (void) atomicio(write, remout, path, strlen(path));
617 if (response() < 0) {
622 (void) sprintf(path, "D%04o %d %.1024s\n",
623 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
625 fprintf(stderr, "Entering directory: %s", path);
626 (void) atomicio(write, remout, path, strlen(path));
627 if (response() < 0) {
631 while ((dp = readdir(dirp))) {
634 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
636 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
637 run_err("%s/%s: name too long", name, dp->d_name);
640 (void) sprintf(path, "%s/%s", name, dp->d_name);
644 (void) closedir(dirp);
645 (void) atomicio(write, remout, "E\n", 2);
661 int amt, count, exists, first, mask, mode, ofd, omode;
663 int setimes, targisdir, wrerrno = 0;
664 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
666 struct timeval tv[2];
668 #define SCREWUP(str) { why = str; goto screwup; }
670 setimes = targisdir = 0;
675 run_err("ambiguous target");
679 if (targetshouldbedirectory)
682 (void) atomicio(write, remout, "", 1);
683 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
685 for (first = 1;; first = 0) {
687 if (atomicio(read, remin, cp, 1) <= 0)
690 SCREWUP("unexpected <newline>");
692 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
693 SCREWUP("lost connection");
695 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
698 if (buf[0] == '\01' || buf[0] == '\02') {
700 (void) atomicio(write, STDERR_FILENO,
701 buf + 1, strlen(buf + 1));
708 (void) atomicio(write, remout, "", 1);
714 #define getnum(t) (t) = 0; \
715 while (*cp >= '0' && *cp <= '9') (t) = (t) * 10 + (*cp++ - '0');
720 getnum(tv[1].tv_sec);
722 SCREWUP("mtime.sec not delimited");
726 SCREWUP("mtime.usec not delimited");
727 getnum(tv[0].tv_sec);
729 SCREWUP("atime.sec not delimited");
733 SCREWUP("atime.usec not delimited");
734 (void) atomicio(write, remout, "", 1);
737 if (*cp != 'C' && *cp != 'D') {
739 * Check for the case "rcp remote:foo\* local:bar".
740 * In this case, the line "No match." can be returned
741 * by the shell before the rcp command on the remote is
742 * executed so the ^Aerror_message convention isn't
749 SCREWUP("expected control record");
752 for (++cp; cp < buf + 5; cp++) {
753 if (*cp < '0' || *cp > '7')
755 mode = (mode << 3) | (*cp - '0');
758 SCREWUP("mode not delimited");
760 for (size = 0; *cp >= '0' && *cp <= '9';)
761 size = size * 10 + (*cp++ - '0');
763 SCREWUP("size not delimited");
765 static char *namebuf;
769 need = strlen(targ) + strlen(cp) + 250;
771 namebuf = xmalloc(need);
772 (void) sprintf(namebuf, "%s%s%s", targ,
773 *targ ? "/" : "", cp);
778 exists = stat(np, &stb) == 0;
780 int mod_flag = pflag;
782 if (!S_ISDIR(stb.st_mode)) {
787 (void) chmod(np, mode);
789 /* Handle copying from a read-only
792 if (mkdir(np, mode | S_IRWXU) < 0)
799 if (utimes(np, tv) < 0)
800 run_err("%s: set times: %s",
801 np, strerror(errno));
804 (void) chmod(np, mode);
809 if ((ofd = open(np, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
810 bad: run_err("%s: %s", np, strerror(errno));
813 (void) atomicio(write, remout, "", 1);
814 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
826 for (count = i = 0; i < size; i += 4096) {
832 j = read(remin, cp, amt);
833 if (j == -1 && (errno == EINTR || errno == EAGAIN)) {
836 run_err("%s", j ? strerror(errno) :
837 "dropped connection");
844 if (count == bp->cnt) {
845 /* Keep reading so we stay sync'd up. */
847 j = atomicio(write, ofd, bp->buf, count);
850 wrerrno = j >= 0 ? EIO : errno;
859 if (count != 0 && wrerr == NO &&
860 (j = atomicio(write, ofd, bp->buf, count)) != count) {
862 wrerrno = j >= 0 ? EIO : errno;
865 if (ftruncate(ofd, size)) {
866 run_err("%s: truncate: %s", np, strerror(errno));
871 if (exists || omode != mode)
873 if (fchmod(ofd, omode))
874 #else /* HAVE_FCHMOD */
875 if (chmod(np, omode))
876 #endif /* HAVE_FCHMOD */
877 run_err("%s: set mode: %s",
878 np, strerror(errno));
880 if (!exists && omode != mode)
882 if (fchmod(ofd, omode & ~mask))
883 #else /* HAVE_FCHMOD */
884 if (chmod(np, omode & ~mask))
885 #endif /* HAVE_FCHMOD */
886 run_err("%s: set mode: %s",
887 np, strerror(errno));
889 if (close(ofd) == -1) {
894 if (setimes && wrerr == NO) {
896 if (utimes(np, tv) < 0) {
897 run_err("%s: set times: %s",
898 np, strerror(errno));
904 run_err("%s: %s", np, strerror(wrerrno));
907 (void) atomicio(write, remout, "", 1);
914 run_err("protocol error: %s", why);
921 char ch, *cp, resp, rbuf[2048];
923 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
933 case 1: /* error, followed by error msg */
934 case 2: /* fatal error, "" */
936 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
939 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
942 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
954 (void) fprintf(stderr, "usage: scp "
955 "[-pqrvC46] [-S ssh] [-P port] [-c cipher] [-i identity] f1 f2; or:\n"
956 " scp [options] f1 ... fn directory\n");
961 run_err(const char *fmt,...)
967 if (fp == NULL && !(fp = fdopen(remout, "w")))
969 (void) fprintf(fp, "%c", 0x01);
970 (void) fprintf(fp, "scp: ");
972 (void) vfprintf(fp, fmt, ap);
974 (void) fprintf(fp, "\n");
979 vfprintf(stderr, fmt, ap);
981 fprintf(stderr, "\n");
991 if (*cp == ':') /* Leading colon is part of file name. */
997 if (*cp == '@' && *(cp+1) == '[')
999 if (*cp == ']' && *(cp+1) == ':' && flag)
1001 if (*cp == ':' && !flag)
1015 if (!stat(cp, &stb)) {
1016 if (S_ISDIR(stb.st_mode))
1020 run_err("%s: %s", cp, strerror(errno));
1036 if (!isalpha(c) && !isdigit(c) &&
1037 c != '_' && c != '-' && c != '.' && c != '+')
1042 bad: fprintf(stderr, "%s: invalid user name\n", cp0);
1047 allocbuf(bp, fd, blksize)
1052 #ifdef HAVE_ST_BLKSIZE
1055 if (fstat(fd, &stb) < 0) {
1056 run_err("fstat: %s", strerror(errno));
1059 if (stb.st_blksize == 0)
1062 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
1064 #else /* HAVE_ST_BLKSIZE */
1066 #endif /* HAVE_ST_BLKSIZE */
1067 if (bp->cnt >= size)
1069 if (bp->buf == NULL)
1070 bp->buf = xmalloc(size);
1072 bp->buf = xrealloc(bp->buf, size);
1082 fprintf(stderr, "lost connection\n");
1088 alarmtimer(int wait)
1090 struct itimerval itv;
1092 itv.it_value.tv_sec = wait;
1093 itv.it_value.tv_usec = 0;
1094 itv.it_interval = itv.it_value;
1095 setitimer(ITIMER_REAL, &itv, NULL);
1099 updateprogressmeter(int ignore)
1101 int save_errno = errno;
1110 static pid_t pgrp = -1;
1118 * Cygwin only supports tcgetpgrp() for getting the controlling tty
1121 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1124 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1125 ctty_pgrp == pgrp));
1130 progressmeter(int flag)
1132 static const char prefixes[] = " KMGTP";
1133 static struct timeval lastupdate;
1134 static off_t lastsize;
1135 struct timeval now, td, wait;
1136 off_t cursize, abbrevsize;
1138 int ratio, barlength, i, remaining;
1142 (void) gettimeofday(&start, (struct timezone *) 0);
1146 if (foregroundproc() == 0)
1149 (void) gettimeofday(&now, (struct timezone *) 0);
1150 cursize = statbytes;
1151 if (totalbytes != 0) {
1152 ratio = 100.0 * cursize / totalbytes;
1153 ratio = MAX(ratio, 0);
1154 ratio = MIN(ratio, 100);
1158 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
1160 barlength = getttywidth() - 51;
1161 barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH;
1162 if (barlength > 0) {
1163 i = barlength * ratio / 100;
1164 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1165 "|%.*s%*s|", i, BAR, barlength - i, "");
1168 abbrevsize = cursize;
1169 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1173 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5d %c%c ",
1174 (int) abbrevsize, prefixes[i], 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 struct sigaction sa;
1219 sa.sa_handler = updateprogressmeter;
1220 sigemptyset((sigset_t *)&sa.sa_mask);
1222 sa.sa_flags = SA_RESTART;
1224 sigaction(SIGALRM, &sa, NULL);
1226 } else if (flag == 1) {
1228 atomicio(write, fileno(stdout), "\n", 1);
1236 struct winsize winsize;
1238 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
1239 return (winsize.ws_col ? winsize.ws_col : 80);
1245 addargs(char *fmt, ...)
1251 vsnprintf(buf, sizeof(buf), fmt, ap);
1254 if (args.list == NULL) {
1257 args.list = xmalloc(args.nalloc * sizeof(char *));
1258 } else if (args.num+2 >= args.nalloc) {
1260 args.list = xrealloc(args.list, args.nalloc * sizeof(char *));
1262 args.list[args.num++] = xstrdup(buf);
1263 args.list[args.num] = NULL;