[net.sources] rb and sb sources

caf@cdi.UUCP (caf) (12/22/83)

echo x - rb.1
cat >rb.1 <<'!E!O!F!'
'\" Revision Level 
'\" Last Delta     10-14-83
.TH RB 1 CDI
.SH NAME
rb \- receive files in batch mode
.SH SYNOPSIS
.B rb
[
.B \-7bquv
]
.PP
.B rb
[
.B \-7bcquv
]
.B file
.PP
.B [-][v]rbCOMMAND
.SH DESCRIPTION
.B Rb
receives 0 or more files as sent by yam
in batch mode.
Iff
.B file
is speficied,
a single file is received in MODEM7 single file mode.

Normally, the file contents are converted to
.SM Unix
conventions by stripping carriage returns and all characters
beginning with Control Z (CP/M end of file).
If the raw pathname ends in

".A",
".ARC",
".CCC",
".CL",
".CMD",
".COM",
".CRL",
".DAT",
".DIR",
".EXE",
".O",
".OBJ",
".OVL",
".PAG",
".REL",
".SAV",
".SUB",
".SWP",
".SYS",
".TAR",
".UTL",
".a",
".o",
".tar"

or if the first sector contains
data that suggest a binary file
(parity bits or characters in the range 000 to 006 before the first ^Z),
or if the file mode is transmitted and the 0100000 bit is set,
that file will be received in binary mode anyway.

If the raw pathname ends in .MSG, or .TXT, any existing file will
be appended to rather than replaced.

Slashes in the pathname are changed to underscore.
Any trailing period in the pathname is eliminated.
Normally, each file name is converted to lower case
unless it contains one or more lower case letters.

Rb works with either standard 128 byte sectors or
1024 byte sectors
(yam k option).
The user should determine experimentally
the conditions under which use of 1k blocks
actually improves throughput without causing
problems.

If extended file information is received,
the file length controls the number of bytes written to
the output dataset,
and the modify time and file mode
(iff non zero)
are set accordingly.

If rb is invoked with stdout and stderr to different datasets,
Verbose is set to 2, causing frame by frame progress reports
to stderr.
This may be disabled with the
.B q
option.

.PP
The meanings of the available options are:
.PP
.PD 0
.TP
.B 7
Transfer file(s) using only 7 bit data.
Files will be transferred with the eighth bit reset.
Both ends must use (or not use) this option.
Cannot be used with the crc option.
.TP
.B b
transfer all files in binary
(tell it like it is)
mode.
This option disables any append mode special processing.
.TP
.B c
Request cyclic redundancy check rather than
the MODEM7 checksum.
Cannot be used with the 7 bit option.
.TP
.B q
Quiet suppresses verbosity.
.TP
.B v
.IR Verbose
causes a list of file
names to be appended to
/tmp/rblog .
More v's generate more output.
.TP
.B u
Retain upper case letters in file names unconditionally.
.PD
.SH EXAMPLES
(
.SM Unix
command)
.RS
$rb
rb: ready C
.br
.RE
(yam command)
.RS
^E
>>>a0: sbt *.h *.c
.br
.RE
.SH SEE ALSO
sq(ucb), usq(ucb), yam manual, yam(CDI)
.SH NOTES
If rb is invoked as
.B rbCOMMAND
(with an optional leading \- as generated by login(1))
rb will pipe each received file to ``COMMAND filename''
with the file contents as standard input.
A typical usage for this form is rbrmail which calls rmail
to post mail.
On some
.SM Unix
systems, the login directory must contain
COMMAND as login sets SHELL=rsh which disallows absolute
pathnames.
If invoked with a leading ``v'' rb will report progress to LOGFILE.
The following entry works for
.SM Unix
3.0.

rbrmail::5:1::/bin:/usr/ucb/rbrmail
.PP
The following (in a shell script)
may be used to fetch file(s) from a remote computer connected to /dev/tty7
once sb has been started on the remote.


rb -7v >/dev/tty7 </dev/tty7
.SH BUGS
Path names are restricted to 127 characters.
In MODEM7 single file mode, the pathname is still processed
as described above.
!E!O!F!
echo x - sb.1
cat >sb.1 <<'!E!O!F!'
'\" Revision Level 
'\" Last Delta     9-23-83
.TH SB 1 CDI
.SH NAME
sb \- send files in batch mode
.SH SYNOPSIS
.B sb
[
.B \-7fkqv
]
.B file ...
.SH DESCRIPTION
.B Sb
sends one or more files to yam or rb
in batch mode.

Only the file name part of the pathname is transmitted.

On Unix systems, additional information about the file is transmitted.
If the receiving program uses this information,
the transmitted file length controls the exact number of bytes written to
the output dataset,
and the modify time and file mode
are set accordingly.

If sb is invoked with stdout and stderr to different datasets,
Verbose is set to 2, causing frame by frame progress reports
to stderr.
This may be disabled with the
.B q
option.

Iff sb is invoked with $SHELL set and iff that vairable contains the
string ``rsh'' (restricted shell), sb operates in restricted mode.
Restricted mode restricts pathnames to the current directory and
PUBDIR (conventionally, /usr/spool/uucppublic) and/or subdirectories
thereof.

.PP
The meanings of the available options are:
.PP
.PD 0
.TP
.B 7
Transfer file(s) using only 7 bit data.
Files will be transferred with the eighth bit reset.
Both ends must use (or not use) this option.
Cannot be used with the crc option.
.TP
.B f
Send Full pathnname.
Normally directory prefices are stripped from the transmitted
filename.
.TP
.B k
Send files using 1024 byte blocks
rather than the default 128 byte blocks.
The user should determine experimentally
the conditions under which use of 1k blocks
actually improves throughput without causing
problems.
.TP
.B q
Quiet suppresses verbosity.
.TP
.B v
.IR Verbose
causes a list of file
names to be appended to
/tmp/rblog .
More v's generate more output.
.PD
.SH EXAMPLES
(Unix command)
.RS
sb *.c
.br
.RE
(yam command)
.RS
^E
>>>a0: ryt
.br
.RE
.SH SEE ALSO
rb(cdi), sq(ucb), usq(ucb), yam manual, yam(cdi), mkbin(ucb)
.SH BUGS
!E!O!F!
echo x - rb.c
cat >rb.c <<'!E!O!F!'
#define VERSION "2.11 12-21-83"

