⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crzsz.sh

📁 Linux下基于Z-MODEM的串口通讯程序源码
💻 SH
📖 第 1 页 / 共 5 页
字号:
X *  Assumes file name frame is in secbufX */rzfile(){X	register c, n;XX	Eofseen=FALSE;X	n = 20; rxbytes = 0l;XX	if (c = procheader(secbuf)) {X		return (tryzhdrtype = c);X	}XX	for (;;) {#ifdef SEGMENTSX		chinseg = 0;#endifX		stohdr(rxbytes);X		zshhdr(4,ZRPOS, Txhdr);nxthdr:X		switch (c = zgethdr(Rxhdr)) {X		default:X			vfile("rzfile: Wrong header %d", c);X			if ( --n < 0) {X				sprintf(endmsg, "rzfile: Wrong header %d", c);X				return ERROR;X			}X			continue;X		case ZCAN:X			sprintf(endmsg, "Sender CANcelled");X			return ERROR;X		case ZNAK:#ifdef SEGMENTSX			putsec(secbuf, chinseg);X			chinseg = 0;#endifX			if ( --n < 0) {X				sprintf(endmsg, "rzfile: got ZNAK", c);X				return ERROR;X			}X			continue;X		case TIMEOUT:#ifdef SEGMENTSX			putsec(secbuf, chinseg);X			chinseg = 0;#endifX			if ( --n < 0) {X				sprintf(endmsg, "rzfile: TIMEOUT", c);X				return ERROR;X			}X			continue;X		case ZFILE:X			zrdata(secbuf, 1024);X			continue;X		case ZEOF:#ifdef SEGMENTSX			putsec(secbuf, chinseg);X			chinseg = 0;#endifX			if (rclhdr(Rxhdr) != rxbytes) {X				/*X				 * Ignore eof if it's at wrong place - forceX				 *  a timeout because the eof might have goneX				 *  out before we sent our zrpos.X				 */X				errors = 0;  goto nxthdr;X			}X			if (closeit()) {X				tryzhdrtype = ZFERR;X				vfile("rzfile: closeit returned <> 0");X				sprintf(endmsg,"Error closing file");X				return ERROR;X			}X			vfile("rzfile: normal EOF");X			return c;X		case ERROR:	/* Too much garbage in header search error */#ifdef SEGMENTSX			putsec(secbuf, chinseg);X			chinseg = 0;#endifX			if ( --n < 0) {X				sprintf(endmsg, "Persistent CRC or other ERROR");X				return ERROR;X			}X			zmputs(Attn);X			continue;X		case ZSKIP:#ifdef SEGMENTSX			putsec(secbuf, chinseg);X			chinseg = 0;#endifX			Modtime = 1;X			closeit();X			sprintf(endmsg, "Sender SKIPPED file");X			return c;X		case ZDATA:X			if (rclhdr(Rxhdr) != rxbytes) {X				if ( --n < 0) {X					sprintf(endmsg,"Data has bad addr");X					return ERROR;X				}#ifdef SEGMENTSX				putsec(secbuf, chinseg);X				chinseg = 0;#endifX				zmputs(Attn);  continue;X			}moredata:X			fprintf(stderr, "%7ld ZMODEM%s\n",X			  rxbytes, Crc32r?" CRC-32":"");#ifdef SEGMENTSX			if (chinseg >= (1024 * SEGMENTS)) {X				putsec(secbuf, chinseg);X				chinseg = 0;X			}X			switch (c = zrdata(secbuf+chinseg, 1024))#elseX			switch (c = zrdata(secbuf, 1024))#endifX			{X			case ZCAN:#ifdef SEGMENTSX				putsec(secbuf, chinseg);X				chinseg = 0;#endifX				sprintf(endmsg, "Sender CANcelled");X				return ERROR;X			case ERROR:	/* CRC error */#ifdef SEGMENTSX				putsec(secbuf, chinseg);X				chinseg = 0;#endifX				if ( --n < 0) {X					sprintf(endmsg, "Persistent CRC or other ERROR");X					return ERROR;X				}X				zmputs(Attn);X				continue;X			case TIMEOUT:#ifdef SEGMENTSX				putsec(secbuf, chinseg);X				chinseg = 0;#endifX				if ( --n < 0) {X					sprintf(endmsg, "TIMEOUT");X					return ERROR;X				}X				continue;X			case GOTCRCW:X				n = 20;#ifdef SEGMENTSX				chinseg += Rxcount;X				putsec(secbuf, chinseg);X				chinseg = 0;#elseX				putsec(secbuf, Rxcount);#endifX				rxbytes += Rxcount;X				stohdr(rxbytes);X				sendline(XON);X				zshhdr(4,ZACK, Txhdr);X				goto nxthdr;X			case GOTCRCQ:X				n = 20;#ifdef SEGMENTSX				chinseg += Rxcount;#elseX				putsec(secbuf, Rxcount);#endifX				rxbytes += Rxcount;X				stohdr(rxbytes);X				zshhdr(4,ZACK, Txhdr);X				goto moredata;X			case GOTCRCG:X				n = 20;#ifdef SEGMENTSX				chinseg += Rxcount;#elseX				putsec(secbuf, Rxcount);#endifX				rxbytes += Rxcount;X				goto moredata;X			case GOTCRCE:X				n = 20;#ifdef SEGMENTSX				chinseg += Rxcount;#elseX				putsec(secbuf, Rxcount);#endifX				rxbytes += Rxcount;X				goto nxthdr;X			}X		}X	}}XX/*X * Close the receive dataset, return OK or ERRORX */closeit(){X	time_t time();XX	if (fout == stdout) {X		fflush(stdout);  fout = 0;  return OK;X	}X	if (fclose(fout)==ERROR) {X		fprintf(stderr, "File close ERROR\n");X		return ERROR;X	}X	if (Modtime) {X		timep[0] = time(NULL);X		timep[1] = Modtime;X		utime(Pathname, timep);X	}X	return OK;}XX/*X * Strip leading ! if present, do shell escape. X */sys2(s)register char *s;{X	if (*s == '!')X		++s;X	return system(s);}/*X * Strip leading ! if present, do exec.X */voidexec2(s)register char *s;{X	if (*s == '!')X		++s;X	mode(0);X	execl("/bin/sh", "sh", "-c", s);}X/* End of crz.c */SHAR_EOFchmod 0664 crz.c ||echo 'restore of crz.c failed'Wc_c="`wc -c < 'crz.c'`"test 27738 -eq "$Wc_c" ||	echo 'crz.c: original size 27738, current size' "$Wc_c"fi# ============= csz.c ==============if test -f 'csz.c' -a X"$1" != X"-c"; then	echo 'x - skipping csz.c (File already exists)'elseecho 'x - extracting csz.c (Text)'sed 's/^X//' << 'SHAR_EOF' > 'csz.c' &&#define VERSION "1.03 10-01-99"X/*X **************************************************************************X *X * csz.c By Chuck Forsberg,  Omen Technology INCX *    Copyright 1998 Omen Technology Inc All Rights ReservedX * X *********************************************************************X *********************************************************************X * X *X *	This version implements numerous enhancements including ZMODEMX *	Run Length Encoding and variable length headers.  TheseX *	features were not funded by the original Telenet developmentX *	contract.X * X * X *  Any programs which incorporate part or all of this code must beX *  provided in source form with this notice intact except byX *  prior written permission from Omen Technology Incorporated.X *  This includes compiled executables of this program.X *X *   The .doc files and the file "mailer.crz" must also be included.X * X * Use of this software for commercial or administrative purposesX * except when exclusively limited to interfacing Omen TechnologyX * products requires license payment of $20.00 US per userX * (less in quantity, see mailer.crz).  Use of this code byX * inclusion, decompilation, reverse engineering or any other meansX * constitutes agreement to these conditions and acceptance ofX * liability to license the materials and payment of reasonableX * legal costs necessary to enforce this license agreement.X *X *X *		Omen Technology IncX *		Post Office Box 4681X *		Portland OR 97208X *X *	This code is made available in the hope it will be useful,X *	BUT WITHOUT ANY WARRANTY OF ANY KIND OR LIABILITY FOR ANYX *	DAMAGES OF ANY KIND.X *X *  USG UNIX (3.0) ioctl conventions courtesy Jeff MartinX */Xchar *Copyright = "Copyright 1998 Omen Technology Inc All Rights Reserved";Xchar *substr();X#define LOGFILE "/tmp/cszlog"#define LOGFILE2 "cszlog"#include <stdio.h>#include <signal.h>#include <ctype.h>#include <errno.h>extern int errno;#define STATICX#define PATHLEN 1000#define OK 0#define FALSE 0#ifdef TRUE#undef TRUE#endif#define TRUE 1#define ERROR (-1)/* 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 SYN 026#define CPMEOF 032#define WANTCRC 0103	/* send C not NAK to get crc not checksum */#define WANTG 0107	/* Send G not NAK to get nonstop batch xmsn */#define TIMEOUT (-2)#define RCDO (-3)#define GCOUNT (-4)#define RETRYMAX 10XX#define HOWMANY 2STATIC int Zmodem=0;		/* ZMODEM protocol requested by receiver */unsigned Baudrate = 9600;		/* Default, set by first mode() call */STATIC unsigned Txwindow;	/* Control the size of the transmitted window */STATIC unsigned Txwspac;	/* Spacing between zcrcq requests */STATIC unsigned Txwcnt;	/* Counter used to space ack requests */STATIC long Lrxpos;	/* Receiver's last reported offset */STATIC int errors;char Zsendmask[33];	/* Additional control chars to mask */X#include "rbsb.c"	/* most of the system dependent stuff here */X#include "crctab.c"XSTATIC int Filesleft;STATIC long Totalleft;X/*X * Attention string to be executed by receiver to interrupt streaming dataX *  when an error is detected.  A pause (0336) may be needed before theX *  ^C (03) or after it.X */#ifdef READCHECKSTATIC char Myattn[] = { 0 };#else#ifdef USGSTATIC char Myattn[] = { 03, 0336, 0 };#endif#endifXFILE *in;XSTATIC int Canseek = 1;	/* 1: Can seek 0: only rewind -1: neither (pipe) */X#ifndef SMALL#ifndef TXBSIZE#define TXBSIZE 32768#endifX#define TXBMASK (TXBSIZE-1)STATIC char Txb[TXBSIZE + 1024];	/* Circular buffer for file reads */STATIC char *txbuf = Txb;		/* Pointer to current file segment */#elsechar txbuf[1024];#endifXXSTATIC long vpos = 0;		/* Number of bytes read from file */XSTATIC char Lastrx;STATIC char Crcflg;STATIC int Modem2=0;		/* XMODEM Protocol - don't send pathnames */STATIC int Fullname=0;		/* transmit full pathname */STATIC int Unlinkafter=0;	/* Unlink file after it is sent */STATIC int firstsec;STATIC int errcnt=0;		/* number of files unreadable */STATIC int Skipbitch=0;STATIC int Skipcount=0;		/* Count of skipped files */STATIC int blklen=128;		/* length of transmitted records */STATIC int Optiong;		/* Let it rip no wait for sector ACK's */STATIC int Eofseen;		/* EOF seen on input set by zfilbuf */STATIC int BEofseen;		/* EOF seen on input set by fooseek */STATIC int Totsecs;		/* total number of sectors this file */STATIC int Filcnt=0;		/* count of number of files opened */STATIC unsigned Rxbuflen=16384;	/* Receiver's max buffer length */STATIC long Tframlen = 0;	/* Override for tx frame length */STATIC int blkopt=0;		/* Override value for zmodem blklen */STATIC int Rxflags = 0;STATIC long bytcnt, maxbytcnt;STATIC int Wantfcs32 = TRUE;	/* want to send 32 bit FCS */STATIC char Lzconv;	/* Local ZMODEM file conversion request */STATIC char Lzmanag;	/* Local ZMODEM file management request */STATIC int Lskipnocor;STATIC char Lztrans;STATIC int Command;		/* Send a command, then exit. */STATIC char *Cmdstr;		/* Pointer to the command string */STATIC int Cmdack1;		/* Rx ACKs command, then do it */STATIC int Exitcode;STATIC long Lastsync;	/* Last offset to which we got a ZRPOS */STATIC int Beenhereb4;		/* How many times we've been ZRPOS'd here */XSTATIC jmp_buf intrjmp;	/* For the interrupt on RX CAN */XX/* send cancel string to get the other end to shut up */canit(){X	static char canistr[] = {X	 24,24,24,24,24,24,24,24,24,24,8,8,8,8,8,8,8,8,8,8,0X	};XX	zmputs(canistr);X	Lleft=0;	/* Do read next time ... */}XX/* called by signal interrupt or terminate to clean things up */voidbibi(n){X	canit(); fflush(stdout); mode(0);X	fprintf(stderr, "csz: caught signal %d; exiting\n", n);X	if (n == SIGQUIT)X		abort();X	if (n == 99)X		fprintf(stderr, "mode(2) in rbsb.c not implemented!!\n");X	exit(3);}X/* Called when ZMODEM gets an interrupt (^X) */voidonintr(c){X	signal(SIGINT, SIG_IGN);X	longjmp(intrjmp, -1);}XSTATIC int Zctlesc;	/* Encode control characters */STATIC int Nozmodem = 0;	/* If invoked as "sb" */STATIC char *Progname = "csz";STATIC int Zrwindow = 1400;	/* RX window size (controls garbage count) */XX/*X * Log an errorX */voidzperr1(s,p,u)char *s, *p, *u;{X	fprintf(stderr, "Retry %d: ", errors);X	fprintf(stderr, s);X	fprintf(stderr, "\n");}Xvoidzperr2(s,p,u)char *s, *p, *u;{X	fprintf(stderr, "Retry %d: ", errors);X	fprintf(stderr, s, p);X	fprintf(stderr, "\n");}Xvoidzperr3(s,p,u)char *s, *p, *u;{X	fprintf(stderr, "Retry %d: ", errors);X	fprintf(stderr, s, p, u);X	fprintf(stderr, "\n");}XX#include "zm.c"#include "zmr.c"Xmain(argc, argv)char *argv[];{X	register char *cp;X	register npats;X	char **patts;XX	if ((cp = getenv("ZNULLS")) && *cp)X		Znulls = atoi(cp);X	Tty = 1;  Ttystream = stdout;X	chkinvok(argv[0]);XX	Rxtimeout = 600;X	npats=0;X	if (argc<2)X		usage();X	while (--argc) {X		cp = *++argv;X		if (*cp++ == '-' && *cp) {X			while ( *cp) {X				if (isdigit(*cp)) {X					++cp;  continue;X				}X				switch(*cp++) {X				case '+':X					Lzmanag = ZMAPND; break;X				case 'a':X					if (Nozmodem || Modem2)X						usage();X					Lzconv = ZCNL;  break;X				case 'b':X					Lzconv = ZCBIN; break;X				case 'c':X					Lzmanag = ZMCHNG;  break;X				case 'f':X					Fullname=TRUE; break;X				case 'e':X					Zctlesc = 1; break;X				case 'k':X					blklen=1024; break;X				case 'L':X					if (isdigit(*cp))X						blkopt = atoi(cp);X					else {X						if (--argc < 1)X							usage();X						blkopt = atoi(*++argv);X					}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -