[net.sources] X/YMODEM VMS C file transfer programs

caf@omen.UUCP (Chuck Forsberg WA7KGX) (01/27/86)

In response to numerous requests, here is are the latest versions of
the VAX rb and sb programs.  The man
pages have been extensively revised since their last posting.

Rb and sb support XMODEM and YMODEM protocol text and binary file transfers
to (rb) and from (sb) Unix systems.  YMODEM supports batch transfers,
1k packets with YMODEM programs such as YAM, IMP, MEX, and others.

Nroff/Troff sources of the manual pages are included with the Unix sources
being posted at the same time.

#	This is a shell archive.
#	Remove everything above and including the cut line.
#	Then run the rest of the file through sh.
-----cut here-----cut here-----cut here-----cut here-----
#!/bin/sh
# shar:	Shell Archiver
#	Run the following text with /bin/sh to create:
#	vrbsb.doc
#	vmodem.h
#	vvmodem.c
#	vvrb.c
#	vvsb.c
# This archive created: Mon Jan 27 01:44:08 1986
echo shar: extracting vrbsb.doc '(11505 characters)'
cat << \SHAR_EOF > vrbsb.doc



     RRRRBBBB((((1111))))		   XXXXEEEENNNNIIIIXXXX SSSSyyyysssstttteeeemmmm	VVVV ((((OOOOMMMMEEEENNNN))))		 RRRRBBBB((((1111))))



     NNNNAAAAMMMMEEEE
	  rb - X/YMODEM	batch file receive

     SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
	  rrbb [ --11bbqquuvv ]

	  rrbb [ --11bbccqquuvv ] _f_i_l_e

	  [[--]][[vv]]rrbbCCOOMMMMAANNDD

     DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
	  RRbb uses the XMODEM and YMODEM	error correcting protocols to
	  receive files	over a serial port from	a variety of programs
	  running under	PC-DOS,	CP/M, *nix, and	other operating
	  systems.

	  The first form of rrbb (Receive	Batch) receives	files with
	  YMODEM batch protocol.  Rb accepts either standard 128 byte
	  sectors or 1024 byte sectors (YAM kk option).	The user
	  should determine experimentally the conditions under which
	  use of 1k blocks actually improves throughput	without
	  causing problems.

	  Normally, the	file contents are converted to 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 packet
	  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 (most
	  significant) bit is set, that	file will be received in
	  binary mode anyway.  Otherwise, if the raw pathname ends in
	  .MSG,	or .TXT, an existing file will be appended to rather
	  than replaced.

	  If extended file information (file length, etc.) 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 no	extended file information is received, slashes in the
	  pathname are changed to underscore, and 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.

	  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 qq



     Page 1					     (printed 1/27/86)






     RRRRBBBB((((1111))))		   XXXXEEEENNNNIIIIXXXX SSSSyyyysssstttteeeemmmm	VVVV ((((OOOOMMMMEEEENNNN))))		 RRRRBBBB((((1111))))



	  option.

	  The second form of rrbb	receives a single _f_i_l_e with XMODEM
	  protocol.  The user must supply the file name	to both
	  sending and receiving	programs.

	  In the third form, rrbb	is invoked as rrbbCCOOMMMMAANNDD	(with an
	  optional leading - as	generated by login(1)).	 For each
	  received file, rb will pipe the file to ``COMMAND filename''
	  where	filename is the	name of	the transmitted	file with the
	  file contents	as standard input.

	  The file transfer is completed when COMMAND exits with 0
	  status.  A non zero exit status terminates the transfer.

	  A typical use	for this form is _r_b_r_m_a_i_l which calls rmail(1)
	  to post mail to the user specified by	the transmitted	file
	  name.	 For example, sending the file "caf" from a PC-DOS
	  system to _r_b_r_m_a_i_l on a Unix system would result in the
	  contents of the DOS file "caf" being mailed to user "caf".

	  On some Unix systems,	the login directory must contain a
	  link to 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
	  Unix 3.0:
		     rbrmail::5:1::/bin:/usr/local/rbrmail
	  If the SHELL environment variable includes _r_s_h or _r_k_s_h
	  (restricted shell), rb will not accept absolute pathnames or
	  references to	a parent directory, will not modify an
	  existing file, and removes any files received	in error.


	  The meanings of the available	options	are:

	  11    Use file	descriptor 1 for ioctls	and reads (Unix	only).
	       By default, file	descriptor 0 is	used.  This option
	       allows rrbb to be used with the _c_u	~$ command.
	  bb    Transfer	all files in binary (tell it like it is) mode.
	       This option disables file append.
	  cc    Request 16 bit CRC (single file transfers default to 8
	       bit checksum).
	  qq    Quiet suppresses	verbosity.
	  vv    _V_e_r_b_o_s_e causes a	list of	file names to be appended to
	       /tmp/rblog .  More v's generate more output.
	  uu    Retain upper case letters in file names
	       unconditionally.

     EEEEXXXXAAAAMMMMPPPPLLLLEEEESSSS






     Page 2					     (printed 1/27/86)






     RRRRBBBB((((1111))))		   XXXXEEEENNNNIIIIXXXX SSSSyyyysssstttteeeemmmm	VVVV ((((OOOOMMMMEEEENNNN))))		 RRRRBBBB((((1111))))



	  ( Unix command)
	       $ _r_b
	       rb: ready C
	  (Pro-YAM command)
	       <_A_L_T-_2>
	       YAM Command: _s_b *._h *._c

     SSSSEEEEEEEE AAAALLLLSSSSOOOO
	  YMODEM.DOC, IMP(CP/M), cu(1),	Professional-YAM manual,
	  sb(omen), usq(omen), undos(omen)

	  Compile time options required	for various operating systems
	  are described	in the rbsb.c source file.

     BBBBUUUUGGGGSSSS
	  Pathnames are	restricted to 127 characters.  In XMODEM
	  single file mode, the	pathname given on the command line is
	  still	processed as described above.  The CR/LF to NL
	  translation merely deletes CR's; undos(omen) performs	a more
	  intelligent translation.

     VVVVMMMMSSSS VVVVEEEERRRRSSSSIIIIOOOONNNN
	  Some of the #includes	with file names	enclosed with angle
	  brackets <> may need to have the angle brackets changed to
	  "", or vice versa.

	  The VMS version does not set binary mode according to	the
	  incoming file	type.  Non binary file processing consists of
	  stripping all	characters beginning with CPMEOF (^Z).

	  The VMS version does not set the file	time.

	  VMS sometimes	loses incoming characters, resulting in
	  retries and degradation of throughput.

	  The VMS C standard i/o package and RMS sometimes interact to
	  modify file contents unexpectedly.

	  The VMS version does not support invocation as rrbbCCOOMMMMAANNDD ..
















     Page 3					     (printed 1/27/86)






     SSSSBBBB((((1111))))		   XXXXEEEENNNNIIIIXXXX SSSSyyyysssstttteeeemmmm	VVVV ((((OOOOMMMMEEEENNNN))))		 SSSSBBBB((((1111))))



     NNNNAAAAMMMMEEEE
	  sb - X/YMODEM	Batch file Send

     SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
	  ssbb [ --11ddffkkqquuvv	] _f_i_l_e ...
	  ssbb --XX	[ --11kkqquuvv ] _f_i_l_e

     DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
	  SSbb uses the XMODEM and YMODEM	error correcting protocols to
	  send files over a serial port	to a variety of	programs
	  running under	PC-DOS,	CP/M, *nix, and	other operating
	  systems.

	  The first form of ssbb sends one or more files with YMODEM
	  batch	protocol.  Normally, 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.

	  The second form of ssbb	uses the --XX flag to send a single _f_i_l_e
	  with XMODEM protocol.	 The user must supply the file name to
	  both sending and receiving programs.

	  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 qq
	  option.

	  Iff sb is invoked with $SHELL	set and	iff that variable
	  contains the string _r_s_h or _r_k_s_h (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.

	  Unix ssbb supports YMODEM-g with "cbreak" tty mode, XON/XOFF
	  flow control,	and the	interrupt character set	to CAN.
	  YMODEM-g (Pro-YAM gg option) increases	throughput over	error
	  free channels	(direct	connection, X.PC, etc.)	by not
	  acknowledging	each transmitted sector.

	  The meanings of the available	options	are:

	  11    Use file	descriptor 1 for ioctls	and reads (Unix	only).
	       By default, file	descriptor 0 is	used.  This option
	       allows ssbb to be used with the _c_u	^$ command.
	  XX    (XMODEM protocol) Send a	single file without the
	       filename	packet.
	  dd    Change all instances of "." to "/" in the transmitted



     Page 1					     (printed 1/27/86)






     SSSSBBBB((((1111))))		   XXXXEEEENNNNIIIIXXXX SSSSyyyysssstttteeeemmmm	VVVV ((((OOOOMMMMEEEENNNN))))		 SSSSBBBB((((1111))))



	       pathname.  Thus,	C.omenB0000 (which is unacceptable to
	       MSDOS or	CP/M) is transmitted as	C/omenB0000.  If the
	       resultant filename has more than	8 characters in	the
	       stem, a "." in inserted to allow	a total	of eleven.
	  ff    Send Full pathnname.  Normally directory	prefices are
	       stripped	from the transmitted filename.
	  kk    Send files using	1024 byte blocks rather	than the
	       default 128 byte	blocks.	 1024 byte packets speed file
	       transfers at high bit rates.
	  qq    Quiet suppresses	verbosity.
	  uu    Unlink the file after successful	transmission.
	  vv    _V_e_r_b_o_s_e causes a	list of	file names to be appended to
	       /tmp/sblog .  More v's generate more output.

     EEEEXXXXAAAAMMMMPPPPLLLLEEEESSSS
	  (Unix	command)
	       $ _s_b -_k *._c
	  (Pro-YAM command)
	       <_F_3>
	  (8-bit YAM Commands)
	       ^_E
	       >>>c: _r_t

     SSSSEEEEEEEE AAAALLLLSSSSOOOO
	  rb(omen), YMODEM.DOC,	Professional-YAM manual, IMP(CP/M),
	  cu(1), sq(omen), todos(omen),	tocpm(omen), tomac(omen)

	  Compile time options required	for various operating systems
	  are described	in the rbsb.c source file.

     BBBBUUUUGGGGSSSS
	  On VMS, some of the #includes	with file names	enclosed with
	  angle	brackets <> may	need to	have the angle brackets
	  removed, or vice versa.

	  The VMS version does not transmit the	file date.

	  The VMS version does not recognize YMODEM-g.

	  The VMS version calculates the file length by	reading	the
	  file and counting the	bytes.

	  When VMS is lightly loaded, the response time	may be too
	  quick	for MODEM7 unless the MODEM7 qq modifier	is used.

	  The VMS C standard i/o package and RMS sometimes interact to
	  modify file contents unexpectedly.








     Page 2					     (printed 1/27/86)



SHAR_EOF
echo shar: extracting vmodem.h '(882 characters)'
cat << \SHAR_EOF > vmodem.h
/*
 *  VMODEM.H
 *  VMS support for UMODEM program
 *
 *	#INCLUDE files defining structures associated with terminal
 *	information structure TT_INFO.
 *	Information about the terminal is passed around in UMODEM in a
 *	STRUCT TT_INFO.
 *
 *  Walter Reiher
 *  Harvard University
 *  Department of Chemistry
 *  12 Oxford Street
 *  Cambridge, MA 02138
 *  March 10, 1983
 */

struct	tt_mode				/*  Info for a IO$_SETMODE call  */
{
	char			class;
	char			type;
	short			page_width;
	char			bcharacteristics[3];
	char			page_length;
	int			echaracteristics;
};

struct	tt_mode_iosb			/*  Terminal IO$_SENSEMODE IOSB  */
{
	short			status;
	char			t_speed;
	char			r_speed;
	char			CR_fill;
	char			LF_fill;
	char			parity_flags;
	char			unused2;
};

struct	tt_info				/*  Summary of terminal infomation  */
{
	struct	tt_mode		dev_characteristics;
	struct	tt_mode_iosb	dev_modes;
};
SHAR_EOF
echo shar: extracting vvmodem.c '(6148 characters)'
cat << \SHAR_EOF > vvmodem.c
/*
 *  VMODEM
 *  VMS support for UMODEM and vvrb/vvsb programs
 *
 *	Defined herein are some utility routines to make the UNIX
 *	program UMODEM run under VAX/VMS C:
 *
 *		assign_channel	Calls the VMS System Service $ASSIGN
 *				to assign a channel to a device.
 *				The routine currently has the device
 *				"TT" hardwired into it.
 *		gtty		Gets terminal characteristics, almost
 *				like the UNIX GTTY system call.
 *		filestat	Returns the length of the file.
 *		raw_read	Reads characters from the terminal
 *				without any echoing or interpretation
 *				and with an optional timeout period.
 *		raw_write	Writes a character to the terminal
 *				without any interpretation.
 *		raw_wbuf	Writes a buffer to the terminal
 *				without any interpretation.
 *		stty		Sets terminal characteristics, almost
 *				like the UNIX STTY system call.
 *
 *	Some of the ideas used here were obtained from code written
 *	by Max Benson and Robert Bruccoleri.
 *
 *  Walter Reiher
 *  Harvard University
 *  Department of Chemistry
 *  12 Oxford Street
 *  Cambridge, MA 02138
 *  March 11, 1983
 *
 *  Modified 12-18-85 Chuck Forsberg, Omen Technology Inc
 *  17505-V NW Sauvie IS RD Portland OR 97231 omen!caf
 */
#include descrip
#include iodef
#include rms
#include ssdef
#include stdio
#include "vmodem.h"

#define  TRUE	1
#define  FALSE	0

static char	tt_name[]	= "TT";
static short	tt_chan		= -1;		/*  Terminal channel number  */

struct	tt_io_iosb				/*  Terminal I/O IOSB  */
{
	short	status;
	short	byte_count;
	short	terminator;
	short	terminator_size;
};

/*
 *	Terminator mask for PASSALL reads.
 *	Permits reads of all possible 8-bit characters.
 */
int	t_mask[32] =  {
	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
	0,  0	};

struct	terminator_mask {
	short	size ;
	short	unused ;
	int	*mask ;
}

termin_mask	= { 32, 0, t_mask };

/*
 *	ASSIGN a channel to the logical name TT, which is usually
 *	the terminal.
 */
assign_channel()
{
	int	status;
	$DESCRIPTOR(tt_descriptor, tt_name);

	if (tt_chan == -1)
		status	= sys$assign(&tt_descriptor, &tt_chan, 0, 0);
	else
		status	= SS$_NORMAL;

	if (status != SS$_NORMAL || tt_chan == -1)
		error("ASSIGN_CHANNEL:  error in SYS$ASSIGN\n", FALSE);

	return;
}

/*
 *	Gets terminal information from VMS.
 */
gtty(tt_characteristics)
struct	tt_info	*tt_characteristics;
{
	int				status;

	if (tt_chan == -1)
		assign_channel();

	status	= sys$qiow(0, tt_chan, IO$_SENSEMODE,
	  &(tt_characteristics->dev_modes), NULL, 0,
	  &(tt_characteristics->dev_characteristics), 12,
	  0, 0, 0, 0);
	if (status != SS$_NORMAL ||
	  tt_characteristics->dev_modes.status != SS$_NORMAL)
		error("GTTY:  sense mode QIO error return.\n", FALSE);

	return(status);
}

/*
 *  FILESTAT
 *  Provide FILE STATistics under VMS
 *
 *	Opens the file and counts the bytes (GROSS!!)
 *	This appears to be the only to get the exact number, given
 *	all of RMS's permutations of file structure.
 *	Returns the number of bytes in the file, returns -1 on error.
 */
long	filestat(name)
char	*name;
{
	FILE		*fin;
	long		bytes = -1;

	if (fin = fopen(name, "r")) {
		while (++bytes, getc(fin) != EOF)
			;
		fclose(fin);
	}
	return bytes;
}



/*
 *	Read NCHAR characters from the terminal without echoing or
 *	interpretation.
 *	If the argument SECONDS is non-zero, use that as the
 *	timeout period in seconds for the read.
 *
 *	NOTE THAT THIS FUNCTION RETURNS AN INT, NOT A CHAR!
 *	That is because of the possibility of a SS$_TIMEOUT return.
 *	Returns SS$_TIMEOUT in case of timeout or other error.
 */
int		raw_read(nchar, charbuf, seconds)
char		*charbuf;
int		nchar;
unsigned	seconds;
{
	short			function;
	int			status;
	struct	tt_io_iosb	iosb;

	if (tt_chan == -1)
		assign_channel();

	function	= IO$_READVBLK | IO$M_NOECHO | IO$M_NOFILTR;

	if (seconds)
		status	= sys$qiow(0, tt_chan, function | IO$M_TIMED,
		  &iosb, NULL, 0,
		  charbuf, nchar, seconds,
		  &termin_mask, NULL, 0);
	else
		status	= sys$qiow(0, tt_chan, function,
		  &iosb, NULL, 0,
		  charbuf, nchar, 0,
		  &termin_mask, NULL, 0);

	if (status == SS$_TIMEOUT || iosb.status == SS$_TIMEOUT)
		return(SS$_TIMEOUT);
	else if (status != SS$_NORMAL || iosb.status != SS$_NORMAL)
		return(SS$_TIMEOUT);

	return((int)*charbuf);
}

/*
 *	Writes a character to the terminal without echoing or
 *	interpretation.
 */
raw_write(c)
char	c;
{
	int			status;
	struct	tt_io_iosb	iosb;

	if (tt_chan == -1)
		assign_channel();

	status	= sys$qiow(0, tt_chan,
	  IO$_WRITEVBLK | IO$M_CANCTRLO | IO$M_NOFORMAT,
	  &iosb, NULL, 0,
	  &c, 1, 0, 0, 0, 0);

	if (status != SS$_NORMAL || iosb.status != SS$_NORMAL)
		error("RAW_WRITE:  write QIO error return.\n", TRUE);

	return;
}

/*
 *	Writes a buffer to the terminal without echoing or
 *	interpretation.
 */
raw_wbuf(nchar, charbuf)
char		*charbuf;
int		nchar;
{
	int			status;
	struct	tt_io_iosb	iosb;

	if (tt_chan == -1)
		assign_channel();

	status	= sys$qiow(0, tt_chan,
	  IO$_WRITEVBLK | IO$M_CANCTRLO | IO$M_NOFORMAT,
	  &iosb, NULL, 0,
	  charbuf, nchar, 0, 0, 0, 0);

	if (status != SS$_NORMAL || iosb.status != SS$_NORMAL)
		error("RAW_WRITE:  write QIO error return.\n", TRUE);

	return;
}

/*
 *  Sets terminal information from VMS.
 *	 Modified 12-85 Larry Farr/Chuck Forsberg to not use
 *	 bad parity returned by VMS 4.
 */
stty(tt_characteristics)
struct	tt_info	*tt_characteristics;
{
	short			*f_ptr, /* *p_ptr, */ *s_ptr;
	int			status;
	struct	tt_mode_iosb	iosb;

	if (tt_chan == -1)
		assign_channel();

/*
 *	We do the following in order to get a full short, concatenating
 *	two adjacent chars:
 */
	s_ptr	= &(tt_characteristics->dev_modes.t_speed);	/*  Speeds  */
	f_ptr	= &(tt_characteristics->dev_modes.CR_fill);	/*  Fills  */
	/* p_ptr	= &(tt_characteristics->dev_modes.parity_flags); */

	status	= sys$qiow(0, tt_chan, IO$_SETMODE,
	  &iosb, NULL, 0,
	  &(tt_characteristics->dev_characteristics), 12,
	  /* *s_ptr, *f_ptr, *p_ptr, 0);	*/
	  *s_ptr, *f_ptr, 0, 0);
	if (status != SS$_NORMAL || iosb.status != SS$_NORMAL)
		printf("STTY:  set mode QIO returned %d\n", status);

	return(status);
}

SHAR_EOF
echo shar: extracting vvrb.c '(15541 characters)'
cat << \SHAR_EOF > vvrb.c
#define VERSION "vvrb 1.01 12-18-85"

/*
 *
 * vvrb.c By Chuck Forsberg
 *	with code fragments stolen from VMS version of UMODEM.C
 *
 * A program for VMS which can receive
 *  files from computers running YAM or MODEM.
 *  If no filename is given, YMODEM (YAM batch) protocol is assumed.
 *
 *  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, 
 *  ^Z (which is discarded) acts as end of file.
 *
 *  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
 *
 * To compile on VMS:
 *		cc vvrb.c
 *		cc vvmodem.c
 *		link vvrb,vvmodem
 *	rb :== $disk$user2:[username.subdir]vvrb.exe
 *
 *	Manual page is "rb.1" in the Unix version rbsb.sh file
 */
#define LOGFILE "rblog"

#include <stdio.h>
#include <ctype.h>
#include "vmodem.h"

#ifdef vms
#include ssdef
#include tt2def
#include ttdef
#define SS_NORMAL SS$_NORMAL
#else
#define SS_NORMAL 0
#endif

/*  VMS structures  */
#ifdef vms
/*
 *	TT_INFO structures are used for passing information about
 *	the terminal.  Used in GTTY and STTY calls.
 */
struct	tt_info	ttys, ttysnew, ttystemp;
#endif

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

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 RETRYMAX 10
#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];

int Batch=0;
int Wcsmask=0377;
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 */

unsigned short updcrc();

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

	setbuf(stderr, NULL);
	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(SS_NORMAL);
		}
		setbuf(stderr, NULL);
	}
	setmodes();

	if (wcreceive(npats, patts)==ERROR) {
		exitcode=0200;
		canit();
	}
	restoremodes(FALSE);
	if (exitcode != 0)	/* bellow again with all thy might. */
		canit();
	exit(SS_NORMAL);
}


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

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\n", Pathname);
			goto fubar;
		}
		fprintf(stderr, "\nrb: ready to receive %s ", Pathname);
		if ((fout=fopen(Pathname, "w")) == NULL)
			return ERROR;
		if (wcrx()==ERROR)
			goto fubar;
	}
	return OK;
