[comp.unix.microport] XMODEM.BUNDLE for SystemV/AT 286

root@qetzal.UUCP (Admin) (12/18/87)

[Submitted courtesy of John Rupley reachable at arizona!rupley!local]

The attached bundled files are changes to Steve Grandi's Xmodem
program, to obtain compilation under Microport SysV/AT.

The distribution version of Xmodem (comp.sources.unix, volume7)
is coded for BSD.  The changes below should serve for porting to
any generic SysV environment, even though they are specifically for
Microport on an 80286 machine.

NOTE(12/13/87):  Steve Grandi has just released an updated version of 
Xmodem, which I believe includes many and possibly all of the changes 
below. If you use these particular changes, without wanting to rehack 
code, you should get the older Xmodem distribution.

John Rupley
 uucp: ..{ihnp4 | hao!noao}!arizona!rupley!local
 internet: rupley!local@megaron.arizona.edu
 telex: 9103508679(JARJAR)
 (H) 30 Calle Belleza, Tucson AZ 85716 - (602) 325-4533
 (O) Dept. Biochemistry, Univ. Arizona, Tucson AZ 85721 - (602) 621-3929


++++++++++++++++++BUNDLED FILES -- XMODEM CHANGES+++++++++++++++++++++
# To unbundle, sh this file
echo unbundling readme.031687 1>&2
cat >readme.031687 <<'AlBeRtEiNsTeIn'
MODIFICATION OF XMODEM V3.2 FOR UNIX SYSTEM V COMPATIBILITY
March 16, 1987

John Rupley
 uucp: ..{ihnp4 | hao!noao}!arizona!rupley!local
 internet: rupley!local@megaron.arizona.edu
 telex: 9103508679(JARJAR)
 (H) 30 Calle Belleza, Tucson AZ 85716 - (602) 325-4533
 (O) Dept. Biochemistry, Univ. Arizona, Tucson AZ 85721 - (602) 621-3929


SOURCE OF ORIGINAL DISTRIBUTION:
	mod.sources:	mirror.1273 + mirror.1274
			v07i092:  Full-featured XMODEM, Two Parts
	submitted by Steve Grandi  (....seismo!noao!grandi)
	20 Jan, 1987

COMMENTS: 
	XMODEM is a significantly improved descendant of UMODEM. It 
supports batch transfers under the MODEM7 protocol, and it is easy to 
use.  It solves the problem of transferring files into a host Unix 
system (capable of running XMODEM) from CP/M or MS-DOS computers 
(calling into the Unix system with MODEM7 or YMODEM communications 
programs).  It is an alternative to KERMIT, which may not be available 
on a CP/M machine or may be broken or primitive on the Unix host.  It 
was what I was looking for to transfer large directories of data files 
from a CP/M machine -- a tedious job if one did not have batch 
capabilities for MODEM7 on the Unix end.

	This conversion to System V compatibility has been tested only 
for MODEM7 protocols with 128-byte packets.  The Unix host was a 10 MHz 
AT running Microport System V/AT.  The calling machine was a 6 MHz 
CP/M-80 machine with MDM714.  The XMODEM side limited the transfer 
rate, to about 500+ char/sec on receive and 300+ char/sec on send.  I 
am content enough with this not to want to recode send() and readbuf().  
On receives by XMODEM, the times recorded in the log file can be 
erroneously short and the calculated transfer rates erroneously high 
(perhaps hogging of the system by XMODEM and the blocking of clock 
updates).

	XMODEM handles beautifully.  Compliments and thanks to 
Steve Grandi.

FILES IN THIS PACKAGE:
	readme.031587		this file
	README			Steve Grandi's remarks
	getputusg.c		rewrite of getput.c for System V
	oddlot.c		a bit more System V stuff
	changes.all		diff output for all files of System V version
				vs original distribution, except for getput.c
	makefile