/*% /bin/env - /bin/ncc -DUSG -O -n % -o rb
 *
 * rb.c By Chuck Forsberg
 *
 * A program for Unix which can receive
 *  files from computers running YAM or MODEM.
 *  If no filename is given, YAM batch mode is assumed.
 *
 * Iff the program is invoked by rbCOMMAND, output is piped to 
 * "COMMAND filename"
 *
 *  Supports the CRC option or regular checksum.
 *  Received pathnames containing no lowercase letters will be changed to lower
 *  case unless -u option is given.
 *
 *  Unless the -b (binary) option is given, \r is discarded and
 *  ^Z (which is also discarded) acts as end of file.
 *
 *  Any slashes in the pathname are changed to underscore.
 *  If the raw pathname ends in .MSG or .TXT, any existing file will
 *  be appended to rather than replaced. Trailing periods are eliminated.
 *
 *  If the raw pathname ends in any of the extensions in Extensions,
 *   or .?Q* (squeezed file), or if the first sector contains binary-like
 *   data (parity bits or characters in the range 0 to 6 before ^Z is seen),
 *   or if the transmitted file mode has the 0100000 but set,
 *   that file will be received in binary mode anyway.
 *
 *
 * a log of activities is appended to LOGFILE with the -v option
 * if stdout and stderr refer to different devices, progress is displayed to
 * stderr.
 *
 * rb is derived from yam2.c and sb.c
 * rb uses Unix System III buffered input to reduce CPU time.
 *  USG UNIX (3.0) ioctl conventions courtesy  Jeff Martin
 * 	cc -O -DV7  rb.c -o rb		vanilla Unix version 7
 *	cc -O -DUSG rb.c -o rb		USG (3.0) Unix
 * 	cc -O -DV7 -DCOHERENT rb.c -o rb		Coherent (untested)
 *	cc -o rb.c			Regulus
 *		(Don't try this on Unix, you'll clobber the source!)
 *  Unix is a trademark of Western Electric Company
 *
 *  Regulus conventions 1-10-83 CAF
 */
#define LOGFILE "/tmp/rblog"

#include <stdio.h>
#include <signal.h>
#include <ctype.h>
FILE *popen();

#define OK 0
#define FALSE 0
#define TRUE 1
#define ERROR (-1)

#define HOWMANY 133
#include "rbsb.c"	/* most of the system dependent stuff here */

#ifdef REGULUS
#define READONEONLY
#endif
#ifdef COHERENT
#define READONEONLY
#endif

char *substr();
FILE *fout;

char *Extensions[] = {
".A",
".ARC",
".CCC",
".CL",
".CMD",
".COM",
".CRL",
".DAT",
".DIR",
".EXE",
".O",
".OBJ",
".OVL",
".PAG",
".REL",
".SAV",
".SUB",
".SWP",
".SYS",
".TAR",
".UTL",
".a",
".o",
".tar",
""
};

/* Ward Christensen / CP/M parameters - Don't change these! */
#define ENQ 005
#define CAN ('X'&037)
#define XOFF ('s'&037)
#define XON ('q'&037)
#define SOH 1
#define STX 2
#define EOT 4
#define ACK 6
#define NAK 025
#define CPMEOF 032
#define WANTCRC 0103	/* send C not NAK to get crc not checksum */
#define TIMEOUT (-2)
#define ERRORMAX 5
#define RETRYMAX 5
#define WCEOT (-10)
#define SECSIZ 128	/* cp/m's Magic Number record size */
#define PATHLEN 257	/* ready for 4.2 bsd ? */
#define KSIZE 1024	/* record size with k option */
#define UNIXFILE 0x8000	/* happens to the the S_IFREG file mask bit for stat */

int Lastrx;
int Crcflg;
int Firstsec;
int Eofseen;		/* indicates cpm eof (^Z) has been received */
int totblocks;		/* total number of blocks received */
int errors;

#define DEFBYTL 2000000000L	/* default rx file size */
long Bytesleft;		/* number of bytes of incoming file left */
long Modtime;		/* Unix style mod time for incoming file */
short Filemode;		/* Unix style mode for incoming file */
char Pathname[PATHLEN];
char *Progname;		/* the name by which we were called */

int Batch=0;
int Wcsmask=0377;
int Topipe=0;
int MakeLCPathname=TRUE;	/* make received pathname lower case */
int Verbose=0;
int Quiet=0;		/* overrides logic that would otherwise set verbose */
int Rxbinary=FALSE;	/* receive all files in bin mode */
int Thisbinary;		/* current file is to be received in bin mode */
int Blklen;		/* record length of received packets */
char secbuf[KSIZE];
char linbuf[KSIZE];
int Lleft=0;		/* number of characters in linbuf */
int Llorig;

unsigned short updcrc();

/* Actual effect is to give an error return on read */
alrm()
{
	signal(SIGALRM, alrm);
}

/* called by signal interrupt or terminate to clean things up */
bibi(n)
{
	canit(); mode(0);
	fprintf(stderr, "sb: caught signal %d; exiting\r", n);
	exit(128+n);
}

