Version 4.0 readme
[unix-history] / usr / src / usr.bin / rlogin / rlogin.c
index 0d88ecb..68b2251 100644 (file)
+/*
+ * Copyright (c) 1983 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)rlogin.c   4.4 82/11/15";
-#endif
+char copyright[] =
+"@(#) Copyright (c) 1983 The Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif /* not lint */
 
 
-#include <stdio.h>
-#include <sgtty.h>
-#include <sys/types.h>
+#ifndef lint
+static char sccsid[] = "@(#)rlogin.c   5.12 (Berkeley) %G%";
+#endif /* not lint */
+
+/*
+ * rlogin - remote login
+ */
+#include <sys/param.h>
+#include <sys/errno.h>
+#include <sys/file.h>
 #include <sys/socket.h>
 #include <sys/socket.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <sys/wait.h>
+
 #include <netinet/in.h>
 #include <netinet/in.h>
+
+#include <stdio.h>
+#include <sgtty.h>
 #include <errno.h>
 #include <pwd.h>
 #include <errno.h>
 #include <pwd.h>
+#include <signal.h>
+#include <setjmp.h>
+#include <netdb.h>
 
 
-/*
- * rlogin - remote login; this is a hacked version of cu
- */
-char   *index(), *rindex(), *malloc(), *getenv();
+# ifndef TIOCPKT_WINDOW
+# define TIOCPKT_WINDOW 0x80
+# endif TIOCPKT_WINDOW
+
+/* concession to sun */
+# ifndef SIGUSR1
+# define SIGUSR1 30
+# endif SIGUSR1
+
+char   *index(), *rindex(), *malloc(), *getenv(), *strcat(), *strcpy();
 struct passwd *getpwuid();
 struct passwd *getpwuid();
-struct passwd *pwd;
-char   *name, *pass;
+char   *name;
 int    rem;
 char   cmdchar = '~';
 int    rem;
 char   cmdchar = '~';
-int    rcmdoptions = 0;
 int    eight;
 int    eight;
+int    litout;
 char   *speeds[] =
     { "0", "50", "75", "110", "134", "150", "200", "300",
       "600", "1200", "1800", "2400", "4800", "9600", "19200", "38400" };
 char   *speeds[] =
     { "0", "50", "75", "110", "134", "150", "200", "300",
       "600", "1200", "1800", "2400", "4800", "9600", "19200", "38400" };
-char   term[64] = "dumb";
+char   term[256] = "network";
+extern int errno;
+int    lostpeer();
+int    dosigwinch = 0;
+#ifndef sigmask
+#define sigmask(m)     (1 << ((m)-1))
+#endif
+#ifdef sun
+struct winsize {
+       unsigned short ws_row, ws_col;
+       unsigned short ws_xpixel, ws_ypixel;
+};
+#endif sun
+struct winsize winsize;
+int    sigwinch(), oob();
+
+/*
+ * The following routine provides compatibility (such as it is)
+ * between 4.2BSD Suns and others.  Suns have only a `ttysize',
+ * so we convert it to a winsize.
+ */
+#ifdef sun
+int
+get_window_size(fd, wp)
+       int fd;
+       struct winsize *wp;
+{
+       struct ttysize ts;
+       int error;
+
+       if ((error = ioctl(0, TIOCGSIZE, &ts)) != 0)
+               return (error);
+       wp->ws_row = ts.ts_lines;
+       wp->ws_col = ts.ts_cols;
+       wp->ws_xpixel = 0;
+       wp->ws_ypixel = 0;
+       return (0);
+}
+#else sun
+#define get_window_size(fd, wp)        ioctl(fd, TIOCGWINSZ, wp)
+#endif sun
 
 main(argc, argv)
        int argc;
        char **argv;
 {
 
 main(argc, argv)
        int argc;
        char **argv;
 {
-       int pid;
-       char *host, *cp, **ap, buf[BUFSIZ];
-       register int cc;
+       char *host, *cp;
        struct sgttyb ttyb;
        struct passwd *pwd;
        struct sgttyb ttyb;
        struct passwd *pwd;
+       struct servent *sp;
+       int uid, options = 0, oldmask;
+       int on = 1;
 
        host = rindex(argv[0], '/');
        if (host)
 
        host = rindex(argv[0], '/');
        if (host)
@@ -45,28 +127,33 @@ main(argc, argv)
        if (!strcmp(host, "rlogin"))
                host = *argv++, --argc;
 another:
        if (!strcmp(host, "rlogin"))
                host = *argv++, --argc;
 another:
-       if (!strcmp(*argv, "-d")) {
+       if (argc > 0 && !strcmp(*argv, "-d")) {
                argv++, argc--;
                argv++, argc--;
-               rcmdoptions |= SO_DEBUG;
+               options |= SO_DEBUG;
                goto another;
        }
                goto another;
        }
-       if (!strcmp(*argv, "-l")) {
+       if (argc > 0 && !strcmp(*argv, "-l")) {
                argv++, argc--;
                if (argc == 0)
                        goto usage;
                name = *argv++; argc--;
                goto another;
        }
                argv++, argc--;
                if (argc == 0)
                        goto usage;
                name = *argv++; argc--;
                goto another;
        }
-       if (!strncmp(*argv, "-e", 2)) {
+       if (argc > 0 && !strncmp(*argv, "-e", 2)) {
                cmdchar = argv[0][2];
                argv++, argc--;
                goto another;
        }
                cmdchar = argv[0][2];
                argv++, argc--;
                goto another;
        }
-       if (!strcmp(*argv, "-8")) {
+       if (argc > 0 && !strcmp(*argv, "-8")) {
                eight = 1;
                argv++, argc--;
                goto another;
        }
                eight = 1;
                argv++, argc--;
                goto another;
        }
+       if (argc > 0 && !strcmp(*argv, "-L")) {
+               litout = 1;
+               argv++, argc--;
+               goto another;
+       }
        if (host == 0)
                goto usage;
        if (argc > 0)
        if (host == 0)
                goto usage;
        if (argc > 0)
@@ -76,565 +163,505 @@ another:
                fprintf(stderr, "Who are you?\n");
                exit(1);
        }
                fprintf(stderr, "Who are you?\n");
                exit(1);
        }