YOU NEED TO FTP or UUCP:
	full set of source files from an archive site for 
		comp.sources.unix
			volume 7
			xmodem 		(2 parts) Full-featured XMODEM
					by Steve Grandi, NOAO
			about 75 Kbytes

NOTE (12/13/87):
	there is a distribution just posted that may have incorporated
	most or all of the changes given here.



CHANGES (line nos refer to original distribution):

xmodem.h:
	l.0	#define USG 1 -- generates System V changes in all files
	l.4	changes in #includes for Sysytem V consistency
	1.6	move #include sgtty.h (and termio.h) to file where used

batch.c:
	l.167	strchr() replaces BSD function rindex()
	l.174		ditto

getput.c:
	l.7	compile this file ONLY if NOT USG (ie, if BSD)
		#include sgtty.h (moved here from xmodem.h to ease
		switching between getput.c and getputusg.c with oddlot.c)
	l.367	close of #if of l.7--don't forget to do it

misc.c:
	l.79 |	PCC lint does not take pointer array initialization
	l.107|	so change to doubly indexed array
	l.142|	sep[3][10]
	l.108	minor bug in logfile output: change '/0' to ' '
	l.143		ditto

receive.c:
	l.244	added checksum values to logfile error output
	l.335	bug: crash on transfer of a zero-length
		file into XMODEM (ok on send from XMODEM);
		correction: on empty file reception, signaled by
		firstchar == EOT with rec.. == 0, do not send "C",
		rather close out the reception;
		this results in no file being created in the Unix
		host directory; 
		if one wants a zero-length file corresponding 
		to that of the sender, then changes in code would 
		have to be more extensive.


ODDLOT.C (NEW FILE):

logparms() 	writes values of termio structure to logfile, if DEBUG is set;
		useful to track what reset of sgttyb in getputusg is doing



GETPUTUSG.C (NEW FILE; ADAPTATION OF GETPUT.C):

	l.23	compile this file only if USG is set;
		if USG is not set, then the routines of getput.c will
		be compiled and linked;
		do not forget the closing #endif at the end of the file

	l.28	define HUB6 if need to incorporate fix for these drivers

	l.35	use the sgttyb structure; done for my convenience; 
		someone else should be pure and switch to termio

onalrm()
	l.112	alarm signal catcher; just returns control

readbyte()
	l.122ff convert from BSD polling with select() to timing out 
		with alarm();
		the alarm signal causes return from an unsatisfied read();
		if read satisfied, turn alarm off
	l.131ff bug: HUB6 driver precedes a 377 char with another 
		377 char (even if the stuff in c_iflag is off)
		correction: if 377 received, read another byte; I may not
		like the kludge, but what can one say?

readbuf()
	l.166ff remove BSD definitions and inits for select()
	l.184ff	convert from BSD polling with select()--see readbyte()
	l.202ff	bug correction--see readbyte()
	l.240ff	no napping for USG--the function napms() is deleted;
		one could adapt it, but I was lazy and did not feel the
		need for it on my own system, which cannot keep up
		at 9600 baud anyway; System V routines that give 
		fractional-second ticks are there; but note that one 
		cannot use the alarm clock trick

setmodes()
	l.284	declare logparms()--writes termio parameter values to logfile
	l.300	call logparms() to display starting system termio settings
	l.312ff reset terminal parameters with struct sgtty ttysnew;
		minor changes from BSD settings: TAB2 -> TAB1; TANDEM deleted
	l.327   display new settings with logparms()
	l.342	removed some signal code here and from restoremodes();
		if do more than set the signal catcher, on abnormal exit
		one can go off into space and lock up the Unix host.
		
restoremodes()
	l.361	display restored settings (eq to original?) with logparms()

stamptime()
	l.380ff convert to System V time functions and structures	
