sink() returns multiple messages; bug report 4.3BSD-tahoe/bin/26
[unix-history] / usr / src / bin / rcp / rcp.c
index dff2277..7d3661f 100644 (file)
@@ -22,7 +22,7 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)rcp.c      5.18 (Berkeley) %G%";
+static char sccsid[] = "@(#)rcp.c      5.21 (Berkeley) %G%";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -54,7 +54,6 @@ Key_schedule schedule;
 #endif
 
 extern int errno;
 #endif
 
 extern int errno;
-extern char *sys_errlist[];
 struct passwd *pwd;
 int errs, pflag, port, rem, userid;
 int iamremote, iamrecursive, targetshouldbedirectory;
 struct passwd *pwd;
 int errs, pflag, port, rem, userid;
 int iamremote, iamrecursive, targetshouldbedirectory;
@@ -205,7 +204,7 @@ toremote(targ, argc, argv)
                        host = index(argv[i], '@');
                        if (!(bp = malloc((u_int)(strlen(_PATH_RSH) +
                                    strlen(argv[i]) + strlen(src) +
                        host = index(argv[i], '@');
                        if (!(bp = malloc((u_int)(strlen(_PATH_RSH) +
                                    strlen(argv[i]) + strlen(src) +
-                                   strlen(tuser) + strlen(thost) +
+                                   tuser ? strlen(tuser) : 0 + strlen(thost) +
                                    strlen(targ)) + CMDNEEDS + 20)))
                                        nospace();
                        if (host) {
                                    strlen(targ)) + CMDNEEDS + 20)))
                                        nospace();
                        if (host) {
@@ -356,7 +355,7 @@ verifydir(cp)
                        return;
                errno = ENOTDIR;
        }
                        return;
                errno = ENOTDIR;
        }
-       error("rcp: %s: %s.\n", cp, sys_errlist[errno]);
+       error("rcp: %s: %s.\n", cp, strerror(errno));
        exit(1);
 }
 
        exit(1);
 }
 
@@ -429,7 +428,7 @@ source(argc, argv)
        for (x = 0; x < argc; x++) {
                name = argv[x];
                if ((f = open(name, O_RDONLY, 0)) < 0) {
        for (x = 0; x < argc; x++) {
                name = argv[x];
                if ((f = open(name, O_RDONLY, 0)) < 0) {
-                       error("rcp: %s: %s\n", name, sys_errlist[errno]);
+                       error("rcp: %s: %s\n", name, strerror(errno));
                        continue;
                }
                if (fstat(f, &stb) < 0)
                        continue;
                }
                if (fstat(f, &stb) < 0)
@@ -493,7 +492,7 @@ notreg:                     (void)close(f);
                if (readerr == 0)
                        (void)write(rem, "", 1);
                else
                if (readerr == 0)
                        (void)write(rem, "", 1);
                else
-                       error("rcp: %s: %s\n", name, sys_errlist[readerr]);
+                       error("rcp: %s: %s\n", name, strerror(readerr));
                (void)response();
        }
 }
                (void)response();
        }
 }
@@ -507,7 +506,7 @@ rsource(name, statp)
        char *last, *vect[1], path[MAXPATHLEN];
 
        if (!(d = opendir(name))) {
        char *last, *vect[1], path[MAXPATHLEN];
 
        if (!(d = opendir(name))) {
-               error("rcp: %s: %s\n", name, sys_errlist[errno]);
+               error("rcp: %s: %s\n", name, strerror(errno));
                return;
        }
        last = rindex(name, '/');
                return;
        }
        last = rindex(name, '/');
@@ -596,11 +595,12 @@ sink(argc, argv)
        static BUF buffer;
        struct stat stb;
        struct timeval tv[2];
        static BUF buffer;
        struct stat stb;
        struct timeval tv[2];
+       enum { YES, NO, DISPLAYED } wrerr;
        BUF *bp, *allocbuf();
        off_t i, j;
        char ch, *targ, *why;
        int amt, count, exists, first, mask, mode;
        BUF *bp, *allocbuf();
        off_t i, j;
        char ch, *targ, *why;
        int amt, count, exists, first, mask, mode;