-       cc = 0;
+       sp = getservbyname("login", "tcp");
+       if (sp == 0) {
+               fprintf(stderr, "rlogin: login/tcp: unknown service\n");
+               exit(2);
+       }
        cp = getenv("TERM");
        if (cp)
        cp = getenv("TERM");
        if (cp)
-               strcpy(term, cp);
-       if (gtty(0, &ttyb)==0) {
-               strcat(term, "/");
-               strcat(term, speeds[ttyb.sg_ospeed]);
+               (void) strcpy(term, cp);
+       if (ioctl(0, TIOCGETP, &ttyb) == 0) {
+               (void) strcat(term, "/");
+               (void) strcat(term, speeds[ttyb.sg_ospeed]);
        }
        }
-        rem = rcmd(&host, IPPORT_LOGINSERVER, pwd->pw_name,
+       (void) get_window_size(0, &winsize);
+       (void) signal(SIGPIPE, lostpeer);
+       /* will use SIGUSR1 for window size hack, so hold it off */
+       oldmask = sigblock(sigmask(SIGURG) | sigmask(SIGUSR1));
+        rem = rcmd(&host, sp->s_port, pwd->pw_name,
            name ? name : pwd->pw_name, term, 0);
         if (rem < 0)
                 exit(1);
            name ? name : pwd->pw_name, term, 0);
         if (rem < 0)
                 exit(1);
-       setuid(getuid());
-       cumain();
-       exit(0);
+       if (options & SO_DEBUG &&
+           setsockopt(rem, SOL_SOCKET, SO_DEBUG, &on, sizeof (on)) < 0)
+               perror("rlogin: setsockopt (SO_DEBUG)");
+       uid = getuid();
+       if (setuid(uid) < 0) {
+               perror("rlogin: setuid");
+               exit(1);
+       }
+       doit(oldmask);
+       /*NOTREACHED*/
 usage:
        fprintf(stderr,
 usage:
        fprintf(stderr,
-           "usage: rlogin host [ -ex ] [ -l username ]\n");
+           "usage: rlogin host [ -ex ] [ -l username ] [ -8 ] [ -L ]\n");
        exit(1);
 }
 
        exit(1);
 }
 
-#include <ctype.h>
-#include <signal.h>
-
 #define CRLF "\r\n"
 #define CRLF "\r\n"
