wsetup.c revision 266692
190075Sobrien/*
290075Sobrien * Copyright (c) 2000-2002 Proofpoint, Inc. and its suppliers.
390075Sobrien *      All rights reserved.
490075Sobrien * Copyright (c) 1990, 1993
590075Sobrien *	The Regents of the University of California.  All rights reserved.
690075Sobrien *
790075Sobrien * This code is derived from software contributed to Berkeley by
890075Sobrien * Chris Torek.
990075Sobrien *
1090075Sobrien * By using this file, you agree to the terms and conditions set
1190075Sobrien * forth in the LICENSE file which can be found at the top level of
1290075Sobrien * the sendmail distribution.
1390075Sobrien */
1490075Sobrien
1590075Sobrien#include <sm/gen.h>
1690075SobrienSM_RCSID("@(#)$Id: wsetup.c,v 1.21 2013-11-22 20:51:44 ca Exp $")
1790075Sobrien#include <stdlib.h>
1890075Sobrien#include <errno.h>
1990075Sobrien#include <sm/io.h>
2090075Sobrien#include "local.h"
2190075Sobrien
2290075Sobrien/*
2390075Sobrien**  SM_WSETUP -- check writing is safe
2490075Sobrien**
2590075Sobrien**  Various output routines call wsetup to be sure it is safe to write,
2690075Sobrien**  because either flags does not include SMMWR, or buf is NULL.
2790075Sobrien**  Used in the macro "cantwrite" found in "local.h".
2890075Sobrien**
2990075Sobrien**	Parameters:
3090075Sobrien**		fp -- the file pointer
3190075Sobrien**
3290075Sobrien**	Results:
3390075Sobrien**		Failure: SM_IO_EOF and sets errno
3490075Sobrien**		Success: 0 (zero)
3590075Sobrien*/
3690075Sobrien
3790075Sobrienint
3890075Sobriensm_wsetup(fp)
3990075Sobrien	register SM_FILE_T *fp;
4090075Sobrien{
4190075Sobrien	/* make sure stdio is set up */
4290075Sobrien	if (!Sm_IO_DidInit)
4390075Sobrien		sm_init();
4490075Sobrien
4590075Sobrien	/* If we are not writing, we had better be reading and writing. */
4690075Sobrien	if ((fp->f_flags & SMWR) == 0)
4790075Sobrien	{
4890075Sobrien		if ((fp->f_flags & SMRW) == 0)
4990075Sobrien		{
5090075Sobrien			errno = EBADF;
5190075Sobrien			return SM_IO_EOF;
5290075Sobrien		}
5390075Sobrien		if (fp->f_flags & SMRD)
5490075Sobrien		{
5590075Sobrien			/* clobber any ungetc data */
5690075Sobrien			if (HASUB(fp))
5790075Sobrien				FREEUB(fp);
5890075Sobrien
5990075Sobrien			/* discard read buffer */
6090075Sobrien			fp->f_flags &= ~(SMRD|SMFEOF);
6190075Sobrien			fp->f_r = 0;
6290075Sobrien			fp->f_p = fp->f_bf.smb_base;
6390075Sobrien		}
6490075Sobrien		fp->f_flags |= SMWR;
6590075Sobrien	}
6690075Sobrien
67117395Skan	/* Make a buffer if necessary, then set w. */
6890075Sobrien	if (fp->f_bf.smb_base == NULL)
6990075Sobrien		sm_makebuf(fp);
7090075Sobrien	if (fp->f_flags & SMLBF)
7190075Sobrien	{
7290075Sobrien		/*
7390075Sobrien		**  It is line buffered, so make lbfsize be -bufsize
7490075Sobrien		**  for the sm_putc() macro.  We will change lbfsize back
7590075Sobrien		**  to 0 whenever we turn off SMWR.
7690075Sobrien		*/
7790075Sobrien
7890075Sobrien		fp->f_w = 0;
7990075Sobrien		fp->f_lbfsize = -fp->f_bf.smb_size;
8090075Sobrien	}
8190075Sobrien	else
8290075Sobrien		fp->f_w = fp->f_flags & SMNBF ? 0 : fp->f_bf.smb_size;
8390075Sobrien	return 0;
8490075Sobrien}
8590075Sobrien