main(argc, argv)
char *argv[];
{
	register char *cp;
	register npats;
	char *virgin, **patts;
	int exitcode;

	setbuf(stderr, NULL);
	chkinvok(virgin=argv[0]);	/* if called as [-]rbCOMMAND set flag */
	npats = 0;
	while (--argc) {
		cp = *++argv;
		if (*cp == '-') {
			while( *++cp) {
				switch(*cp) {
				case '7':
					Wcsmask = 0177;
				case 'b':
					Rxbinary=TRUE; break;
				case 'k':
				case 'c':
					Crcflg=TRUE; break;
				case 'q':
					Quiet=TRUE; Verbose=0; break;
				case 'u':
					MakeLCPathname=FALSE; break;
				case 'v':
					++Verbose; break;
				default:
					usage();
				}
			}
		}
		else if ( !npats && argc>0) {
			if (argv[0][0]) {
				npats=argc;
				patts=argv;
			}
		}
	}
	if (npats > 1)
		usage();
	if (Verbose) {
		if (freopen(LOGFILE, "a", stderr)==NULL) {
			printf("Can't open log file %s\n",LOGFILE);
			exit(0200);
		}
		setbuf(stderr, NULL);
		fprintf(stderr, "argv[0]=%s Progname=%s\n", virgin, Progname);
	}
	if (fromcu() && !Quiet) {
		if (Verbose == 0)
			Verbose = 2;
	}
	mode(1);
	if (signal(SIGINT, bibi) == SIG_IGN) {
		signal(SIGINT, SIG_IGN); signal(SIGKILL, SIG_IGN);
	}
	else {
		signal(SIGINT, bibi); signal(SIGKILL, bibi);
	}
	signal(SIGALRM, alrm);
	if (wcreceive(npats, patts)==ERROR) {
		exitcode=0200;
		canit();
	}
	mode(0);
	if (exitcode != 0)	/* bellow again with all thy might. */
		canit();
#ifdef REGULUS
	else
		printf("\6\6\6\6\6\n");	/* Regulus doesn't wait ... */
#endif
	exit(exitcode);
}


usage()
{
	fprintf(stderr,"%s %s by Chuck Forsberg\n", Progname, VERSION);
	fprintf(stderr,"Usage:	rb [-7buv]\n\tor rb [-bcuv] file\n");
	exit(1);
}

wcreceive(argc, argp)
char **argp;
{
	if (Batch || argc==0) {
		Crcflg=(Wcsmask==0377); fprintf(stderr, "rb: ready ");
		for (;;) {
			totblocks=0;
			if (wcrxpn(secbuf)== ERROR)
				goto fubar;
			if (secbuf[0]==0)
				return OK;
			if (procheader(secbuf) == ERROR)
				goto fubar;
			if (wcrx()==ERROR)
				goto fubar;
		}
	} else {
		totblocks=0; Bytesleft = DEFBYTL; Filemode = 0; Modtime = 0L;

		strcpy(Pathname, *argp);
		if (fopen(*argp, "r") != NULL) {
			fprintf(stderr, "rb: %s exists\r\n", Pathname);
			goto fubar;
		}
		fprintf(stderr, "\r\nrb: ready to receive %s ", Pathname);
		if ((fout=fopen(Pathname, "w")) == NULL)
			return ERROR;
		if (wcrx()==ERROR)
			goto fubar;
	}
	return OK;
fubar:
	canit();
	if (Topipe && fout) {
		pclose(fout);  return ERROR;
	}
	if (fout)
		fclose(fout);
	return ERROR;
}


/*
 * Fetch a pathname from the other end as a C ctyle ASCIZ string.
 * Length is indeterminate as long as less than Blklen
 * a null string represents no more files
 */
wcrxpn(rpn)
char *rpn;	/* receive a pathname */
{
	purgeline();
	Firstsec=TRUE;
	sendline(Crcflg?WANTCRC:NAK);
	if (wcgetsec(rpn, 100) != 0) {
		log( "Pathname fetch failed\n");
		return ERROR;
	}
	sendline(ACK);
	return OK;
}

/*
 * Adapted from CMODEM13.C, written by
 * Jack M. Wierda and Roderick W. Hart
 */

wcrx()
{
	register int sectnum, sectcurr;
	register char sendchar;
	register char *p;
	int cblklen;			/* bytes to dump this block */
	time_t timep[2];

	Firstsec=TRUE;sectnum=0; Eofseen=FALSE;
	sendchar=Crcflg?WANTCRC:NAK;

	for (;;) {
/*
		purgeline();
*/
		sendline(sendchar);	/* send it now, we're ready! */
		sectcurr=wcgetsec(secbuf, (sectnum&0177)?50:130);
		report(sectcurr);
		if (sectcurr==(sectnum+1 &Wcsmask)) {

			if (sectnum==0 && !Thisbinary)
				for (p=secbuf,sectcurr=Blklen;
				  *p != 032 && --sectcurr>=0; ++p)
					if (*p < 07 || (*p & 0200)) {
						Thisbinary++;
						if (Verbose)
							fprintf(stderr, "Changed to BIN\n");
						break;
					}
			sectnum++;
			cblklen = Bytesleft>Blklen ? Blklen:Bytesleft;
			if (putsec(secbuf, cblklen)==ERROR)
				return ERROR;
			if ((Bytesleft-=cblklen) < 0)
				Bytesleft = 0;
			sendchar=ACK;
		}
		else if (sectcurr==(sectnum&Wcsmask)) {
			log( "Received dup Sector\n");
			sendchar=ACK;
		}
		else if (sectcurr==WCEOT) {
			if (Topipe) {
				if (pclose(fout)!=ERROR) {
					sendline(ACK);
					return OK;
				}
				canit(); return ERROR;
			}
			sendline(ACK);
			if (fclose(fout)==ERROR) {
				if (Verbose)
					fprintf(stderr, "fclose returned ERROR\n");
				return ERROR;
			}
			if (Modtime) {
				timep[0] = time(NULL);
				timep[1] = Modtime;
				utime(Pathname, timep);
			}
			if (Filemode)
				chmod(Pathname, (07777 & Filemode));
			return OK;
		}
		else if (sectcurr==ERROR)
			return ERROR;
		else {
			log( "Sync Error\n");
			return ERROR;
		}
	}
}

