All pastes #492701 Raw Edit

winserver

public cpp v1 · immutable
·published 2007-05-17 11:10 UTC
rendered paste body
/* * Read a line from a descriptor.  Read the line one byte at a time, * looking for the newline.  We store the newline in the buffer, * then follow it with a null (the same as fgets(3)). * We return the number of characters up to, but not including, * the null (the same as strlen(3)). */#ifdef WIN32_LEAN_AND_MEAN#include <winsock2.h>#else#include <sys/socket.h>#endifintreadline(fd, ptr, maxlen)register int	fd;register char	*ptr;register int	maxlen;{	int	n, rc;	char	c;	for (n = 1; n < maxlen; n++) {		if ( (rc = recv(fd, &c, 1, 0)) == 1) {			*ptr++ = c;			if (c == '\n')				break;		} else if (rc == 0) {			if (n == 1)				return(0);	/* EOF, no data read */			else				break;		/* EOF, some data was read */		} else			return(-1);	/* error */	}	*ptr = 0;	return(n);}/* * Write "n" bytes to a descriptor. * Use in place of write() when fd is a stream socket. */intwriten(fd, ptr, nbytes)register int	fd;register char	*ptr;register int	nbytes;{	int	nleft, nwritten;	nleft = nbytes;	while (nleft > 0) {		nwritten = send(fd, ptr, nleft, 0);		if (nwritten <= 0)			return(nwritten);		/* error */		nleft -= nwritten;		ptr   += nwritten;	}	return(nbytes - nleft);}