-#define wrc(ds) write(ds,&c,1)
-
-char   tkill, terase;  /* current input kill & erase */
-int    efk;            /* process of id of listener  */
-char   c, oc;
-int    pipes[] = {-1,-1};
-int    intr, sig2();
-int    parent;
 
 
-int    nhup;
-int    done();
+int    child;
+int    catchild();
+int    copytochild(), writeroob();
 
 
-struct tchars deftchars;
-struct tchars notchars = { 0377, 0377, 'q'&037, 's'&037, 0377, 0377 };
-struct ltchars defltchars;
-struct ltchars noltchars = { 0377, 0377, 0377, 0377, 0377, 0377 };
-char defkill, deferase, defflags;
+int    defflags, tabflag;
+int    deflflags;
+char   deferase, defkill;
+struct tchars deftc;
+struct ltchars defltc;
+struct tchars notc =   { -1, -1, -1, -1, -1, -1 };
+struct ltchars noltc = { -1, -1, -1, -1, -1, -1 };
 
 
-cumain()
+doit(oldmask)
 {
 {
-       int fk;
-       int speed;
-       char *telno;
-       struct sgttyb stbuf;
        int exit();
        int exit();
-
-       gtty(0, &stbuf);
-       defkill = stbuf.sg_kill;
-       deferase = stbuf.sg_erase;
-       defflags = stbuf.sg_flags & (ECHO | CRMOD);
-       ioctl(0, TIOCGETC, &deftchars);
-       ioctl(0, TIOCGLTC, &defltchars);
-       signal(SIGINT, exit);
-       signal(SIGHUP, exit);
-       signal(SIGQUIT, exit);
-       pipe(pipes);
-       parent = getpid();
-       fk = fork();
-       nhup = (int)signal(SIGINT, SIG_IGN);
-       if (fk == 0) {
-               rd();
+       struct sgttyb sb;
+
+       (void) ioctl(0, TIOCGETP, (char *)&sb);
+       defflags = sb.sg_flags;
+       tabflag = defflags & TBDELAY;
+       defflags &= ECHO | CRMOD;
+       deferase = sb.sg_erase;
+       defkill = sb.sg_kill;
+       (void) ioctl(0, TIOCLGET, (char *)&deflflags);
+       (void) ioctl(0, TIOCGETC, (char *)&deftc);
+       notc.t_startc = deftc.t_startc;
+       notc.t_stopc = deftc.t_stopc;
+       (void) ioctl(0, TIOCGLTC, (char *)&defltc);
+       (void) signal(SIGINT, SIG_IGN);
+       setsignal(SIGHUP, exit);
+       setsignal(SIGQUIT, exit);
+       child = fork();
+       if (child == -1) {
+               perror("rlogin: fork");
+               done(1);
+       }
+       if (child == 0) {
+               mode(1);
+               if (reader(oldmask) == 0) {
+                       prf("Connection closed.");
+                       exit(0);
+               }
                sleep(1);
                sleep(1);
-               prf("\007Lost connection.");
+               prf("\007Connection closed.");
                exit(3);
        }
                exit(3);
        }
-       signal(SIGCHLD, done);
-       mode(1);
-       efk = fk;
-       wr();
-       if (fk != -1) kill(fk, SIGKILL);
-       prf("Disconnected.");
-       done();
+
+       /*
+        * We may still own the socket, and may have a pending SIGURG
+        * (or might receive one soon) that we really want to send to
+        * the reader.  Set a trap that simply copies such signals to
+        * the child.
+        */
+       (void) signal(SIGURG, copytochild);
+       (void) signal(SIGUSR1, writeroob);
+       (void) sigsetmask(oldmask);
+       (void) signal(SIGCHLD, catchild);
+       writer();
+       prf("Closed connection.");
+       done(0);
+}
+
+/*
+ * Trap a signal, unless it is being ignored.
+ */
+setsignal(sig, act)
+       int sig, (*act)();
+{
+       int omask = sigblock(sigmask(sig));
+
+       if (signal(sig, act) == SIG_IGN)
+               (void) signal(sig, SIG_IGN);
+       (void) sigsetmask(omask);
 }
 
 }
 