AlBeRtEiNsTeIn
echo unbundling README 1>&2
cat >README <<'AlBeRtEiNsTeIn'
The xmodem program implements the Christensen (XMODEM) file transfer
protocol for moving files between 4.2/4.3BSD Unix systems and microcomputers.
The XMODEM/CRC protocol, the MODEM7 batch protocol, the XMODEM-1K
block protocol and the YMODEM batch protocol are all supported by xmodem.
For details of the protocols, see the document edited by Chuck Forsberg titled
XMODEM/YMODEM Protocol Reference.

This program runs on 4.2/4.3BSD systems ONLY.  It has been tested on VAXes
and Suns against the MEX-PC program from Niteowl Software.

I have tried to keep the 4.2isms (select system call, 4.2BSD/v7 tty structures,
gettimeofday system call, etc.) confined to the source file getput.c; but I 
make no guarantees.  Also, I have made no attempt to keep variable names 
under 7 characters.


Program history:

Descended from UMODEM 3.5 by Lauren Weinstein, Richard Conn, and others.

Based on XMODEM Version 1.0 by Brian Kantor, UCSD (3/84)  (Don't blame him 
for what follows....)

Version 2.0 (CRC-16 and Modem7 batch file transfer) (5/85)

Version 2.1 (1K packets) (7/85)

Version 2.2 (general clean-ups and multi-character read speed-ups) (9/85)

Version 2.3 (nap while reading packets; split into several source files) (1/86)

Version 3.0 (Ymodem batch receive; associated changes) (2/86)

Version 3.1 (Ymodem batch send; associated changes) (8/86)

Version 3.2 (general cleanups) (9/86)



Please send bug fixes, additions and comments to:
Steve Grandi, National Optical Astronomy Observatories (Tucson, Arizona)
       {ihnp4,seismo,hao,arizona,...}!noao!grandi 
AlBeRtEiNsTeIn
echo unbundling makefile 1>&2
cat >makefile <<'AlBeRtEiNsTeIn'
FILES= xmodem.c misc.c send.c receive.c batch.c getput.c \
		oddlot.c getputusg.c 

OBJECTS= xmodem.o misc.o send.o receive.o batch.o getput.o\
		oddlot.o getputusg.o

CFLAGS= -O

xmodem: $(OBJECTS)
	cc $(CFLAGS) $(OBJECTS) -o xmodem

$(OBJECTS): xmodem.h

print:
	pr makefile xmodem.h $(FILES) | lp

lint:
	lint -uvx $(FILES) | tee lintout.all

install:
	cp xmodem /usr/local/lbin
AlBeRtEiNsTeIn
echo unbundling changes.all 1>&2
cat >changes.all <<'AlBeRtEiNsTeIn'






batch.c compared with ../xmodem/batch.c
56c56
<                if (isupper((int ) temp))
---
>                if (isupper(temp))
168,170d167
< #if USG
< 	char *strchr();
< #else
172d168
< #endif
179,181d174
< #if USG
<        if ((iptr = strchr(string,'/')))
< #else
183d175
< #endif






getput.c compared with ../xmodem/getput.c
8,11d7
< #if !USG
< 
< #include <sgtty.h>
< 
372,373d367
< 
< #endif






getputusg.c compared with ../xmodem/getputusg.c






misc.c compared with ../xmodem/misc.c
79c79
< char sep[3][10] = { "second", "minute", "hour" };
---
> char *sep[] = { "second", "minute", "hour" };
94d93
<        seconds = (seconds > 0) ? seconds : 0;
98c97
<                rate = (128L * numsect)/seconds;
---
>                rate = 128 * numsect/Seconds;
108,109c107,108
<                        fprintf (LOGFP, "%d %s%c ", nums[i], &sep[i][0],
<                        		nums[i] == 1 ? ' ' : 's');
---
>                        fprintf (LOGFP, "%d %s%c ", nums[i], sep[i],
>                                nums[i] == 1 ? '\0' : 's');
133c132
<        seconds = (1422L * numsect) / ttyspeed + 1;
---
>        seconds = 1422 * numsect / ttyspeed + 1;
143,144c142,143
<                        fprintf (fd, "%d %s%c ", nums[i], &sep[i][0],
<                                nums[i] == 1 ? ' ' : 's');
---
>                        fprintf (fd, "%d %s%c ", nums[i], sep[i],
>                                nums[i] == 1 ? '\0' : 's');