/*
 * wcgetsec fetches a Ward Christensen type sector.
 * Returns sector number encountered or ERROR if valid sector not received,
 * or CAN CAN received
 * or WCEOT if eot sector
 * time is timeout for first char, set to 4 seconds thereafter
 ***************** NO ACK IS SENT IF SECTOR IS RECEIVED OK **************
 *    (Caller must do that when he is good and ready to get next sector)
 */

wcgetsec(rxbuf, maxtime)
char *rxbuf;
int maxtime;
{
	register checksum, wcj, firstch;
	register unsigned short oldcrc;
	register char *p;
	int sectcurr;

	for (Lastrx=errors=0; errors<RETRYMAX; errors++) {

		if ((firstch=readline(maxtime))==STX) {
			Blklen=KSIZE; goto get2;
		}
		if (firstch==SOH) {
			Blklen=SECSIZ;
get2:
			sectcurr=readline(1);
			if ((sectcurr+(oldcrc=readline(1)))==Wcsmask) {
				oldcrc=checksum=0;
				for (p=rxbuf,wcj=Blklen; --wcj>=0; ) {
					if ((firstch=readline(1)) < 0)
						goto bilge;
					oldcrc=updcrc(firstch, oldcrc);
					checksum += (*p++ = firstch);
				}
				if ((firstch=readline(1)) < 0)
					goto bilge;
				if (Crcflg) {
					oldcrc=updcrc(firstch, oldcrc);
					if ((firstch=readline(1)) < 0)
						goto bilge;
					oldcrc=updcrc(firstch, oldcrc);
					if (oldcrc)
						log("CRC=0%o\n", oldcrc);
					else {
						Firstsec=FALSE;
						return sectcurr;
					}
				}
				else if (((checksum-firstch)&Wcsmask)==0) {
					Firstsec=FALSE;
					return sectcurr;
				}
				else
					log( "Checksum Error\n");
			}
			else
				log("Sector number garbled 0%o 0%o\n",
				 sectcurr, oldcrc);
		}
		/* make sure eot really is eot and not just mixmash */
		else if (firstch==EOT && readline(1)==TIMEOUT)
			return WCEOT;
		else if (firstch==CAN) {
			if (Lastrx==CAN) {
				log( "Sender CANcelled\n");
				return ERROR;
			} else {
				Lastrx=CAN;
				continue;
			}
		}
		else if (firstch==TIMEOUT) {
			if (Firstsec)
				goto humbug;
bilge:
			log( "Timeout\n");
		}
		else
			log( "Got 0%o sector header\n", firstch);

humbug:
		Lastrx=0;
		while(readline(1)!=TIMEOUT)
			;
		if (Firstsec)
			sendline(Crcflg?WANTCRC:NAK);
		else {
			maxtime=40; sendline(NAK);
		}
	}
	/* try to stop the bubble machine. */
	canit();
	return ERROR;
}

/*
 * This version of readline is reasoably well suited for
 * reading many characters.
 *  (except, currently, for the Regulus version!)
 *
 * timeout is in tenths of seconds
 */
#define BOTHER 70	/* don't do alarm stuff if last fetch > BOTHER */
readline(timeout)
int timeout;
{
	static char *cdq;	/* pointer for removing chars from linbuf */
	if (--Lleft>=0)
		return (*cdq++ & Wcsmask);
	if (Llorig<BOTHER)
		settimeout(timeout);
#ifdef DEBUG
	if (Verbose > 3) {
		fprintf(stderr, "Calling read: ", Llorig);
	}
#endif
#ifdef READONEONLY
	/* Verry sorry, Some don't work right in raw mode! */
	Llorig=Lleft=read(0, cdq=linbuf, 1);
#else
	Llorig=Lleft=read(0, cdq=linbuf, KSIZE);
#endif
#ifdef DEBUG
	if (Verbose > 3) {
		fprintf(stderr, "Read returned %d bytes\n", Llorig);
	}
#endif
	if (Llorig < 1)
		return TIMEOUT;
	if (Lleft<BOTHER)
		alarm(0);
	--Lleft;
	return (*cdq++ & Wcsmask);
}



/*
 * timeout is in tenths of seconds
 */
settimeout(timeout)
{
	register int c;

	if ((c = timeout/10)<2)
		c=3;
	alarm(c);
}

purgeline()
{
	Lleft=0;
	lseek(0, 0L, 2);
}

/* update CRC */
unsigned short
updcrc(c, crc)
register c;
register unsigned crc;
{
	register count;

	for (count=8; --count>=0;) {
		if (crc & 0x8000) {
			crc <<= 1;
			crc += (((c<<=1) & 0400)  !=  0);
			crc ^= 0x1021;
		}
		else {
			crc <<= 1;
			crc += (((c<<=1) & 0400)  !=  0);
		}
	}
	return crc;	
}

/*
 * process incoming header
 */
