v16i013: Modem communications package, Part08/08

Rich Salz rsalz at uunet.uu.net
Wed Sep 14 02:46:50 AEST 1988


Submitted-by: Emmet P Gray <fthood!egray>
Posting-number: Volume 16, Issue 13
Archive-name: pcomm2/part08

#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
#	x_send.c
#	x_win.c
#	xmodem.c
export PATH; PATH=/bin:/usr/bin:$PATH
echo shar: "extracting 'x_send.c'" '(11511 characters)'
if test -f 'x_send.c'
then
	echo shar: "will not over-write existing file 'x_send.c'"
else
sed 's/^X//' << \SHAR_EOF > 'x_send.c'
X/*
X * Send a list of files using a version of Ward Christensen's file
X * transfer protocol.  A non-zero return code means an error must be
X * acknowledged by the user (a user generated abort returns a 0).
X */
X
X#include <stdio.h>
X#include <curses.h>
X#include <sys/types.h>
X#include <sys/stat.h>
X#include "config.h"
X#include "dial_dir.h"
X#include "misc.h"
X#include "xmodem.h"
X
Xstatic int tot_err, err_method;
X
Xint
Xsend_xmodem(win, list, type, fast)
XWINDOW *win;
Xchar *list;
Xint type, fast;
X{
X	extern char *protocol[];
X	FILE *fp, *my_fopen();
X	int i, block_size, file_count, secs, mins, hours, big_blocks;
X	int small_blocks, err_count, got_it, num, is_batch, code;
X	int max_block, default_err;
X	long size, block, sent, xmit_size;
X	char *file, *strtok(), *name, *strrchr();
X	unsigned short crc, calc_crc();
X	unsigned char buf[1029], blk, calc_sum();
X	unsigned int packet, sleep();
X	float performance, percent;
X	struct stat stbuf;
X					/* which protocol? */
X	switch (type) {
X		case XMODEM:
X			is_batch = 0;
X			default_err = CRC_CHECKSUM;
X			max_block = 128;
X			performance = 1.36;
X			break;
X		case XMODEM_1k:
X			is_batch = 0;
X			default_err = CRC_CHECKSUM;
X			max_block = 1024;
X			performance = 1.09;
X			break;
X		case MODEM7:
X			is_batch = 1;
X			default_err = CHECKSUM;
X			max_block = 128;
X			performance = 1.36;
X			break;
X		case YMODEM:
X			is_batch = 1;
X			default_err = CRC;
X			max_block = 1024;
X			performance = 1.09;
X			break;
X		case YMODEM_G:
X			is_batch = 1;
X			default_err = NONE;
X			max_block = 1024;
X			performance = 1.02;
X			break;
X		default:
X			return(1);
X	}
X
X	tot_err = 0;
X	file_count = 0;
X	mvwaddstr(win, 2, 24, protocol[type]);
X	mvwaddstr(win, 11, 24, "0  ");
X
X					/* each one in the list */
X	file = strtok(list, " 	");
X	do {
X					/* is it a batch type? */
X		file_count++;
X		if (file_count > 1 && !is_batch)
X			break;
X					/* display the name */
X		clear_line(win, 3, 24, 1);
X		if ((name = strrchr(file, '/')))
X			name++;
X		else
X			name = file;
X		waddstr(win, name);
X		wrefresh(win);
X					/* get the file size */
X		if (stat(file, &stbuf) < 0) {
X			beep();
X			clear_line(win, 12, 24, 1);
X			wattrstr(win, A_BOLD, "CAN'T FIND FILE");
X			wrefresh(win);
X			sleep(3);
X			continue;
X		}
X					/* sanity checking */
X		if ((stbuf.st_mode & S_IFREG) != S_IFREG) {
X			beep();
X			clear_line(win, 12, 24, 1);
X			wattrstr(win, A_BOLD, "NOT REGULAR FILE");
X			wrefresh(win);
X			sleep(3);
X			continue;
X		}
X
X		size = stbuf.st_size;
X		mvwprintw(win, 4, 24, "%-10ld", size);
X		clear_line(win, 5, 24, 1);
X
X		if (!(fp = my_fopen(file, "r"))) {
X			beep();
X			clear_line(win, 12, 24, 1);
X			wattrstr(win, A_BOLD, "PERMISSION DENIED");
X			wrefresh(win);
X			sleep(3);
X			continue;
X		}
X					/* get the xmit size */
X		block_size = max_block;
X		big_blocks = 0;
X		small_blocks = 0;
X		if (block_size == 128) {
X			small_blocks = size / 128;
X			if (size % 128)
X				small_blocks++;
X		}
X		else {
X			big_blocks = size / 1024;
X			small_blocks = (size % 1024) / 128;
X			if (size % 128)
X				small_blocks++;
X
X			if (small_blocks == 8 && !big_blocks) {
X				big_blocks++;
X				small_blocks = 0;
X			}
X					/* if tiny file */
X			if (big_blocks == 0)
X				block_size = 128;
X		}
X
X		xmit_size = ((unsigned int) big_blocks * 1024L) + ((unsigned int) small_blocks * 128L);
X					/* add block 0 to the size */
X		if (type == YMODEM || type == YMODEM_G)
X			xmit_size += 128L;
X
X		secs = (xmit_size * 10.0 / dir->baud[dir->d_cur]) * performance;
X		hours = secs / 3600;
X		mins = (secs % 3600) / 60;
X		secs = (secs % 3600) % 60;
X
X		mvwprintw(win, 6, 24, "%d:%02d:%02d", hours, mins, secs);
X
X					/* some starting numbers */
X		mvwaddstr(win, 7, 24, "     ");
X		mvwaddstr(win, 8, 24, "0%  ");
X		mvwaddstr(win, 9, 24, "0          ");
X		mvwaddstr(win, 10, 24, "0 ");
X		clear_line(win, 12, 24, 1);
X		waddstr(win, "NONE");
X		wrefresh(win);
X					/* send the batch stuff */
X		switch (type) {
X			case MODEM7:
X				if (code = rcv_first(win, default_err)) {
X					fclose(fp);
X					return(code +1);
X				}
X
X				if (send_modem7(win, name)) {
X					fclose(fp);
X					return(1);
X				}
X				break;
X			case YMODEM:
X			case YMODEM_G:
X				if (code = rcv_first(win, default_err)) {
X					fclose(fp);
X					return(code +1);
X				}
X
X				if (code = send_ymodem(win, name, size)) {
X					fclose(fp);
X					/*
X					 * CANCEL now means that the other
X					 * end can't open that file.
X					 */
X					if (code == CANCEL)
X						break;
X					return(code +1);
X				}
X				xmit_size -= 128L;
X				break;
X			default:
X				code = 0;
X				break;
X		}
X					/* remote can't receive that file? */
X		if (code == CANCEL)
X			break;
X					/* wait for first character */
X		if (code = rcv_first(win, default_err)) {
X			fclose(fp);
X			return(code +1);
X		}
X					/* here we go... */
X		clear_line(win, 12, 24, 1);
X		waddstr(win, "NONE");
X		wrefresh(win);
X		sent = 0L;
X		block = 1L;
X		blk = 1;
X		while (num = fread((char *) &buf[3], sizeof(char), block_size, fp)) {
X
X					/* fill short block */
X			if (num < block_size) {
X				for (i=num; i<block_size; i++)
X					buf[i+3] = CTRLZ;
X			}
X
X					/* show current stats */
X			mvwprintw(win, 7, 24, "%-5ld", block);
X			if (fast) {
X				percent = sent * 100.0 / xmit_size;
X				mvwprintw(win, 8, 24, "%0.1f%%", percent);
X				mvwprintw(win, 9, 24, "%-10ld", sent);
X			}
X			wrefresh(win);
X
X					/* build the header */
X			if (block_size == 128)
X				buf[0] = SOH;
X			else
X				buf[0] = STX;
X
X			buf[1] = blk;
X			buf[2] = ~blk;
X
X					/* build the error detection stuff */
X			switch (err_method) {
X				case CHECKSUM:
X					buf[block_size+3] = calc_sum(&buf[3], block_size);
X#ifdef DEBUG
X					fprintf(stderr, "blk=%d, checksum=%d\n", blk, buf[block_size+3]);
X#endif /* DEBUG */
X					packet = block_size +4;
X					break;
X				case CRC:
X					crc = calc_crc(&buf[3], block_size);
X					buf[block_size+3] = crc >> 8;
X					buf[block_size+4] = crc;
X#ifdef DEBUG
X					fprintf(stderr, "blk=%d, crc1=%d, crc2=%d\n", blk, buf[block_size+3], buf[block_size+4]);
X#endif /* DEBUG */
X					packet = block_size +5;
X					break;
X				case NONE:
X					buf[block_size+3] = 0;
X					buf[block_size+4] = 0;
X					packet = block_size +5;
X					break;
X			}
X
X					/* send the block */
X			if (code = send_block(win, buf, packet)) {
X				fclose(fp);
X				return(code +1);
X			}
X			block++;
X			blk++;
X			sent = sent + (unsigned int) block_size;
X
X					/* change block size? */
X			if (xmit_size - sent < 1024)
X				block_size = 128;
X		}
X		mvwaddstr(win, 8, 24, "100%  ");
X		mvwprintw(win, 9, 24, "%-10ld", sent);
X					/* at the end of the file */
X		err_count = 0;
X		got_it = 0;
X		while (err_count < MAX_ERRORS) {
X			putc_line(EOT);
X			if (getc_line(10) == ACK) {
X				got_it++;
X				break;
X			}
X			err_count++;
X		}
X		clear_line(win, 12, 24, 1);
X		if (!got_it) {
X			/*
X			 * So what???  We don't do anything if there is
X			 * no acknowledge from the host!!
X			 */
X			waddstr(win, "NO ACKNOWLEDGE");
X		}
X		else
X			waddstr(win, "TRANSFER COMPLETE");
X		if (!is_batch)
X			beep();
X		wrefresh(win);
X		sleep(2);
X					/* prepare to start again */
X		fclose(fp);
X	} while (file = strtok((char *) NULL, " 	"));
X
X	/*
X	 * The end of batch markers... For modem7 it's an ACK and EOT, for
X	 * ymodem, it's an empty block 0.
X	 */
X	switch (type) {
X		case MODEM7:
X			if (code = rcv_first(win, default_err))
X				return(code +1);
X			putc_line(ACK);
X			putc_line(EOT);
X			beep();
X			wrefresh(win);
X			break;
X		case YMODEM:
X		case YMODEM_G:
X			if (code = rcv_first(win, default_err))
X				return(code +1);
X
X			if (code = send_ymodem(win, "", 0L))
X				return(code +1);
X			beep();
X			wrefresh(win);
X			break;
X		default:
X			break;
X	}
X	return(0);
X}
X
X/*
X * Wait for the first character to start the transmission.  This first
X * character also sets the crc/checksum method.  Returns the standard
X * error codes, or 0 on success.  The variable err_method is global.
X */
X
Xstatic int
Xrcv_first(win, default_err)
XWINDOW *win;
Xint default_err;
X{
X	int i, err_count;
X	unsigned int sleep();
X	void cancel_xfer();
X
X	err_count = 0;
X	while (err_count < MAX_ERRORS) {
X
X					/* scan the keyboard for abort */
X		if (wgetch(win) == ESC) {
X			beep();
X			clear_line(win, 12, 24, 1);
X			waddstr(win, "ABORTED");
X			wrefresh(win);
X			cancel_xfer(0);
X			sleep(3);
X			return(ABORT);
X		}
X					/* scan the TTY line */
X		i = getc_line(10);
X#ifdef DEBUG
X		fprintf(stderr, "rcv_first: got '%c', %02x, %03o, %d\n", i, i, i, i);
X#endif /* DEBUG */
X		switch (i) {
X			case -1:	/* timed out */
X				clear_line(win, 12, 24, 1);
X				wattrstr(win, A_BOLD, "NO RESPONSE");
X				err_count++;
X				break;
X			case NAK:	/* checksum marker */
X				if (default_err == CHECKSUM || default_err == CRC_CHECKSUM) {
X					mvwaddstr(win, 5, 24, "CHECKSUM");
X					err_method = CHECKSUM;
X					return(0);
X				}
X				err_count++;
X				break;
X			case 'C':	/* CRC marker */
X				if (default_err == CRC_CHECKSUM || default_err == CRC) {
X					mvwaddstr(win, 5, 24, "CRC");
X					err_method = CRC;
X					return(0);
X				}
X				err_count++;
X				break;
X			case 'G':	/* ymodem-g marker */
X				if (default_err == NONE) {
X					mvwaddstr(win, 5, 24, "NONE");
X					err_method = NONE;
X					return(0);
X				}
X				err_count++;
X				break;
X			case CAN:	/* two CAN's and you're out! */
X				if (getc_line(2) == CAN) {
X					beep();
X					clear_line(win, 12, 24, 1);
X					wattrstr(win, A_BOLD, "REMOTE ABORTED");
X					wrefresh(win);
X					return(CANCEL);
X				}
X				err_count++;
X				break;
X			default:
X				clear_line(win, 12, 24, 1);
X				waddstr(win, "BAD HEADER");
X				err_count++;
X				break;
X		}
X		mvwprintw(win, 10, 24, "%-2d", err_count);
X		wrefresh(win);
X	}
X					/* failed to get it right? */
X	beep();
X	clear_line(win, 12, 24, 1);
X	wattrstr(win, A_BOLD, "TIMED OUT");
X	wrefresh(win);
X	return(ERROR);
X}
X
X/*
X * Send a block of data, scan the keyboard for a user abort, and check
X * the return codes from the host.  Returns standard error codes or 0
X * on success.
X */
X
Xint
Xsend_block(win, blk, packet)
XWINDOW *win;
Xunsigned char *blk;
Xunsigned int packet;
X{
X	extern int fd;
X	int i, err_count;
X	void cancel_xfer();
X
X	err_count = 0;
X	mvwaddstr(win, 10, 24, "0 ");
X
X	while (err_count < MAX_ERRORS) {
X					/* write the block */
X		write(fd, (char *) blk, packet);
X					/* scan the keyboard */
X		if (wgetch(win) == ESC) {
X			beep();
X			clear_line(win, 12, 24, 1);
X			waddstr(win, "ABORTED");
X			wrefresh(win);
X			cancel_xfer(0);
X			sleep(3);
X			return(ABORT);
X		}
X					/* ymodem-g doesn't need ACKs */
X		if (err_method == NONE)
X			return(0);
X					/* wait for acknowledge */
X		i = getc_line(10);
X#ifdef DEBUG
X		fprintf(stderr, "send_block: got '%c', %02x, %03o, %d\n", i, i, i, i);
X#endif /* DEBUG */
X		switch (i) {
X			case -1:	/* timed out */
X				clear_line(win, 12, 24, 1);
X				waddstr(win, "NO RESPONSE");
X				err_count++;
X				tot_err++;
X				break;
X			case ACK:	/* Hooray!! we got it */
X				return(0);
X			case NAK:	/* show our disappointment... */
X				clear_line(win, 12, 24, 1);
X				if (err_method == CRC)
X					waddstr(win, "CRC FAILED");
X				else
X					waddstr(win, "CHECKSUM FAILED");
X				err_count++;
X				tot_err++;
X				break;
X			case CAN:	/* two CAN's and you're out! */
X				if (getc_line(2) == CAN) {
X					beep();
X					clear_line(win, 12, 24, 1);
X					wattrstr(win, A_BOLD, "REMOTE ABORTED");
X					wrefresh(win);
X					return(CANCEL);
X				}
X				/* fall thru... */
X			default:
X				clear_line(win, 12, 24, 1);
X				waddstr(win, "RESENDING");
X				err_count++;
X				tot_err++;
X				break;
X		}
X		mvwprintw(win, 10, 24, "%-2d", err_count);
X		mvwprintw(win, 11, 24, "%-3d", tot_err);
X		wrefresh(win);
X	}
X					/* failed to get it right */
X	beep();
X	clear_line(win, 12, 24, 1);
X	wattrstr(win, A_BOLD, "TOO MANY ERRORS");
X	wrefresh(win);
X	cancel_xfer(0);
X	return(ERROR);
X}
SHAR_EOF
if test 11511 -ne "`wc -c < 'x_send.c'`"
then
	echo shar: "error transmitting 'x_send.c'" '(should have been 11511 characters)'