-       int ofd, setimes, size, targisdir, wrerr;
+       int ofd, setimes, size, targisdir;
        char *np, *vect[1], buf[BUFSIZ], *malloc();
 
 #define        atime   tv[0]
        char *np, *vect[1], buf[BUFSIZ], *malloc();
 
 #define        atime   tv[0]
@@ -632,8 +632,6 @@ sink(argc, argv)
                                SCREWUP("lost connection");
                        *cp++ = ch;
                } while (cp < &buf[BUFSIZ - 1] && ch != '\n');
                                SCREWUP("lost connection");
                        *cp++ = ch;
                } while (cp < &buf[BUFSIZ - 1] && ch != '\n');
-               if (ch == '\n')
-                       *--cp;
                *cp = 0;
 
                if (buf[0] == '\01' || buf[0] == '\02') {
                *cp = 0;
 
                if (buf[0] == '\01' || buf[0] == '\02') {
@@ -649,6 +647,9 @@ sink(argc, argv)
                        return;
                }
 
                        return;
                }
 
+               if (ch == '\n')
+                       *--cp = 0;
+
 #define getnum(t) (t) = 0; while (isdigit(*cp)) (t) = (t) * 10 + (*cp++ - '0');
                cp = buf;
                if (*cp == 'T') {
 #define getnum(t) (t) = 0; while (isdigit(*cp)) (t) = (t) * 10 + (*cp++ - '0');
                cp = buf;
                if (*cp == 'T') {
@@ -704,7 +705,7 @@ sink(argc, argv)
                        need = strlen(targ) + strlen(cp) + 250;
                        if (need > cursize) {
                                if (!(namebuf = malloc((u_int)need)))
                        need = strlen(targ) + strlen(cp) + 250;
                        if (need > cursize) {
                                if (!(namebuf = malloc((u_int)need)))
-                                       error("out of memory");
+                                       error("out of memory\n");
                        }
                        (void)sprintf(namebuf, "%s%s%s", targ,
                            *targ ? "/" : "", cp);
                        }
                        (void)sprintf(namebuf, "%s%s%s", targ,
                            *targ ? "/" : "", cp);
@@ -729,12 +730,12 @@ sink(argc, argv)
                                setimes = 0;
                                if (utimes(np, tv) < 0)
                                    error("rcp: can't set times on %s: %s\n",
                                setimes = 0;
                                if (utimes(np, tv) < 0)
                                    error("rcp: can't set times on %s: %s\n",
-                                       np, sys_errlist[errno]);
+                                       np, strerror(errno));
                        }
                        continue;
                }
                if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
                        }
                        continue;
                }
                if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
-bad:                   error("rcp: %s: %s\n", np, sys_errlist[errno]);
+bad:                   error("rcp: %s: %s\n", np, strerror(errno));
                        continue;
                }
                if (exists && pflag)
                        continue;
                }
                if (exists && pflag)
@@ -746,7 +747,7 @@ bad:                        error("rcp: %s: %s\n", np, sys_errlist[errno]);
                }
                cp = bp->buf;
                count = 0;
                }
                cp = bp->buf;
                count = 0;
