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

📄 sz.c

📁 开源串口利用Xmodem,Ymodem,ZModem 下载,上传的程序.在Linux,Arm-Linux 都可使用
💻 C
📖 第 1 页 / 共 3 页
字号:
/* Send send-init information */sendzsinit(){	register c;	if (Myattn[0] == '\0' && (!Zctlesc || (Rxflags & TESCCTL)))		return OK;	errors = 0;	for (;;) {		stohdr(0L);#ifdef ALTCANOFF		Txhdr[ALTCOFF] = ALTCANOFF;#endif		if (Zctlesc) {			Txhdr[ZF0] |= TESCCTL; zshhdr(4, ZSINIT, Txhdr);		}		else			zsbhdr(4, ZSINIT, Txhdr);		zsdata(Myattn, ZATTNLEN, ZCRCW);		c = zgethdr(Rxhdr);		switch (c) {		case ZCAN:			return ERROR;		case ZACK:			return OK;		default:			if (++errors > 19)				return ERROR;			continue;		}	}}/* Send file name and related info */zsendfile(buf, blen)char *buf;{	register c;	register unsigned long crc;	int m, n, i;	char *p;	long lastcrcrq = -1;	long lastcrcof = -1;	long l;	for (errors=0; ++errors<11;) {		Txhdr[ZF0] = Lzconv;	/* file conversion request */		Txhdr[ZF1] = Lzmanag;	/* file management request */		if (Lskipnocor)			Txhdr[ZF1] |= ZMSKNOLOC;		Txhdr[ZF2] = Lztrans;	/* file transport request */		Txhdr[ZF3] = 0;		zsbhdr(4, ZFILE, Txhdr);		zsdata(buf, blen, ZCRCW);again:		c = zgethdr(Rxhdr);		switch (c) {		case ZRINIT:			while ((c = readline(50)) > 0)				if (c == ZPAD) {					goto again;				}			continue;		case ZCAN:		case TIMEOUT:		case ZABORT:		case ZFIN:			sprintf(endmsg, "Got %s on pathname", frametypes[c+FTOFFSET]);			return ERROR;		default:			sprintf(endmsg, "Got %d frame type on pathname", c);			continue;		case ERROR:		case ZNAK:			continue;		case ZCRC:			l = Rxhdr[9] & 0377;			l = (l<<8) + (Rxhdr[8] & 0377);			l = (l<<8) + (Rxhdr[7] & 0377);			l = (l<<8) + (Rxhdr[6] & 0377);			if (Rxpos != lastcrcrq || l != lastcrcof) {				lastcrcrq = Rxpos;				crc = 0xFFFFFFFFL;				if (Canseek >= 0) {					fseek(in, bytcnt = l, 0);  i = 0;					vfile("CRC32 on %ld bytes", Rxpos);					do {						/* No rx timeouts! */						if (--i < 0) {							i = 32768L/blklen;							sendline(SYN);							flushmoc();						}						bytcnt += m = n = zfilbuf();						if (bytcnt > maxbytcnt)							maxbytcnt = bytcnt;						for (p = txbuf; --m >= 0; ++p) {							c = *p & 0377;							crc = UPDC32(c, crc);						}#ifdef DEBUG						vfile("bytcnt=%ld crc=%08lX",						  bytcnt, crc);#endif					} while (n && bytcnt < lastcrcrq);					crc = ~crc;#ifndef MMIO					clearerr(in);	/* Clear possible EOF */#endif				}			}			stohdr(crc);			zsbhdr(4, ZCRC, Txhdr);			goto again;		case ZFERR:		case ZSKIP:			++Skipcount;			if (Skipbitch)				++errcnt;			fclose(in); return c;		case ZRPOS:			/*			 * Suppress zcrcw request otherwise triggered by			 * lastyunc==bytcnt			 */			if (fseek(in, Rxpos, 0))				return ERROR;			Lastsync = (maxbytcnt = bytcnt = Txpos = Lrxpos = Rxpos) -1;			return zsendfdata();		}	}	fclose(in); return ERROR;}/* Send the data in the file */zsendfdata(){	register c, e, n;	register newcnt;	register long tcount = 0;	int junkcount;		/* Counts garbage chars received by TX */	static int tleft = 6;	/* Counter for test mode */	junkcount = 0;	Beenhereb4 = 0;somemore:	if (setjmp(intrjmp)) {waitack:		junkcount = 0;		c = getinsync(0);gotack:		switch (c) {		default:		case ZCAN:			fclose(in);			return ERROR;		case ZRINIT:			fclose(in);			return ZSKIP;		case ZSKIP:			++Skipcount;			if (Skipbitch)				++errcnt;			fclose(in);			return c;		case ZACK:		case ZRPOS:			break;		}#ifdef READCHECK		/*		 * If the reverse channel can be tested for data,		 *  this logic may be used to detect error packets		 *  sent by the receiver, in place of setjmp/longjmp		 *  rdchk(Tty) returns non 0 if a character is available		 */		while (rdchk(Tty)) {#ifdef EATSIT			switch (checked)#else			switch (readline(1))#endif			{			case CAN:			case ZPAD:				c = getinsync(1);				goto gotack;			case XOFF:		/* Wait a while for an XON */				readline(100);			}		}#endif	}	signal(SIGINT, onintr);	newcnt = Rxbuflen;	Txwcnt = 0;	stohdr(Txpos);	zsbhdr(4, ZDATA, Txhdr);	/*	 * Special testing mode.  This should force receiver to Attn,ZRPOS	 *  many times.  Each time the signal should be caught, causing the	 *  file to be started over from the beginning.	 */	if (Test) {		if ( --tleft)			while (tcount < 20000) {				printf(qbf); fflush(stdout);				tcount += strlen(qbf);#ifdef READCHECK				while (rdchk(Tty)) {#ifdef EATSIT					switch (checked)#else					switch (readline(1))#endif					{					case CAN:					case ZPAD:						goto waitack;					case XOFF:	/* Wait for XON */						readline(100);					}				}#endif			}		signal(SIGINT, SIG_IGN); canit();		sleep(20); purgeline(); mode(0);		printf("\nsz: Tcount = %ld\n", tcount);		if (tleft) {			printf("ERROR: Interrupts Not Caught\n");			exit(1);		}		exit(0);	}	do {		n = zfilbuf();		if (Eofseen)			e = ZCRCE;		else if (junkcount > 3)			e = ZCRCW;		else if (bytcnt == Lastsync)			e = ZCRCW;		else if (Rxbuflen && (newcnt -= n) <= 0)			e = ZCRCW;		else if (Txwindow && (Txwcnt += n) >= Txwspac) {			Txwcnt = 0;  e = ZCRCQ;		} else			e = ZCRCG;		if (Verbose>1)			fprintf(stderr, "%7ld ZMODEM%s\n",			  Txpos, Crc32t?" CRC-32":"");		zsdata(txbuf, n, e);		bytcnt = Txpos += n;		if (bytcnt > maxbytcnt)			maxbytcnt = bytcnt;		if (e == ZCRCW)			goto waitack;#ifdef READCHECK		/*		 * If the reverse channel can be tested for data,		 *  this logic may be used to detect error packets		 *  sent by the receiver, in place of setjmp/longjmp		 *  rdchk(Tty) returns non 0 if a character is available		 */		fflush(stdout);		while (rdchk(Tty)) {#ifdef EATSIT			switch (checked)#else			switch (readline(1))#endif			{			case CAN:			case ZPAD:				c = getinsync(1);				if (c == ZACK)					break;				/* zcrce - dinna wanna starta ping-pong game */				zsdata(txbuf, 0, ZCRCE);				goto gotack;			case XOFF:		/* Wait a while for an XON */				readline(100);			default:				++junkcount;			}		}#endif	/* READCHECK */		if (Txwindow) {			while ((tcount = (Txpos - Lrxpos)) >= Txwindow) {				vfile("%ld window >= %u", tcount, Txwindow);				if (e != ZCRCQ)					zsdata(txbuf, 0, e = ZCRCQ);				c = getinsync(1);				if (c != ZACK) {					zsdata(txbuf, 0, ZCRCE);					goto gotack;				}			}			vfile("window = %ld", tcount);		}	} while (!Eofseen);	signal(SIGINT, SIG_IGN);	for (;;) {		stohdr(Txpos);		zsbhdr(4, ZEOF, Txhdr);egotack:		switch (getinsync(0)) {		case ZACK:			goto egotack;		case ZNAK:			continue;		case ZRPOS:			goto somemore;		case ZRINIT:			fclose(in);			return OK;		case ZSKIP:			++Skipcount;			if (Skipbitch)				++errcnt;			fclose(in);			return c;		default:			sprintf(endmsg, "Got %d trying to send end of file", c);		case ERROR:			fclose(in);			return ERROR;		}	}}/* * Respond to receiver's complaint, get back in sync with receiver */getinsync(flag){	register c;	for (;;) {		if (Test) {			printf("\r\n\n\n***** Signal Caught *****\r\n");			Rxpos = 0; c = ZRPOS;		} else			c = zgethdr(Rxhdr);		switch (c) {		case ZCAN:		case ZABORT:		case ZFIN:		case TIMEOUT:			sprintf(endmsg, "Got %s sending data", frametypes[c+FTOFFSET]);			return ERROR;		case ZRPOS:			if (Rxpos > bytcnt) {				vfile("getinsync: Rxpos=%lx bytcnt=%lx Maxbytcnt=%lx",				  Rxpos, bytcnt, maxbytcnt);				if (Rxpos > maxbytcnt)					sprintf(endmsg,					  "Nonstandard Protocol at %lX", Rxpos);				return ZRPOS;			}			/* ************************************* */			/*  If sending to a buffered modem, you  */			/*   might send a break at this point to */			/*   dump the modem's buffer.		 */			clearerr(in);	/* In case file EOF seen */			if (fseek(in, Rxpos, 0)) {				sprintf(endmsg, "Bad Seek to %ld", Rxpos);				return ERROR;			}			Eofseen = 0;			bytcnt = Lrxpos = Txpos = Rxpos;			if (Lastsync == Rxpos) {				if (++Beenhereb4 > 12) {					sprintf(endmsg, "Can't send block");					return ERROR;				}				if (Beenhereb4 > 4)					if (blklen > 32)						blklen /= 2;			} else				Beenhereb4 = 0;			Lastsync = Rxpos;			return c;		case ZACK:			Lrxpos = Rxpos;			if (flag || Txpos == Rxpos)				return ZACK;			continue;		case ZRINIT:			return c;		case ZSKIP:			++Skipcount;			if (Skipbitch)				++errcnt;			return c;		case ERROR:		default:			zsbhdr(4, ZNAK, Txhdr);			continue;		}	}}/* Send command and related info */zsendcmd(buf, blen)char *buf;{	register c;	long cmdnum;	cmdnum = getpid();	errors = 0;	for (;;) {		stohdr(cmdnum);		Txhdr[ZF0] = Cmdack1;		zsbhdr(4, ZCOMMAND, Txhdr);		zsdata(buf, blen, ZCRCW);listen:		Rxtimeout = 100;		/* Ten second wait for resp. */		Usevhdrs = 0;		/* Allow rx to send fixed len headers */		c = zgethdr(Rxhdr);		switch (c) {		case ZRINIT:			goto listen;	/* CAF 8-21-87 */		case ERROR:		case GCOUNT:		case TIMEOUT:			if (++errors > 11)				return ERROR;			continue;		case ZCAN:		case ZABORT:		case ZFIN:		case ZSKIP:		case ZRPOS:			return ERROR;		default:			if (++errors > 20)				return ERROR;			continue;		case ZCOMPL:			Exitcode = Rxpos;			saybibi();			return OK;		case ZRQINIT:			vfile("******** RZ *******");			system("rz");			vfile("******** SZ *******");			goto listen;		}	}}/* * If called as sb use YMODEM protocol */chkinvok(s)char *s;{	register char *p;	p = s;	while (*p == '-')		s = ++p;	while (*p)		if (*p++ == '/')			s = p;	if (*s == 'v') {		Verbose=1; ++s;	}	Progname = s;	if (s[0]=='z' && s[1] == 'c') {		Command = TRUE;		if (s[8] == 'i')			Cmdack1 = ZCACK1;	}	if (s[0]=='s' && s[1]=='b') {		Nozmodem = TRUE; blklen=1024;	}	if (s[0]=='s' && s[1]=='x') {		Modem2 = TRUE;	}}countem(argc, argv)register char **argv;{	struct stat f;	for (Totalleft = 0, Filesleft = 0; --argc >=0; ++argv) {		f.st_size = -1;		if (Verbose>2) {			fprintf(stderr, "\nCountem: %03d %s ", argc, *argv);			fflush(stderr);		}		if (access(*argv, 04) >= 0 && stat(*argv, &f) >= 0) {			++Filesleft;  Totalleft += f.st_size;		}		if (Verbose>2)			fprintf(stderr, " %ld", f.st_size);	}	if (Verbose>2)		fprintf(stderr, "\ncountem: Total %d %ld\n",		  Filesleft, Totalleft);}chartest(m){	register n;	mode(m);	printf("\r\n\nCharacter Transparency Test Mode %d\r\n", m);	printf("If Pro-YAM/ZCOMM is not displaying ^M hit ALT-V NOW.\r\n");	printf("Hit Enter.\021");  fflush(stdout);	readline(500);	for (n = 0; n < 256; ++n) {		if (!(n%8))			printf("\r\n");		printf("%02x ", n);  fflush(stdout);		sendline(n);	flushmo();		printf("  ");  fflush(stdout);		if (n == 127) {			printf("Hit Enter.\021");  fflush(stdout);			readline(500);			printf("\r\n");  fflush(stdout);		}	}	printf("\021\r\nEnter Characters, echo is in hex.\r\n");	printf("Hit SPACE or pause 40 seconds for exit.\r\n");	while (n != TIMEOUT && n != ' ') {		n = readline(400);		printf("%02x\r\n", n);		fflush(stdout);	}	printf("\r\nMode %d character transparency test ends.\r\n", m);	fflush(stdout);}/* * Set additional control chars to mask in Zsendmask * according to bit array stored in char array at p */initzsendmsk(p)register char *p;{	register c;	for (c = 0; c < 33; ++c) {		if (p[c>>3] & (1 << (c & 7))) {			Zsendmask[c] = 1;			vfile("Escaping %02o", c);		}	}}/* End of sz.c */

⌨️ 快捷键说明

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