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

📄 sz.c

📁 DOS下采用中断接收数据的串口通讯的例子,很难找到的好东西!
💻 C
📖 第 1 页 / 共 3 页
字号:
	q = (char *) 0;	if (Dottoslash) {		/* change . to . */		for (p=name; *p; ++p) {			if (*p == '/')				q = p;			else if (*p == '.')				*(q=p) = '/';		}		if (q && strlen(++q) > 8) {	/* If name>8 chars */			q += 8;			/*   make it .ext */			strcpy(name2, q);	/* save excess of name */			*q = '.';			strcpy(++q, name2);	/* add it back */		}	}	for (p=name, q=txbuf ; *p; )		if ((*q++ = *p++) == '/' && !Fullname)			q = txbuf;	*q++ = 0;	p=q;	while (q < (txbuf + 1024))		*q++ = 0;	if (!Ascii && (in!=stdin) && *name && fstat(fileno(in), &f)!= -1)		sprintf(p, "%lu %lo %o 0 %d %ld", f.st_size, f.st_mtime,		  f.st_mode, Filesleft, Totalleft);	Totalleft -= f.st_size;	if (--Filesleft <= 0)		Totalleft = 0;	if (Totalleft < 0)		Totalleft = 0;	/* force 1k blocks if name won't fit in 128 byte block */	if (txbuf[125])		blklen=1024;	else {		/* A little goodie for IMP/KMD */		txbuf[127] = (f.st_size + 127) >>7;		txbuf[126] = (f.st_size + 127) >>15;	}	if (Zmodem)		return zsendfile(txbuf, 1+strlen(p)+(p-txbuf));	if (wcputsec(txbuf, 0, 128)==ERROR)		return ERROR;	return OK;}getnak(){	register firstch;	Lastrx = 0;	for (;;) {		switch (firstch = readock(800,1)) {		case ZPAD:			if (getzrxinit())				return ERROR;			Ascii = 0;	/* Receiver does the conversion */			return FALSE;		case TIMEOUT:			zperr("Timeout on pathname");			return TRUE;		case WANTG:#ifdef MODE2OK			mode(2);	/* Set cbreak, XON/XOFF, etc. */#endif			Optiong = TRUE;			blklen=1024;		case WANTCRC:			Crcflg = TRUE;		case NAK:			return FALSE;		case CAN:			if ((firstch = readock(20,1)) == CAN && Lastrx == CAN)				return TRUE;		default:			break;		}		Lastrx = firstch;	}}wctx(flen)long flen;{	register int thisblklen;	register int sectnum, attempts, firstch;	long charssent;	charssent = 0;  firstsec=TRUE;  thisblklen = blklen;	vfile("wctx:file length=%ld", flen);	while ((firstch=readock(Rxtimeout, 2))!=NAK && firstch != WANTCRC	  && firstch != WANTG && firstch!=TIMEOUT && firstch!=CAN)		;	if (firstch==CAN) {		zperr("Receiver CANcelled");		return ERROR;	}	if (firstch==WANTCRC)		Crcflg=TRUE;	if (firstch==WANTG)		Crcflg=TRUE;	sectnum=0;	for (;;) {		if (flen <= (charssent + 896L))			thisblklen = 128;		if ( !filbuf(txbuf, thisblklen))			break;		if (wcputsec(txbuf, ++sectnum, thisblklen)==ERROR)			return ERROR;		charssent += thisblklen;	}	fclose(in);	attempts=0;	do {		purgeline();		sendline(EOT);		fflush(stdout);		++attempts;	}		while ((firstch=(readock(Rxtimeout, 1)) != ACK) && attempts < RETRYMAX);	if (attempts == RETRYMAX) {		zperr("No ACK on EOT");		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>2)		fprintf(stderr, "Sector %3d %2dk\n", Totsecs, Totsecs/8 );	else if (Verbose>1)		fprintf(stderr, "\rSector %3d %2dk ", Totsecs, Totsecs/8 );	for (attempts=0; attempts <= RETRYMAX; attempts++) {		Lastrx= firstch;		sendline(cseclen==1024?STX:SOH);		sendline(sectnum);		sendline(-sectnum -1);		oldcrc=checksum=0;		for (wcj=cseclen,cp=buf; --wcj>=0; ) {			sendline(*cp);			oldcrc=updcrc((0377& *cp), oldcrc);			checksum += *cp++;		}		if (Crcflg) {			oldcrc=updcrc(0,updcrc(0,oldcrc));			sendline((int)oldcrc>>8);			sendline((int)oldcrc);		}		else			sendline(checksum);		if (Optiong) {			firstsec = FALSE; return OK;		}		firstch = readock(Rxtimeout, (Noeofseen&&sectnum) ? 2:1);gotnak:		switch (firstch) {		case CAN:			if(Lastrx == CAN) {cancan:				zperr("Cancelled");  return ERROR;			}			break;		case TIMEOUT:			zperr("Timeout on sector ACK"); continue;		case WANTCRC:			if (firstsec)				Crcflg = TRUE;		case NAK:			zperr("NAK on sector"); continue;		case ACK: 			firstsec=FALSE;			Totsecs += (cseclen>>7);			return OK;		case ERROR:			zperr("Got burst for sector ACK"); break;		default:			zperr("Got %02x for sector ACK", firstch); break;		}		for (;;) {			Lastrx = firstch;			if ((firstch = readock(Rxtimeout, 2)) == TIMEOUT)				break;			if (firstch == NAK || firstch == WANTCRC)				goto gotnak;			if (firstch == CAN && Lastrx == CAN)				goto cancan;		}	}	zperr("Retry Count Exceeded");	return ERROR;}/* fill buf with count chars padding with ^Z for CPM */filbuf(buf, count)register char *buf;{	register c, m;	if ( !Ascii) {		m = read(fileno(in), buf, count);		if (m <= 0)			return 0;		while (m < count)			buf[m++] = 032;		return count;	}	m=count;	if (Lfseen) {		*buf++ = 012; --m; Lfseen = 0;	}	while ((c=getc(in))!=EOF) {		if (c == 012) {			*buf++ = 015;			if (--m == 0) {				Lfseen = TRUE; break;			}		}		*buf++ =c;		if (--m == 0)			break;	}	if (m==count)		return 0;	else		while (--m>=0)			*buf++ = CPMEOF;	return count;}/* fill buf with count chars */zfilbuf(buf, count)register char *buf;{	register c, m;	m=count;	while ((c=getc(in))!=EOF) {		*buf++ =c;		if (--m == 0)			break;	}	return (count - m);}/* VARARGS1 */vfile(f, a, b, c)register char *f;{	if (Verbose > 2) {		fprintf(stderr, f, a, b, c);		fprintf(stderr, "\n");	}}alrm(){	longjmp(tohere, -1);}/* * readock(timeout, count) reads character(s) from file descriptor 0 *  (1 <= count <= 3) * it attempts to read count characters. If it gets more than one, * it is an error unless all are CAN * (otherwise, only normal response is ACK, CAN, or C) *  Only looks for one if Optiong, which signifies cbreak, not raw input * * timeout is in tenths of seconds */readock(timeout, count){	register int c;	static char byt[5];	if (Optiong)		count = 1;	/* Special hack for cbreak */	fflush(stdout);	if (setjmp(tohere)) {		zperr("TIMEOUT");		return TIMEOUT;	}	c = timeout/10;	if (c<2)		c=2;	if (Verbose>5) {		fprintf(stderr, "Timeout=%d Calling alarm(%d) ", timeout, c);		byt[1] = 0;	}	signal(SIGALRM, alrm); alarm(c);#ifdef ONEREAD	c=read(iofd, byt, 1);		/* regulus raw read is unique */#else	c=read(iofd, byt, count);#endif	alarm(0);	if (Verbose>5)		fprintf(stderr, "ret cnt=%d %x %x\n", c, byt[0], byt[1]);	if (c<1)		return TIMEOUT;	if (c==1)		return (byt[0]&0377);	else		while (c)			if (byt[--c] != CAN)				return ERROR;	return CAN;}readline(n){	return (readock(n, 1));}purgeline(){#ifdef USG	ioctl(iofd, TCFLSH, 0);#else	lseek(iofd, 0L, 2);#endif}/* send cancel string to get the other end to shut up */canit(){	static char canistr[] = {	 24,24,24,24,24,24,24,24,24,24,8,8,8,8,8,8,8,8,8,8,0	};	printf(canistr);	fflush(stdout);}/* * Log an error *//*VARARGS1*/zperr(s,p,u)char *s, *p, *u;{	if (Verbose <= 0)		return;	fprintf(stderr, "Retry %d: ", errors);	fprintf(stderr, s, p, u);	fprintf(stderr, "\n");}/* * 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;}char *babble[] = {	"Send file(s) with ZMODEM/YMODEM/XMODEM Protocol",	"	(Y) = Option applies to YMODEM only",	"	(Z) = Option applies to ZMODEM only",	"Usage:	sz [-12+abdefkLlNnquvwYy] [-] file ...",	"	sz [-12Ceqv] -c COMMAND",	"	sb [-12adfkquv] [-] file ...",	"	sx [-12akquv] [-] file",	"	1 Use stdout for modem input",#ifdef CSTOPB	"	2 Use 2 stop bits",#endif	"	+ Append to existing destination file (Z)",	"	a (ASCII) change NL to CR/LF",	"	b Binary file transfer override",	"	c send COMMAND (Z)",	"	d Change '.' to '/' in pathnames (Y/Z)",	"	e Escape all control characters (Z)",	"	f send Full pathname (Y/Z)",	"	i send COMMAND, ack Immediately (Z)",	"	k Send 1024 byte packets (Y)",	"	L N Limit subpacket length to N bytes (Z)",	"	l N Limit frame length to N bytes (l>=L) (Z)",	"	n send file if source newer (Z)",	"	N send file if source newer or longer (Z)",	"	o Use 16 bit CRC instead of 32 bit CRC (Z)",	"	p Protect existing destination file (Z)",	"	r Resume/Recover interrupted file transfer (Z)",	"	q Quiet (no progress reports)",	"	u Unlink file after transmission",	"	v Verbose - provide debugging information",	"	w N Window is N bytes (Z)",	"	Y Yes, overwrite existing file, skip if not present at rx (Z)",	"	y Yes, overwrite existing file (Z)",	"- as pathname sends standard input as sPID.sz or environment ONAME",	""};usage(){	char **pp;	for (pp=babble; **pp; ++pp)		fprintf(stderr, "%s\n", *pp);	fprintf(stderr, "%s for %s by Chuck Forsberg, Omen Technology INC\n",	 VERSION, OS);	fprintf(stderr, "\t\t\042The High Reliability Software\042\n");	cucheck();	exit(1);}/* * Get the receiver's init parameters */getzrxinit(){	register n;	struct stat f;	for (n=10; --n>=0; ) {				switch (zgethdr(Rxhdr, 1)) {		case ZCHALLENGE:	/* Echo receiver's challenge numbr */			stohdr(Rxpos);			zshhdr(ZACK, Txhdr);			continue;		case ZCOMMAND:		/* They didn't see out ZRQINIT */			stohdr(0L);			zshhdr(ZRQINIT, Txhdr);			continue;		case ZRINIT:			Rxflags = 0377 & Rxhdr[ZF0];			Txfcs32 = (Wantfcs32 && (Rxflags & CANFC32));			Zctlesc |= Rxflags & TESCCTL;			Rxbuflen = (0377 & Rxhdr[ZP0])+((0377 & Rxhdr[ZP1])<<8);			if ( !(Rxflags & CANFDX))				Txwindow = 0;			vfile("Rxbuflen=%d Tframlen=%d", Rxbuflen, Tframlen);			if ( !Fromcu)				signal(SIGINT, SIG_IGN);#ifdef MODE2OK			mode(2);	/* Set cbreak, XON/XOFF, etc. */#endif#ifndef READCHECK#ifndef USG			/* Use 1024 byte frames if no sample/interrupt */			if (Rxbuflen < 32 || Rxbuflen > 1024) {				Rxbuflen = 1024;				vfile("Rxbuflen=%d", Rxbuflen);			}#endif#endif			/* Override to force shorter frame length */			if (Rxbuflen && (Rxbuflen>Tframlen) && (Tframlen>=32))				Rxbuflen = Tframlen;			if ( !Rxbuflen && (Tframlen>=32) && (Tframlen<=1024))				Rxbuflen = Tframlen;			vfile("Rxbuflen=%d", Rxbuflen);			/* If using a pipe for testing set lower buf len */			fstat(iofd, &f);			if ((f.st_mode & S_IFMT) != S_IFCHR			  && (Rxbuflen == 0 || Rxbuflen > 4096))				Rxbuflen = 4096;			/*			 * If input is not a regular file, force ACK's each 1024			 *  (A smarter strategey could be used here ...)			 */			if ( !Command) {				fstat(fileno(in), &f);				if (((f.st_mode & S_IFMT) != S_IFREG)				  && (Rxbuflen == 0 || Rxbuflen > 1024))					Rxbuflen = 1024;			}			if (Baudrate > 300)	/* Set initial subpacket len */				blklen = 256;			if (Baudrate > 1200)				blklen = 512;			if (Baudrate > 2400)				blklen = 1024;			if (Rxbuflen && blklen>Rxbuflen)				blklen = Rxbuflen;			if (blkopt && blklen > blkopt)				blklen = blkopt;			vfile("Rxbuflen=%d blklen=%d", Rxbuflen, blklen);			vfile("Txwindow = %u Txwspac = %d", Txwindow, Txwspac);

⌨️ 快捷键说明

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