1 /* $OpenBSD: scp.c,v 1.143 2006/07/06 16:03:53 stevesk Exp $ */
3 * scp - secure remote copy. This is basically patched BSD rcp which
4 * uses ssh to do the data transfer (instead of using rcmd).
6 * NOTE: This version should NOT be suid root. (This uses ssh to
7 * do the transfer and ssh has the necessary privileges.)
9 * 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi>
11 * As far as I am concerned, the code I have written for this software
12 * can be used freely for any purpose. Any derived versions of this
13 * software must be clearly marked as such, and if the derived work is
14 * incompatible with the protocol description in the RFC file, it must be
15 * called by a name other than "ssh" or "Secure Shell".
18 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
19 * Copyright (c) 1999 Aaron Campbell. All rights reserved.
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 * Copyright (c) 1983, 1990, 1992, 1993, 1995
46 * The Regents of the University of California. All rights reserved.
48 * Redistribution and use in source and binary forms, with or without
49 * modification, are permitted provided that the following conditions
51 * 1. Redistributions of source code must retain the above copyright
52 * notice, this list of conditions and the following disclaimer.
53 * 2. Redistributions in binary form must reproduce the above copyright
54 * notice, this list of conditions and the following disclaimer in the
55 * documentation and/or other materials provided with the distribution.
56 * 3. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
76 #include <sys/types.h>
77 #ifdef HAVE_SYS_STAT_H
78 # include <sys/stat.h>
89 #include "pathnames.h"
92 #include "progressmeter.h"
94 extern char *__progname;
96 int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout);
100 /* Struct for addargs */
103 /* Bandwidth limit */
104 off_t limit_rate = 0;
106 /* Name of current file being transferred. */
109 /* This is set to non-zero to enable verbose mode. */
110 int verbose_mode = 0;
112 /* This is set to zero if the progressmeter is not desired. */
113 int showprogress = 1;
115 /* This is the program to execute for the secured connection. ("ssh" or -S) */
116 char *ssh_program = _PATH_SSH_PROGRAM;
118 /* This is used to store the pid of ssh_program */
119 pid_t do_cmd_pid = -1;
124 if (do_cmd_pid > 1) {
125 kill(do_cmd_pid, signo ? signo : SIGTERM);
126 waitpid(do_cmd_pid, NULL, 0);
135 do_local_cmd(arglist *a)
142 fatal("do_local_cmd: no arguments");
145 fprintf(stderr, "Executing:");
146 for (i = 0; i < a->num; i++)
147 fprintf(stderr, " %s", a->list[i]);
148 fprintf(stderr, "\n");
150 if ((pid = fork()) == -1)
151 fatal("do_local_cmd: fork: %s", strerror(errno));
154 execvp(a->list[0], a->list);
160 signal(SIGTERM, killchild);
161 signal(SIGINT, killchild);
162 signal(SIGHUP, killchild);
164 while (waitpid(pid, &status, 0) == -1)
166 fatal("do_local_cmd: waitpid: %s", strerror(errno));
170 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
177 * This function executes the given command as the specified user on the
178 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
179 * assigns the input and output file descriptors on success.
183 do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout)
185 int pin[2], pout[2], reserved[2];
189 "Executing: program %s host %s, user %s, command %s\n",
191 remuser ? remuser : "(unspecified)", cmd);
194 * Reserve two descriptors so that the real pipes won't get
195 * descriptors 0 and 1 because that will screw up dup2 below.
197 if (pipe(reserved) < 0)
198 fatal("pipe: %s", strerror(errno));
200 /* Create a socket pair for communicating with ssh. */
202 fatal("pipe: %s", strerror(errno));
204 fatal("pipe: %s", strerror(errno));
206 /* Free the reserved descriptors. */
210 /* Fork a child to execute the command on the remote host using ssh. */
212 if (do_cmd_pid == 0) {
221 replacearg(&args, 0, "%s", ssh_program);
223 addargs(&args, "-l%s", remuser);
224 addargs(&args, "%s", host);
225 addargs(&args, "%s", cmd);
227 execvp(ssh_program, args.list);
230 } else if (do_cmd_pid == -1) {
231 fatal("fork: %s", strerror(errno));
233 /* Parent. Close the other side, and return the local side. */
238 signal(SIGTERM, killchild);
239 signal(SIGINT, killchild);
240 signal(SIGHUP, killchild);
249 BUF *allocbuf(BUF *, int, int);
252 void run_err(const char *,...);
253 void verifydir(char *);
257 int errs, remin, remout;
258 int pflag, iamremote, iamrecursive, targetshouldbedirectory;
261 char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
264 void rsource(char *, struct stat *);
265 void sink(int, char *[]);
266 void source(int, char *[]);
267 void tolocal(int, char *[]);
268 void toremote(char *, int, char *[]);
272 main(int argc, char **argv)
274 int ch, fflag, tflag, status;
280 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
283 __progname = ssh_get_progname(argv[0]);
285 memset(&args, '\0', sizeof(args));
287 addargs(&args, "%s", ssh_program);
288 addargs(&args, "-x");
289 addargs(&args, "-oForwardAgent no");
290 addargs(&args, "-oPermitLocalCommand no");
291 addargs(&args, "-oClearAllForwardings yes");
294 while ((ch = getopt(argc, argv, "dfl:prtvBCc:i:P:q1246S:o:F:")) != -1)
296 /* User-visible flags. */
302 addargs(&args, "-%c", ch);
308 addargs(&args, "-%c%s", ch, optarg);
311 addargs(&args, "-p%s", optarg);
314 addargs(&args, "-oBatchmode yes");
317 speed = strtod(optarg, &endp);
318 if (speed <= 0 || *endp != '\0')
320 limit_rate = speed * 1024;
329 ssh_program = xstrdup(optarg);
332 addargs(&args, "-v");
336 addargs(&args, "-q");
340 /* Server options. */
342 targetshouldbedirectory = 1;
344 case 'f': /* "from" */
352 setmode(0, O_BINARY);
361 if ((pwd = getpwuid(userid = getuid())) == NULL)
362 fatal("unknown user %u", (u_int) userid);
364 if (!isatty(STDERR_FILENO))
367 remin = STDIN_FILENO;
368 remout = STDOUT_FILENO;
371 /* Follow "protocol", send data. */
384 targetshouldbedirectory = 1;
388 /* Command to be executed on remote system using "ssh". */
389 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
390 verbose_mode ? " -v" : "",
391 iamrecursive ? " -r" : "", pflag ? " -p" : "",
392 targetshouldbedirectory ? " -d" : "");
394 (void) signal(SIGPIPE, lostconn);
396 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
397 toremote(targ, argc, argv);
399 if (targetshouldbedirectory)
400 verifydir(argv[argc - 1]);
401 tolocal(argc, argv); /* Dest is local host. */
404 * Finally check the exit status of the ssh process, if one was forked
405 * and no error has occured yet
407 if (do_cmd_pid != -1 && errs == 0) {
411 (void) close(remout);
412 if (waitpid(do_cmd_pid, &status, 0) == -1)
415 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
423 toremote(char *targ, int argc, char **argv)
425 char *bp, *host, *src, *suser, *thost, *tuser, *arg;
429 memset(&alist, '\0', sizeof(alist));
436 arg = xstrdup(argv[argc - 1]);
437 if ((thost = strrchr(arg, '@'))) {
448 if (tuser != NULL && !okname(tuser)) {
453 for (i = 0; i < argc - 1; i++) {
454 src = colon(argv[i]);
455 if (src) { /* remote to remote */
457 addargs(&alist, "%s", ssh_program);
459 addargs(&alist, "-v");
460 addargs(&alist, "-x");
461 addargs(&alist, "-oClearAllForwardings yes");
462 addargs(&alist, "-n");
467 host = strrchr(argv[i], '@');
471 host = cleanhostname(host);
474 suser = pwd->pw_name;
475 else if (!okname(suser))
477 addargs(&alist, "-l");
478 addargs(&alist, "%s", suser);
480 host = cleanhostname(argv[i]);
482 addargs(&alist, "%s", host);
483 addargs(&alist, "%s", cmd);
484 addargs(&alist, "%s", src);
485 addargs(&alist, "%s%s%s:%s",
486 tuser ? tuser : "", tuser ? "@" : "",
488 if (do_local_cmd(&alist) != 0)
490 } else { /* local to remote */
492 xasprintf(&bp, "%s -t %s", cmd, targ);
493 host = cleanhostname(thost);
494 if (do_cmd(host, tuser, bp, &remin,
508 tolocal(int argc, char **argv)
510 char *bp, *host, *src, *suser;
514 memset(&alist, '\0', sizeof(alist));
517 for (i = 0; i < argc - 1; i++) {
518 if (!(src = colon(argv[i]))) { /* Local to local. */
520 addargs(&alist, "%s", _PATH_CP);
522 addargs(&alist, "-r");
524 addargs(&alist, "-p");
525 addargs(&alist, "%s", argv[i]);
526 addargs(&alist, "%s", argv[argc-1]);
527 if (do_local_cmd(&alist))
534 if ((host = strrchr(argv[i], '@')) == NULL) {
541 suser = pwd->pw_name;
543 host = cleanhostname(host);
544 xasprintf(&bp, "%s -f %s", cmd, src);
545 if (do_cmd(host, suser, bp, &remin, &remout) < 0) {
551 sink(1, argv + argc - 1);
558 source(int argc, char **argv)
563 off_t i, amt, statbytes;
565 int fd = -1, haderr, indx;
566 char *last, *name, buf[2048];
569 for (indx = 0; indx < argc; ++indx) {
573 while (len > 1 && name[len-1] == '/')
575 if (strchr(name, '\n') != NULL) {
576 run_err("%s: skipping, filename contains a newline",
580 if ((fd = open(name, O_RDONLY, 0)) < 0)
582 if (fstat(fd, &stb) < 0) {
583 syserr: run_err("%s: %s", name, strerror(errno));
586 switch (stb.st_mode & S_IFMT) {
596 run_err("%s: not a regular file", name);
599 if ((last = strrchr(name, '/')) == NULL)
606 * Make it compatible with possible future
607 * versions expecting microseconds.
609 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
610 (u_long) stb.st_mtime,
611 (u_long) stb.st_atime);
612 (void) atomicio(vwrite, remout, buf, strlen(buf));
616 #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
617 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
618 (u_int) (stb.st_mode & FILEMODEMASK),
619 (long long)stb.st_size, last);
621 fprintf(stderr, "Sending file modes: %s", buf);
623 (void) atomicio(vwrite, remout, buf, strlen(buf));
626 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
627 next: if (fd != -1) {
634 start_progress_meter(curfile, stb.st_size, &statbytes);
635 /* Keep writing after an error so that we stay sync'd up. */
636 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
638 if (i + amt > stb.st_size)
639 amt = stb.st_size - i;
641 result = atomicio(read, fd, bp->buf, amt);
646 (void) atomicio(vwrite, remout, bp->buf, amt);
648 result = atomicio(vwrite, remout, bp->buf, amt);
657 stop_progress_meter();
660 if (close(fd) < 0 && !haderr)
665 (void) atomicio(vwrite, remout, "", 1);
667 run_err("%s: %s", name, strerror(haderr));
673 rsource(char *name, struct stat *statp)
677 char *last, *vect[1], path[1100];
679 if (!(dirp = opendir(name))) {
680 run_err("%s: %s", name, strerror(errno));
683 last = strrchr(name, '/');
689 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
690 (u_long) statp->st_mtime,
691 (u_long) statp->st_atime);
692 (void) atomicio(vwrite, remout, path, strlen(path));
693 if (response() < 0) {
698 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
699 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
701 fprintf(stderr, "Entering directory: %s", path);
702 (void) atomicio(vwrite, remout, path, strlen(path));
703 if (response() < 0) {
707 while ((dp = readdir(dirp)) != NULL) {
710 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
712 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
713 run_err("%s/%s: name too long", name, dp->d_name);
716 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
720 (void) closedir(dirp);
721 (void) atomicio(vwrite, remout, "E\n", 2);
728 static struct timeval bwstart, bwend;
729 static int lamt, thresh = 16384;
731 struct timespec ts, rm;
733 if (!timerisset(&bwstart)) {
734 gettimeofday(&bwstart, NULL);
742 gettimeofday(&bwend, NULL);
743 timersub(&bwend, &bwstart, &bwend);
744 if (!timerisset(&bwend))
748 waitlen = (double)1000000L * lamt / limit_rate;
750 bwstart.tv_sec = waitlen / 1000000L;
751 bwstart.tv_usec = waitlen % 1000000L;
753 if (timercmp(&bwstart, &bwend, >)) {
754 timersub(&bwstart, &bwend, &bwend);
756 /* Adjust the wait time */
761 } else if (bwend.tv_usec < 100) {
767 TIMEVAL_TO_TIMESPEC(&bwend, &ts);
768 while (nanosleep(&ts, &rm) == -1) {
776 gettimeofday(&bwstart, NULL);
780 sink(int argc, char **argv)
790 int amt, exists, first, ofd;
791 mode_t mode, omode, mask;
792 off_t size, statbytes;
793 int setimes, targisdir, wrerrno = 0;
794 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
795 struct timeval tv[2];
799 #define SCREWUP(str) { why = str; goto screwup; }
801 setimes = targisdir = 0;
806 run_err("ambiguous target");
810 if (targetshouldbedirectory)
813 (void) atomicio(vwrite, remout, "", 1);
814 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
816 for (first = 1;; first = 0) {
818 if (atomicio(read, remin, cp, 1) != 1)
821 SCREWUP("unexpected <newline>");
823 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
824 SCREWUP("lost connection");
826 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
829 fprintf(stderr, "Sink: %s", buf);
831 if (buf[0] == '\01' || buf[0] == '\02') {
833 (void) atomicio(vwrite, STDERR_FILENO,
834 buf + 1, strlen(buf + 1));
841 (void) atomicio(vwrite, remout, "", 1);
851 mtime.tv_sec = strtol(cp, &cp, 10);
852 if (!cp || *cp++ != ' ')
853 SCREWUP("mtime.sec not delimited");
854 mtime.tv_usec = strtol(cp, &cp, 10);
855 if (!cp || *cp++ != ' ')
856 SCREWUP("mtime.usec not delimited");
857 atime.tv_sec = strtol(cp, &cp, 10);
858 if (!cp || *cp++ != ' ')
859 SCREWUP("atime.sec not delimited");
860 atime.tv_usec = strtol(cp, &cp, 10);
861 if (!cp || *cp++ != '\0')
862 SCREWUP("atime.usec not delimited");
863 (void) atomicio(vwrite, remout, "", 1);
866 if (*cp != 'C' && *cp != 'D') {
868 * Check for the case "rcp remote:foo\* local:bar".
869 * In this case, the line "No match." can be returned
870 * by the shell before the rcp command on the remote is
871 * executed so the ^Aerror_message convention isn't
878 SCREWUP("expected control record");
881 for (++cp; cp < buf + 5; cp++) {
882 if (*cp < '0' || *cp > '7')
884 mode = (mode << 3) | (*cp - '0');
887 SCREWUP("mode not delimited");
889 for (size = 0; isdigit(*cp);)
890 size = size * 10 + (*cp++ - '0');
892 SCREWUP("size not delimited");
893 if ((strchr(cp, '/') != NULL) || (strcmp(cp, "..") == 0)) {
894 run_err("error: unexpected filename: %s", cp);
898 static char *namebuf;
899 static size_t cursize;
902 need = strlen(targ) + strlen(cp) + 250;
903 if (need > cursize) {
906 namebuf = xmalloc(need);
909 (void) snprintf(namebuf, need, "%s%s%s", targ,
910 strcmp(targ, "/") ? "/" : "", cp);
915 exists = stat(np, &stb) == 0;
917 int mod_flag = pflag;
919 SCREWUP("received directory without -r");
921 if (!S_ISDIR(stb.st_mode)) {
926 (void) chmod(np, mode);
928 /* Handle copying from a read-only
931 if (mkdir(np, mode | S_IRWXU) < 0)
934 vect[0] = xstrdup(np);
938 if (utimes(vect[0], tv) < 0)
939 run_err("%s: set times: %s",
940 vect[0], strerror(errno));
943 (void) chmod(vect[0], mode);
950 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
951 bad: run_err("%s: %s", np, strerror(errno));
954 (void) atomicio(vwrite, remout, "", 1);
955 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
964 start_progress_meter(curfile, size, &statbytes);
965 for (count = i = 0; i < size; i += 4096) {
971 j = atomicio(read, remin, cp, amt);
973 run_err("%s", j ? strerror(errno) :
974 "dropped connection");
985 if (count == bp->cnt) {
986 /* Keep reading so we stay sync'd up. */
988 if (atomicio(vwrite, ofd, bp->buf,
999 stop_progress_meter();
1000 if (count != 0 && wrerr == NO &&
1001 atomicio(vwrite, ofd, bp->buf, count) != count) {
1005 if (wrerr == NO && ftruncate(ofd, size) != 0) {
1006 run_err("%s: truncate: %s", np, strerror(errno));
1010 if (exists || omode != mode)
1012 if (fchmod(ofd, omode)) {
1013 #else /* HAVE_FCHMOD */
1014 if (chmod(np, omode)) {
1015 #endif /* HAVE_FCHMOD */
1016 run_err("%s: set mode: %s",
1017 np, strerror(errno));
1021 if (!exists && omode != mode)
1023 if (fchmod(ofd, omode & ~mask)) {
1024 #else /* HAVE_FCHMOD */
1025 if (chmod(np, omode & ~mask)) {
1026 #endif /* HAVE_FCHMOD */
1027 run_err("%s: set mode: %s",
1028 np, strerror(errno));
1032 if (close(ofd) == -1) {
1037 if (setimes && wrerr == NO) {
1039 if (utimes(np, tv) < 0) {
1040 run_err("%s: set times: %s",
1041 np, strerror(errno));
1047 run_err("%s: %s", np, strerror(wrerrno));
1050 (void) atomicio(vwrite, remout, "", 1);
1057 run_err("protocol error: %s", why);
1064 char ch, *cp, resp, rbuf[2048];
1066 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
1076 case 1: /* error, followed by error msg */
1077 case 2: /* fatal error, "" */
1079 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
1082 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
1085 (void) atomicio(vwrite, STDERR_FILENO, rbuf, cp - rbuf);
1097 (void) fprintf(stderr,
1098 "usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
1099 " [-l limit] [-o ssh_option] [-P port] [-S program]\n"
1100 " [[user@]host1:]file1 [...] [[user@]host2:]file2\n");
1105 run_err(const char *fmt,...)
1111 if (fp != NULL || (remout != -1 && (fp = fdopen(remout, "w")))) {
1112 (void) fprintf(fp, "%c", 0x01);
1113 (void) fprintf(fp, "scp: ");
1115 (void) vfprintf(fp, fmt, ap);
1117 (void) fprintf(fp, "\n");
1123 vfprintf(stderr, fmt, ap);
1125 fprintf(stderr, "\n");
1134 if (!stat(cp, &stb)) {
1135 if (S_ISDIR(stb.st_mode))
1139 run_err("%s: %s", cp, strerror(errno));
1154 if (!isalpha(c) && !isdigit(c)) {
1169 bad: fprintf(stderr, "%s: invalid user name\n", cp0);
1174 allocbuf(BUF *bp, int fd, int blksize)
1177 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1180 if (fstat(fd, &stb) < 0) {
1181 run_err("fstat: %s", strerror(errno));
1184 size = roundup(stb.st_blksize, blksize);
1187 #else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1189 #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1190 if (bp->cnt >= size)
1192 if (bp->buf == NULL)
1193 bp->buf = xmalloc(size);
1195 bp->buf = xrealloc(bp->buf, 1, size);
1196 memset(bp->buf, 0, size);
1205 write(STDERR_FILENO, "lost connection\n", 16);