Subject: rcp patch
To: None <tech-userlevel@netbsd.org>
From: None <hypnosses@pulltheplug.org>
List: tech-userlevel
Date: 06/08/2006 13:01:23
heres a little patch i wrote to clean up some of the rcp code.

Index: extern.h
===================================================================
RCS file: /cvsroot/src/bin/rcp/extern.h,v
retrieving revision 1.6
diff -u -r1.6 extern.h
--- extern.h    11 Mar 2005 02:55:23 -0000      1.6
+++ extern.h    8 Jun 2006 00:51:14 -0000
@@ -36,7 +36,7 @@
        char *buf;
 } BUF;

-extern int iamremote;
+extern int i_am_remote;

 BUF    *allocbuf(BUF *, int, int);
 char   *colon(char *);
Index: rcp.c
===================================================================
RCS file: /cvsroot/src/bin/rcp/rcp.c,v
retrieving revision 1.42
diff -u -r1.42 rcp.c
--- rcp.c       20 Mar 2006 04:03:10 -0000      1.42
+++ rcp.c       8 Jun 2006 00:51:14 -0000
@@ -74,7 +74,7 @@
 u_short        port;
 uid_t  userid;
 int errs, rem;
-int pflag, iamremote, iamrecursive, targetshouldbedirectory;
+int pflag, i_am_remote, i_am_recursive, target_should_be_directory;
 int family = AF_UNSPEC;
 static char dot[] = ".";

@@ -112,18 +112,18 @@
                        pflag = 1;
                        break;
                case 'r':
-                       iamrecursive = 1;
+                       i_am_recursive = 1;
                        break;
                                                /* Server options. */
                case 'd':
-                       targetshouldbedirectory = 1;
+                       target_should_be_directory = 1;
                        break;
                case 'f':                       /* "from" */
-                       iamremote = 1;
+                       i_am_remote = 1;
                        fflag = 1;
                        break;
                case 't':                       /* "to" */
-                       iamremote = 1;
+                       i_am_remote = 1;
                        tflag = 1;
                        break;
                case '?':
@@ -133,8 +133,7 @@
        argc -= optind;
        argv += optind;

-       sp = getservbyname(shell = "shell", "tcp");
-       if (sp == NULL)
+       if ( ( sp = getservbyname(shell = "shell", "tcp")) == NULL)
                errx(1, "%s/tcp: unknown service", shell);
        port = sp->s_port;

@@ -160,13 +159,13 @@
        if (argc < 2)
                usage();
        if (argc > 2)
-               targetshouldbedirectory = 1;
+               target_should_be_directory = 1;

        rem = -1;
        /* Command to be executed on remote system using "rsh". */
        (void)snprintf(cmd, sizeof(cmd), "rcp%s%s%s",
-           iamrecursive ? " -r" : "", pflag ? " -p" : "",
-           targetshouldbedirectory ? " -d" : "");
+           i_am_recursive ? " -r" : "", pflag ? " -p" : "",
+           target_should_be_directory ? " -d" : "");

        (void)signal(SIGPIPE, lostconn);

@@ -174,7 +173,7 @@
                toremote(targ, argc, argv);
        else {
                tolocal(argc, argv);            /* Dest is local host. */
-               if (targetshouldbedirectory)
+               if (target_should_be_directory)
                        verifydir(argv[argc - 1]);
        }
        exit(errs);
@@ -276,7 +275,7 @@
                        if (!(bp = malloc(len)))
                                err(1, NULL);
                        (void)snprintf(bp, len, "exec %s%s%s %s %s",
_PATH_CP,
-                           iamrecursive ? " -r" : "", pflag ? " -p" : "",
+                           i_am_recursive ? " -r" : "", pflag ? " -p" : "",
                            argv[i], argv[argc - 1]);
                        if (susystem(bp))
                                ++errs;
@@ -342,7 +341,7 @@
                case S_IFREG:
                        break;
                case S_IFDIR:
-                       if (iamrecursive) {
+                       if (i_am_recursive) {
                                rsource(name, &stb);
                                goto next;
                        }
@@ -386,15 +385,13 @@
                        if (i + amt > stb.st_size)
                                amt = stb.st_size - i;
                        if (!haderr) {
-                               result = read(fd, bp->buf, amt);
-                               if (result != amt)
+                               if ( ( result = read(fd, bp->buf, amt)) !=
amt)
                                        haderr = result >= 0 ? EIO : errno;
                        }
                        if (haderr)
                                (void)write(rem, bp->buf, amt);
                        else {
-                               result = write(rem, bp->buf, amt);
-                               if (result != amt)
+                               if ( ( result = write(rem, bp->buf, amt))
!= amt)
                                        haderr = result >= 0 ? EIO : errno;
                        }
                }
@@ -419,8 +416,7 @@
                run_err("%s: %s", name, strerror(errno));
                return;
        }
-       last = strrchr(name, '/');
-       if (last == 0)
+       if ( ( last = strrchr(name, '/')) == 0)
                last = name;
        else
                last++;
@@ -495,7 +491,7 @@
                exit(1);
        }
        targ = *argv;
-       if (targetshouldbedirectory)
+       if (target_should_be_directory)
                verifydir(targ);
        (void)write(rem, "", 1);
        if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
@@ -514,7 +510,7 @@
                *cp = 0;

                if (buf[0] == '\01' || buf[0] == '\02') {
-                       if (iamremote == 0)
+                       if (i_am_remote == 0)
                                (void)write(STDERR_FILENO,
                                    buf + 1, strlen(buf + 1));
                        if (buf[0] == '\02')
@@ -582,8 +578,7 @@
                        static int cursize;
                        size_t need;

-                       need = strlen(targ) + strlen(cp) + 250;
-                       if (need > cursize) {
+                       if ( ( need = strlen(targ) + strlen(cp) + 250) >
cursize) {
                                if (!(namebuf = malloc(need)))
                                        run_err("%s", strerror(errno));
                        }
@@ -639,8 +634,7 @@
                                amt = size - i;
                        count += amt;
                        do {
-                               j = read(rem, cp, amt);
-                               if (j <= 0) {
+                               if ( ( j = read(rem, cp, amt)) <= 0) {
                                        run_err("%s", j ? strerror(errno) :
                                            "dropped connection");
                                        exit(1);
@@ -651,8 +645,7 @@
                        if (count == bp->cnt) {
                                /* Keep reading so we stay sync'd up. */
                                if (wrerr == NO) {
-                                       j = write(ofd, bp->buf, count);
-                                       if (j != count) {
+                                       if ( ( j = write(ofd, bp->buf,
count)) != count) {
                                                wrerr = YES;
                                                wrerrno = j >= 0 ? EIO :
errno;
                                        }
@@ -744,7 +737,7 @@
                        *cp++ = ch;
                } while (cp < &rbuf[BUFSIZ] && ch != '\n');

-               if (!iamremote)
+               if (!i_am_remote)
                        (void)write(STDERR_FILENO, rbuf, cp - rbuf);
                ++errs;
                if (resp == 1)
@@ -785,7 +778,7 @@
        (void)fflush(fp);
        va_end(ap);

-       if (!iamremote) {
+       if (!i_am_remote) {
                va_start(ap, fmt);
                vwarnx(fmt, ap);
                va_end(ap);
Index: util.c
===================================================================
RCS file: /cvsroot/src/bin/rcp/util.c,v
retrieving revision 1.9
diff -u -r1.9 util.c
--- util.c      11 Mar 2005 02:55:23 -0000      1.9
+++ util.c      8 Jun 2006 00:51:14 -0000
@@ -75,8 +75,7 @@
        char *ep;

        if (*cp == '[') {
-               ep = cp + (strlen(cp) - 1);
-               if (*ep == ']') {
+               if ( ( ep = cp + (strlen(cp) - 1)) == ']') {
                        *ep = '\0';
                        ++cp;
                }
@@ -156,8 +155,7 @@
                run_err("fstat: %s", strerror(errno));
                return (0);
        }
-       size = roundup(stb.st_blksize, blksize);
-       if (size == 0)
+       if ( ( size = roundup(stb.st_blksize, blksize)) == 0)
                size = blksize;
        if (bp->cnt >= size)
                return (bp);
@@ -176,7 +174,7 @@
 void
 lostconn(int signo)
 {
-       if (!iamremote)
+       if (!i_am_remote)
                warnx("lost connection");
        exit(1);
        /* NOTREACHED */