-done()
+done(status)
+       int status;
 {
 {
+       int w;
 
        mode(0);
 
        mode(0);
-       wait((int *)NULL);
-       exit(0);
+       if (child > 0) {
+               /* make sure catchild does not snap it up */
+               (void) signal(SIGCHLD, SIG_DFL);
+               if (kill(child, SIGKILL) >= 0)
+                       while ((w = wait((union wait *)0)) > 0 && w != child)
+                               /*void*/;
+       }
+       exit(status);
+}
+
+/*
+ * Copy SIGURGs to the child process.
+ */
+copytochild()
+{
+
+       (void) kill(child, SIGURG);
 }
 
 /*
 }
 
 /*
- *     wr: write to remote: 0 -> line.
- *     ~.      terminate
- *     ~<file  send file
- *     ~!      local login-style shell
- *     ~!cmd   execute cmd locally
- *     ~$proc  execute proc locally, send output to line
- *     ~%cmd   execute builtin cmd (put and take)
- *     ~^Z     suspend cu process.
+ * This is called when the reader process gets the out-of-band (urgent)
+ * request to turn on the window-changing protocol.
  */
  */
+writeroob()
+{
 
 
-wr()
+       if (dosigwinch == 0) {
+               sendwindow();
+               (void) signal(SIGWINCH, sigwinch);
+       }
+       dosigwinch = 1;
+}
+
+catchild()
 {
 {
-       int ds,fk,lcl,x;
-       char *p,b[600];
+       union wait status;
+       int pid;
+
+again:
+       pid = wait3(&status, WNOHANG|WUNTRACED, (struct rusage *)0);
+       if (pid == 0)
+               return;
+       /*
+        * if the child (reader) dies, just quit
+        */
+       if (pid < 0 || pid == child && !WIFSTOPPED(status))
+               done((int)(status.w_termsig | status.w_retcode));
+       goto again;
+}
+
+/*
+ * writer: write to remote: 0 -> line.
+ * ~.  terminate
+ * ~^Z suspend rlogin process.
+ * ~^Y  suspend rlogin process, but leave reader alone.
+ */
+writer()
+{
+       char c;
+       register n;
+       register bol = 1;               /* beginning of line */
+       register local = 0;
+
        for (;;) {
        for (;;) {
-               p=b;
-               while (rdc(0) == 1) {
-                       if (p == b) lcl=(c == cmdchar);
-                       if (p == b+1 && b[0] == cmdchar) lcl=(c!=cmdchar);
-                       if (!lcl) {
-                               c = oc;
-                               if (wrc(rem) == 0) {
-                                       prf("line gone"); return;
-                               }
-                               if (eight == 0)
-                                       c &= 0177;
-                       }
-                       if (lcl) {
-                               if (c == 0177) c=tkill;
-                               if (c == '\r' || c == '\n') goto A;
-                               wrc(0);
-                       }
-                       *p++=c;
-                       if (c == terase) {
-                               p=p-2; 
-                               if (p<b) p=b;
-                       }
-                       if (c == tkill || c == 0177 || c == '\4' || c == '\r' || c == '\n') p=b;
+               n = read(0, &c, 1);
+               if (n <= 0) {
+                       if (n < 0 && errno == EINTR)
+                               continue;
+                       break;
                }
                }
-               return;
-A: 
-               echo("");
-               *p=0;
-               switch (b[1]) {
-               case '.':
-               case '\004':
-                       return;
-               case '!':
-               case '$':
-                       fk = fork();
-                       signal(SIGCHLD, SIG_DFL);
-                       if (fk == 0) {
-                               char *shell = getenv("SHELL");
-                               if (shell == 0) shell = "/bin/sh";
-                               close(1);
-                               dup(b[1] == '$'? rem:2);
-                               close(rem);
-                               mode(0);
-                               if (!nhup) signal(SIGINT, SIG_DFL);
-                               if (b[2] == 0) execl(shell,shell,0);
-                               /* if (b[2] == 0) execl(shell,"-",0); */
-                               else execl(shell,"sh","-c",b+2,0);
-                               prf("Can't execute shell");
-                               exit(~0);
-                       }
-                       if (fk!=(-1)) {
-                               while (wait(&x)!=fk);
+               /*
+                * If we're at the beginning of the line
+                * and recognize a command character, then
+                * we echo locally.  Otherwise, characters
+                * are echo'd remotely.  If the command
+                * character is doubled, this acts as a 
+                * force and local echo is suppressed.
+                */
+               if (bol) {
+                       bol = 0;
+                       if (c == cmdchar) {
+                               bol = 0;
+                               local = 1;
+                               continue;
                        }
                        }
-                       signal(SIGCHLD, done);
-                       mode(1);
-                       if (b[1] == '!') echo("!");
-                       break;
-               case '<':
-                       if (b[2] == 0) break;
-                       if ((ds=open(b+2,0))<0) {
-                               prf("Can't divert %s",b+1); 
+               } else if (local) {
+                       local = 0;
+                       if (c == '.' || c == deftc.t_eofc) {
+                               echo(c);
                                break;
                        }
                                break;
                        }
-                       intr=x=0;
-                       mode(2);
-                       if (!nhup) signal(SIGINT, sig2);
-                       while (!intr && rdc(ds) == 1) {
-                               if (wrc(rem) == 0) {
-                                       x=1; 
-                                       break;
-                               }
-                       }
-                       signal(SIGINT, SIG_IGN);
-                       close(ds);
-                       mode(1);
-                       if (x) return;
-                       break;
-               case '>':
-               case ':':
-                       {
-                       register char *q;
-
-                       q = b+1;
-                       if(*q=='>') q++;
-                       write(pipes[1],q,strlen(q)+1);
-                       if (efk != -1) kill(efk,SIGEMT);
+                       if (c == defltc.t_suspc || c == defltc.t_dsuspc) {
+                               bol = 1;
+                               echo(c);
+                               stop(c);
+                               continue;
                        }
                        }
+                       if (c != cmdchar)
+                               (void) write(rem, &cmdchar, 1);
+               }
+               if (write(rem, &c, 1) == 0) {
+                       prf("line gone");
                        break;
                        break;
-#ifdef SIGTSTP
-#define CTRLZ  26
-               case CTRLZ:
-                       mode(0);
-                       signal(SIGCHLD, SIG_IGN);
-                       kill(0, SIGTSTP);
-                       signal(SIGCHLD, done);
-                       mode(1);
-                       break;
-#endif
-               case '%':
-                       dopercen(&b[2]);
-                       break;
-               default:
-                       prf("Use `%c%c' to start line with `%c'", cmdchar, cmdchar, cmdchar);
                }
                }
-               continue;
+               bol = c == defkill || c == deftc.t_eofc ||
+                   c == deftc.t_intrc || c == defltc.t_suspc ||
+                   c == '\r' || c == '\n';
        }
 }
 
        }
 }
 
-dopercen(line)
-register char *line;
+echo(c)
+register char c;
 {
 {
-       char *args[10];
-       register narg, f;
-       int rcount;
-       for (narg = 0; narg < 10;) {
-               while(*line == ' ' || *line == '\t')
-                       line++;
-               if (*line == '\0')
-                       break;
-               args[narg++] = line;
-               while(*line != '\0' && *line != ' ' && *line != '\t')
-                       line++;
-               if (*line == '\0')
-                       break;
-               *line++ = '\0';
-       }
-       if (equal(args[0], "take")) {
-               if (narg < 2) {
-                       prf("usage: %c%%take from [to]", cmdchar);
-                       return;
-               }
-               if (narg < 3)
-                       args[2] = args[1];
-               write(pipes[1], ">/dev/null",sizeof(">/dev/null"));
-               if (efk != -1) kill(efk,SIGEMT);
-               sleep(5);
-               wrln("echo '%c>:", cmdchar);
-               wrln(args[2]);
-               wrln("'; tee /dev/null <");
-               wrln(args[1]);
-               wrln(";echo '%c>'\n", cmdchar);
-               return;
-       } else if (equal(args[0], "put")) {
-               prf("%c%%put doesn't work yet (use rsh)", cmdchar);
-               return;
-/*
-               if (narg < 2) {
-                       prf("usage: %c%%put from [to]", cmdchar);
-                       return;
-               }
-               if (narg < 3)
-                       args[2] = args[1];
-               if ((f = open(args[1], 0)) < 0) {
-                       prf("cannot open: %s", args[1]);
-                       return;
-               }
-               wrln("stty -echo;cat >");
-               wrln(args[2]);
-               wrln(";stty echo\n");
-               sleep(5);
-               intr = 0;
-               if (!nhup)
-                       signal(SIGINT, sig2);
-               mode(2);
-               rcount = 0;
-               while(!intr && rdc(f) == 1) {
-                       rcount++;
-                       if (c == tkill || c == terase)
-                               wrln("\\");
-                       if (wrc(rem) != 1) {
-                               sleep(2);
-                               if (wrc(rem) != 1) {
-                                       prf("character missed");
-                                       intr = 1;
-                                       break;
-                               }
-                       }
-               }
-               signal(SIGINT, SIG_IGN);
-               close(f);
-               if (intr) {
-                       wrln("\n");
-                       prf("stopped after %d bytes", rcount);
-               }
-               wrln("\004");
-               sleep(5);
-               mode(1);
-               return;
-*/
-       }
-       prf("%c%%%s unknown\n", cmdchar, args[0]);
+       char buf[8];
+       register char *p = buf;
+
+       c &= 0177;
+       *p++ = cmdchar;
+       if (c < ' ') {
+               *p++ = '^';
+               *p++ = c + '@';
+       } else if (c == 0177) {
+               *p++ = '^';
+               *p++ = '?';
+       } else
+               *p++ = c;
+       *p++ = '\r';
+       *p++ = '\n';
+       (void) write(1, buf, p - buf);
 }
 
 }
 
-equal(s1, s2)
-register char *s1, *s2;
+stop(cmdc)
+       char cmdc;
 {
 {
-       while (*s1++ == *s2)
-               if (*s2++ == '\0')
-                       return(1);
-       return(0);
+       mode(0);
+       (void) signal(SIGCHLD, SIG_IGN);
+       (void) kill(cmdc == defltc.t_suspc ? 0 : getpid(), SIGTSTP);
+       (void) signal(SIGCHLD, catchild);
+       mode(1);
+       sigwinch();                     /* check for size changes */
 }
 
 }
 
-wrln(s, p1, p2, p3)
-register char *s;
-int p1, p2, p3;
+sigwinch()
 {
 {
-       char wbuf[256];
+       struct winsize ws;
 
 
-       sprintf(wbuf, s, p1, p2, p3);
-       s = wbuf;
-       while (*s)
-               write(rem, s++, 1);
+       if (dosigwinch && get_window_size(0, &ws) == 0 &&
+           bcmp(&ws, &winsize, sizeof (ws))) {
+               winsize = ws;
+               sendwindow();
+       }
 }
 }
-int ds,slnt;
-int justrung;
 
 /*
 
 /*
- *     rd: read from remote: line -> 1
- *     catch:
- *     ~>[>][:][file]
- *     stuff from file...
- *     ~>      (ends diversion)
+ * Send the window size to the server via the magic escape
  */
  */
-
-int ds,slnt,taking;
-int justrung;
-readmsg(){
-       static char dobuff[128], morejunk[256];
-       int n;
-       justrung = 1;
-       signal(SIGEMT,readmsg);
-       n = read(pipes[0],morejunk,256);
-       dodiver(morejunk);
-}
-
-dodiver(msg)
-char *msg;
+sendwindow()
 {
 {
-       register char *cp = msg; 
-
-       if (*cp=='>') cp++;
-       if (*cp==':') {
-               cp++;
-               if(*cp==0) {
-                       slnt ^= 1;
-                       return;
-               } else  {
-                       slnt = 1;
-               }
-       }
-       if (ds >= 0) close(ds);
-       if (*cp==0) {
-               slnt = 0;
-               ds = -1;
-               return;
-       }
-       if (*msg!='>' || (ds=open(cp,1))<0) ds=creat(cp,0644);
-       lseek(ds, (long)0, 2);
-       if(ds < 0) prf("Creat failed:"), prf(cp);
-       if (ds<0) prf("Can't divert %s",cp+1);
+       char obuf[4 + sizeof (struct winsize)];
+       struct winsize *wp = (struct winsize *)(obuf+4);
+
+       obuf[0] = 0377;
+       obuf[1] = 0377;
+       obuf[2] = 's';
+       obuf[3] = 's';
+       wp->ws_row = htons(winsize.ws_row);
+       wp->ws_col = htons(winsize.ws_col);
+       wp->ws_xpixel = htons(winsize.ws_xpixel);
+       wp->ws_ypixel = htons(winsize.ws_ypixel);
+       (void) write(rem, obuf, sizeof(obuf));
 }
 
 }
 
-
 /*
 /*
- *     rd: read from remote: line -> 1
- *     catch: diversion caught by interrupt routine
+ * reader: read from remote: line -> 1
  */
  */
+#define        READING 1
+#define        WRITING 2
 
 
-#define ORDIN 0
-#define SAWCR 1
-#define EOL   2
-#define SAWTL 3
-#define DIVER 4
+char   rcvbuf[8 * 1024];
+int    rcvcnt;
+int    rcvstate;
+int    ppid;
+jmp_buf        rcvtop;
 
 oob()
 {
 
 oob()
 {
-       int mark, cc, out = 1+1;
-       char waste[512];
-
-       signal(SIGURG, oob);
-       ioctl(1, TIOCFLUSH, &out);
-       for (;;) {
-               if (ioctl(rem, SIOCATMARK, &mark) < 0) {
-                       perror("ioctl");
-                       break;
-               }
-               if (mark)
-                       break;
-               cc = read(rem, waste, 512);
+       int out = FWRITE, atmark, n;
+       int rcvd = 0;
+       char waste[BUFSIZ], mark;
+       struct sgttyb sb;
+
+       while (recv(rem, &mark, 1, MSG_OOB) < 0)
+               switch (errno) {
+               
+               case EWOULDBLOCK:
+                       /*
+                        * Urgent data not here yet.
+                        * It may not be possible to send it yet
+                        * if we are blocked for output
+                        * and our input buffer is full.
+                        */
+                       if (rcvcnt < sizeof(rcvbuf)) {
+                               n = read(rem, rcvbuf + rcvcnt,
+                                       sizeof(rcvbuf) - rcvcnt);
+                               if (n <= 0)
+                                       return;
+                               rcvd += n;
+                       } else {
+                               n = read(rem, waste, sizeof(waste));
+                               if (n <= 0)
+                                       return;
+                       }
+                       continue;
+                               
+               default:
+                       return;
+       }
+       if (mark & TIOCPKT_WINDOW) {
+               /*
+                * Let server know about window size changes
+                */
+               (void) kill(ppid, SIGUSR1);
        }
        }
-       recv(rem, &mark, 1, SOF_OOB);
-       if (mark & TIOCPKT_NOSTOP) {
-               notchars.t_stopc = 0377;
-               notchars.t_startc = 0377;
-               ioctl(0, TIOCSETC, &notchars);
+       if (!eight && (mark & TIOCPKT_NOSTOP)) {
+               (void) ioctl(0, TIOCGETP, (char *)&sb);
+               sb.sg_flags &= ~CBREAK;
+               sb.sg_flags |= RAW;
+               (void) ioctl(0, TIOCSETN, (char *)&sb);
+               notc.t_stopc = -1;
+               notc.t_startc = -1;
+               (void) ioctl(0, TIOCSETC, (char *)&notc);
        }
        }
-       if (mark & TIOCPKT_DOSTOP) {
-               notchars.t_stopc = 's'&037;
-               notchars.t_startc = 'q'&037;
-               ioctl(0, TIOCSETC, &notchars);
+       if (!eight && (mark & TIOCPKT_DOSTOP)) {
+               (void) ioctl(0, TIOCGETP, (char *)&sb);
+               sb.sg_flags &= ~RAW;
+               sb.sg_flags |= CBREAK;
+               (void) ioctl(0, TIOCSETN, (char *)&sb);
+               notc.t_stopc = deftc.t_stopc;
+               notc.t_startc = deftc.t_startc;
+               (void) ioctl(0, TIOCSETC, (char *)&notc);
+       }
+       if (mark & TIOCPKT_FLUSHWRITE) {
+               (void) ioctl(1, TIOCFLUSH, (char *)&out);
+               for (;;) {
+                       if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
+                               perror("ioctl");
+                               break;
+                       }
+                       if (atmark)
+                               break;
+                       n = read(rem, waste, sizeof (waste));
+                       if (n <= 0)
+                               break;
+               }
+               /*
+                * Don't want any pending data to be output,
+                * so clear the recv buffer.
+                * If we were hanging on a write when interrupted,
+                * don't want it to restart.  If we were reading,
+                * restart anyway.
+                */
+               rcvcnt = 0;
+               longjmp(rcvtop, 1);
        }
        }
+
+       /*
+        * oob does not do FLUSHREAD (alas!)
+        */
+
+       /*
+        * If we filled the receive buffer while a read was pending,
+        * longjmp to the top to restart appropriately.  Don't abort
+        * a pending write, however, or we won't know how much was written.
+        */
+       if (rcvd && rcvstate == READING)
+               longjmp(rcvtop, 1);
 }
 
 }
 
