else { timeval.tv_usec = 0; timeval.tv_sec  = TIMEOUT;
static void yapp_upload(int filefd, char *filename, long filelength){	struct timeval timeval;	fd_set sock_read;	fd_set sock_write;	int    n;	unsigned char buffer[1024];	int    buflen = 0;	int    length;	int    used;	char   Message[80];	Write_Status("SendInit");	readlen = (paclen - 2 > 253) ? 253 : paclen - 2;	state   = STATE_S;	total   = 0;	yappc   = 0;	Send_SI();	while (TRUE)	{		FD_ZERO(&sock_read);		FD_ZERO(&sock_write);		FD_SET(STDIN_FILENO, &sock_read);		FD_SET(fd, &sock_read);		if (state == STATE_SD)		{			FD_SET(fd, &sock_write);					n = select(fd + 1, &sock_read, &sock_write, NULL, NULL);		}		else		{			timeval.tv_usec = 0;			timeval.tv_sec  = TIMEOUT;			n = select(fd + 1, &sock_read, NULL, NULL, &timeval);		}						if (n == -1)		{			if (!interrupted && errno == EAGAIN)				continue;			if (!interrupted)				perror("select");			Write_Status("SndABORT");			Send_CN("Internal error");			return;		}				if (n == 0)		/* Timeout, not STATE_SD */		{			Write_Status("SndABORT");			Send_CN("Timeout");			return;		}				if (FD_ISSET(STDIN_FILENO, &sock_read))		{			Write_Status("SndABORT");			Send_CN("Cancelled by user");			return;		}		if (FD_ISSET(fd, &sock_write))	/* Writable, only STATE_SD */		{			if (outlen > 0)			{				if ((n = write(fd, outbuffer + outbufptr, outlen)) > 0)				{					outbufptr += n;					outlen    -= n;					total     += n;				}			}							if (outlen == 0)			{				total -= yappc;				if ((outlen = read(filefd, outbuffer, readlen)) > 0)				{					sprintf(Message, "SendData  %5d bytes transmitted", total);					Write_Status(Message);					outbufptr = 0;					Send_DT(outlen); 					if (yappc)					{						outbuffer[outlen] = checksum(outbuffer, outlen);						outlen++;					}				}				else				{					Write_Status("SendEof");					state = STATE_SE;					Send_EF();				}			}		}				if (FD_ISSET(fd, &sock_read))		{			if ((length = read(fd, buffer + buflen, 511)) > 0)			{				buflen += length;								do				{					used = FALSE;									switch (buffer[0])					{					case ACK:					case ENQ:					case ETX:					case EOT:						if (buflen >= 2)						{							if (!yapp_upload_data(filefd, filename, filelength, buffer))								return;							buflen -= 2;							memcpy(buffer, buffer + 2, buflen);							used = TRUE;						}						break;					default:						if ((length = buffer[1]) == 0)							length = 256;						if (buflen >= (length + 2))						{							if (!yapp_upload_data(filefd, filename, filelength, buffer))								return;							buflen -= length + 2;							memcpy(buffer, buffer + length + 2, buflen);							used = TRUE;						}						break;					}				}				while (used);			}		}	}}