fi
fi
echo shar: "extracting 'x_win.c'" '(2577 characters)'
if test -f 'x_win.c'
then
	echo shar: "will not over-write existing file 'x_win.c'"
else
sed 's/^X//' << \SHAR_EOF > 'x_win.c'
X/*
X * Display the file transfer window, and invoke the transfer protocol.
X */
X
X#include <stdio.h>
X#include <curses.h>
X#include "config.h"
X#ifdef OLDCURSES
X#include <termio.h>
X#endif /* OLDCURSES */
X#include "dial_dir.h"
X#include "misc.h"
X#include "xmodem.h"
X
Xvoid
Xxfer_win(list, up, type)
Xchar *list;
Xint up, type;
X{
X	extern int fd;
X	WINDOW *xf_win, *newwin();
X	int ret_code, fast, my_speed;
X	void xmodem_mode(), input_off(), line_set(), error_win(), st_line();
X	static int speed[15] = {0, 50, 75, 110, 134, 150, 200, 300, 600,
X	1200, 1800, 2400, 4800, 9600, 19200};
X	struct termio tbuf;
X
X	touchwin(stdscr);
X	refresh();
X	st_line("");
X
X	xf_win = newwin(15, 44, 2, 30);
X	/*
X	 * This window should be in the non-blocking mode, so we can
X	 * scan the keyboard for input while transferring a file.
X	 */
X	nodelay(xf_win, 1);
X					/* basic window stuff */
X	mvwaddstr(xf_win, 2, 14, "Protocol:");
X	mvwaddstr(xf_win, 3, 13, "File name:");
X	mvwaddstr(xf_win, 4, 13, "File size:");
X	mvwaddstr(xf_win, 5, 4, "Error check method:");
X	mvwaddstr(xf_win, 6, 5, "Est transfer time:");
X	mvwaddstr(xf_win, 7, 11, "Block count:");
X	mvwaddstr(xf_win, 8, 6, "Percent complete:");
X	mvwaddstr(xf_win, 9, 5, "Bytes transferred:");
X	mvwaddstr(xf_win, 10, 5, "Errors this block:");
X	mvwaddstr(xf_win, 11, 5, "Total error count:");
X	mvwaddstr(xf_win, 12, 10, "Last message: NONE");
X	box(xf_win, VERT, HORZ);
X
X	if (up)
X		mvwattrstr(xf_win, 0, 17, A_BOLD, " Uploading ");
X	else
X		mvwattrstr(xf_win, 0, 16, A_BOLD, " Downloading ");
X	mvwaddstr(xf_win, 14, 11, " Press <ESC> to abort ");
X	wrefresh(xf_win);
X					/* fix up the terminal mode */
X	input_off();
X	xmodem_mode(fd);
X
X	/*
X	 * Is your terminal slower than the xfer baud rate?  For example:
X	 * I'm at home with my PC and 1200 baud modem, I call my system
X	 * at work so I can use their 2400 baud modems to call some other
X	 * system.  In this case, I don't wanna spend too much time updating
X	 * my screen at 1200 baud, when I'm transferring the file at 2400 baud.
X	 */
X	fast = 0;
X
X	ioctl(0, TCGETA, &tbuf);
X	my_speed = speed[tbuf.c_cflag & CBAUD];
X
X	if (my_speed >= dir->baud[dir->d_cur])
X		fast++;
X
X	if (up)
X		ret_code = send_xmodem(xf_win, list, type, fast);
X	else
X		ret_code = rcv_xmodem(xf_win, list, type, fast);
X
X	nodelay(xf_win, 0);
X					/* prompt for a key on errors */
X	if (ret_code) {
X		beep();
X		clear_line(xf_win, 13, 9, 1);
X		wattrstr(xf_win, A_BOLD, "Press any key to continue");
X		wrefresh(xf_win);
X		wgetch(xf_win);
X	}
X	werase(xf_win);
X	wrefresh(xf_win);
X	delwin(xf_win);
X					/* undo what xmodem_mode() did */
X	line_set();
X	return;
X}
SHAR_EOF
if test 2577 -ne "`wc -c < 'x_win.c'`"
then
	echo shar: "error transmitting 'x_win.c'" '(should have been 2577 characters)'