-rd()
+/*
+ * reader: read from remote: line -> 1
+ */
+reader(oldmask)
+       int oldmask;
 {
 {
-       extern int ds,slnt;
-       char rb[600], lb[600], *rlim, *llim, c;
-       register char *p,*q;
-       int cnt, state = 0, mustecho, oldslnt, readmsg();
-
-       signal(SIGEMT,readmsg);  /* set up child for catching diversion msgs
-                                   from parent */
-       signal(SIGURG,oob);
-       { int pid = -getpid();
-         ioctl(rem, SIOCSPGRP, &pid); }
-       ds=(-1);
-       p = lb; llim = lb+600;
-agin:
+#if !defined(BSD) || BSD < 43
+       int pid = -getpid();
+#else
+       int pid = getpid();
+#endif
+       int n, remaining;
+       char *bufp = rcvbuf;
+
+       (void) signal(SIGTTOU, SIG_IGN);
+       (void) signal(SIGURG, oob);
+       ppid = getppid();
+       (void) fcntl(rem, F_SETOWN, pid);
+       (void) setjmp(rcvtop);
+       (void) sigsetmask(oldmask);
        for (;;) {
        for (;;) {
-               extern errno;
-               errno = 0;
-               cnt = read(rem,rb,600);
-               if (cnt <= 0) {
-                       if (errno == EINTR) {
-                               errno = 0;
+               while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) {
+                       rcvstate = WRITING;
+                       n = write(1, bufp, remaining);
+                       if (n < 0) {
+                               if (errno != EINTR)
+                                       return (-1);
                                continue;
                        }
                                continue;
                        }
-                       break;
+                       bufp += n;
                }
                }
-               if(!slnt) write(1,rb,cnt);
-               if(ds < 0) continue;
-               oldslnt = slnt;
-               for( q=rb, rlim = rb + cnt - 1; q <= rlim; ) {
-                       if (eight == 0)
-                       c &= 0177;
-                       if(p < llim) *p++ = c;
-                       switch(state) {
-                       case ORDIN:
-                               if(c=='\r') state = SAWCR;
-                               break;
-                       case SAWCR:
-                               if(c=='\n') {
-                                       state = EOL;
-                                       p--;
-                                       p[-1] = '\n';
-                               } else state = ORDIN;
-                               break;
-                       case EOL:
-                               state = (c==cmdchar ? SAWTL : 
-                                        (c=='\r' ? SAWCR : ORDIN));
-                               break;
-                       case SAWTL:
-                               state = (c=='>' ? DIVER : 
-                                        (c=='\r' ? SAWCR : ORDIN));
-                               break;
-                       case DIVER:
-                               if(c=='\r') {
-                                       p--;
-                               } else if (c=='\n') {
-                                       state = ORDIN;
-                                       p[-1] = 0;
-                                       dodiver(lb+2);
-                                       c = 0; p = lb;
-                               }
-                       }
-                       if(slnt==0 && oldslnt) {
-                               if(c=='\n') {
-                                       write(rem,lb,p-lb-1);
-                                       write(rem,CRLF,sizeof(CRLF));
-                               } else if(q==rlim) {
-                                       write(rem,lb,p-lb);
-                                       c = '\n';  /*force flush to file*/
-                               }
-                       }
-                       if(c=='\n') {
-                               if(ds >= 0)
-                                       write(ds,lb,p-lb);
-                               p = lb;
-                       }
+               bufp = rcvbuf;
+               rcvcnt = 0;
+               rcvstate = READING;
+               rcvcnt = read(rem, rcvbuf, sizeof (rcvbuf));
+               if (rcvcnt == 0)
+                       return (0);
+               if (rcvcnt < 0) {
+                       if (errno == EINTR)
+                               continue;
+                       perror("read");
+                       return (-1);
                }
        }
                }
        }
-       if(justrung) {
-               justrung = 0;
-               goto agin;
-       }
 }
 
 }
 
