--- rbsb.c.orig	Wed Mar  3 10:28:10 1999
+++ rbsb.c	Thu May 20 19:50:25 1999
@@ -17,8 +17,9 @@
 #ifdef LLITOUT
 long Locmode;		/* Saved "local mode" for 4.x BSD "new driver" */
 long Locbit = LLITOUT;	/* Bit SUPPOSED to disable output translations */
-#include <strings.h>
 #endif
+#include <strings.h>
+char *ttyname(), *getenv();
 #endif
 
 #ifdef USG
@@ -83,6 +84,7 @@
 
 char *Nametty;
 FILE *Ttystream;
+FILE *Logstream;
 int Tty;
 char linbuf[HOWMANY];
 char xXbuf[BUFSIZ];
@@ -359,7 +361,7 @@
 		(void) tcflow(Tty, TCOON);	/* Restart output */
 #else
 		(void) ioctl(Tty, TCSBRK, 1);	/* Wait for output to drain */
-		(void) ioctl(Tty, TCFLSH, 1);	/* Flush input queue */
+		(void) ioctl(Tty, TCFLSH, 0);	/* Flush input queue */
 		(void) ioctl(Tty, TCSETAW, &oldtty);	/* Restore modes */
 		(void) ioctl(Tty, TCXONC,1);	/* Restart output */
 #endif
@@ -371,6 +373,13 @@
 #ifdef LLITOUT
 		ioctl(Tty, TIOCLSET, &Locmode);
 #endif
+#ifdef TIOCFLUSH
+		{ static int fread = 1;
+		ioctl(Tty, TIOCFLUSH, &fread);	 /* Flush input	queue */
+		}
+#else
+		lseek(Tty, 0L, 2);
+#endif
 #endif
 
 		return OK;
@@ -384,7 +393,6 @@
 #ifdef V7
 #ifdef TIOCSBRK
 #define CANBREAK
-	sleep(1);
 	ioctl(Tty, TIOCSBRK, 0);
 	sleep(1);
 	ioctl(Tty, TIOCCBRK, 0);
@@ -403,16 +411,13 @@
 /* Initialize tty device for serial file xfer */
 inittty()
 {
-	if ((Nametty = ttyname(2)) && *Nametty) {
-		Tty = open(Nametty, 2);
-	} else {
-		Tty = open(Nametty = "/dev/tty", 2);
-	}
-
-	if (Tty <= 0) {
-		perror(Nametty);  exit(2);
-	}
-	Ttystream = fdopen(Tty, "w");
+	Tty = 0;
+	Ttystream = stdout;
+	Nametty	= ttyname(Tty);
+	if (!Nametty ||	!*Nametty)
+		Nametty	= "|pipe|";
+	Logstream = stderr;
+	setbuf(Ttystream, xXbuf);
 }
 
 flushmoc()
@@ -443,7 +448,7 @@
 
 	if (--Lleft >= 0) {
 		if (Verbose > 8) {
-			fprintf(stderr, "%02x ", *cdq&0377);
+			fprintf(Logstream, "%02x ", *cdq&0377);
 		}
 		return (*cdq++ & 0377);
 	}
@@ -451,15 +456,12 @@
 	if (n < 2)
 		n = 2;
 	if (Verbose > 5)
-		fprintf(stderr, "Calling read: alarm=%d  Readnum=%d ",
+		fprintf(Logstream, "Calling read: alarm=%d  Readnum=%d ",
 		  n, Readnum);
 	if (setjmp(tohere)) {
-#ifdef TIOCFLUSH
-/*		ioctl(Tty, TIOCFLUSH, 0); */
-#endif
-		Lleft = 0;
+		purgeline();
 		if (Verbose>1)
-			fprintf(stderr, "Readline:TIMEOUT\n");
+			fprintf(Logstream, "Readline:TIMEOUT\n");
 		return TIMEOUT;
 	}
 	signal(SIGALRM, alrm); alarm(n);
@@ -467,16 +469,16 @@
 	Lleft=read(Tty, cdq=linbuf, Readnum);
 	alarm(0);
 	if (Verbose > 5) {
-		fprintf(stderr, "Read returned %d bytes errno=%d\n",
+		fprintf(Logstream, "Read returned %d bytes errno=%d\n",
 		  Lleft, errno);
 	}
 	if (Lleft < 1)
 		return TIMEOUT;
 	if (Verbose > 8) {
 		for (n = Lleft; --n >= 0; ) {
-			fprintf(stderr, "%02x ", *cdq&0377);
+			fprintf(Logstream, "%02x ", *cdq&0377);
 		}
-		fprintf(stderr, "\n");
+		fprintf(Logstream, "\n");
 	}
 	--Lleft;
 	return (*cdq++ & 0377);
@@ -492,15 +494,45 @@
 	Lleft = 0;
 #ifdef USG
 #ifdef POSIX
-	tcflush(Tty, 0);
+	tcflush(Tty, TCIFLUSH);
 #else
 	ioctl(Tty, TCFLSH, 0);
 #endif
 #else
+#ifdef TIOCFLUSH
+	{ static int fread = 1;
+	ioctl(Tty, TIOCFLUSH, &fread);
+	}
+#else
 	lseek(Tty, 0L, 2);
 #endif
+#endif
 }
 
+/*
+ * Purge the modem output queue of all characters
+ */
+purgeout()
+{
+#ifdef __FreeBSD__
+	fpurge(Ttystream);
+#else
+	rewind(Ttystream);
+#endif
+#ifdef POSIX
+	tcflush(Tty, TCOFLUSH);
+#else
+#ifdef TCFLSH
+	ioctl(Tty, TCFLSH, 1);
+#else
+#ifdef TIOCFLUSH
+	{ static int fwrite = 2;
+	ioctl(Tty, TIOCFLUSH, &fwrite);
+	}
+#endif
+#endif
+#endif
+}
 
 /* send cancel string to get the other end to shut up */
 canit()
@@ -509,8 +541,8 @@
 	 24,24,24,24,24,24,24,24,24,24,8,8,8,8,8,8,8,8,8,8,0
 	};
 
+	purgeline();        /* Do read next time ... */
 	zmputs(canistr);
-	Lleft=0;	/* Do read next time ... */
 }
 
 /*
@@ -543,8 +575,8 @@
 long a, b, c, d;
 {
 	if (Verbose > 2) {
-		fprintf(stderr, f, a, b, c, d);
-		fprintf(stderr, "\n");
+		fprintf(Logstream, f, a, b, c, d);
+		fprintf(Logstream, "\n");
 	}
 }
 
