if (buffer[0] == ETX && buffer[1] == 0x01) { Send_AF(); Write_Status("RcvEof"); state = STATE_RH; close(*filefd); *filefd = -1; break; }
write(*filefd, buffer + 2, length); break; }
static int yapp_download_data(int *filefd, unsigned char *buffer){	int length,file_time;	char Message[50];	if (buffer[0] == CAN || buffer[0] == NAK)	{		Write_Status("RcdABORT");		return(FALSE);	}	switch (state)	{		case STATE_R:			if (buffer[0] == ENQ && buffer[1] == 0x01)			{				Send_RR();				Write_Status("RcvHeader");				state = STATE_RH;				break;			}			Send_CN("Unknown code");			Write_Status("SndABORT");			return(FALSE);							case STATE_RH:			if (buffer[0] == SOH)			{				/* Parse header: 3 fields == YAPP C */				char *hptr, *hfield[3];				if ((length = buffer[1]) == 0) length = 256;				hptr = (char *)buffer + 2;				while (length > 0)				{					int hlen;					hlen = strlen(hptr) + 1;					hfield[(int)yappc++] = hptr;					hptr   += hlen;					length -= hlen;				}				if (yappc < 3)				{					yappc = 0;				}				else				{					file_time = yapp2unix(hfield[2]);					yappc = 1;				}				if (*filefd == -1)				{					if ((*filefd = open(hfield[0], O_RDWR | O_APPEND | O_CREAT, 0666)) == -1)					{						printf("\n[Unable to open %s]\n", hfield[0]);						Send_NR("Invalid filename");						return(FALSE);					}				}				printf("Receiving %s %s %s", hfield[0], hfield[1],				    yappc ? ctime((time_t *)&file_time) : " ");					if (yappc) 				{					struct stat sb;					if (!fstat(*filefd, &sb) && sb.st_size)						Send_RS(sb.st_size);					else						Send_RT();				}				else				{					Send_RF();				}				state = STATE_RD;				break;			}			if (buffer[0] == ENQ && buffer[1] == 0x01)			{				break;			}			if (buffer[0] == EOT && buffer[1] == 0x01)			{				Send_AT();				Write_Status("RcvEOT");				return(FALSE);			}			Send_CN("Unknown code");			Write_Status("SndABORT");			return(FALSE);					case STATE_RD:			if (buffer[0] == STX)			{				if ((length = buffer[1]) == 0) length = 256;				total += length;				sprintf(Message, "RcvData  %5d bytes received", total);				Write_Status(Message);				if (yappc)				{					int i;					unsigned char checksum = 0;					for (i = 0; i < length; i++)						 checksum += buffer[i + 2];					if (checksum != buffer[length + 2])					{						Send_CN("Bad Checksum");						Write_Status("SndABORT: Bad Checksum");						return(FALSE);					}				}				write(*filefd, buffer + 2, length);				break;			}			if (buffer[0] == ETX && buffer[1] == 0x01)			{				Send_AF();				Write_Status("RcvEof");				state = STATE_RH;				close(*filefd);				*filefd = -1;				break;			}			Send_CN("Unknown code");			Write_Status("SndABORT");			return(FALSE);	}		return(TRUE);}