oddlot.c compared with ../xmodem/oddlot.c






receive.c compared with ../xmodem/receive.c
234,235c234
<                                    if (write(fd, (char *) buff, 
<                                    		(unsigned ) bufctr) < 0)
---
>                                    if (write(fd, (char *) buff, bufctr) < 0)
245,248c244
<                                    logitarg("Checksum Error on Sector %s: ", 
<                                    	sectdisp(recvsectcnt,bufsize,1));
<                                    logitarg("sent=%x  ", inchecksum);
<                                    logitarg("recvd=%x\n", checksum);
---
>                                    logitarg("Checksum Error on Sector %s\n", sectdisp(recvsectcnt,bufsize,1));
339,341c335
<        if ((errorflag && !fatalerror) 
< 		|| ((recvsectcnt == 0) && (firstchar != EOT)))
< 			/* check on errors or batch transfers */
---
>        if ((errorflag && !fatalerror) || recvsectcnt == 0) /* check on errors or batch transfers */






send.c compared with ../xmodem/send.c






xmodem.c compared with ../xmodem/xmodem.c






xmodem.h compared with ../xmodem/xmodem.h
1,2d0
< #define USG	1		/* set to 1 for System V, 0 for BSD */
< 
7,10d4
< #if USG
< #include <string.h>
< #include <time.h>
< #else
12c6
< #endif
---
> #include <sgtty.h>
AlBeRtEiNsTeIn
echo unbundling getputusg.c 1>&2
cat >getputusg.c <<'AlBeRtEiNsTeIn'
/*
 * 3/8/87-- Modification for System V/release 2 compatibility
 *
 *	Microport System V/AT is a true port of ATT System V to the
 *	Intel processor, and the following code developed with it
 *	should be generic and should compile and run on any machine 
 *	with System V, release 2.
 *
 *	Included are some bad kludges (see comment lines with ????)
 *	for the Bell Technologies HUB6 serial port card drivers.
 *
 *	J. A. Rupley, 30 Calle Belleza, Tucson, AZ  85716
 *	(602)325-4533
 *	......arizona!rupley!root
 */
/*
 * Contains system routines to get and put bytes, change tty modes, etc
 * Most of the routines were VERY 4.2BSD Specific!!!
 */

#include "xmodem.h"

#if USG

/* delete the following #define if you do not use the */
/* Bell Technologies HUB6 serial card, which requires a special kludge */
/*
#define HUB6
*/
/*
 * stick with the old io protocols
 * Someone else can switch to termio.h
 */

#include <sgtty.h>

/*
 *
 *     Get a byte from the specified file.  Buffer the read so we don't
 *     have to use a system call for each character.
 *
 */
getbyte(fildes, ch)                            /* Buffered disk read */
int fildes;
char *ch;

       {
       static char buf[BUFSIZ];        /* Remember buffer */
       static char *bufp = buf;        /* Remember where we are in buffer */
       
       if (nbchr == 0)                 /* Buffer exausted; read some more */
               {
               if ((nbchr = read(fildes, buf, BUFSIZ)) < 0)
                       error("File Read Error", TRUE);
               bufp = buf;             /* Set pointer to start of array */
               }
       if (--nbchr >= 0)
               {
               *ch = *bufp++;
               return(0);
               }
       else
               {
               return(EOF);
               }
       }

/*   CRC-16 constant array...
     from Usenet contribution by Mark G. Mendel, Network Systems Corp.
     (ihnp4!umn-cs!hyper!mark)
*/

