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.
16 SM_RCSID("@(#)$Id: setvbuf.c,v 1.32 2001/09/11 04:04:49 gshapiro Exp $")
22 #include <sm/assert.h>
27 ** SM_IO_SETVBUF -- set the buffering type for a file
29 ** Set one of the different kinds of buffering, optionally including
31 ** If 'size' is == 0 then an "optimal" size will be selected.
32 ** If 'buf' is == NULL then space will be allocated at 'size'.
35 ** fp -- the file that buffering is to be changed for
36 ** timeout -- time allowed for completing the function
37 ** buf -- buffer to use
38 ** mode -- buffering method to use
39 ** size -- size of 'buf'
47 sm_io_setvbuf(fp
, timeout
, buf
, mode
, size
)
60 SM_REQUIRE_ISA(fp
, SmFileMagic
);
63 ** Verify arguments. The `int' limit on `size' is due to this
64 ** particular implementation. Note, buf and size are ignored
65 ** when setting SM_IO_NBF.
68 if (mode
!= SM_IO_NBF
)
69 if ((mode
!= SM_IO_FBF
&& mode
!= SM_IO_LBF
&&
70 mode
!= SM_IO_NOW
) || (int) size
< 0)
74 ** Write current buffer, if any. Discard unread input (including
75 ** ungetc data), cancel line buffering, and free old buffer if
76 ** malloc()ed. We also clear any eof condition, as if this were
81 SM_CONVERT_TIME(fp
, fd
, timeout
, &to
);
82 (void) sm_flush(fp
, &timeout
);
85 fp
->f_r
= fp
->f_lbfsize
= 0;
89 sm_free((void *) fp
->f_bf
.smb_base
);
90 fp
->f_bf
.smb_base
= NULL
;
92 flags
&= ~(SMLBF
| SMNBF
| SMMBF
| SMOPT
| SMNPT
| SMFEOF
| SMNOW
|
95 /* If setting unbuffered mode, skip all the hard work. */
96 if (mode
== SM_IO_NBF
)
100 ** Find optimal I/O size for seek optimization. This also returns
101 ** a `tty flag' to suggest that we check isatty(fd), but we do not
102 ** care since our caller told us how to buffer.
105 flags
|= sm_whatbuf(fp
, &iosize
, &ttyflag
);
108 buf
= NULL
; /* force local allocation */
112 /* Allocate buffer if needed. */
115 if ((buf
= sm_malloc(size
)) == NULL
)
118 ** Unable to honor user's request. We will return
119 ** failure, but try again with file system size.
126 buf
= sm_malloc(size
);
131 /* No luck; switch to unbuffered I/O. */
133 fp
->f_flags
= flags
| SMNBF
;
135 fp
->f_bf
.smb_base
= fp
->f_p
= fp
->f_nbuf
;
136 fp
->f_bf
.smb_size
= 1;
143 ** Kill any seek optimization if the buffer is not the
146 ** SHOULD WE ALLOW MULTIPLES HERE (i.e., ok iff (size % iosize) == 0)?
153 ** Fix up the SM_FILE_T fields, and set sm_cleanup for output flush on
154 ** exit (since we are buffered in some way).
157 if (mode
== SM_IO_LBF
)
159 else if (mode
== SM_IO_NOW
)
161 else if (mode
== SM_IO_FBF
)
164 fp
->f_bf
.smb_base
= fp
->f_p
= (unsigned char *)buf
;
165 fp
->f_bf
.smb_size
= size
;
166 /* fp->f_lbfsize is still 0 */
170 ** Begin or continue writing: see sm_wsetup(). Note
171 ** that SMNBF is impossible (it was handled earlier).
177 fp
->f_lbfsize
= -fp
->f_bf
.smb_size
;
184 /* begin/continue reading, or stay in intermediate state */