procheader(name)
char *name;
{
	register char *openmode, *p, **pp;

	/* set default parameters */
	openmode = "w"; Thisbinary=Rxbinary;
	Bytesleft = DEFBYTL; Filemode = 0; Modtime = 0L;

	p = name + 1 + strlen(name);
	if (*p) {	/* file coming from Unix type system */
		sscanf(p, "%ld%lo%o", &Bytesleft, &Modtime, &Filemode);
		if (Filemode & UNIXFILE)
			++Thisbinary;
		if (Verbose) {
			fprintf(stderr,  "Incoming: %s %ld %lo %o\n",
			  name, Bytesleft, Modtime, Filemode);
		}
	}
	else {		/* File coming from CP/M type system */
		for (p=name; *p; ++p)		/* change / to _ */
			if ( *p == '/')
				*p = '_';

		if ( *--p == '.')		/* zap trailing period */
			*p = 0;
	}

	/* scan for extensions that signify a binary file */
	if (p=substr(name, ".")) {
		if ( p[2] == 'Q'  || p[2] == 'q' )
			Thisbinary = TRUE;
		else for (pp=Extensions; **pp; ++pp)
			if (strcmp(p, *pp)==0) {
				Thisbinary=TRUE; break;
				}
	}
	/* scan for files which should be appended */
	if ( !Thisbinary
	  && (substr(name, ".TXT")
	  || substr(name, ".txt")
	  || substr(name, ".MSG")))
		openmode = "a";
	if (MakeLCPathname && !IsAnyLower(name))
		uncaps(name);
	if (Topipe) {
		sprintf(Pathname, "%s %s", Progname+2, name);
		if (Verbose)
			fprintf(stderr,  "Topipe: %s %s\n",
			  Pathname, Thisbinary?"BIN":"ASCII");
		if ((fout=popen(Pathname, "w")) == NULL)
			return ERROR;
	} else {
		strcpy(Pathname, name);
		if (Verbose) {
			fprintf(stderr,  "Receiving %s %s %s\n",
			  name, Thisbinary?"BIN":"ASCII", openmode);
		}
		if ((fout=fopen(name, openmode)) == NULL)
			return ERROR;
	}
	return OK;
}

/* make string s lower case */
uncaps(s)
register char *s;
{
	for ( ; *s; ++s)
		if (isupper(*s))
			*s = tolower(*s);
}


/*
 * IsAnyLower returns TRUE if string s has lower case letters.
 */
IsAnyLower(s)
register char *s;
{
	for ( ; *s; ++s)
		if (islower(*s))
			return TRUE;
	return FALSE;
}
/*
 * putsec writes the n characters of buf to receive file fout.
 *  If not in binary mode, carriage returns, and all characters
 *  starting with CPMEOF are discarded.
 */
putsec(buf, n)
char *buf;
register n;
{
	register char *p;

	++totblocks;
	if (Thisbinary)
	{
		for (p=buf; --n>=0; )
			putc( *p++, fout);
	}
	else {
		if (Eofseen)
			return OK;
		for (p=buf; --n>=0; ++p ) {
			if ( *p == '\r')
				continue;
			if (*p == CPMEOF) {
				Eofseen=TRUE; return OK;
			}
			putc(*p ,fout);
		}
	}
	return OK;
}
sendline(c)
{
	putchar(c);
	fflush(stdout);
	Llorig=0;	/* do bother next time ... */
}


/*
 * substr(string, token) searches for token in string s
 * returns pointer to token within string if found, NULL otherwise
 */
char *
substr(s, t)
register char *s,*t;
{
	register char *ss,*tt;
	/* search for first char of token */
	for (ss=s; *s; s++)
		if (*s == *t)
			/* compare token with substring */
			for (ss=s,tt=t; ;) {
				if (*tt == 0)
					return s;
				if (*ss++ != *tt++)
					break;
			}
	return NULL;
}

/*VARARGS1*/
log(s,p,u)
char *s, *p, *u;
{
	if (!Verbose)
		return;
	fprintf(stderr, "error %d: ", errors);
	fprintf(stderr, s, p, u);
}

/* send 10 CAN's to try to get the other end to shut up */
canit()
{
	register n;
	for (n=10; --n>=0; )
		sendline(CAN);
}

#ifdef REGULUS
/*
 * copies count bytes from s to d
 * (No structure assignment in Regulus C compiler)
 */

movmem(s, d, count)
register char *s, *d;
register count;
{
	while (--count >= 0)
		*d++ = *s++;
}
#endif

/*
 * return 1 iff stdout and stderr are different devices
 *  indicating this program operating with a modem on a
 *  different line
 */
fromcu()
{
	struct stat a, b;
	fstat(1, &a); fstat(2, &b);
	return (a.st_rdev != b.st_rdev);
}

report(sct)
int sct;
{
	if (Verbose>1)
		fprintf(stderr,"%3d%c",sct,sct%10? ' ' : '\r');
}

/*
 * if called as [-][dir/../]vrbCOMMAND set Verbose to 1
 * if called as [-][dir/../]rbCOMMAND set the pipe flag
 */
chkinvok(s)
char *s;
{
	register char *p;

	p = s;
	while (*p == '-')
		s = ++p;
	while (*p)
		if (*p++ == '/')
			s = p;
	if (*s == 'v') {
		Verbose=1; ++s;
	}
	Progname = s;
	if (s[2] && s[0]=='r' && s[1]=='b')
		Topipe=TRUE;
}

!E!O!F!
echo x - sb.c
cat >sb.c <<'!E!O!F!'
#define VERSION "sb 2.10 12-21-83"
#define PUBDIR "/usr/spool/uucppublic"

/*% /bin/env - /bin/ncc -O -Dvoid=int -DUSG -n sb.c -o sb

 * sb.c By Chuck Forsberg
 *
 * A small program for Unix which can send 1 or more
 * files in Batch mode to computers running YAM. (Use "rb" in yam.)
 * Supports the CRC option or regular checksum.
 * Exit status is 0 for all transfers completed successfully,
 * 1 for 1 or more unreadable files or'd with 200 for incomplete file xfer.
 *
 * accepts -k option for 1kb record length.
 *
 * sb is derived from yam2.c
 * Uses buffered i/o to reduce the CPU time compared to UMODEM.
 *  USG UNIX (3.0) ioctl conventions courtesy  Jeff Martin
 * 	cc -O -DV7  sb.c -o sb		vanilla Unix version 7
 *	cc -O -DUSG sb.c -o sb		USG (3.0) Unix
 *  Unix is a trademark of Western Electric Company
 */

#define LOGFILE "/tmp/rblog"

#include <stdio.h>
#include <signal.h>
#include <ctype.h>

#define OK 0
#define FALSE 0
#define TRUE 1
#define ERROR (-1)

#define HOWMANY 3
#include "rbsb.c"	/* most of the system dependent stuff here */

