2 * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
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
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 #pragma ident "%Z%%M% %I% %E% SMI"
18 SM_RCSID("@(#)$Id: wbuf.c,v 1.19 2001/03/05 03:22:41 ca Exp $")
23 /* Note: This function is called from a macro located in <sm/io.h> */
26 ** SM_WBUF -- write character to and flush (likely now full) buffer
28 ** Write the given character into the (probably full) buffer for
29 ** the given file. Flush the buffer out if it is or becomes full,
30 ** or if c=='\n' and the file is line buffered.
33 ** fp -- the file pointer
34 ** timeout -- time to complete operation (milliseconds)
35 ** c -- int representation of the character to add
38 ** Failure: -1 and sets errno
39 ** Success: int value of 'c'
43 sm_wbuf(fp
, timeout
, c
)
44 register SM_FILE_T
*fp
;
51 ** In case we cannot write, or longjmp takes us out early,
52 ** make sure w is 0 (if fully- or un-buffered) or -bf.smb_size
53 ** (if line buffered) so that we will get called again.
54 ** If we did not do this, a sufficient number of sm_io_putc()
55 ** calls might wrap w from negative to positive.
58 fp
->f_w
= fp
->f_lbfsize
;
67 ** If it is completely full, flush it out. Then, in any case,
68 ** stuff c into the buffer. If this causes the buffer to fill
69 ** completely, or if c is '\n' and the file is line buffered,
70 ** flush it (perhaps a second time). The second flush will always
71 ** happen on unbuffered streams, where bf.smb_size==1; sm_io_flush()
72 ** guarantees that sm_io_putc() will always call sm_wbuf() by setting
73 ** w to 0, so we need not do anything else.
74 ** Note for the timeout, only one of the sm_io_flush's will get called.
77 n
= fp
->f_p
- fp
->f_bf
.smb_base
;
78 if (n
>= fp
->f_bf
.smb_size
)
80 if (sm_io_flush(fp
, timeout
))
81 return SM_IO_EOF
; /* sm_io_flush() sets errno */
86 if (++n
== fp
->f_bf
.smb_size
|| (fp
->f_flags
& SMLBF
&& c
== '\n'))
87 if (sm_io_flush(fp
, timeout
))
88 return SM_IO_EOF
; /* sm_io_flush() sets errno */