17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * Copyright (c) 2000-2002 Sendmail, Inc. and its suppliers.
37c478bdstevel@tonic-gate *      All rights reserved.
47c478bdstevel@tonic-gate * Copyright (c) 1990, 1993
57c478bdstevel@tonic-gate *	The Regents of the University of California.  All rights reserved.
67c478bdstevel@tonic-gate *
77c478bdstevel@tonic-gate * This code is derived from software contributed to Berkeley by
87c478bdstevel@tonic-gate * Chris Torek.
97c478bdstevel@tonic-gate *
107c478bdstevel@tonic-gate * By using this file, you agree to the terms and conditions set
117c478bdstevel@tonic-gate * forth in the LICENSE file which can be found at the top level of
127c478bdstevel@tonic-gate * the sendmail distribution.
137c478bdstevel@tonic-gate */
147c478bdstevel@tonic-gate
157c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
167c478bdstevel@tonic-gate
177c478bdstevel@tonic-gate#include <sm/gen.h>
187c478bdstevel@tonic-gateSM_RCSID("@(#)$Id: wsetup.c,v 1.20 2002/02/07 18:02:45 ca Exp $")
197c478bdstevel@tonic-gate#include <stdlib.h>
207c478bdstevel@tonic-gate#include <errno.h>
217c478bdstevel@tonic-gate#include <sm/io.h>
227c478bdstevel@tonic-gate#include "local.h"
237c478bdstevel@tonic-gate
247c478bdstevel@tonic-gate/*
257c478bdstevel@tonic-gate**  SM_WSETUP -- check writing is safe
267c478bdstevel@tonic-gate**
277c478bdstevel@tonic-gate**  Various output routines call wsetup to be sure it is safe to write,
287c478bdstevel@tonic-gate**  because either flags does not include SMMWR, or buf is NULL.
297c478bdstevel@tonic-gate**  Used in the macro "cantwrite" found in "local.h".
307c478bdstevel@tonic-gate**
317c478bdstevel@tonic-gate**	Parameters:
327c478bdstevel@tonic-gate**		fp -- the file pointer
337c478bdstevel@tonic-gate**
347c478bdstevel@tonic-gate**	Results:
357c478bdstevel@tonic-gate**		Failure: SM_IO_EOF and sets errno
367c478bdstevel@tonic-gate**		Success: 0 (zero)
377c478bdstevel@tonic-gate*/
387c478bdstevel@tonic-gate
397c478bdstevel@tonic-gateint
407c478bdstevel@tonic-gatesm_wsetup(fp)
417c478bdstevel@tonic-gate	register SM_FILE_T *fp;
427c478bdstevel@tonic-gate{
437c478bdstevel@tonic-gate	/* make sure stdio is set up */
447c478bdstevel@tonic-gate	if (!Sm_IO_DidInit)
457c478bdstevel@tonic-gate		sm_init();
467c478bdstevel@tonic-gate
477c478bdstevel@tonic-gate	/* If we are not writing, we had better be reading and writing. */
487c478bdstevel@tonic-gate	if ((fp->f_flags & SMWR) == 0)
497c478bdstevel@tonic-gate	{
507c478bdstevel@tonic-gate		if ((fp->f_flags & SMRW) == 0)
517c478bdstevel@tonic-gate		{
527c478bdstevel@tonic-gate			errno = EBADF;
537c478bdstevel@tonic-gate			return SM_IO_EOF;
547c478bdstevel@tonic-gate		}
557c478bdstevel@tonic-gate		if (fp->f_flags & SMRD)
567c478bdstevel@tonic-gate		{
577c478bdstevel@tonic-gate			/* clobber any ungetc data */
587c478bdstevel@tonic-gate			if (HASUB(fp))
597c478bdstevel@tonic-gate				FREEUB(fp);
607c478bdstevel@tonic-gate
617c478bdstevel@tonic-gate			/* discard read buffer */
627c478bdstevel@tonic-gate			fp->f_flags &= ~(SMRD|SMFEOF);
637c478bdstevel@tonic-gate			fp->f_r = 0;
647c478bdstevel@tonic-gate			fp->f_p = fp->f_bf.smb_base;
657c478bdstevel@tonic-gate		}
667c478bdstevel@tonic-gate		fp->f_flags |= SMWR;
677c478bdstevel@tonic-gate	}
687c478bdstevel@tonic-gate
697c478bdstevel@tonic-gate	/* Make a buffer if necessary, then set w. */
707c478bdstevel@tonic-gate	if (fp->f_bf.smb_base == NULL)
717c478bdstevel@tonic-gate		sm_makebuf(fp);
727c478bdstevel@tonic-gate	if (fp->f_flags & SMLBF)
737c478bdstevel@tonic-gate	{
747c478bdstevel@tonic-gate		/*
757c478bdstevel@tonic-gate		**  It is line buffered, so make lbfsize be -bufsize
767c478bdstevel@tonic-gate		**  for the sm_putc() macro.  We will change lbfsize back
777c478bdstevel@tonic-gate		**  to 0 whenever we turn off SMWR.
787c478bdstevel@tonic-gate		*/
797c478bdstevel@tonic-gate
807c478bdstevel@tonic-gate		fp->f_w = 0;
817c478bdstevel@tonic-gate		fp->f_lbfsize = -fp->f_bf.smb_size;
827c478bdstevel@tonic-gate	}
837c478bdstevel@tonic-gate	else
847c478bdstevel@tonic-gate		fp->f_w = fp->f_flags & SMNBF ? 0 : fp->f_bf.smb_size;
857c478bdstevel@tonic-gate	return 0;
867c478bdstevel@tonic-gate}
87