2 * Copyright (c) 2000-2001 Proofpoint, 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.33 2013-11-22 20:51:43 ca Exp $")
20 #include <sm/limits.h>
23 #include <sm/assert.h>
28 ** SM_IO_SETVBUF -- set the buffering type for a file
30 ** Set one of the different kinds of buffering, optionally including
32 ** If 'size' is == 0 then an "optimal" size will be selected.
33 ** If 'buf' is == NULL then space will be allocated at 'size'.
36 ** fp -- the file that buffering is to be changed for
37 ** timeout -- time allowed for completing the function
38 ** buf -- buffer to use
39 ** mode -- buffering method to use
40 ** size -- size of 'buf'
48 sm_io_setvbuf(fp, timeout, buf, mode, size)
61 SM_REQUIRE_ISA(fp, SmFileMagic);
64 ** Verify arguments. The `int' limit on `size' is due to this
65 ** particular implementation. Note, buf and size are ignored
66 ** when setting SM_IO_NBF.
69 if (mode != SM_IO_NBF)
70 if ((mode != SM_IO_FBF && mode != SM_IO_LBF &&
71 mode != SM_IO_NOW) || size > INT_MAX)
75 ** Write current buffer, if any. Discard unread input (including
76 ** ungetc data), cancel line buffering, and free old buffer if
77 ** malloc()ed. We also clear any eof condition, as if this were
82 SM_CONVERT_TIME(fp, fd, timeout, &to);
83 (void) sm_flush(fp, &timeout);
86 fp->f_r = fp->f_lbfsize = 0;
90 sm_free((void *) fp->f_bf.smb_base);
91 fp->f_bf.smb_base = NULL;
93 flags &= ~(SMLBF | SMNBF | SMMBF | SMOPT | SMNPT | SMFEOF | SMNOW |
96 /* If setting unbuffered mode, skip all the hard work. */
97 if (mode == SM_IO_NBF)
101 ** Find optimal I/O size for seek optimization. This also returns
102 ** a `tty flag' to suggest that we check isatty(fd), but we do not
103 ** care since our caller told us how to buffer.
106 flags |= sm_whatbuf(fp, &iosize, &ttyflag);
109 buf = NULL; /* force local allocation */
113 /* Allocate buffer if needed. */
116 if ((buf = sm_malloc(size)) == NULL)
119 ** Unable to honor user's request. We will return
120 ** failure, but try again with file system size.
127 buf = sm_malloc(size);
132 /* No luck; switch to unbuffered I/O. */
134 fp->f_flags = flags | SMNBF;
136 fp->f_bf.smb_base = fp->f_p = fp->f_nbuf;
137 fp->f_bf.smb_size = 1;
144 ** Kill any seek optimization if the buffer is not the
147 ** SHOULD WE ALLOW MULTIPLES HERE (i.e., ok iff (size % iosize) == 0)?
154 ** Fix up the SM_FILE_T fields, and set sm_cleanup for output flush on
155 ** exit (since we are buffered in some way).
158 if (mode == SM_IO_LBF)
160 else if (mode == SM_IO_NOW)
162 else if (mode == SM_IO_FBF)
165 fp->f_bf.smb_base = fp->f_p = (unsigned char *)buf;
166 fp->f_bf.smb_size = size;
167 /* fp->f_lbfsize is still 0 */
171 ** Begin or continue writing: see sm_wsetup(). Note
172 ** that SMNBF is impossible (it was handled earlier).
178 fp->f_lbfsize = -fp->f_bf.smb_size;
185 /* begin/continue reading, or stay in intermediate state */