/* crctab as calculated by initcrctab() */
unsigned short crctab[1<<B] = { 
    0x0000,  0x1021,  0x2042,  0x3063,  0x4084,  0x50a5,  0x60c6,  0x70e7,
    0x8108,  0x9129,  0xa14a,  0xb16b,  0xc18c,  0xd1ad,  0xe1ce,  0xf1ef,
    0x1231,  0x0210,  0x3273,  0x2252,  0x52b5,  0x4294,  0x72f7,  0x62d6,
    0x9339,  0x8318,  0xb37b,  0xa35a,  0xd3bd,  0xc39c,  0xf3ff,  0xe3de,
    0x2462,  0x3443,  0x0420,  0x1401,  0x64e6,  0x74c7,  0x44a4,  0x5485,
    0xa56a,  0xb54b,  0x8528,  0x9509,  0xe5ee,  0xf5cf,  0xc5ac,  0xd58d,
    0x3653,  0x2672,  0x1611,  0x0630,  0x76d7,  0x66f6,  0x5695,  0x46b4,
    0xb75b,  0xa77a,  0x9719,  0x8738,  0xf7df,  0xe7fe,  0xd79d,  0xc7bc,
    0x48c4,  0x58e5,  0x6886,  0x78a7,  0x0840,  0x1861,  0x2802,  0x3823,
    0xc9cc,  0xd9ed,  0xe98e,  0xf9af,  0x8948,  0x9969,  0xa90a,  0xb92b,
    0x5af5,  0x4ad4,  0x7ab7,  0x6a96,  0x1a71,  0x0a50,  0x3a33,  0x2a12,
    0xdbfd,  0xcbdc,  0xfbbf,  0xeb9e,  0x9b79,  0x8b58,  0xbb3b,  0xab1a,
    0x6ca6,  0x7c87,  0x4ce4,  0x5cc5,  0x2c22,  0x3c03,  0x0c60,  0x1c41,
    0xedae,  0xfd8f,  0xcdec,  0xddcd,  0xad2a,  0xbd0b,  0x8d68,  0x9d49,
    0x7e97,  0x6eb6,  0x5ed5,  0x4ef4,  0x3e13,  0x2e32,  0x1e51,  0x0e70,
    0xff9f,  0xefbe,  0xdfdd,  0xcffc,  0xbf1b,  0xaf3a,  0x9f59,  0x8f78,
    0x9188,  0x81a9,  0xb1ca,  0xa1eb,  0xd10c,  0xc12d,  0xf14e,  0xe16f,
    0x1080,  0x00a1,  0x30c2,  0x20e3,  0x5004,  0x4025,  0x7046,  0x6067,
    0x83b9,  0x9398,  0xa3fb,  0xb3da,  0xc33d,  0xd31c,  0xe37f,  0xf35e,
    0x02b1,  0x1290,  0x22f3,  0x32d2,  0x4235,  0x5214,  0x6277,  0x7256,
    0xb5ea,  0xa5cb,  0x95a8,  0x8589,  0xf56e,  0xe54f,  0xd52c,  0xc50d,
    0x34e2,  0x24c3,  0x14a0,  0x0481,  0x7466,  0x6447,  0x5424,  0x4405,
    0xa7db,  0xb7fa,  0x8799,  0x97b8,  0xe75f,  0xf77e,  0xc71d,  0xd73c,
    0x26d3,  0x36f2,  0x0691,  0x16b0,  0x6657,  0x7676,  0x4615,  0x5634,
    0xd94c,  0xc96d,  0xf90e,  0xe92f,  0x99c8,  0x89e9,  0xb98a,  0xa9ab,
    0x5844,  0x4865,  0x7806,  0x6827,  0x18c0,  0x08e1,  0x3882,  0x28a3,
    0xcb7d,  0xdb5c,  0xeb3f,  0xfb1e,  0x8bf9,  0x9bd8,  0xabbb,  0xbb9a,
    0x4a75,  0x5a54,  0x6a37,  0x7a16,  0x0af1,  0x1ad0,  0x2ab3,  0x3a92,
    0xfd2e,  0xed0f,  0xdd6c,  0xcd4d,  0xbdaa,  0xad8b,  0x9de8,  0x8dc9,
    0x7c26,  0x6c07,  0x5c64,  0x4c45,  0x3ca2,  0x2c83,  0x1ce0,  0x0cc1,
    0xef1f,  0xff3e,  0xcf5d,  0xdf7c,  0xaf9b,  0xbfba,  0x8fd9,  0x9ff8,
    0x6e17,  0x7e36,  0x4e55,  0x5e74,  0x2e93,  0x3eb2,  0x0ed1,  0x1ef0
    };

