3 * scp - secure remote copy. This is basically patched BSD rcp which uses ssh
4 * to do the data transfer (instead of using rcmd).
6 * NOTE: This version should NOT be suid root. (This uses ssh to do the transfer
7 * and ssh has the necessary privileges.)
9 * 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi>
14 * Copyright (c) 1983, 1990, 1992, 1993, 1995
15 * The Regents of the University of California. All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. All advertising materials mentioning features or use of this software
26 * must display the following acknowledgement:
27 * This product includes software developed by the University of
28 * California, Berkeley and its contributors.
29 * 4. Neither the name of the University nor the names of its contributors
30 * may be used to endorse or promote products derived from this software
31 * without specific prior written permission.
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 RCSID("$OpenBSD: scp.c,v 1.32 2000/06/20 01:39:44 markus Exp $");
56 /* For progressmeter() -- number of seconds before xfer considered "stalled" */
59 /* Visual statistics about files as they are transferred. */
60 void progressmeter(int);
62 /* Returns width of the terminal (for progress meter calculations). */
63 int getttywidth(void);
65 /* Time a transfer started. */
66 static struct timeval start;
68 /* Number of bytes of current file transferred so far. */
69 volatile unsigned long statbytes;
71 /* Total size of current file. */
74 /* Name of current file being transferred. */
77 /* This is set to non-zero if IPv4 is desired. */
80 /* This is set to non-zero if IPv6 is desired. */
83 /* This is set to non-zero to enable verbose mode. */
86 /* This is set to non-zero if compression is desired. */
87 int compress_flag = 0;
89 /* This is set to zero if the progressmeter is not desired. */
92 /* This is set to non-zero if running in batch mode (that is, password
93 and passphrase queries are not allowed). */
96 /* This is set to the cipher type string if given on the command line. */
99 /* This is set to the RSA authentication identity file name if given on
101 char *identity = NULL;
103 /* This is the port to use in contacting the remote site (is non-NULL). */
107 * This function executes the given command as the specified user on the
108 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
109 * assigns the input and output file descriptors on success.
113 do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout)
115 int pin[2], pout[2], reserved[2];
118 fprintf(stderr, "Executing: host %s, user %s, command %s\n",
119 host, remuser ? remuser : "(unspecified)", cmd);
122 * Reserve two descriptors so that the real pipes won't get
123 * descriptors 0 and 1 because that will screw up dup2 below.
127 /* Create a socket pair for communicating with ssh. */
129 fatal("pipe: %s", strerror(errno));
131 fatal("pipe: %s", strerror(errno));
133 /* Free the reserved descriptors. */
137 /* For a child to execute the command on the remote host using ssh. */
151 args[i++] = SSH_PROGRAM;
153 args[i++] = "-oFallBackToRsh no";
163 args[i++] = "-oBatchMode yes";
164 if (cipher != NULL) {
168 if (identity != NULL) {
170 args[i++] = identity;
176 if (remuser != NULL) {
184 execvp(SSH_PROGRAM, args);
188 /* Parent. Close the other side, and return the local side. */
197 fatal(const char *fmt,...)
203 vsnprintf(buf, sizeof(buf), fmt, ap);
205 fprintf(stderr, "%s\n", buf);
209 /* This stuff used to be in BSD rcp extern.h. */
216 extern int iamremote;
218 BUF *allocbuf(BUF *, int, int);
223 void run_err(const char *,...);
224 void verifydir(char *);
226 /* Stuff from BSD rcp.c continues. */
230 int errs, remin, remout;
231 int pflag, iamremote, iamrecursive, targetshouldbedirectory;
234 char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
237 void rsource(char *, struct stat *);
238 void sink(int, char *[]);
239 void source(int, char *[]);
240 void tolocal(int, char *[]);
241 void toremote(char *, int, char *[]);
249 int ch, fflag, tflag;
255 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46")) != EOF)
257 /* User-visible flags. */
273 /* Server options. */
275 targetshouldbedirectory = 1;
277 case 'f': /* "from" */
310 if ((pwd = getpwuid(userid = getuid())) == NULL)
311 fatal("unknown user %d", (int) userid);
313 if (!isatty(STDERR_FILENO))
316 remin = STDIN_FILENO;
317 remout = STDOUT_FILENO;
320 /* Follow "protocol", send data. */
333 targetshouldbedirectory = 1;
336 /* Command to be executed on remote system using "ssh". */
337 (void) sprintf(cmd, "scp%s%s%s%s", verbose_mode ? " -v" : "",
338 iamrecursive ? " -r" : "", pflag ? " -p" : "",
339 targetshouldbedirectory ? " -d" : "");
341 (void) signal(SIGPIPE, lostconn);
343 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
344 toremote(targ, argc, argv);
346 tolocal(argc, argv); /* Dest is local host. */
347 if (targetshouldbedirectory)
348 verifydir(argv[argc - 1]);
357 if (*host == '[' && host[strlen(host) - 1] == ']') {
358 host[strlen(host) - 1] = '\0';
365 toremote(targ, argc, argv)
370 char *bp, *host, *src, *suser, *thost, *tuser;
376 if ((thost = strchr(argv[argc - 1], '@'))) {
379 tuser = argv[argc - 1];
382 else if (!okname(tuser))
385 thost = argv[argc - 1];
389 for (i = 0; i < argc - 1; i++) {
390 src = colon(argv[i]);
391 if (src) { /* remote to remote */
395 host = strchr(argv[i], '@');
396 len = strlen(SSH_PROGRAM) + strlen(argv[i]) +
397 strlen(src) + (tuser ? strlen(tuser) : 0) +
398 strlen(thost) + strlen(targ) + CMDNEEDS + 32;
402 host = cleanhostname(host);
405 suser = pwd->pw_name;
406 else if (!okname(suser))
409 "%s%s -x -o'FallBackToRsh no' -n -l %s %s %s %s '%s%s%s:%s'",
410 SSH_PROGRAM, verbose_mode ? " -v" : "",
411 suser, host, cmd, src,
412 tuser ? tuser : "", tuser ? "@" : "",
415 host = cleanhostname(argv[i]);
417 "exec %s%s -x -o'FallBackToRsh no' -n %s %s %s '%s%s%s:%s'",
418 SSH_PROGRAM, verbose_mode ? " -v" : "",
420 tuser ? tuser : "", tuser ? "@" : "",
424 fprintf(stderr, "Executing: %s\n", bp);
427 } else { /* local to remote */
429 len = strlen(targ) + CMDNEEDS + 20;
431 (void) sprintf(bp, "%s -t %s", cmd, targ);
432 host = cleanhostname(thost);
433 if (do_cmd(host, tuser,
434 bp, &remin, &remout) < 0)
451 char *bp, *host, *src, *suser;
453 for (i = 0; i < argc - 1; i++) {
454 if (!(src = colon(argv[i]))) { /* Local to local. */
455 len = strlen(_PATH_CP) + strlen(argv[i]) +
456 strlen(argv[argc - 1]) + 20;
458 (void) sprintf(bp, "exec %s%s%s %s %s", _PATH_CP,
459 iamrecursive ? " -r" : "", pflag ? " -p" : "",
460 argv[i], argv[argc - 1]);
462 fprintf(stderr, "Executing: %s\n", bp);
471 if ((host = strchr(argv[i], '@')) == NULL) {
478 suser = pwd->pw_name;
479 else if (!okname(suser))
482 host = cleanhostname(host);
483 len = strlen(src) + CMDNEEDS + 20;
485 (void) sprintf(bp, "%s -f %s", cmd, src);
486 if (do_cmd(host, suser, bp, &remin, &remout) < 0) {
492 sink(1, argv + argc - 1);
507 int amt, fd, haderr, indx, result;
508 char *last, *name, buf[2048];
510 for (indx = 0; indx < argc; ++indx) {
513 if ((fd = open(name, O_RDONLY, 0)) < 0)
515 if (fstat(fd, &stb) < 0) {
516 syserr: run_err("%s: %s", name, strerror(errno));
519 switch (stb.st_mode & S_IFMT) {
529 run_err("%s: not a regular file", name);
532 if ((last = strrchr(name, '/')) == NULL)
539 * Make it compatible with possible future
540 * versions expecting microseconds.
542 (void) sprintf(buf, "T%lu 0 %lu 0\n",
543 (unsigned long) stb.st_mtime,
544 (unsigned long) stb.st_atime);
545 (void) atomicio(write, remout, buf, strlen(buf));
549 #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
550 (void) sprintf(buf, "C%04o %lu %s\n",
551 (unsigned int) (stb.st_mode & FILEMODEMASK),
552 (unsigned long) stb.st_size,
555 fprintf(stderr, "Sending file modes: %s", buf);
558 (void) atomicio(write, remout, buf, strlen(buf));
561 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
562 next: (void) close(fd);
566 totalbytes = stb.st_size;
569 /* Keep writing after an error so that we stay sync'd up. */
570 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
572 if (i + amt > stb.st_size)
573 amt = stb.st_size - i;
575 result = atomicio(read, fd, bp->buf, amt);
577 haderr = result >= 0 ? EIO : errno;
580 (void) atomicio(write, remout, bp->buf, amt);
582 result = atomicio(write, remout, bp->buf, amt);
584 haderr = result >= 0 ? EIO : errno;
591 if (close(fd) < 0 && !haderr)
594 (void) atomicio(write, remout, "", 1);
596 run_err("%s: %s", name, strerror(haderr));
608 char *last, *vect[1], path[1100];
610 if (!(dirp = opendir(name))) {
611 run_err("%s: %s", name, strerror(errno));
614 last = strrchr(name, '/');
620 (void) sprintf(path, "T%lu 0 %lu 0\n",
621 (unsigned long) statp->st_mtime,
622 (unsigned long) statp->st_atime);
623 (void) atomicio(write, remout, path, strlen(path));
624 if (response() < 0) {
629 (void) sprintf(path, "D%04o %d %.1024s\n",
630 (unsigned int) (statp->st_mode & FILEMODEMASK),
633 fprintf(stderr, "Entering directory: %s", path);
634 (void) atomicio(write, remout, path, strlen(path));
635 if (response() < 0) {
639 while ((dp = readdir(dirp))) {
642 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
644 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
645 run_err("%s/%s: name too long", name, dp->d_name);
648 (void) sprintf(path, "%s/%s", name, dp->d_name);
652 (void) closedir(dirp);
653 (void) atomicio(write, remout, "E\n", 2);
669 int amt, count, exists, first, mask, mode, ofd, omode;
670 int setimes, size, targisdir, wrerrno = 0;
671 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
675 #define SCREWUP(str) { why = str; goto screwup; }
677 setimes = targisdir = 0;
682 run_err("ambiguous target");
686 if (targetshouldbedirectory)
689 (void) atomicio(write, remout, "", 1);
690 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
692 for (first = 1;; first = 0) {
694 if (atomicio(read, remin, cp, 1) <= 0)
697 SCREWUP("unexpected <newline>");
699 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
700 SCREWUP("lost connection");
702 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
705 if (buf[0] == '\01' || buf[0] == '\02') {
707 (void) atomicio(write, STDERR_FILENO,
708 buf + 1, strlen(buf + 1));
715 (void) atomicio(write, remout, "", 1);
721 #define getnum(t) (t) = 0; \
722 while (*cp >= '0' && *cp <= '9') (t) = (t) * 10 + (*cp++ - '0');
729 SCREWUP("mtime.sec not delimited");
732 SCREWUP("mtime.usec not delimited");
735 SCREWUP("atime.sec not delimited");
738 SCREWUP("atime.usec not delimited");
739 (void) atomicio(write, remout, "", 1);
742 if (*cp != 'C' && *cp != 'D') {
744 * Check for the case "rcp remote:foo\* local:bar".
745 * In this case, the line "No match." can be returned
746 * by the shell before the rcp command on the remote is
747 * executed so the ^Aerror_message convention isn't
754 SCREWUP("expected control record");
757 for (++cp; cp < buf + 5; cp++) {
758 if (*cp < '0' || *cp > '7')
760 mode = (mode << 3) | (*cp - '0');
763 SCREWUP("mode not delimited");
765 for (size = 0; *cp >= '0' && *cp <= '9';)
766 size = size * 10 + (*cp++ - '0');
768 SCREWUP("size not delimited");
770 static char *namebuf;
774 need = strlen(targ) + strlen(cp) + 250;
776 namebuf = xmalloc(need);
777 (void) sprintf(namebuf, "%s%s%s", targ,
778 *targ ? "/" : "", cp);
783 exists = stat(np, &stb) == 0;
785 int mod_flag = pflag;
787 if (!S_ISDIR(stb.st_mode)) {
792 (void) chmod(np, mode);
794 /* Handle copying from a read-only
797 if (mkdir(np, mode | S_IRWXU) < 0)
804 if (utime(np, &ut) < 0)
805 run_err("%s: set times: %s",
806 np, strerror(errno));
809 (void) chmod(np, mode);
814 if ((ofd = open(np, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
815 bad: run_err("%s: %s", np, strerror(errno));
818 (void) atomicio(write, remout, "", 1);
819 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
831 for (count = i = 0; i < size; i += 4096) {
837 j = atomicio(read, remin, cp, amt);
839 run_err("%s", j ? strerror(errno) :
840 "dropped connection");
847 if (count == bp->cnt) {
848 /* Keep reading so we stay sync'd up. */
850 j = atomicio(write, ofd, bp->buf, count);
853 wrerrno = j >= 0 ? EIO : errno;
862 if (count != 0 && wrerr == NO &&
863 (j = atomicio(write, ofd, bp->buf, count)) != count) {
865 wrerrno = j >= 0 ? EIO : errno;
868 if (ftruncate(ofd, size)) {
869 run_err("%s: truncate: %s", np, strerror(errno));
874 if (exists || omode != mode)
875 if (fchmod(ofd, omode))
876 run_err("%s: set mode: %s",
877 np, strerror(errno));
879 if (!exists && omode != mode)
880 if (fchmod(ofd, omode & ~mask))
881 run_err("%s: set mode: %s",
882 np, strerror(errno));
886 if (setimes && wrerr == NO) {
888 if (utime(np, &ut) < 0) {
889 run_err("%s: set times: %s",
890 np, strerror(errno));
896 run_err("%s: %s", np, strerror(wrerrno));
899 (void) atomicio(write, remout, "", 1);
906 run_err("protocol error: %s", why);
913 char ch, *cp, resp, rbuf[2048];
915 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
925 case 1: /* error, followed by error msg */
926 case 2: /* fatal error, "" */
928 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
931 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
934 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
946 (void) fprintf(stderr,
947 "usage: scp [-pqrvC46] [-P port] [-c cipher] [-i identity] f1 f2; or:\n scp [options] f1 ... fn directory\n");
952 run_err(const char *fmt,...)
958 if (fp == NULL && !(fp = fdopen(remout, "w")))
960 (void) fprintf(fp, "%c", 0x01);
961 (void) fprintf(fp, "scp: ");
963 (void) vfprintf(fp, fmt, ap);
965 (void) fprintf(fp, "\n");
970 vfprintf(stderr, fmt, ap);
972 fprintf(stderr, "\n");
976 /* Stuff below is from BSD rcp util.c. */
979 * Copyright (c) 1992, 1993
980 * The Regents of the University of California. All rights reserved.
982 * Redistribution and use in source and binary forms, with or without
983 * modification, are permitted provided that the following conditions
985 * 1. Redistributions of source code must retain the above copyright
986 * notice, this list of conditions and the following disclaimer.
987 * 2. Redistributions in binary form must reproduce the above copyright
988 * notice, this list of conditions and the following disclaimer in the
989 * documentation and/or other materials provided with the distribution.
990 * 3. All advertising materials mentioning features or use of this software
991 * must display the following acknowledgement:
992 * This product includes software developed by the University of
993 * California, Berkeley and its contributors.
994 * 4. Neither the name of the University nor the names of its contributors
995 * may be used to endorse or promote products derived from this software
996 * without specific prior written permission.
998 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
999 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1000 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1001 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1002 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1003 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1004 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1005 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1006 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1007 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1010 * $OpenBSD: scp.c,v 1.32 2000/06/20 01:39:44 markus Exp $
1019 if (*cp == ':') /* Leading colon is part of file name. */
1025 if (*cp == '@' && *(cp+1) == '[')
1027 if (*cp == ']' && *(cp+1) == ':' && flag)
1029 if (*cp == ':' && !flag)
1043 if (!stat(cp, &stb)) {
1044 if (S_ISDIR(stb.st_mode))
1048 run_err("%s: %s", cp, strerror(errno));
1064 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
1069 bad: fprintf(stderr, "%s: invalid user name\n", cp0);
1074 allocbuf(bp, fd, blksize)
1081 if (fstat(fd, &stb) < 0) {
1082 run_err("fstat: %s", strerror(errno));
1085 if (stb.st_blksize == 0)
1088 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
1090 if (bp->cnt >= size)
1092 if (bp->buf == NULL)
1093 bp->buf = xmalloc(size);
1095 bp->buf = xrealloc(bp->buf, size);
1105 fprintf(stderr, "lost connection\n");
1111 alarmtimer(int wait)
1113 struct itimerval itv;
1115 itv.it_value.tv_sec = wait;
1116 itv.it_value.tv_usec = 0;
1117 itv.it_interval = itv.it_value;
1118 setitimer(ITIMER_REAL, &itv, NULL);
1122 updateprogressmeter(int ignore)
1124 int save_errno = errno;
1133 static pid_t pgrp = -1;
1139 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1140 ctty_pgrp == pgrp));
1144 progressmeter(int flag)
1146 static const char prefixes[] = " KMGTP";
1147 static struct timeval lastupdate;
1148 static off_t lastsize;
1149 struct timeval now, td, wait;
1150 off_t cursize, abbrevsize;
1152 int ratio, barlength, i, remaining;
1156 (void) gettimeofday(&start, (struct timezone *) 0);
1160 if (foregroundproc() == 0)
1163 (void) gettimeofday(&now, (struct timezone *) 0);
1164 cursize = statbytes;
1165 if (totalbytes != 0) {
1166 ratio = 100.0 * cursize / totalbytes;
1167 ratio = MAX(ratio, 0);
1168 ratio = MIN(ratio, 100);
1172 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
1174 barlength = getttywidth() - 51;
1175 if (barlength > 0) {
1176 i = barlength * ratio / 100;
1177 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1179 "*****************************************************************************"
1180 "*****************************************************************************",
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(&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);