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: setvbuf.c,v 1.30 2001/02/28 20:25:18 rodney Exp $")
24 #include <sm/assert.h>
29 ** SM_IO_SETVBUF -- set the buffering type for a file
31 ** Set one of the different kinds of buffering, optionally including
33 ** If 'size' is == 0 then an "optimal" size will be selected.
34 ** If 'buf' is == NULL then space will be allocated at 'size'.
37 ** fp -- the file that buffering is to be changed for
38 ** timeout -- time allowed for completing the function
39 ** buf -- buffer to use
40 ** mode -- buffering method to use
41 ** size -- size of 'buf'
49 sm_io_setvbuf(fp
, timeout
, buf
, mode
, size
)
62 SM_REQUIRE_ISA(fp
, SmFileMagic
);
65 ** Verify arguments. The `int' limit on `size' is due to this
66 ** particular implementation. Note, buf and size are ignored
67 ** when setting SM_IO_NBF.
70 if (mode
!= SM_IO_NBF
)
71 if ((mode
!= SM_IO_FBF
&& mode
!= SM_IO_LBF
&&
72 mode
!= SM_IO_NOW
) || (int) size
< 0)
76 ** Write current buffer, if any. Discard unread input (including
77 ** ungetc data), cancel line buffering, and free old buffer if
78 ** malloc()ed. We also clear any eof condition, as if this were
83 SM_CONVERT_TIME(fp
, fd
, timeout
, &to
);
84 (void) sm_flush(fp
, &timeout
);
87 fp
->f_r
= fp
->f_lbfsize
= 0;
91 sm_free((void *) fp
->f_bf
.smb_base
);
92 fp
->f_bf
.smb_base
= NULL
;
94 flags
&= ~(SMLBF
| SMNBF
| SMMBF
| SMOPT
| SMNPT
| SMFEOF
| SMNOW
|
97 /* If setting unbuffered mode, skip all the hard work. */
98 if (mode
== SM_IO_NBF
)
102 ** Find optimal I/O size for seek optimization. This also returns
103 ** a `tty flag' to suggest that we check isatty(fd), but we do not
104 ** care since our caller told us how to buffer.
107 flags
|= sm_whatbuf(fp
, &iosize
, &ttyflag
);
110 buf
= NULL
; /* force local allocation */
114 /* Allocate buffer if needed. */
117 if ((buf
= sm_malloc(size
)) == NULL
)
120 ** Unable to honor user's request. We will return
121 ** failure, but try again with file system size.
128 buf
= sm_malloc(size
);
133 /* No luck; switch to unbuffered I/O. */
135 fp
->f_flags
= flags
| SMNBF
;
137 fp
->f_bf
.smb_base
= fp
->f_p
= fp
->f_nbuf
;
138 fp
->f_bf
.smb_size
= 1;
145 ** Kill any seek optimization if the buffer is not the
148 ** SHOULD WE ALLOW MULTIPLES HERE (i.e., ok iff (size % iosize) == 0)?
155 ** Fix up the SM_FILE_T fields, and set sm_cleanup for output flush on
156 ** exit (since we are buffered in some way).
159 if (mode
== SM_IO_LBF
)
161 else if (mode
== SM_IO_NOW
)
163 else if (mode
== SM_IO_FBF
)
166 fp
->f_bf
.smb_base
= fp
->f_p
= (unsigned char *)buf
;
167 fp
->f_bf
.smb_size
= size
;
168 /* fp->f_lbfsize is still 0 */
172 ** Begin or continue writing: see sm_wsetup(). Note
173 ** that SMNBF is impossible (it was handled earlier).
179 fp
->f_lbfsize
= -fp
->f_bf
.smb_size
;
186 /* begin/continue reading, or stay in intermediate state */