-               wrerr = 0;
+               wrerr = NO;
                for (i = 0; i < size; i += BUFSIZ) {
                        amt = BUFSIZ;
                        if (i + amt > size)
                for (i = 0; i < size; i += BUFSIZ) {
                        amt = BUFSIZ;
                        if (i + amt > size)
@@ -755,8 +756,8 @@ bad:                        error("rcp: %s: %s\n", np, sys_errlist[errno]);
                        do {
                                j = read(rem, cp, amt);
                                if (j <= 0) {
                        do {
                                j = read(rem, cp, amt);
                                if (j <= 0) {
-                                       error("rcp: %s",
-                                           j ? sys_errlist[errno] :
+                                       error("rcp: %s\n",
+                                           j ? strerror(errno) :
                                            "dropped connection");
                                        exit(1);
                                }
                                            "dropped connection");
                                        exit(1);
                                }
@@ -764,31 +765,41 @@ bad:                      error("rcp: %s: %s\n", np, sys_errlist[errno]);
                                cp += j;
                        } while (amt > 0);
                        if (count == bp->cnt) {
                                cp += j;
                        } while (amt > 0);
                        if (count == bp->cnt) {
-                               if (wrerr == 0 &&
+                               if (wrerr == NO &&
                                    write(ofd, bp->buf, count) != count)
                                    write(ofd, bp->buf, count) != count)
-                                       wrerr++;
+                                       wrerr = YES;
                                count = 0;
                                cp = bp->buf;
                        }
                }
                                count = 0;
                                cp = bp->buf;
                        }
                }
-               if (count != 0 && wrerr == 0 &&
+               if (count != 0 && wrerr == NO &&
                    write(ofd, bp->buf, count) != count)
                    write(ofd, bp->buf, count) != count)
-                       wrerr++;
-               if (ftruncate(ofd, size))
+                       wrerr = YES;
+               if (ftruncate(ofd, size)) {
                        error("rcp: can't truncate %s: %s\n", np,
                        error("rcp: can't truncate %s: %s\n", np,
-                           sys_errlist[errno]);
+                           strerror(errno));
+                       wrerr = DISPLAYED;
+               }
                (void)close(ofd);
                (void)response();
                (void)close(ofd);
                (void)response();
-               if (setimes) {
+               if (setimes && wrerr == NO) {
                        setimes = 0;
                        setimes = 0;
-                       if (utimes(np, tv) < 0)
+                       if (utimes(np, tv) < 0) {
                                error("rcp: can't set times on %s: %s\n",
                                error("rcp: can't set times on %s: %s\n",
-                                   np, sys_errlist[errno]);
-               }                                  
-               if (wrerr)
-                       error("rcp: %s: %s\n", np, sys_errlist[errno]);
-               else
+                                   np, strerror(errno));
+                               wrerr = DISPLAYED;
+                       }
+               }
+               switch(wrerr) {
+               case YES:
+                       error("rcp: %s: %s\n", np, strerror(errno));
+                       break;
+               case NO:
                        (void)write(rem, "", 1);
                        (void)write(rem, "", 1);
+                       break;
+               case DISPLAYED:
+                       break;
+               }
        }
 screwup:
        error("rcp: protocol screwup: %s\n", why);
        }
 screwup:
        error("rcp: protocol screwup: %s\n", why);
@@ -805,7 +816,7 @@ allocbuf(bp, fd, blksize)
        char *malloc();
 
        if (fstat(fd, &stb) < 0) {
        char *malloc();
 
        if (fstat(fd, &stb) < 0) {
-               error("rcp: fstat: %s\n", sys_errlist[errno]);
+               error("rcp: fstat: %s\n", strerror(errno));
                return(0);
        }
        size = roundup(stb.st_blksize, blksize);
                return(0);
        }
        size = roundup(stb.st_blksize, blksize);
@@ -829,16 +840,16 @@ error(fmt, a1, a2, a3)
        char *fmt;
        int a1, a2, a3;
 {
        char *fmt;
        int a1, a2, a3;
 {
-       int len;
-       char buf[BUFSIZ];
+       static FILE *fp;
 
        ++errs;
 
        ++errs;
-       buf[0] = 0x01;
-       (void)sprintf(buf + 1, fmt, a1, a2, a3);
-       len = strlen(buf);
-       (void)write(rem, buf, len);
+       if (!fp && !(fp = fdopen(rem, "w")))
+               return;
+       (void)fprintf(fp, "%c", 0x01);
+       (void)fprintf(fp, fmt, a1, a2, a3);
+       (void)fflush(fp);
        if (!iamremote)
        if (!iamremote)
-               (void)write(2, buf + 1, len - 1);
+               (void)fprintf(stderr, fmt, a1, a2, a3);
 }
 
 nospace()
 }
 
 nospace()