fubar:
	canit();
	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 */
{
	register c;

et_tu:
	Firstsec=TRUE;
	sendline(Crcflg?WANTCRC:NAK);
	while ((c = wcgetsec(rpn, 100)) != 0) {
		log( "Pathname fetch returned %d\n", c);
		if (c == WCEOT) {
			sendline(ACK); readline(1); goto et_tu;
		}
		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 */
	long timep[2];

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

	for (;;) {
		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 (fclose(fout)==ERROR) {
				canit();
				fprintf(stderr, "file close ERROR\n");
				return ERROR;
			}
			if (Modtime) {
				timep[0] = time(NULL);
				timep[1] = Modtime;
/*
				utime(Pathname, timep);
*/
			}
			if (Filemode)
				chmod(Pathname, (07777 & Filemode));
			sendline(ACK);
			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(5);
			if ((sectcurr+(oldcrc=readline(5)))==Wcsmask) {
				oldcrc=checksum=0;
				for (p=rxbuf, wcj=Blklen; wcj; wcj -= 128) {
					if (raw_read(128,p,7) == SS$_TIMEOUT) {
						firstch=TIMEOUT; goto bilge;
					}
					p += 128;
				}
				for (p=rxbuf, wcj=Blklen; --wcj>=0; ) {
					firstch = *p++ & Wcsmask;
					oldcrc=updcrc(firstch, oldcrc);
					checksum += (firstch);
				}
				if ((firstch=readline(5)) < 0)
					goto bilge;
				if (Crcflg) {
					oldcrc=updcrc(firstch, oldcrc);
					if ((firstch=readline(5)) < 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 && Lleft==0)
			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;
		if (firstch != TIMEOUT)
			junkpacket();
		if (Firstsec)
			sendline(Crcflg?WANTCRC:NAK);
		else {
			maxtime=40; sendline(NAK);
		}
	}
	/* try to stop the bubble machine. */
	canit();
	return ERROR;
}


/* 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, "."))
		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);
	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;
}


/*
 * 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


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

/* set tty modes for vvrb transfers */
setmodes()
{
/*  Device characteristics for VMS  */
#ifdef vms
	int	*iptr, parameters;

/*
 *	Get current terminal parameters.
 */
	if (gtty(&ttys) != SS$_NORMAL)
		error("SETMODES:  error return from GTTY (1)", FALSE);
	if (gtty(&ttysnew) != SS$_NORMAL)
		error("SETMODES:  error return from GTTY (2)", FALSE);

/*
 *	Set new terminal parameters.
 *	Note that there are three bytes of terminal characteristics,
 *	so we should make sure the fourth byte of the integer is unchanged.
 */
	iptr	= &(ttysnew.dev_characteristics.bcharacteristics);
	parameters	= *iptr;

	parameters	&= ~TT$M_ESCAPE;		/*  ESCAPE   OFF  */
	parameters	&= ~TT$M_HOSTSYNC;		/*  HOSTSYNC OFF  */
	parameters	|=  TT$M_NOECHO;		/*  NOECHO   ON   */
	parameters	|=  TT$M_PASSALL;		/*  PASSALL  ON   */
	parameters	&= ~TT$M_READSYNC;		/*  READSYNC OFF  */
	parameters	&= ~TT$M_TTSYNC;		/*  TTSYNC   OFF  */
	parameters	&= ~TT$M_WRAP;			/*  WRAP     OFF  */
	parameters	|= TT$M_EIGHTBIT;		/*  EIGHTBIT ON   */

	*iptr		= parameters;

	if (stty(&ttysnew) != SS_NORMAL)
		error("SETMODES:  error return from STTY", TRUE);
#endif
}



/* restore normal tty modes */
restoremodes(errcall)
int errcall;
{
/*  Device characteristic restoration for VMS  */
#ifdef vms
	if (stty(&ttys) != SS_NORMAL)		/*  Restore original modes  */
		{
		if (!errcall)
			error("Error restoring original terminal params.",
									FALSE);
		else
			{
			printf("vvrb/RESTOREMODES:  ");
			printf("Error restoring original terminal params.\n");
			}
		}
#endif
}

/*
	BBUFSIZ  - 128 or 1024 etc.
	raw_read(BBUFSIZ + 7, inbuf, 5 + 3 * (BBUFSIZ + 6));
*/




/* get a byte from data stream -- timeout if "dseconds" elapses */
/*	NOTE, however, that this function returns an INT, not a BYTE!!!  */
readline(dseconds)
{
	int seconds;
	int	c;

	seconds = dseconds/10;
	if (seconds < 5)
		seconds = 5;
#ifdef vms
	c	= raw_read(1, &c, seconds);

	if (c == SS$_TIMEOUT)
		return(TIMEOUT);

	return(c & 0377);  /* return the char */
#endif
}

/* junkpacket gets the line cleared */
junkpacket()
{
	int	c;

#ifdef vms
	for (;;) {
		c	= raw_read(1, &c, 1);

		if (c == SS$_TIMEOUT)
			return;
		}
#endif
}

/* send a byte to data stream */
sendline(data)
{
	char	dataout;

	dataout	= data;
#ifdef vms
	raw_write(dataout);
#endif

}


/* print error message and exit; if mode == TRUE, restore normal tty modes */
error(msg, mode)
char *msg;
int mode;
{
	if (mode)
		restoremodes(TRUE);  /* put back normal tty modes */
	printf("vvrb:  %s\n", msg);
	exit(SS_NORMAL);
}

SHAR_EOF
echo shar: extracting vvsb.c '(11234 characters)'
cat << \SHAR_EOF > vvsb.c
#define VERSION "vvsb 1.03 01-26-86"

/*
 * vvsb.c By Chuck Forsberg
 *
 * A small program for VMS which can send 1 or more
 * files with YMODEM protocol to computers running YAM or compatible programs.
 * (Use the "rb" command in YAM.)
 * Supports the CRC option or regular checksum.
 *
 * accepts -k option for 1kb record length.
 *
 * Can send one file with XOMDEM protocol: use "sb -X file"
 *
 * Typical compile and install sequence:
 *		cc vvsb.c
 *		cc vvmodem.c
 *		link vvsb,vvmodem
 *	sb :== $disk$user2:[username.subdir]vvsb.exe
 *
 *	Manual page is "sb.1" in the Unix version rbsb.sh file
 */

#define LOGFILE "sblog"

#include stdio.h
#include ctype.h
#include "vmodem.h"

#ifdef vms
#include ssdef
#include tt2def
#include ttdef
#define SS_NORMAL SS$_NORMAL
#else
#define SS_NORMAL 0
#endif

/*  VMS structures  */
#ifdef vms
/*
 *	TT_INFO structures are used for passing information about
 *	the terminal.  Used in GTTY and STTY calls.
 */
struct	tt_info	ttys, ttysnew, ttystemp;
#endif


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

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 RETRYMAX 10
#define SECSIZ 128	/* cp/m's Magic Number record size */
#define KSIZE 1024

char Lastrx;
char Crcflg;
int Verbose=0;
int Quiet=0;		/* overrides logic that would otherwise set verbose */
int Modem=0;		/* MODEM - don't send pathnames */
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();



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

	npats=0;
	if (argc<2)
		goto usage;
#ifndef REGULUS
	setbuf(stdout, xXbuf);		
#endif
	while (--argc) {
		cp = *++argv;
		if (*cp == '-') {
			while ( *++cp) {
				switch(*cp) {
				case 'f':
					Fullname=TRUE; break;
				case 'k':
					blklen=KSIZE; break;
				case 'q':
					Quiet=TRUE; Verbose=0; break;
				case 'v':
					++Verbose; break;
				case 'x':
				case 'X':
					++Modem; 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 [-fkqv] file ... (YMODEM)\n");
		fprintf(stderr,"or     sb -X [-kqv] file   (XMODEM)\n");
		exit(SS_NORMAL);
	}
	
	if (Verbose) {
		if (freopen(LOGFILE, "a", stderr)==NULL) {
			printf("Can't open log file %s\n",LOGFILE);
			exit(SS_NORMAL);
		}
		setbuf(stderr, NULL);
	}
	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++);
		}
	}
	if ( !Modem)
		printf("\r\n\bSending in Batch Mode\r\n");
	fflush(stdout);

	setmodes();

	if (wcsend(npats, patts)==ERROR) {
		exitcode=0200;
		canit();
	}
	fflush(stdout);
	restoremodes(FALSE);
	exit(SS_NORMAL);
}

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;

	if ((in=fopen(name, "r"))==NULL) {
		++errcnt;
		return OK;	/* pass over it, there may be others */
	}
	++Filcnt;
	if (wctxpn(name)!= ERROR)
		return wctx();
	else {
		return ERROR;
	}
}

/*
 * generate and transmit pathname block consisting of
 *  pathname (null terminated),
 *  file length provided by RMS
 */
wctxpn(name)
char *name;
{
	register firstch;
	register char *p, *q;
	long filestat();

	uncaps(name);		/* Get it out of VMS RADIX50 */

	if (Modem)
		return OK;
	logent("\r\nAwaiting pathname nak for %s\r\n", *name?name:"<END>");
	if ((firstch=readline(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;

	if (*name)
		sprintf(p, "%lu", filestat(name));

	/* 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=readline(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 {
		logent(" EOT ");
		readline(1);
		sendline(EOT);
		fflush(stdout);
		++attempts;
	}
		while ((firstch=(readline(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 (cp=txbuf, wcj=cseclen; wcj; wcj -= 128) {
			raw_wbuf(128, cp);
			cp += 128;
		}
		for (cp=txbuf, wcj=cseclen; --wcj>=0; ) {
			oldcrc=updcrc(*cp, oldcrc);
			checksum += (*cp++);
		}
		if (Crcflg) {
			oldcrc=updcrc(0,updcrc(0,oldcrc));
			sendline((int)oldcrc>>8);
			sendline((int)oldcrc);
		}
		else
			sendline(checksum);


		firstch = readline(400);
gotnak:
		switch (firstch) {
		case CAN:
			if(Lastrx == CAN) {
cancan:
				logent("Cancelled\n");  return ERROR;
			}
			break;
		case TIMEOUT:
			logent("Timeout on sector ACK\n"); continue;
		case WANTCRC:
			if (firstsec)
				Crcflg = TRUE;
		case NAK:
			logent("NAK on sector\n"); continue;
		case ACK: 
			firstsec=FALSE;
			Totsecs += (cseclen>>7);
			return OK;
		default:
			logent("Got %02x for sector ACK\n", firstch);
			break;
		}
		for (;;) {
			Lastrx = firstch;
			if ((firstch = readline(400)) == TIMEOUT)
				break;
			if (firstch == NAK || firstch == WANTCRC)
				goto gotnak;
			if (firstch == CAN && Lastrx == CAN)
				goto cancan;
		}
	}
	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;
}





/* 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);
}


/*
 * 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;
}


/* set tty modes for sb transfers */
setmodes()
{
/*  Device characteristics for VMS  */
#ifdef vms
	int	*iptr, parameters;

/*
 *	Get current terminal parameters.
 */
	if (gtty(&ttys) != SS$_NORMAL)
		error("SETMODES:  error return from GTTY (1)", FALSE);
	if (gtty(&ttysnew) != SS$_NORMAL)
		error("SETMODES:  error return from GTTY (2)", FALSE);

/*
 *	Set new terminal parameters.
 *	Note that there are three bytes of terminal characteristics,
 *	so we should make sure the fourth byte of the integer is unchanged.
 */
	iptr	= &(ttysnew.dev_characteristics.bcharacteristics);
	parameters	= *iptr;

	parameters	&= ~TT$M_ESCAPE;		/*  ESCAPE   OFF  */
	parameters	&= ~TT$M_HOSTSYNC;		/*  HOSTSYNC OFF  */
	parameters	|=  TT$M_NOECHO;		/*  NOECHO   ON   */
	parameters	|=  TT$M_PASSALL;		/*  PASSALL  ON   */
	parameters	&= ~TT$M_READSYNC;		/*  READSYNC OFF  */
	parameters	&= ~TT$M_TTSYNC;		/*  TTSYNC   OFF  */
	parameters	&= ~TT$M_WRAP;			/*  WRAP     OFF  */
	parameters	|= TT$M_EIGHTBIT;		/*  EIGHTBIT ON   */

	*iptr		= parameters;

	if (stty(&ttysnew) != SS_NORMAL)
		error("SETMODES:  error return from STTY", TRUE);
#endif
}



/* restore normal tty modes */
restoremodes(errcall)
int errcall;
{
/*  Device characteristic restoration for VMS  */
#ifdef vms
	if (stty(&ttys) != SS_NORMAL)		/*  Restore original modes  */
		{
		if (!errcall)
			error("Error restoring original terminal params.",
									FALSE);
		else
			{
			printf("sb/RESTOREMODES:  ");
			printf("Error restoring original terminal params.\n");
			}
		}
#endif
}

/* get a byte from data stream -- timeout if "dseconds" elapses */
/*	NOTE, however, that this function returns an INT, not a BYTE!!!  */
readline(dseconds)
{
	int seconds;
	int	c;

	seconds = dseconds/10;
	if (seconds < 5)
		seconds = 5;
#ifdef vms
	c	= raw_read(1, &c, seconds);

	if (c == SS$_TIMEOUT)
		return(TIMEOUT);

	return(c & 0377);  /* return the char */
#endif
}

/* send a byte to data stream */
sendline(data)
{
	char	dataout;

	dataout	= data;
#ifdef vms
	raw_write(dataout);
#endif

}


/* print error message and exit; if mode == TRUE, restore normal tty modes */
error(msg, mode)
char *msg;
int mode;
{
	if (mode)
		restoremodes(TRUE);  /* put back normal tty modes */
	printf("sb:  %s\n", msg);
	logent("Fatal Error:  %s\n", msg);
	exit(SS_NORMAL);
}


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


SHAR_EOF
#	End of shell archive
exit 0
-- 
   Chuck Forsberg WA7KGX  ...!tektronix!reed!omen!caf   CIS:70715,131
   Author of Professional-YAM communications Tools for PCDOS and Unix
 Omen Technology Inc     17505-V NW Sauvie Island Road Portland OR 97231
Voice: 503-621-3406 TeleGodzilla: 621-3746 300/1200 L.sys entry for omen:
omen Any ACU 1200 1-503-621-3746 se:--se: link ord: Giznoid in:--in: uucp