xref: /freebsd/contrib/sendmail/libsm/wbuf.c (revision ee7b0571c2c18bdec848ed2044223cc88db29bd8)
140266059SGregory Neil Shapiro /*
25dd76dd0SGregory Neil Shapiro  * Copyright (c) 2000-2001 Proofpoint, Inc. and its suppliers.
340266059SGregory Neil Shapiro  *      All rights reserved.
440266059SGregory Neil Shapiro  * Copyright (c) 1990, 1993
540266059SGregory Neil Shapiro  *	The Regents of the University of California.  All rights reserved.
640266059SGregory Neil Shapiro  *
740266059SGregory Neil Shapiro  * This code is derived from software contributed to Berkeley by
840266059SGregory Neil Shapiro  * Chris Torek.
940266059SGregory Neil Shapiro  *
1040266059SGregory Neil Shapiro  * By using this file, you agree to the terms and conditions set
1140266059SGregory Neil Shapiro  * forth in the LICENSE file which can be found at the top level of
1240266059SGregory Neil Shapiro  * the sendmail distribution.
1340266059SGregory Neil Shapiro  */
1440266059SGregory Neil Shapiro 
1540266059SGregory Neil Shapiro #include <sm/gen.h>
16*4313cc83SGregory Neil Shapiro SM_RCSID("@(#)$Id: wbuf.c,v 1.22 2013-11-22 20:51:44 ca Exp $")
1740266059SGregory Neil Shapiro #include <errno.h>
1840266059SGregory Neil Shapiro #include <sm/io.h>
1940266059SGregory Neil Shapiro #include "local.h"
2040266059SGregory Neil Shapiro 
2140266059SGregory Neil Shapiro /* Note: This function is called from a macro located in <sm/io.h> */
2240266059SGregory Neil Shapiro 
2340266059SGregory Neil Shapiro /*
2440266059SGregory Neil Shapiro **  SM_WBUF -- write character to and flush (likely now full) buffer
2540266059SGregory Neil Shapiro **
2640266059SGregory Neil Shapiro **  Write the given character into the (probably full) buffer for
2740266059SGregory Neil Shapiro **  the given file.  Flush the buffer out if it is or becomes full,
2840266059SGregory Neil Shapiro **  or if c=='\n' and the file is line buffered.
2940266059SGregory Neil Shapiro **
3040266059SGregory Neil Shapiro **	Parameters:
3140266059SGregory Neil Shapiro **		fp -- the file pointer
3240266059SGregory Neil Shapiro **		timeout -- time to complete operation (milliseconds)
3340266059SGregory Neil Shapiro **		c -- int representation of the character to add
3440266059SGregory Neil Shapiro **
3540266059SGregory Neil Shapiro **	Results:
3640266059SGregory Neil Shapiro **		Failure: -1 and sets errno
3740266059SGregory Neil Shapiro **		Success: int value of 'c'
3840266059SGregory Neil Shapiro */
3940266059SGregory Neil Shapiro 
4040266059SGregory Neil Shapiro int
4140266059SGregory Neil Shapiro sm_wbuf(fp, timeout, c)
4240266059SGregory Neil Shapiro 	register SM_FILE_T *fp;
4340266059SGregory Neil Shapiro 	int timeout;
4440266059SGregory Neil Shapiro 	register int c;
4540266059SGregory Neil Shapiro {
4640266059SGregory Neil Shapiro 	register int n;
4740266059SGregory Neil Shapiro 
4840266059SGregory Neil Shapiro 	/*
4940266059SGregory Neil Shapiro 	**  In case we cannot write, or longjmp takes us out early,
5040266059SGregory Neil Shapiro 	**  make sure w is 0 (if fully- or un-buffered) or -bf.smb_size
5140266059SGregory Neil Shapiro 	**  (if line buffered) so that we will get called again.
5240266059SGregory Neil Shapiro 	**  If we did not do this, a sufficient number of sm_io_putc()
5340266059SGregory Neil Shapiro 	**  calls might wrap w from negative to positive.
5440266059SGregory Neil Shapiro 	*/
5540266059SGregory Neil Shapiro 
5640266059SGregory Neil Shapiro 	fp->f_w = fp->f_lbfsize;
5740266059SGregory Neil Shapiro 	if (cantwrite(fp))
5840266059SGregory Neil Shapiro 	{
5940266059SGregory Neil Shapiro 		errno = EBADF;
6040266059SGregory Neil Shapiro 		return SM_IO_EOF;
6140266059SGregory Neil Shapiro 	}
6240266059SGregory Neil Shapiro 	c = (unsigned char)c;
6340266059SGregory Neil Shapiro 
6440266059SGregory Neil Shapiro 	/*
6540266059SGregory Neil Shapiro 	**  If it is completely full, flush it out.  Then, in any case,
6640266059SGregory Neil Shapiro 	**  stuff c into the buffer.  If this causes the buffer to fill
6740266059SGregory Neil Shapiro 	**  completely, or if c is '\n' and the file is line buffered,
6840266059SGregory Neil Shapiro 	**  flush it (perhaps a second time).  The second flush will always
6940266059SGregory Neil Shapiro 	**  happen on unbuffered streams, where bf.smb_size==1; sm_io_flush()
7040266059SGregory Neil Shapiro 	**  guarantees that sm_io_putc() will always call sm_wbuf() by setting
7140266059SGregory Neil Shapiro 	**  w to 0, so we need not do anything else.
7240266059SGregory Neil Shapiro 	**  Note for the timeout, only one of the sm_io_flush's will get called.
7340266059SGregory Neil Shapiro 	*/
7440266059SGregory Neil Shapiro 
7540266059SGregory Neil Shapiro 	n = fp->f_p - fp->f_bf.smb_base;
7640266059SGregory Neil Shapiro 	if (n >= fp->f_bf.smb_size)
7740266059SGregory Neil Shapiro 	{
7840266059SGregory Neil Shapiro 		if (sm_io_flush(fp, timeout))
7940266059SGregory Neil Shapiro 			return SM_IO_EOF; /* sm_io_flush() sets errno */
8040266059SGregory Neil Shapiro 		n = 0;
8140266059SGregory Neil Shapiro 	}
8240266059SGregory Neil Shapiro 	fp->f_w--;
8340266059SGregory Neil Shapiro 	*fp->f_p++ = c;
8440266059SGregory Neil Shapiro 	if (++n == fp->f_bf.smb_size || (fp->f_flags & SMLBF && c == '\n'))
8540266059SGregory Neil Shapiro 		if (sm_io_flush(fp, timeout))
8640266059SGregory Neil Shapiro 			return SM_IO_EOF; /* sm_io_flush() sets errno */
8740266059SGregory Neil Shapiro 	return c;
8840266059SGregory Neil Shapiro }
89