#ifdef REGULUS
#define READONEONLY
#endif
#ifdef COHERENT
#define READONEONLY
#endif

FILE *in;

/* Ward Christensen / CP/M parameters - Don't change these! */
#define ENQ 005
#define CAN ('X'&037)
#define XOFF ('s'&037)
#define XON ('q'&037)
#define SOH 1
#define STX 2
#define EOT 4
#define ACK 6
#define NAK 025
#define CPMEOF 032
#define WANTCRC 0103	/* send C not NAK to get crc not checksum */
#define TIMEOUT (-2)
#define ERRORMAX 5
#define RETRYMAX 5
#define WCEOT (-10)
#define SECSIZ 128	/* cp/m's Magic Number record size */
#define KSIZE 1024

char Lastrx;
char Crcflg;
int Wcsmask=0377;
int Verbose=0;
int Restricted=0;	/* restricted; no /.. or ../ in filenames */
int Quiet=0;		/* overrides logic that would otherwise set verbose */
int Fullname=0;		/* transmit full pathname */
int firstsec;
int errcnt=0;		/* number of files unreadable */
int blklen=SECSIZ;	/* length of transmitted records */
int Totsecs;		/* total number of sectors this file */
char txbuf[KSIZE];
int Filcnt=0;		/* count of number of files opened */

unsigned updcrc();
char *substr(), *getenv();

/* called by signal interrupt or terminate to clean things up */
bibi(n)
{
	canit(); mode(0);
	fprintf(stderr, "sb: caught signal %d; exiting\r", n);
	exit(128+n);
}

#ifdef REGULUS
sendline(c)
{
	static char d[2];

	d[0] = c&Wcsmask;
	write(1, d, 1);
}
#else
#define sendline(c) putchar(c & Wcsmask)
#endif

main(argc, argv)
char *argv[];
{
	register char *cp;
	register npats;
	int agcnt; char **agcv;
	char **patts;
	int exitcode;
#ifndef REGULUS
	static char xXbuf[BUFSIZ];
#endif

	if ((cp=getenv("SHELL")) && substr(cp, "rsh"))
		Restricted=TRUE;

	npats=0;
	if (argc<2)
		goto usage;
#ifndef REGULUS
	setbuf(stdout, xXbuf);		
#endif
	while (--argc) {
		cp = *++argv;
		if (*cp == '-') {
			while ( *++cp) {
				switch(*cp) {
				case '7':
					Wcsmask=0177; break;
				case 'f':
					Fullname=TRUE; break;
				case 'k':
					blklen=KSIZE; break;
				case 'q':
					Quiet=TRUE; Verbose=0; break;
				case 'v':
					++Verbose; break;
				default:
					goto usage;
				}
			}
		}
		else if ( !npats && argc>0) {
			if (argv[0][0]) {
				npats=argc;
				patts=argv;
			}
		}
	}
	if (npats < 1) {
usage:
		fprintf(stderr,"%s by Chuck Forsberg\n", VERSION);
		fprintf(stderr,"Usage: sb [-7fkqv] file ...\n");
		exit(1);
	}
	
	if (Verbose) {
		if (freopen(LOGFILE, "a", stderr)==NULL) {
			printf("Can't open log file %s\n",LOGFILE);
			exit(0200);
		}
		setbuf(stderr, NULL);
	}
	if (fromcu() && !Quiet) {
		if (Verbose == 0)
			Verbose = 2;
	}
	if (Verbose != 1) {
		fprintf(stderr, "sb: %d file%s requested:\r\n",
		 npats, npats>1?"s":"");
		for ( agcnt=npats, agcv=patts; --agcnt>=0; ) {
			fprintf(stderr, "%s ", *agcv++);
		}
	}

	mode(1);
	if (signal(SIGINT, bibi) == SIG_IGN) {
		signal(SIGINT, SIG_IGN); signal(SIGKILL, SIG_IGN);
	}
	else {
		signal(SIGINT, bibi); signal(SIGKILL, bibi);
	}

	if (wcsend(npats, patts)==ERROR) {
		exitcode=0200;
		canit();
	}
	fflush(stdout);
	mode(0);
	exit((errcnt != 0) | exitcode);
}

wcsend(argc, argp)
char *argp[];
{
	register n;

	Crcflg=FALSE;
	firstsec=TRUE;
	for (n=0; n<argc; ++n) {
		Totsecs = 0;
		if (wcs(argp[n])==ERROR)
			goto fubar;
	}
	Totsecs = 0;
	if (Filcnt==0) {	/* bitch if we couldn't open ANY files */
		fprintf(stderr,"\r\nCan't open any requested files.\n");
		return ERROR;
	}
	else if (wctxpn("")==ERROR)
		goto fubar;
	return OK;
fubar:
	canit(); return ERROR;
}

wcs(name)
char *name;
{
	register c;
	struct stat f;

	if (Restricted) {
		/* restrict pathnames to current tree or uucppublic */
		if ( substr(name, "../")
		 || (name[0]== '/' && strncmp(name, PUBDIR, strlen(PUBDIR))) ) {
			canit();
			fprintf(stderr,"\r\nsb:\tSecurity Violation\r\n");
			return ERROR;
		}
	}
	if ((in=fopen(name, "r"))==NULL) {
		++errcnt;
		return OK;	/* pass over it, there may be others */
	}
	/* Check for directory or block special files */
#ifndef REGULUS		/* This doesn't seem to work on Regulus */
	fstat(fileno(in), &f);
	c = f.st_mode & S_IFMT;
	if (c == S_IFDIR || c == S_IFBLK) {
		fclose(in);
		return OK;
	}
#endif
	++Filcnt;
	if (wctxpn(name)!= ERROR)
		return wctx();
	else {
		return ERROR;
	}
}

/*
 * generate and transmit pathname block consisting of
 *  pathname (null terminated),
 *  file length, mode time and file mode in octal
 *  as provided by the Unix fstat call.
 */