/* get a byte from data stream -- timeout if "seconds" elapses */
/* This routine was VERY 4.2 specific */

onalrm()
{
	return;
}


int
readbyte(seconds)
int seconds;
       {
       unsigned char c;
       
       seconds = (seconds > 0) ? seconds : 1;
       signal(SIGALRM, onalrm);
       alarm((unsigned ) seconds);
       if (read(0, (char *) &c, 1) < 0)
       		return(TIMEOUT);
       alarm((unsigned ) 0);

#ifdef HUB6
       /* kludge specific for HUB6 */
       /* double read for 0xff-- ??????? */
       if (c == 0xff)
       {
       		read(0, (char *) &c, 1);
       		if (DEBUG)
       			fprintf(LOGFP, "DEBUG: 0xff read before byte %x\n", c);
       	}
#endif

       if (DEBUG)
               fprintf(LOGFP, "DEBUG: readbyte %02xh\n", c & 0xff);

       return(c & 0xff);  /* return the char */
       }

/* 
 get a buffer (length bufsize) from data stream -- timeout if "seconds" elapses.
 Read bunches of characters to save system overhead;
 Further process data while kernel is reading stream (calculating "checksum").
 Try to nap long enough so kernel collects 100 characters or so until we wake up
*/

/* This routine was VERY 4.2 specific */

int
readbuf(bufsize, seconds, tmode, checksum, bufctr)

int bufsize,   /* number of chars to be read */
seconds,       /* timeout period for each read */
tmode,                 /* transmission mode: TRUE if text, FALSE if binary */
*checksum,     /* pointer to checksum value */
*bufctr;       /* length of actual data string in buffer */

{
       int numread;            /* number of chars read */
       int left;               /* number of chars left to read */
       int recfin = 0;         /* flag that EOF read */
       char inbuf[BBUFSIZ];    /* buffer for incoming packet */
       register unsigned char c;       /* character being processed */
       register unsigned short chksm;  /* working copy of checksum */
       register int bfctr;     /* working copy of bufctr */
       int j;                  /* loop index */

       chksm = 0;
       bfctr = 0;
       seconds = (seconds > 0) ? seconds : 1;

       for (left = bufsize; left > 0;) {

               /* read however many characters are waiting */
               
	       signal(SIGALRM, onalrm);
	       alarm((unsigned ) seconds);
	       if ((numread = read(0, inbuf, (unsigned ) left)) < 0)
	       {
       			return(TIMEOUT);
	       }
	       alarm((unsigned ) 0);
               left -= numread;

               if (DEBUG)
                       fprintf(LOGFP, "DEBUG: readbuf--read %d characters\n", numread);

               /* now process part of packet we just read */

               for (j =  0; j < numread; j++) 
                       {  
                               buff[bfctr] = c = inbuf[j] & 0xff;

#ifdef HUB6
			       /* kludge specific for HUB6 */
			       /* double read for 0xff-- ??????? */
			       if (c == 0xff)
			       {
				       left++;
				       j++;
	                               buff[bfctr] = c = inbuf[j] & 0xff;
	                               if (DEBUG)
						fprintf(LOGFP, "DEBUG: 0xff read before byte %x\n", c);
			       }
#endif

                               if (CRCMODE)  /* CRC */
                                       chksm = (chksm<<B) ^ crctab[(chksm>>(W-B)) ^ c];

                               else        /* checksum */
                                       chksm = ((chksm+c) & 0xff);

                               /* binary mode */
                               if (!tmode)
                                       {  
                                       bfctr++;
                                       continue;
                                       }

                               /* text mode */
                               buff[bfctr] &= 0x7f; /* nuke bit 8 */
                               if (c == CR)       /* skip CR's */
                               continue;
                               if (c == CTRLZ)  /* CP/M EOF char */
                                       {  
                                       recfin = TRUE;
                                       continue;
                                       }
                               if (!recfin)
                                       bfctr++;
                       }       
               /* no nap for USG
               */
       }

       *checksum = chksm;
       *bufctr = bfctr;
       return(0);
}

