summaryrefslogtreecommitdiffstats
path: root/contrib/sendmail/libsm/wsetup.c
blob: 1d8b48f30bba253e98a80893034e09de7e8b11b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * Copyright (c) 2000-2002 Proofpoint, Inc. and its suppliers.
 *      All rights reserved.
 * Copyright (c) 1990, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * By using this file, you agree to the terms and conditions set
 * forth in the LICENSE file which can be found at the top level of
 * the sendmail distribution.
 */

#include <sm/gen.h>
SM_RCSID("@(#)$Id: wsetup.c,v 1.21 2013/11/22 20:51:44 ca Exp $")
#include <stdlib.h>
#include <errno.h>
#include <sm/io.h>
#include "local.h"

/*
**  SM_WSETUP -- check writing is safe
**
**  Various output routines call wsetup to be sure it is safe to write,
**  because either flags does not include SMMWR, or buf is NULL.
**  Used in the macro "cantwrite" found in "local.h".
**
**	Parameters:
**		fp -- the file pointer
**
**	Results:
**		Failure: SM_IO_EOF and sets errno
**		Success: 0 (zero)
*/

int
sm_wsetup(fp)
	register SM_FILE_T *fp;
{
	/* make sure stdio is set up */
	if (!Sm_IO_DidInit)
		sm_init();

	/* If we are not writing, we had better be reading and writing. */
	if ((fp->f_flags & SMWR) == 0)
	{
		if ((fp->f_flags & SMRW) == 0)
		{
			errno = EBADF;
			return SM_IO_EOF;
		}
		if (fp->f_flags & SMRD)
		{
			/* clobber any ungetc data */
			if (HASUB(fp))
				FREEUB(fp);

			/* discard read buffer */
			fp->f_flags &= ~(SMRD|SMFEOF);
			fp->f_r = 0;
			fp->f_p = fp->f_bf.smb_base;
		}
		fp->f_flags |= SMWR;
	}

	/* Make a buffer if necessary, then set w. */
	if (fp->f_bf.smb_base == NULL)
		sm_makebuf(fp);
	if (fp->f_flags & SMLBF)
	{
		/*
		**  It is line buffered, so make lbfsize be -bufsize
		**  for the sm_putc() macro.  We will change lbfsize back
		**  to 0 whenever we turn off SMWR.
		*/

		fp->f_w = 0;
		fp->f_lbfsize = -fp->f_bf.smb_size;
	}
	else
		fp->f_w = fp->f_flags & SMNBF ? 0 : fp->f_bf.smb_size;
	return 0;
}
OpenPOWER on IntegriCloud