wctxpn(name)
char *name;
{
	register firstch;
	register char *p, *q;
	struct stat f;

	logent("\r\nAwaiting pathname nak for %s\r\n", *name?name:"<END>");
	if ((firstch=readone(800))==TIMEOUT) {
		logent("Timeout on pathname\n");
		return ERROR;
	}
	if (firstch==WANTCRC)
		Crcflg=TRUE;
	for (p=name, q=txbuf ; *p; )
		if ((*q++ = *p++) == '/' && !Fullname)
			q = txbuf;
	*q++ = 0;
	p=q;
	while (q < (txbuf + KSIZE))
		*q++ = 0;
#ifndef REGULUS		/* This doesn't seem to work on Regulus */
	if (*name && fstat(fileno(in), &f)!= -1)
		sprintf(p, "%lu %lo %o", f.st_size, f.st_mtime, f.st_mode);
#endif
	/* force 1k blocks if name won't fit in 128 byte block */
	if (strlen(txbuf) > 127)
		blklen=KSIZE;
	if (wcputsec(txbuf, 0, SECSIZ)==ERROR)
		return ERROR;
	return OK;
}

wctx()
{
	register int sectnum, attempts, firstch;

	firstsec=TRUE;

	while ((firstch=readone(400))!=NAK && firstch != WANTCRC
	  && firstch!=TIMEOUT && firstch!=CAN)
		;
	if (firstch==CAN) {
		logent("Receiver CANcelled\n");
		return ERROR;
	}
	if (firstch==WANTCRC)
		Crcflg=TRUE;
	sectnum=1;
	while (filbuf(txbuf, blklen)) {
		if (wcputsec(txbuf, sectnum, blklen)==ERROR) {
			return ERROR;
		} else
			sectnum++;
	}
	if (Verbose>1)
		fprintf(stderr, " Closing ");
	fclose(in);
	attempts=0;
	do {
		sendline(EOT);
		purgeline();
		attempts++;
	}
		while ((firstch=(readone(100)) != ACK) && attempts < RETRYMAX);
	if (attempts == RETRYMAX) {
		logent("No ACK on EOT\n");
		return ERROR;
	}
	else
		return OK;
}

wcputsec(buf, sectnum, cseclen)
char *buf;
int sectnum;
int cseclen;	/* data length of this sector to send */
{
	register checksum, wcj;
	register char *cp;
	unsigned oldcrc;
	int firstch;
	int attempts;

	firstch=0;	/* part of logic to detect CAN CAN */

	if (Verbose>1)
		fprintf(stderr, "\rSector %3d %2dk ", Totsecs, Totsecs/8 );
	for (attempts=0; attempts <= RETRYMAX; attempts++) {
		Lastrx= firstch;
		sendline(cseclen==KSIZE?STX:SOH);
		sendline(sectnum);
		sendline(-sectnum -1);
		oldcrc=checksum=0;
		for (wcj=cseclen,cp=buf; --wcj>=0; ) {
			sendline(*cp);
			oldcrc=updcrc(*cp, oldcrc);
			checksum += *cp++;
		}
		if (Crcflg) {
			oldcrc=updcrc(0,updcrc(0,oldcrc));
			sendline((int)oldcrc>>8);
			sendline((int)oldcrc);
		}
		else
			sendline(checksum);
		purgeline();

		firstch=readone(100);
		if (firstch==CAN && Lastrx==CAN) {
			logent("Receiver CANcelled\n");
			return ERROR;
		}
		else if (firstch==ACK) {
			firstsec=FALSE;
			Totsecs += (cseclen>>7);
			return OK;
		}
		else if (firstch==TIMEOUT)
			logent("TIMEOUT on sector ACK\n");
		else if (firstch==ERROR)
			logent("Got error burst for sector ACK\n");
		else {
			logent("Got 0%o for Sector ACK\n", firstch);
			if (firstsec && firstch==WANTCRC)
				Crcflg=TRUE;
			for (;;) {
				Lastrx=firstch;

				if ((firstch=readone(4))==TIMEOUT)
					break;
				if (firstch==CAN && Lastrx==CAN) {
					if (Verbose)
						logent("Receiver CANcelled\n");
					return ERROR;
				}
			}
		}
	}
	if (Verbose)
		logent("Retry Count Exceeded\n");
	return ERROR;
}


/* fill buf with count chars padding with ^Z for CPM */
filbuf(buf, count)
register char *buf;
{
	register c, m;
	m=count;
	while ((c=getc(in))!=EOF) {
		*buf++ =c;
		if (--m == 0)
			break;
	}
	if (m==count)
		return 0;
	else
		while (--m>=0)
			*buf++ = CPMEOF;
	return count;
}


alrm()
{
	/* does nothing; actual effect is to give an error return on read */
}


/*
 * readone(timeout) reads character(s) from file descriptor 0
 * it attempts to read 3 characters. If it gets more than one,
 * it is an error unless all are CAN
 * (otherwise, only normal response is ACK CAN or C)
 *
 * timeout is in tenths of seconds
 */
readone(timeout)
{
	register int c;
	char byt[4];

	fflush(stdout);
	signal(SIGALRM, alrm);
	c = timeout/10;
	if (c<2)
		c=2;
	if (Verbose>3)
		logent("Timeout=%d Calling alarm(%d)\n", timeout, c);
	alarm(c);
#ifndef READONEONLY
	if ((c=read(0, byt, 3))<1)
		return TIMEOUT;
#else
	if ((c=read(0, byt, 1))<1)	/* regulus raw read is unique */
		return TIMEOUT;
#endif
	alarm(0);
	if (c==1)
		return (byt[0]&0377);
	else
		while (c)
			if (byt[--c] != CAN)
				return ERROR;
	return CAN;
}

purgeline()
{
	lseek(0, 0L, 2);
}