/* send a byte to data stream */

sendbyte(data)
char data;
       {
       if (DEBUG)
               fprintf(LOGFP, "DEBUG: sendbyte %02xh\n", data & 0xff);

       if (write(1, &data, 1) != 1)    /* write the byte (assume it goes NOW; no flushing needed) */
               error ("Write error on stream", TRUE);
       return;
       }

/* send a buffer to data stream */
writebuf(buffer, nbytes)
char *buffer;
int  nbytes;
       {
       if (DEBUG)
               fprintf(LOGFP, "DEBUG: writebuf (%d bytes)\n", nbytes);

       if (write(1, buffer, (unsigned ) nbytes) != nbytes)         /* write the buffer (assume no TIOCFLUSH needed) */
               error ("Write error on stream", TRUE);
       return;
       }

/* set and restore tty modes for XMODEM transfers */
/* These routines were 4.2/v7(?) specific */

struct sgttyb ttys, ttysnew;   /* for stty terminal mode calls */
struct stat statbuf;           /* for terminal message on/off control */

int wason;                     /* holds status of tty read write/modes */
char *tty;                     /* current tty name */

extern void logparms();

setmodes()
       {
       char *ttyname();

       extern onintr();

       sleep(2);                       /* let the output appear */
       if (ioctl(0,TIOCGETP,&ttys)<0)  /* get tty params [V7] */
               error("Can't get TTY Parameters", TRUE);

       /* the several calls to logparms() allow comparison of the */
       /* contents of the sgttyb and termio structures */
       /* thus if you turn DEBUG on, you can see the */
       /* correspondence between sgtty and termio structures */
       if (DEBUG)
       	       logparms();

       tty = ttyname(0);  /* identify current tty */
       
       /* transfer current modes to new structure */
       ttysnew.sg_ispeed = ttys.sg_ispeed;     /* copy input speed */
       ttysnew.sg_ospeed = ttys.sg_ospeed;     /* copy output speed */
       ttysnew.sg_erase  = ttys.sg_erase;      /* copy erase flags */
       ttysnew.sg_flags  = ttys.sg_flags;      /* copy flags */
       ttysnew.sg_kill   = ttys.sg_kill;       /* copy std terminal flags */

       /* make changes */
       ttysnew.sg_flags |= RAW;    /* set for RAW Mode */
       ttysnew.sg_flags &= ~ECHO;  /* set for no echoing */
       ttysnew.sg_flags &= ~XTABS;  /* set for no tab expansion */
       ttysnew.sg_flags &= ~LCASE;  /* set for no upper-to-lower case xlate */
       ttysnew.sg_flags |= ANYP;  /* set for ANY Parity */
       ttysnew.sg_flags &= ~NL3;  /* turn off ALL 3s - new line */
       ttysnew.sg_flags &= ~TAB1; /* turn off tab 3s */
       ttysnew.sg_flags &= ~CR3;  /* turn off CR 3s */
       ttysnew.sg_flags &= ~FF1;  /* turn off FF 3s */
       ttysnew.sg_flags &= ~BS1;  /* turn off BS 3s */

       /* set new paramters */
       if (ioctl(0,TIOCSETP,&ttysnew) < 0)
               error("Can't set new TTY Parameters", TRUE);
       if (DEBUG)
       	       logparms();

       if (stat(tty, &statbuf) < 0)  /* get tty status */ 
               error("Can't get your TTY Status", TRUE);

       if (statbuf.st_mode & 022)      /* Need to turn messages off */
               if (chmod(tty, (int)statbuf.st_mode & ~022) < 0)
                       error("Can't change  TTY mode", TRUE);
               else 
                       wason = TRUE;
       else 
               wason = FALSE;

       /* set up signal catcher to restore tty state if we are KILLed */

       signal(SIGTERM, onintr);

       }