-struct {char lobyte; char hibyte;};
 mode(f)
 {
 mode(f)
 {
-       struct sgttyb stbuf;
-       ioctl(0, TIOCGETP, &stbuf);
-       if (f == 0) {
-               stbuf.sg_flags &= ~CBREAK;
-               stbuf.sg_flags |= defflags;
-               ioctl(0, TIOCSETC, &deftchars);
-               ioctl(0, TIOCSLTC, &defltchars);
-               stbuf.sg_kill = defkill;
-               stbuf.sg_erase = deferase;
-       }
-       if (f == 1) {
-               stbuf.sg_flags |= CBREAK;
-               stbuf.sg_flags &= ~(ECHO|CRMOD);
-               ioctl(0, TIOCSETC, &notchars);
-               ioctl(0, TIOCSLTC, &noltchars);
-               stbuf.sg_kill = 0377;
-               stbuf.sg_erase = 0377;
-       }
-       if (f == 2) {
-               stbuf.sg_flags &= ~CBREAK;
-               stbuf.sg_flags &= ~(ECHO|CRMOD);
-               ioctl(0, TIOCSETC, &deftchars);
-               ioctl(0, TIOCSLTC, &defltchars);
-               stbuf.sg_kill = 0377;
-               stbuf.sg_erase = 0377;
+       struct tchars *tc;
+       struct ltchars *ltc;
+       struct sgttyb sb;
+       int     lflags;
+
+       (void) ioctl(0, TIOCGETP, (char *)&sb);
+       (void) ioctl(0, TIOCLGET, (char *)&lflags);
+       switch (f) {
+
+       case 0:
+               sb.sg_flags &= ~(CBREAK|RAW|TBDELAY);
+               sb.sg_flags |= defflags|tabflag;
+               tc = &deftc;
+               ltc = &defltc;
+               sb.sg_kill = defkill;
+               sb.sg_erase = deferase;
+               lflags = deflflags;
+               break;
+
+       case 1:
+               sb.sg_flags |= (eight ? RAW : CBREAK);
+               sb.sg_flags &= ~defflags;
+               /* preserve tab delays, but turn off XTABS */
+               if ((sb.sg_flags & TBDELAY) == XTABS)
+                       sb.sg_flags &= ~TBDELAY;
+               tc = &notc;
+               ltc = &noltc;
+               sb.sg_kill = sb.sg_erase = -1;
+               if (litout)
+                       lflags |= LLITOUT;
+               break;
+
+       default:
+               return;
        }
        }
-       ioctl(0, TIOCSETN, &stbuf);
+       (void) ioctl(0, TIOCSLTC, (char *)ltc);
+       (void) ioctl(0, TIOCSETC, (char *)tc);
+       (void) ioctl(0, TIOCSETN, (char *)&sb);
+       (void) ioctl(0, TIOCLSET, (char *)&lflags);
 }
 
 }
 
-echo(s)
-char *s;
+/*VARARGS*/
+prf(f, a1, a2, a3, a4, a5)
+       char *f;
 {
 {
-       char *p;
-       for (p=s;*p;p++);
-       if (p>s) write(0,s,p-s);
-       write(0,CRLF, sizeof(CRLF));
-}
 
 
-prf(f, a1, a2, a3)
-char *f;
-{
-       fprintf(stderr, f, a1, a2, a3);
+       fprintf(stderr, f, a1, a2, a3, a4, a5);
        fprintf(stderr, CRLF);
 }
 
        fprintf(stderr, CRLF);
 }
 
-exists(devname)
-char *devname;
+lostpeer()
 {
 {
-       if (access(devname, 0)==0)
-               return(1);
-       prf("%s does not exist", devname);
-       return(0);
-}
 
 
-rdc(ds)
-{
-
-       ds=read(ds,&c,1); 
-       oc = c;
-       if (eight == 0)
-               c &= 0177;
-       return (ds);
-}
-
-sig2()
-{
-       signal(SIGINT, SIG_IGN); 
-       intr = 1;
+       (void) signal(SIGPIPE, SIG_IGN);
+       prf("\007Connection closed.");
+       done(1);
 }
 }