/* update CRC */
unsigned updcrc(c, crc)
register c;
register unsigned crc;
{
	register count;

	for (count=8; --count>=0;) {
		if (crc & 0x8000) {
			crc <<= 1;
			crc += (((c<<=1) & 0400)  !=  0);
			crc ^= 0x1021;
		}
		else {
			crc <<= 1;
			crc += (((c<<=1) & 0400)  !=  0);
		}
	}
	return crc;	
}

/* send 10 CAN's to try to get the other end to shut up */
canit()
{
	register n;
	for (n=10; --n>=0; )
		sendline(CAN);
}

#ifdef REGULUS
/*
 * copies count bytes from s to d
 * (No structure assignment in Regulus C compiler)
 */
movmem(s, d, count)
register char *s, *d;
register count;
{
	while (--count >= 0)
		*d++ = *s++;
}
#endif

/*VARARGS1*/
logent(a, b, c)
char *a, *b, *c;
{
	if(Verbose)
		fprintf(stderr, a, b, c);
}

/*
 * return 1 iff stdout and stderr are different devices
 *  indicating this program operating with a modem on a
 *  different line
 */
fromcu()
{
	struct stat a, b;
	fstat(1, &a); fstat(2, &b);
	return (a.st_rdev != b.st_rdev);
}

/*
 * substr(string, token) searches for token in string s
 * returns pointer to token within string if found, NULL otherwise
 */
char *
substr(s, t)
register char *s,*t;
{
	register char *ss,*tt;
	/* search for first char of token */
	for (ss=s; *s; s++)
		if (*s == *t)
			/* compare token with substring */
			for (ss=s,tt=t; ;) {
				if (*tt == 0)
					return s;
				if (*ss++ != *tt++)
					break;
			}
	return NULL;
}

!E!O!F!
echo x - rbsb.c
cat >rbsb.c <<'!E!O!F!'
/*
 * mode function and most of the rest of the system dependent
 * stuff for rb.c and sb.c   This file is #included so the includer
 * can set parameters such as HOWMANY.
 */

#ifdef USG
#include <termio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#define OS "USG"
#endif

#ifdef V7
#include <sgtty.h>
#include <sys/types.h>
#include <sys/stat.h>
#define OS "V7"
#endif

#ifndef OS
#include <termio.h>
#include <sys/ioctl.h>
#include <stat.h>
#define REGULUS
#define OS "REGULUS"
#define void int
#define time_t long
#endif


#ifdef ICANON
struct termio oldtty, tty;
#else
struct sgttyb oldtty, tty;
#endif

/*
 * mode(n)
 *  1: save old tty stat, set raw mode 
 *  0: restore original tty mode
 */
mode(n)
{
	static did0 = FALSE;

	switch(n) {
	case 1:
#ifdef USG
		if(!did0)
			(void) ioctl(0, TCGETA, &oldtty);
		tty = oldtty;

		 /* No echo, crlf mapping, INTR, QUIT, delays, no erase/kill */
		tty.c_lflag &= ~(ECHO | ICANON | ISIG);

		tty.c_iflag = IGNBRK;

		tty.c_oflag = 0;	/* Transparent output */

		tty.c_cflag &= ~PARENB;	/* Leave baud rate alone, disable parity */
		tty.c_cflag |= CS8;	/* Set character size = 8 */
		tty.c_cc[VMIN] = HOWMANY; /* Satisfy reads when this many chars in */
		tty.c_cc[VTIME] = 1;	/* ... or in this many tenths of seconds */
		(void) ioctl(0, TCSETA, &tty);
#endif
#ifdef V7
		if(!did0) {
			ioctl(0, TIOCEXCL, 0);
			ioctl(0, TIOCGETP, &oldtty);
		}
		tty = oldtty;
		tty.sg_flags |= RAW;
		tty.sg_flags &= ~ECHO;
		ioctl(0, TIOCSETP, &tty);
#endif
#ifdef REGULUS
		if(!did0) {
			ioctl(0, TCGETA, &oldtty);
		}
		tty = oldtty;

		tty.c_lflag = 0;
		tty.c_iflag = IGNBRK;
		tty.c_oflag = 0;	/* Transparent output */

		tty.c_cflag &= ~PARENB;	/* Leave baud rate alone, disable parity */
		tty.c_cflag |= CS8;	/* Set character size = 8 */
		tty.c_cc[VMIN] = HOWMANY; /* Satisfy reads when this many chars in */
		tty.c_cc[VTIME] = 1;	/* ... or in this many tenths of seconds */
		ioctl(0, TCSETA, &tty);
#endif
#ifdef REGULUS10
		if(!did0) {
			ioctl(0, TIOCGETP, &oldtty);
		}
		/* Sorry, No structure assignment in Regulus  C */
		movmem( (char *)&oldtty, (char *)&tty, sizeof(tty));
		tty.sg_flags |= (EIGHTBIT|RAW);
		tty.sg_flags &= ~ECHO;
		tty.sg_ledit &= ~LEDIT;
		ioctl(0, TIOCSETP, &tty);
#endif
		did0 = TRUE;
		return OK;
	case 0:
		if(!did0)
			return ERROR;
#ifdef USG
		(void) ioctl(0, TCSBRK, 1);	/* Wait for output to drain */
		(void) ioctl(0, TCFLSH, 1);	/* Flush input queue */
		(void) ioctl(0, TCSETAW, &oldtty);	/* Restore original modes */
		(void) ioctl(0, TCXONC,1);	/* Restart output */
#endif
#ifdef V7
		ioctl(0, TIOCSETP, &oldtty);
		ioctl(0, TIOCNXCL, 0);
#endif
#ifdef REGULUS
		ioctl(0, TCSETAW, &oldtty);	/* Restore original modes */
#endif
#ifdef REGULUS10
		ioctl(0, TIOCSETP, &oldtty);
#endif
		return OK;
	default:
		return ERROR;
	}
}

!E!O!F!
-- 
Chuck Forsberg WA7KGX CDI Portland OR (503)-646-1599 cdi!caf