/* restore normal tty modes */

restoremodes(errcall)
int errcall;
       {
       if (wason)
               if (chmod(tty, (int)statbuf.st_mode | 022) < 0)
                       error("Can't change TTY mode", FALSE);
       if (ioctl(0,TIOCSETP,&ttys) < 0)
               { if (!errcall)
                  error("RESET - Can't restore normal TTY Params", FALSE);
               else
                    printf("RESET - Can't restore normal TTY Params\n");
               }
       if (DEBUG)
       	       logparms();
       return;
       }



/* signal catcher */
onintr()
       {
       error("Kill signal; bailing out", TRUE);
       }

/* create string with a timestamp for log file */
/* This routine was 4.2/v7(?) specific */

char *stamptime()
{
       char *asctime();                /* stuff to get timestamp */
       long time();
       struct tm *localtime();
       long system_time;
       
       system_time = time((long *)0);
       return(asctime(localtime((long *) &system_time)));
}


/* get tty speed for time estimates */

getspeed()
       {
       static int speedtbl[] = {0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800,
                       2400, 4800, 9600, 19200, 0};
       if (ioctl(0,TIOCGETP,&ttys) < 0)        /* get tty structure */
               error("Can't get TTY parameters", FALSE);

       ttyspeed = speedtbl[ttys.sg_ispeed];
       logitarg ("Line speed = %d bits per second\n", ttyspeed);
       }

#endif

AlBeRtEiNsTeIn
echo unbundling oddlot.c 1>&2
cat >oddlot.c <<'AlBeRtEiNsTeIn'
#include "xmodem.h"

#include <termio.h>
#include <fcntl.h>

void
logparms()
{
	struct termio ttyr;
	
	int fd_io;

	if ((fd_io = open("/dev/tty", O_RDWR)) == -1)
		error("Open (rdwr and nodelay) failed\n", FALSE);

 	if (ioctl(fd_io, TCGETA, &ttyr) == -1)
		error("Ioctl failed: not a tty\n", FALSE);

fprintf (LOGFP, 
"DEBUG: flags: i=%4xh o=%4xh c=%4xh l=%4xh disc:%2x\n",
ttyr.c_iflag, ttyr.c_oflag, ttyr.c_cflag, ttyr.c_lflag, ttyr.c_line);

fprintf (LOGFP, 
"INTR=%2xh QUIT=%2xh ERAse=%2xh KILL=%2xh EOF/min=%2xh EOL/time=%2xh ?=%2xh SWTCH=%2xh\n",
ttyr.c_cc[VINTR], ttyr.c_cc[VQUIT], ttyr.c_cc[VERASE], ttyr.c_cc[VKILL], 
ttyr.c_cc[VEOF], ttyr.c_cc[VEOL], ttyr.c_cc[VEOL2], ttyr.c_cc[VSWTCH]);

}

AlBeRtEiNsTeIn

-- 
//////////////////286 Moderator -- comp.unix.microport\\\\\\\\\\\\\\\\\
Email to microport@uwspan for info on the newsgroup comp.unix.microport.
otherwise mail to microport@uwspan with a Subject containing one of:
386 286 Bug Source Merge or "Send Buglist" (rutgers!uwvax!uwspan!microport)