fi
fi
echo shar: "extracting 'xmodem.c'" '(6652 characters)'
if test -f 'xmodem.c'
then
	echo shar: "will not over-write existing file 'xmodem.c'"
else
sed 's/^X//' << \SHAR_EOF > 'xmodem.c'
X/*
X * Miscellaneous routines to support the xmodem file transfer protocols.
X */
X
X#define CLIST 64
X
X#include <stdio.h>
X#include <signal.h>
X#include <termio.h>
X#include <sys/types.h>
X#include <sys/stat.h>
X#include "config.h"
X#include "misc.h"
X#include "param.h"
X#include "xmodem.h"
X
X/*
X * Calculate the CRC for the given buffer
X */
X
Xunsigned short
Xcalc_crc(buf, len)
Xunsigned char *buf;
Xint len;
X{
X	register int i;
X	unsigned short crc;
X	static unsigned short crctab[256] = {
X	0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
X	0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
X	0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
X	0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
X	0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
X	0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
X	0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
X	0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
X	0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
X	0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
X	0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
X	0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
X	0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
X	0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
X	0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
X	0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
X	0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
X	0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
X	0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
X	0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
X	0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
X	0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
X	0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
X	0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
X	0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
X	0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
X	0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
X	0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
X	0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
X	0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
X	0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
X	0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0};
X
X	crc = 0;
X	for (i=0; i<len; i++)
X		crc = (crc<<8) ^ crctab[(crc>>8) ^ *buf++];
X
X	return(crc);
X}
X
X/*
X * Calculate the checksum for the given buffer.
X */
X
Xunsigned char
Xcalc_sum(buf, len)
Xunsigned char *buf;
Xint len;
X{
X	unsigned char sum;
X
X	sum = 0;
X	while (--len >= 0)
X		sum += *buf++;
X
X	return(sum);
X}
X
X/*
X * Get a single character from the line with a specified time-out period
X * in seconds.  If the function times-out, it returns a -1.
X */
X
Xstatic int gl_flag;
X
Xint
Xgetc_line(sec)
Xunsigned int sec;
X{
X	extern int fd;
X	int gl_force();
X	char c;
X	unsigned int alarm();
X
X	signal(SIGALRM, gl_force);
X	gl_flag = 0;
X
X	alarm(sec);
X	if (read(fd, &c, 1) <= 0) {
X		alarm(0);
X		return(-1);
X	}
X	if (gl_flag)
X		return(-1);
X	alarm(0);
X	return(c & 0xff);
X}
X/*ARGSUSED*/
Xstatic int
Xgl_force(dummy)
Xint dummy;
X{
X	gl_flag = 1;
X}
X
X/*
X * Same as above, but reads a bunch of characters.  The return code is
X * now just a success/fail indicator.
X */
X
Xstatic int rl_flag;
X
Xint
Xfread_line(buf, len, sec)
Xunsigned char *buf;
Xunsigned int len, sec;
X{
X	extern int fd;
X	int n, rl_force();
X	unsigned int try, alarm();
X
X	signal(SIGALRM, rl_force);
X	rl_flag = 0;
X
X	alarm(sec);
X	while (len) {
X					/* read at most CLIST characters */
X		try = (len > CLIST) ? CLIST : len;
X		if ((n = read(fd, (char *) buf, try)) <= 0) {
X			alarm(0);
X			return(-1);
X		}
X		if (rl_flag)
X			return(-1);
X		len -= n;
X		buf = buf + n;
X	}
X	alarm(0);
X	return(0);
X}
X/*ARGSUSED*/
Xstatic int
Xrl_force(dummy)
Xint dummy;
X{
X	rl_flag = 1;
X}
X
X/*
X * Put a character on the TTY line.  This serves no useful purpose other
X * than making the code look pretty.
X */
X
Xint
Xputc_line(c)
Xunsigned char c;
X{
X	extern int fd;
X
X	return(write(fd, (char *) &c, 1));
X}
X
X/*
X * Put the TTY driver in the mode suitable for xmodem transfers.
X */
X
Xvoid
Xxmodem_mode(filedes)
Xint filedes;
X{
X	struct termio tbuf;
X
X	ioctl(filedes, TCGETA, &tbuf);
X	/*
X	 * Turn off the XON/XOFF flow control, turn off echoing, and
X	 * switch to 8 bit no parity.
X	 */
X	tbuf.c_cc[4] = 1;		/* VMIN */
X	tbuf.c_cc[5] = 0;		/* VTIME */
X	tbuf.c_iflag = 0;		/* no flow control or mapping */
X	tbuf.c_oflag = 0;		/* no char mapping or delays */
X	tbuf.c_lflag = 0;		/* no echo or signals */
X	tbuf.c_cflag &= ~PARENB;	/* no parity */
X	tbuf.c_cflag &= ~CSIZE;
X	tbuf.c_cflag |= CS8;		/* 8 bit */
X
X	ioctl(filedes, TCSETA, &tbuf);
X	ioctl(filedes, TCFLSH, 2);
X	return;
X}
X
X/*
X * Cancel the file transfer.  Send several ^X's to the remote, followed
X * by an equal number of backspaces (in case they have already aborted and
X * we're really at the command line).
X */
X
Xvoid
Xcancel_xfer(recv)
Xint recv;
X{
X	extern char file_name[15];
X
X	if (recv && !strcmp(param->abort, "DELETE"))
X		unlink(file_name);
X
X	putc_line(CAN);
X	putc_line(CAN);
X	putc_line(CAN);
X	putc_line(BS);
X	putc_line(BS);
X	putc_line(BS);
X	return;
X}
X
X/*
X * Shorten a file to a predetermined length.  Used to remove the ^Z
X * padding from the end of files.  (Heaven help us, if one day a binary
X * file actually has ^Z's as part of the end of the file).
X */
X
Xint
Xfix_length(file, len)
Xchar *file;
Xlong len;
X{
X	FILE *fp, *tempfp, *my_fopen();
X	register int num;
X	char *tempfile, *mktemp(), buf[BUFSIZ];
X	struct stat stbuf;
X
X	if (stat(file, &stbuf) < 0)
X		return(1);
X					/* see if we have any work to do */
X	if (len >= stbuf.st_size)
X		return(0);
X
X	if (!(fp = my_fopen(file, "r")))
X		return(1);
X
X	/*
X	 * The temporary file should be in the same directory as the
X	 * file being received because otherwise we'd have no way of
X	 * guaranteeing they would be in the same file system.  (Hard
X	 * links across different file systems aren't allowed).
X	 */
X	tempfile = mktemp("trunXXXXXX");
X	if (!(tempfp = my_fopen(tempfile, "w"))) {
X		fclose(fp);
X		return(1);
X	}
X
X	while (len != 0L) {
X		num = (len > BUFSIZ) ? BUFSIZ : len;
X		fread(buf, sizeof(char), num, fp);
X		if (fwrite(buf, sizeof(char), num, tempfp) != num) {
X			fclose(fp);
X			fclose(tempfp);
X			return(1);
X		}
X		len = len - (unsigned int) num;
X	}
X
X	fclose(fp);
X	fclose(tempfp);
X
X	if (unlink(file) < 0)
X		return(1);
X
X	if (link(tempfile, file) < 0)
X		return(1);
X
X	if (unlink(tempfile) < 0)
X		return(1);
X
X	return(0);
X}
SHAR_EOF
if test 6652 -ne "`wc -c < 'xmodem.c'`"
then
	echo shar: "error transmitting 'xmodem.c'" '(should have been 6652 characters)'
fi
fi
exit 0
#	End of shell archive

-- 
Please send comp.sources.unix-related mail to rsalz at uunet.uu.net.



More information about the Comp.sources.unix mailing list