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

📄 gkermit.c

📁 使用Kermit协议传输文件的程序
💻 C
📖 第 1 页 / 共 3 页
字号:
        if (size == maxlen) return(size); /* If just at end, done. */        if (size > maxlen) {		/* Past end, must save some. */            for (i = 0; (remain[i] = xdata[osize+i]) != NUL; i++) ;            size = osize;            xdata[size] = NUL;            return(size);		/* Return size. */        }    }    return(size);			/* EOF, return size. */}intencstr(s) char *s; { 			/* Fill a packet from string s. */    first = 1;				/* Start lookahead. */    isp = s;				/* Set input string pointer */    getpkt(spsiz);			/* Fill a packet */    isp = NULL;				/* Reset input string pointer */    return(size);			/* Return data field length */}VOIDdecstr(s) char *s; {			/* Decode packet data into a string */    osp = s;				/* Set output string pointer  */    decode(datalen);			/* Decode the string */    *osp = NUL;				/* Terminate with null */    osp = NULL;				/* Reset output string pointer */}VOIDencode(a,next) int a, next; {		/* Encode character a into packet */    int a7, b8;    if (rptflg) {			/* Doing run-length encoding? */	if (a == next) {		/* Yes, got a run? */	    if (++rpt < 94) {		/* Yes, count. */		return;	    } else if (rpt == 94) {	/* If at maximum */	    	xdata[size++] = rptq;	/* Emit prefix, */		xdata[size++] = tochar(rpt);	/* and count, */		rpt = 0;			/* and reset counter. */	    }	} else if (rpt == 1) {		/* Run broken, only two? */	    rpt = 0;			/* Yes, do the character twice */	    encode(a,-1);		/* by calling self recursively. */	    if (size <= maxsiz) osize = size; /* Watch out for boundary. */	    rpt = 0;			/* Call self second time. */	    encode(a,-1);	    return;	} else if (rpt > 1) {		/* Run broken, more than two? */	    xdata[size++] = rptq;	/* Yes, emit prefix and count */	    xdata[size++] = tochar(++rpt);	    rpt = 0;			/* and reset counter. */	}    }    a7 = a & 127;			/* Get low 7 bits of character */    b8 = a & 128;			/* And "parity" bit */    if (ebqflg && b8) {			/* If doing 8th bit prefixing */	xdata[size++] = ebq;		/* and 8th bit on, insert prefix */	a = a7;				/* and clear the 8th bit. */    }    if (a7 < 32 || a7 == 127) {		/* If in control range */        xdata[size++] = sctlq;		/* insert control prefix */        a = ctl(a);			/* and make character printable. */    } else if (a7 == sctlq)		/* If data is control prefix, */      xdata[size++] = sctlq;		/* prefix it. */    else if (ebqflg && a7 == ebq)	/* If doing 8th-bit prefixing, */      xdata[size++] = sctlq;		/* ditto for 8th-bit prefix. */    else if (rptflg && a7 == rptq)	/* If doing run-length encoding, */      xdata[size++] = sctlq;		/* ditto for repeat prefix. */    xdata[size++] = a;			/* Finally, emit the character. */    xdata[size] = NUL;			/* Terminate string with null. */}intdecode(len) int len; {			/* Decode packet data field */    unsigned int a, a7, b8;		/* Local variables */    rpt = 0;				/* Initialize repeat count. */    while (len-- > 0) {	a = *rdatap++;			/* For each character, a, do... */	if (rptflg) {			/* Repeat processing? */	    if (a == rptq) {		/* Yes, have repeat prefix? */		if (len-- < 1) {		    if (debug) fprintf(db,"decode rpt error A\n");		    return(-1);		}		rpt = xunchar(*rdatap++); /* Yes, get count */		if (len-- < 1) {		    if (debug) fprintf(db,"decode rpt error B\n");		    return(-1);		}		a = *rdatap++;		/* and the following character. */	    }	}	b8 = 0;				/* Assume 8th bit not on. */	if (ebqflg) {			/* Doing 8th-bit prefixing? */	    if (a == ebq) {		/* Yes, have 8th-bit prefix? */		b8 = 128;		/* Yes, remember bit 8 on */		if (len-- < 1) {		    if (debug) fprintf(db,"decode ebq error\n");		    return(-1);		}		a = *rdatap++;		/* and get following character. */	    }	}        if (a == rctlq) {		/* Control quote? */	    if (len-- < 1) {		if (debug) fprintf(db,"decode ctl error\n");		return(-1);	    }            a = *rdatap++;		/* Yes, get next character */            a7 = a & 127;		/* and its low 7 bits */            if (a7 > 62 && a7 < 96)	/* Encoded control character? */                a = ctl(a);		/* Yes, controllify. */        }        if (rpt == 0)			/* Rationalize the repeat count. */	  rpt = 1;	a |= b8;			/* OR in the 8th bit. */	a &= 0xff;			/* Undo any sign extension. */	/* The following is UNIX-specific but so is this program */	if (text && a == CR)		/* Don't output carriage returns. */	  continue;	for (; rpt > 0; rpt--) {	/* Output the character. */	    if (osp) {			/* As many copies as indicated by */		*osp++ = a;		/* the repeat count. */	    } else {		putc((char)a,ofp);	/* Use putc macro here to avoid */		if (ferror(ofp)) {	/* function-call overhead. */		    if (debug)		      fprintf(db,"decode putc a=%u errno=%u\n",a,errno);		    failure = 1;		    return(-1);		}	    }	}    }    return(0);				/* Return successfully when done. */}VOIDspar(s) char *s; {			/* Set protocol parameters. */    int k, x;    s--;				/* Line up with field numbers. */    x = (rln >= 1) ? xunchar(s[1]) : 80;/* Limit on size of inbound packets */    spsiz = (x < 10) ? 80 : x;    x = (rln >= 2) ? xunchar(s[2]) : 5;	/* Timeout on inbound packets */    if (timint > 0)			/* "-b 0" overrides */      timint = (x < 0) ? 5 : x;    spadn = 0; spadc = NUL;		/* Outbound Padding */    if (rln >= 3) {	spadn = xunchar(s[3]);	if (rln >= 4) spadc = ctl(s[4]); else spadc = 0;    }    seol = (rln >= 5) ? xunchar(s[5]) : '\r'; /* Outbound Packet Terminator */    if ((seol < 2) || (seol > 037)) seol = '\r';    x = (rln >= 6) ? s[6] : '#';	/* Control prefix */    rctlq = ((x > 32 && x < 63) || (x > 95 && x < 127)) ? x : '#';    rq = (rln >= 7) ? s[7] : 0; 	/* 8th-bit prefix */    if (rq == 'Y') rqf = 1;    else if ((rq > 32 && rq < 63) || (rq > 95 && rq < 127)) rqf = 2;    else rqf = 0;    switch (rqf) {	case 0: ebqflg = 0; break;	case 1: if (parity) { ebqflg = 1; ebq = '&'; } break;	case 2: if ((ebqflg = (ebq == sq || sq == 'Y'))) ebq = rq;    }    x = 1; 				/* Block check */    if (rln >= 8) {	x = s[8] - '0';	if ((x < 1) || (x > 3)) x = 1;    }    if (bctr > 1 && bctr != x) x = 1;    bctr = x;    if (rln >= 9) { 			/* Repeat prefix */	rptq = s[9];	rptflg = ((rptq > 32 && rptq < 63) || (rptq > 95 && rptq < 127));    } else      rptflg = 0;    k = rln;    if (rln >= 10) {			/* Capas */	x = xunchar(s[10]);	if (x & 8) attributes = 1;	if (x & 2) longpackets = 1;	for (k = 10; (rln >= k) && (xunchar(s[k]) & 1); k++)	  ;	if (debug) fprintf(db,"spar attributes=%d longpackets=%d\n",			   attributes, longpackets);    }    if (rln > k+1) {			/* Long packets */	if (longpackets) {	    x = xunchar(s[k+2]) * 95 + xunchar(s[k+3]);	    spsiz = (x > MAXSP) ? MAXSP : x;	    if (spsiz < 10) spsiz = 80;	}    }#ifndef NOSTREAMING    if (rln > k+7) {	x = xunchar(s[k+8]);		/* Other Kermit's WHATAMI info */	if (debug) fprintf(db,"spar whatami = 0x%x\n",x);	if (streamok > -1)	  if (x & WMI_FLAG)	    if (x & WMI_STREAM)	      streamok = 1;	if (debug) fprintf(db,"spar streamok = %d\n",streamok);    }#endif /* NOSTREAMING */    if (rln > k+8) {			/* Other Kermit's system ID */	char buf[16];	int z;	x = xunchar(s[k+9]);	z = k;	k += (9 + x);	if (x > 0 && x < 16 && rln >= k) {	    strncpy(buf,(char *)s+z+10,x);	    buf[x] = NUL;	    if (debug) fprintf(db,"spar sysid [%s]\n",buf);	    if (buf[0] && !manual) {	/* If transfer mode is automatic... */		if (!strcmp(buf,"U1")) { /* If UNIX, same as me... */		    text = 0;		/* Switch to binary mode */  		    literal = 1;	/* and literal filenames */		    if (debug) fprintf(db,"spar recognizes peer\n");		}	    }	}    }}char *					/* Send our protocol parameters */rpar() {    int x;    if (rpsiz > 94)      xdata[1] = tochar(94);		/* Biggest packet I can receive */    else      xdata[1] = tochar(rpsiz);    xdata[2] = tochar(rtimo);		/* When I want to be timed out */    xdata[3] = tochar(rpadn);		/* How much padding I need (none) */    xdata[4] = ctl(rpadc);		/* Padding character I want */    xdata[5] = tochar(reol);		/* End-Of-Line character I want */    xdata[6] = '#';			/* Control-Quote character I send */    switch (rqf) {			/* 8th-bit prefix */      case -1:      case  1: if (parity) ebq = sq = '&'; break;      case  0:      case  2: break;    }    xdata[7] = sq;    xdata[8] = bctr + '0';		/* Block check type */    if (rptflg)				/* Run length encoding */      xdata[9] = rptq;			/* If receiving, agree. */    else      xdata[9] = '~';    xdata[10] = tochar(2+8);		/* Capabilities (LP+Attributes) */    xdata[11] = tochar(1);		/* Window size */    if (urpsiz > 94) {	xdata[12] = (char) tochar(urpsiz / 95); /* Long packet size */	xdata[13] = (char) tochar(urpsiz % 95);    }    xdata[14] = '0';			/* No checkpointing */    xdata[15] = '+';    xdata[16] = '+';    xdata[17] = '+';    x = WMI_FLAG;			/* WHATAMI info */    if (literal) x |= WMI_FNAME;	/* Filenames literal */    if (!text) x |= WMI_FMODE;		/* Text or binary mode */#ifndef NOSTREAMING    if (streamok > -1)			/* If streaming not disabled, */      x |= WMI_STREAM;			/* offer to stream. */    if (debug)      fprintf(db,"rpar streamok=%d whatami=%d\n",streamok,x);#endif /* NOSTREAMING */    xdata[18] = tochar(x);    xdata[19] = tochar(2);		/* System ID lengh is 2 */    xdata[20] = 'U';			/* UNIX system ID is "U1" */    xdata[21] = '1';    x = WMI2_FLAG;			/* WHATAMI2 info */    if (manual) x |= WMI2_XMODE;	/* Transfer-mode is manual */    xdata[22] = tochar(x);    return(xdata+1);			/* Return pointer length field */}VOIDrinit() {				/* RECEIVE initialization */    if (quiet) return;    tmsgl(versio);    tmsgl("Escape back to your local Kermit and give a SEND command.");    tmsgl("");    tmsgl("KERMIT READY TO RECEIVE...");}VOIDginit() {				/* GET initialization */    if (quiet) return;    tmsgl(versio);    tmsgl("Escape back to your local Kermit and give a SERVER command.");    tmsgl("");    tmsgl("KERMIT READY TO GET...");}intgnfile() {				/* Get name of next file to send */    if (cz)				/* Batch was canceled */      return(0);    while (nfils-- > 0) {		/* Otherwise continue thru list */	strncpy(filnam,*cmlist++,MAXPATHLEN);	filnam[MAXPATHLEN-1] = NUL;	if (zchki(filnam) > -1)		/* Skip files that can't be read */	  return(1);    }    return(0);				/* None left */}intrcvfil() {				/* Receive a file */    char myname[MAXPATHLEN+1];		/* Local name buffer */    cx = 0;				/* Reset per-file cancellation flag */    if (cmarg2) {			/* User gave as-name? */	if (debug) fprintf(db,"rcvfil as-name [%s]\n",cmarg2);	strncpy(myname,cmarg2,MAXPATHLEN); /* Use it */	myname[MAXPATHLEN] = NUL;	if (backup) zbackup(myname);	/* Handle collisions */	cmarg2 = NULL;			/* Undo as-name */    } else {	decstr(filnam);			/* Decode name string */	if (zrtol(filnam,myname,backup,MAXPATHLEN) < 0)	  errpkt("Backup file creation failure");	strncpy(filnam,myname,MAXPATHLEN);	filnam[MAXPATHLEN] = NUL;	if (debug) fprintf(db,"rcvfil filename [%s]\n",filnam);    }    if (zchko(myname) < 0)		/* Check writability */      errpkt("Write access denied");    if (zopeno(myname) < 0)		/* Open the output file */      errpkt("Error opening output file");    return(0);}int					/* Send a command packet */#ifdef __STDC__scmd(char t, char *s)#elsescmd(t,s) char t, *s;#endif /* __STDC__ */{    encstr(s);				/* Encode the command string */    return(spacket(t,seq,size,xdata));	/* Send the packet */}

⌨️ 快捷键说明

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