Import sendmail 8.13.4 into a new contrib directory as the first step
[dragonfly.git] / contrib / sendmail-8.13.4 / libsm / wsetup.c
blob400553e17c8cf49f3a862d395e9bd620f575bde1
1 /*
2 * Copyright (c) 2000-2002 Sendmail, Inc. and its suppliers.
3 * All rights reserved.
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek.
10 * By using this file, you agree to the terms and conditions set
11 * forth in the LICENSE file which can be found at the top level of
12 * the sendmail distribution.
15 #include <sm/gen.h>
16 SM_RCSID("@(#)$Id: wsetup.c,v 1.20 2002/02/07 18:02:45 ca Exp $")
17 #include <stdlib.h>
18 #include <errno.h>
19 #include <sm/io.h>
20 #include "local.h"
23 ** SM_WSETUP -- check writing is safe
25 ** Various output routines call wsetup to be sure it is safe to write,
26 ** because either flags does not include SMMWR, or buf is NULL.
27 ** Used in the macro "cantwrite" found in "local.h".
29 ** Parameters:
30 ** fp -- the file pointer
32 ** Results:
33 ** Failure: SM_IO_EOF and sets errno
34 ** Success: 0 (zero)
37 int
38 sm_wsetup(fp)
39 register SM_FILE_T *fp;
41 /* make sure stdio is set up */
42 if (!Sm_IO_DidInit)
43 sm_init();
45 /* If we are not writing, we had better be reading and writing. */
46 if ((fp->f_flags & SMWR) == 0)
48 if ((fp->f_flags & SMRW) == 0)
50 errno = EBADF;
51 return SM_IO_EOF;
53 if (fp->f_flags & SMRD)
55 /* clobber any ungetc data */
56 if (HASUB(fp))
57 FREEUB(fp);
59 /* discard read buffer */
60 fp->f_flags &= ~(SMRD|SMFEOF);
61 fp->f_r = 0;
62 fp->f_p = fp->f_bf.smb_base;
64 fp->f_flags |= SMWR;
67 /* Make a buffer if necessary, then set w. */
68 if (fp->f_bf.smb_base == NULL)
69 sm_makebuf(fp);
70 if (fp->f_flags & SMLBF)
73 ** It is line buffered, so make lbfsize be -bufsize
74 ** for the sm_putc() macro. We will change lbfsize back
75 ** to 0 whenever we turn off SMWR.
78 fp->f_w = 0;
79 fp->f_lbfsize = -fp->f_bf.smb_size;
81 else
82 fp->f_w = fp->f_flags & SMNBF ? 0 : fp->f_bf.smb_size;
83 return 0;