
/*
 *	Function	SendBuf
 *	Programmer	N.d'Alterio
 *	Date		05/05/96
 *
 *  Synopsis:	Sends a buffer full of data to news server one line
 *		at a time. Makes sure line terminates with \r\n and
 *		that if a \n.\n sequence is in the file it is transformed
 *		to \n..\n
 *
 *  Arguments:	soc		Connected socket
 *		buf		Buffer containing data
 *		len		Amount of data in buffer
 *
 *  Returns:	None
 *
 *  Variables:	llen		Length of current line
 *		start		Index of start of current line
 *		remain		Amount of data still to send
 *		done		Loop exit flag
 * 
 *  Functions:	strcspn		Get index of char in string (ANSI)
 *		send		Send data on socket (BSD)
 *
 *  $Id: SendBuf.c 1.1 1996/05/06 22:55:02 nagd Exp $
 *
 */

#include "NPNS.h"

void SendBuf( long soc, char *buf, int len )

{

  int llen;

  int start;
  int remain;
  int done;

  start  = 0;
  remain = len;

  done   = FALSE;

  while ( !done ) {

/*
 *   Send out up to \n straight off
 */

	llen = strcspn( buf+start, "\n" );
	send( soc, buf+start, (long)llen, 0L );

	remain -= (llen+1);

/*
 *   Buffer exhausted
 */

	if ( remain < 1 ) {

		if ( buf[start+llen] == '\n' ) {

			if ( buf[start+llen-1] == '\r' )
				send( soc, "\n", 1L, 0L );
			else
				send( soc, "\r\n", 2L, 0L );

		}   /* if */

		done = TRUE;

	} else {

/*
 *   Handle \n.\n and make sure lines always end with \r\n
 */
 
		if ( buf[start+llen+1] == '.' ) {

			if ( buf[start+llen-1] == '\r' )
				send( soc, "\n.", 2L, 0L );
			else
				send( soc, "\r\n.", 3L, 0L );

		} else {

			if ( buf[start+llen-1] == '\r' )
				send( soc, "\n", 1L, 0L );
			else
				send( soc, "\r\n", 2L, 0L );


		}   /* if */

		start += (llen+1);

	}   /* if */

  }  /* while */
 
  return;

}   /* SendBuf */

/*========================================================================*
                         END FUNCTION SendBuf
 *========================================================================*/
