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. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
74 RCSID("$OpenBSD: scp.c,v 1.131 2006/02/08 23:51:24 stevesk Exp $");
80 #include "pathnames.h"
83 #include "progressmeter.h"
85 extern char *__progname;
89 /* Struct for addargs */
95 /* Name of current file being transferred. */
98 /* This is set to non-zero to enable verbose mode. */
101 /* This is set to zero if the progressmeter is not desired. */
102 int showprogress = 1;
104 /* This is the program to execute for the secured connection. ("ssh" or -S) */
105 char *ssh_program = _PATH_SSH_PROGRAM;
107 /* This is used to store the pid of ssh_program */
108 pid_t do_cmd_pid = -1;
113 if (do_cmd_pid > 1) {
114 kill(do_cmd_pid, signo ? signo : SIGTERM);
115 waitpid(do_cmd_pid, NULL, 0);
124 do_local_cmd(arglist *a)
131 fatal("do_local_cmd: no arguments");
134 fprintf(stderr, "Executing:");
135 for (i = 0; i < a->num; i++)
136 fprintf(stderr, " %s", a->list[i]);
137 fprintf(stderr, "\n");
139 if ((pid = fork()) == -1)
140 fatal("do_local_cmd: fork: %s", strerror(errno));
143 execvp(a->list[0], a->list);
149 signal(SIGTERM, killchild);
150 signal(SIGINT, killchild);
151 signal(SIGHUP, killchild);
153 while (waitpid(pid, &status, 0) == -1)
155 fatal("do_local_cmd: waitpid: %s", strerror(errno));
159 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
166 * This function executes the given command as the specified user on the
167 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
168 * assigns the input and output file descriptors on success.
172 do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
174 int pin[2], pout[2], reserved[2];
178 "Executing: program %s host %s, user %s, command %s\n",
180 remuser ? remuser : "(unspecified)", cmd);
183 * Reserve two descriptors so that the real pipes won't get
184 * descriptors 0 and 1 because that will screw up dup2 below.
188 /* Create a socket pair for communicating with ssh. */
190 fatal("pipe: %s", strerror(errno));
192 fatal("pipe: %s", strerror(errno));
194 /* Free the reserved descriptors. */
198 /* Fork a child to execute the command on the remote host using ssh. */
200 if (do_cmd_pid == 0) {
209 replacearg(&args, 0, "%s", ssh_program);
211 addargs(&args, "-l%s", remuser);
212 addargs(&args, "%s", host);
213 addargs(&args, "%s", cmd);
215 execvp(ssh_program, args.list);
218 } else if (do_cmd_pid == -1) {
219 fatal("fork: %s", strerror(errno));
221 /* Parent. Close the other side, and return the local side. */
226 signal(SIGTERM, killchild);
227 signal(SIGINT, killchild);
228 signal(SIGHUP, killchild);
237 BUF *allocbuf(BUF *, int, int);
241 void run_err(const char *,...);
242 void verifydir(char *);
246 int errs, remin, remout;
247 int pflag, iamremote, iamrecursive, targetshouldbedirectory;
250 char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
253 void rsource(char *, struct stat *);
254 void sink(int, char *[]);
255 void source(int, char *[]);
256 void tolocal(int, char *[]);
257 void toremote(char *, int, char *[]);
261 main(int argc, char **argv)
263 int ch, fflag, tflag, status;
269 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
272 __progname = ssh_get_progname(argv[0]);
274 memset(&args, '\0', sizeof(args));
276 addargs(&args, "%s", ssh_program);
277 addargs(&args, "-x");
278 addargs(&args, "-oForwardAgent no");
279 addargs(&args, "-oPermitLocalCommand no");
280 addargs(&args, "-oClearAllForwardings yes");
283 while ((ch = getopt(argc, argv, "dfl:prtvBCc:i:P:q1246S:o:F:")) != -1)
285 /* User-visible flags. */
291 addargs(&args, "-%c", ch);
297 addargs(&args, "-%c%s", ch, optarg);
300 addargs(&args, "-p%s", optarg);
303 addargs(&args, "-oBatchmode yes");
306 speed = strtod(optarg, &endp);
307 if (speed <= 0 || *endp != '\0')
309 limit_rate = speed * 1024;
318 ssh_program = xstrdup(optarg);
321 addargs(&args, "-v");
325 addargs(&args, "-q");
329 /* Server options. */
331 targetshouldbedirectory = 1;
333 case 'f': /* "from" */
341 setmode(0, O_BINARY);
350 if ((pwd = getpwuid(userid = getuid())) == NULL)
351 fatal("unknown user %u", (u_int) userid);
353 if (!isatty(STDERR_FILENO))
356 remin = STDIN_FILENO;
357 remout = STDOUT_FILENO;
360 /* Follow "protocol", send data. */
373 targetshouldbedirectory = 1;
377 /* Command to be executed on remote system using "ssh". */
378 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
379 verbose_mode ? " -v" : "",
380 iamrecursive ? " -r" : "", pflag ? " -p" : "",
381 targetshouldbedirectory ? " -d" : "");
383 (void) signal(SIGPIPE, lostconn);
385 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
386 toremote(targ, argc, argv);
388 if (targetshouldbedirectory)
389 verifydir(argv[argc - 1]);
390 tolocal(argc, argv); /* Dest is local host. */
393 * Finally check the exit status of the ssh process, if one was forked
394 * and no error has occured yet
396 if (do_cmd_pid != -1 && errs == 0) {
400 (void) close(remout);
401 if (waitpid(do_cmd_pid, &status, 0) == -1)
404 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
412 toremote(char *targ, int argc, char **argv)
415 char *bp, *host, *src, *suser, *thost, *tuser, *arg;
418 memset(&alist, '\0', sizeof(alist));
425 arg = xstrdup(argv[argc - 1]);
426 if ((thost = strrchr(arg, '@'))) {
437 if (tuser != NULL && !okname(tuser)) {
442 for (i = 0; i < argc - 1; i++) {
443 src = colon(argv[i]);
444 if (src) { /* remote to remote */
446 addargs(&alist, "%s", ssh_program);
448 addargs(&alist, "-v");
449 addargs(&alist, "-x");
450 addargs(&alist, "-oClearAllForwardings yes");
451 addargs(&alist, "-n");
456 host = strrchr(argv[i], '@');
460 host = cleanhostname(host);
463 suser = pwd->pw_name;
464 else if (!okname(suser))
466 addargs(&alist, "-l");
467 addargs(&alist, "%s", suser);
469 host = cleanhostname(argv[i]);
471 addargs(&alist, "%s", host);
472 addargs(&alist, "%s", cmd);
473 addargs(&alist, "%s", src);
474 addargs(&alist, "%s%s%s:%s",
475 tuser ? tuser : "", tuser ? "@" : "",
477 if (do_local_cmd(&alist) != 0)
479 } else { /* local to remote */
481 len = strlen(targ) + CMDNEEDS + 20;
483 (void) snprintf(bp, len, "%s -t %s", cmd, targ);
484 host = cleanhostname(thost);
485 if (do_cmd(host, tuser, bp, &remin,
498 tolocal(int argc, char **argv)
501 char *bp, *host, *src, *suser;
504 memset(&alist, '\0', sizeof(alist));
507 for (i = 0; i < argc - 1; i++) {
508 if (!(src = colon(argv[i]))) { /* Local to local. */
510 addargs(&alist, "%s", _PATH_CP);
512 addargs(&alist, "-r");
514 addargs(&alist, "-p");
515 addargs(&alist, "%s", argv[i]);
516 addargs(&alist, "%s", argv[argc-1]);
517 if (do_local_cmd(&alist))
524 if ((host = strrchr(argv[i], '@')) == NULL) {
531 suser = pwd->pw_name;
533 host = cleanhostname(host);
534 len = strlen(src) + CMDNEEDS + 20;
536 (void) snprintf(bp, len, "%s -f %s", cmd, src);
537 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
543 sink(1, argv + argc - 1);
550 source(int argc, char **argv)
555 off_t i, amt, statbytes;
557 int fd = -1, haderr, indx;
558 char *last, *name, buf[2048];
561 for (indx = 0; indx < argc; ++indx) {
565 while (len > 1 && name[len-1] == '/')
567 if (strchr(name, '\n') != NULL) {
568 run_err("%s: skipping, filename contains a newline",
572 if ((fd = open(name, O_RDONLY, 0)) < 0)
574 if (fstat(fd, &stb) < 0) {
575 syserr: run_err("%s: %s", name, strerror(errno));
578 switch (stb.st_mode & S_IFMT) {
588 run_err("%s: not a regular file", name);
591 if ((last = strrchr(name, '/')) == NULL)
598 * Make it compatible with possible future
599 * versions expecting microseconds.
601 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
602 (u_long) stb.st_mtime,
603 (u_long) stb.st_atime);
604 (void) atomicio(vwrite, remout, buf, strlen(buf));
608 #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
609 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
610 (u_int) (stb.st_mode & FILEMODEMASK),
611 (long long)stb.st_size, last);
613 fprintf(stderr, "Sending file modes: %s", buf);
615 (void) atomicio(vwrite, remout, buf, strlen(buf));
618 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
619 next: if (fd != -1) {
626 start_progress_meter(curfile, stb.st_size, &statbytes);
627 /* Keep writing after an error so that we stay sync'd up. */
628 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
630 if (i + amt > stb.st_size)
631 amt = stb.st_size - i;
633 result = atomicio(read, fd, bp->buf, amt);
638 (void) atomicio(vwrite, remout, bp->buf, amt);
640 result = atomicio(vwrite, remout, bp->buf, amt);
649 stop_progress_meter();
652 if (close(fd) < 0 && !haderr)
657 (void) atomicio(vwrite, remout, "", 1);
659 run_err("%s: %s", name, strerror(haderr));
665 rsource(char *name, struct stat *statp)
669 char *last, *vect[1], path[1100];
671 if (!(dirp = opendir(name))) {
672 run_err("%s: %s", name, strerror(errno));
675 last = strrchr(name, '/');
681 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
682 (u_long) statp->st_mtime,
683 (u_long) statp->st_atime);
684 (void) atomicio(vwrite, remout, path, strlen(path));
685 if (response() < 0) {
690 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
691 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
693 fprintf(stderr, "Entering directory: %s", path);
694 (void) atomicio(vwrite, remout, path, strlen(path));
695 if (response() < 0) {
699 while ((dp = readdir(dirp)) != NULL) {
702 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
704 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
705 run_err("%s/%s: name too long", name, dp->d_name);
708 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
712 (void) closedir(dirp);
713 (void) atomicio(vwrite, remout, "E\n", 2);
720 static struct timeval bwstart, bwend;
721 static int lamt, thresh = 16384;
723 struct timespec ts, rm;
725 if (!timerisset(&bwstart)) {
726 gettimeofday(&bwstart, NULL);
734 gettimeofday(&bwend, NULL);
735 timersub(&bwend, &bwstart, &bwend);
736 if (!timerisset(&bwend))
740 waitlen = (double)1000000L * lamt / limit_rate;
742 bwstart.tv_sec = waitlen / 1000000L;
743 bwstart.tv_usec = waitlen % 1000000L;
745 if (timercmp(&bwstart, &bwend, >)) {
746 timersub(&bwstart, &bwend, &bwend);
748 /* Adjust the wait time */
753 } else if (bwend.tv_usec < 100) {
759 TIMEVAL_TO_TIMESPEC(&bwend, &ts);
760 while (nanosleep(&ts, &rm) == -1) {
768 gettimeofday(&bwstart, NULL);
772 sink(int argc, char **argv)
782 int amt, exists, first, mask, mode, ofd, omode;
783 off_t size, statbytes;
784 int setimes, targisdir, wrerrno = 0;
785 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
786 struct timeval tv[2];
790 #define SCREWUP(str) { why = str; goto screwup; }
792 setimes = targisdir = 0;
797 run_err("ambiguous target");
801 if (targetshouldbedirectory)
804 (void) atomicio(vwrite, remout, "", 1);
805 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
807 for (first = 1;; first = 0) {
809 if (atomicio(read, remin, cp, 1) != 1)
812 SCREWUP("unexpected <newline>");
814 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
815 SCREWUP("lost connection");
817 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
820 fprintf(stderr, "Sink: %s", buf);
822 if (buf[0] == '\01' || buf[0] == '\02') {
824 (void) atomicio(vwrite, STDERR_FILENO,
825 buf + 1, strlen(buf + 1));
832 (void) atomicio(vwrite, remout, "", 1);
842 mtime.tv_sec = strtol(cp, &cp, 10);
843 if (!cp || *cp++ != ' ')
844 SCREWUP("mtime.sec not delimited");
845 mtime.tv_usec = strtol(cp, &cp, 10);
846 if (!cp || *cp++ != ' ')
847 SCREWUP("mtime.usec not delimited");
848 atime.tv_sec = strtol(cp, &cp, 10);
849 if (!cp || *cp++ != ' ')
850 SCREWUP("atime.sec not delimited");
851 atime.tv_usec = strtol(cp, &cp, 10);
852 if (!cp || *cp++ != '\0')
853 SCREWUP("atime.usec not delimited");
854 (void) atomicio(vwrite, remout, "", 1);
857 if (*cp != 'C' && *cp != 'D') {
859 * Check for the case "rcp remote:foo\* local:bar".
860 * In this case, the line "No match." can be returned
861 * by the shell before the rcp command on the remote is
862 * executed so the ^Aerror_message convention isn't
869 SCREWUP("expected control record");
872 for (++cp; cp < buf + 5; cp++) {
873 if (*cp < '0' || *cp > '7')
875 mode = (mode << 3) | (*cp - '0');
878 SCREWUP("mode not delimited");
880 for (size = 0; isdigit(*cp);)
881 size = size * 10 + (*cp++ - '0');
883 SCREWUP("size not delimited");
884 if ((strchr(cp, '/') != NULL) || (strcmp(cp, "..") == 0)) {
885 run_err("error: unexpected filename: %s", cp);
889 static char *namebuf;
890 static size_t cursize;
893 need = strlen(targ) + strlen(cp) + 250;
894 if (need > cursize) {
897 namebuf = xmalloc(need);
900 (void) snprintf(namebuf, need, "%s%s%s", targ,
901 strcmp(targ, "/") ? "/" : "", cp);
906 exists = stat(np, &stb) == 0;
908 int mod_flag = pflag;
910 SCREWUP("received directory without -r");
912 if (!S_ISDIR(stb.st_mode)) {
917 (void) chmod(np, mode);
919 /* Handle copying from a read-only
922 if (mkdir(np, mode | S_IRWXU) < 0)
925 vect[0] = xstrdup(np);
929 if (utimes(vect[0], tv) < 0)
930 run_err("%s: set times: %s",
931 vect[0], strerror(errno));
934 (void) chmod(vect[0], mode);
941 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
942 bad: run_err("%s: %s", np, strerror(errno));
945 (void) atomicio(vwrite, remout, "", 1);
946 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
955 start_progress_meter(curfile, size, &statbytes);
956 for (count = i = 0; i < size; i += 4096) {
962 j = atomicio(read, remin, cp, amt);
964 run_err("%s", j ? strerror(errno) :
965 "dropped connection");
976 if (count == bp->cnt) {
977 /* Keep reading so we stay sync'd up. */
979 if (atomicio(vwrite, ofd, bp->buf,
990 stop_progress_meter();
991 if (count != 0 && wrerr == NO &&
992 atomicio(vwrite, ofd, bp->buf, count) != count) {
996 if (wrerr == NO && ftruncate(ofd, size) != 0) {
997 run_err("%s: truncate: %s", np, strerror(errno));
1001 if (exists || omode != mode)
1003 if (fchmod(ofd, omode)) {
1004 #else /* HAVE_FCHMOD */
1005 if (chmod(np, omode)) {
1006 #endif /* HAVE_FCHMOD */
1007 run_err("%s: set mode: %s",
1008 np, strerror(errno));
1012 if (!exists && omode != mode)
1014 if (fchmod(ofd, omode & ~mask)) {
1015 #else /* HAVE_FCHMOD */
1016 if (chmod(np, omode & ~mask)) {
1017 #endif /* HAVE_FCHMOD */
1018 run_err("%s: set mode: %s",
1019 np, strerror(errno));
1023 if (close(ofd) == -1) {
1028 if (setimes && wrerr == NO) {
1030 if (utimes(np, tv) < 0) {
1031 run_err("%s: set times: %s",
1032 np, strerror(errno));
1038 run_err("%s: %s", np, strerror(wrerrno));
1041 (void) atomicio(vwrite, remout, "", 1);
1048 run_err("protocol error: %s", why);
1055 char ch, *cp, resp, rbuf[2048];
1057 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
1067 case 1: /* error, followed by error msg */
1068 case 2: /* fatal error, "" */
1070 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
1073 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
1076 (void) atomicio(vwrite, STDERR_FILENO, rbuf, cp - rbuf);
1088 (void) fprintf(stderr,
1089 "usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
1090 " [-l limit] [-o ssh_option] [-P port] [-S program]\n"
1091 " [[user@]host1:]file1 [...] [[user@]host2:]file2\n");
1096 run_err(const char *fmt,...)
1102 if (fp == NULL && !(fp = fdopen(remout, "w")))
1104 (void) fprintf(fp, "%c", 0x01);
1105 (void) fprintf(fp, "scp: ");
1107 (void) vfprintf(fp, fmt, ap);
1109 (void) fprintf(fp, "\n");
1114 vfprintf(stderr, fmt, ap);
1116 fprintf(stderr, "\n");
1125 if (!stat(cp, &stb)) {
1126 if (S_ISDIR(stb.st_mode))
1130 run_err("%s: %s", cp, strerror(errno));
1145 if (!isalpha(c) && !isdigit(c)) {
1160 bad: fprintf(stderr, "%s: invalid user name\n", cp0);
1165 allocbuf(BUF *bp, int fd, int blksize)
1168 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1171 if (fstat(fd, &stb) < 0) {
1172 run_err("fstat: %s", strerror(errno));
1175 size = roundup(stb.st_blksize, blksize);
1178 #else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1180 #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1181 if (bp->cnt >= size)
1183 if (bp->buf == NULL)
1184 bp->buf = xmalloc(size);
1186 bp->buf = xrealloc(bp->buf, size);
1187 memset(bp->buf, 0, size);
1196 write(STDERR_FILENO, "lost connection\n", 16);