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: makebuf.c,v 1.26 2001/10/31 16:04:08 ca Exp $")
21 #include <sys/types.h>
29 ** SM_MAKEBUF -- make a buffer for the file
32 ** fp -- the file to be buffered
37 ** Allocate a file buffer, or switch to unbuffered I/O.
38 ** By default tty devices default to line buffered.
43 register SM_FILE_T
*fp
;
50 if (fp
->f_flags
& SMNBF
)
52 fp
->f_bf
.smb_base
= fp
->f_p
= fp
->f_nbuf
;
53 fp
->f_bf
.smb_size
= 1;
56 flags
= sm_whatbuf(fp
, &size
, &couldbetty
);
57 if ((p
= sm_malloc(size
)) == NULL
)
60 fp
->f_bf
.smb_base
= fp
->f_p
= fp
->f_nbuf
;
61 fp
->f_bf
.smb_size
= 1;
67 fp
->f_bf
.smb_base
= fp
->f_p
= p
;
68 fp
->f_bf
.smb_size
= size
;
69 if (couldbetty
&& isatty(fp
->f_file
))
75 ** SM_WHATBUF -- determine proper buffer for a file (internal)
77 ** Plus it fills in 'bufsize' for recommended buffer size and
78 ** fills in flag to indicate if 'fp' could be a tty (nothing
79 ** to do with "betty" :-) ).
82 ** fp -- file pointer to be buffered
83 ** bufsize -- new buffer size (a return)
84 ** couldbetty -- could be a tty (returns)
89 ** SMNPT -- not seek opimized
90 ** SMOPT -- seek opimized
94 sm_whatbuf(fp
, bufsize
, couldbetty
)
95 register SM_FILE_T
*fp
;
101 if (fp
->f_file
< 0 || fstat(fp
->f_file
, &st
) < 0)
104 *bufsize
= SM_IO_BUFSIZ
;
108 /* could be a tty iff it is a character device */
109 *couldbetty
= S_ISCHR(st
.st_mode
);
110 if (st
.st_blksize
== 0)
112 *bufsize
= SM_IO_BUFSIZ
;
116 #if SM_IO_MAX_BUF_FILE > 0
117 if (S_ISREG(st
.st_mode
) && st
.st_blksize
> SM_IO_MAX_BUF_FILE
)
118 st
.st_blksize
= SM_IO_MAX_BUF_FILE
;
119 #endif /* SM_IO_MAX_BUF_FILE > 0 */
121 #if SM_IO_MAX_BUF > 0 || SM_IO_MIN_BUF > 0
122 if (!S_ISREG(st
.st_mode
))
124 # if SM_IO_MAX_BUF > 0
125 if (st
.st_blksize
> SM_IO_MAX_BUF
)
126 st
.st_blksize
= SM_IO_MAX_BUF
;
127 # if SM_IO_MIN_BUF > 0
129 # endif /* SM_IO_MIN_BUF > 0 */
130 # endif /* SM_IO_MAX_BUF > 0 */
131 # if SM_IO_MIN_BUF > 0
132 if (st
.st_blksize
< SM_IO_MIN_BUF
)
133 st
.st_blksize
= SM_IO_MIN_BUF
;
134 # endif /* SM_IO_MIN_BUF > 0 */
136 #endif /* SM_IO_MAX_BUF > 0 || SM_IO_MIN_BUF > 0 */
139 ** Optimise fseek() only if it is a regular file. (The test for
140 ** sm_std_seek is mainly paranoia.) It is safe to set _blksize
141 ** unconditionally; it will only be used if SMOPT is also set.
144 if ((fp
->f_flags
& SMSTR
) == 0)
146 *bufsize
= st
.st_blksize
;
147 fp
->f_blksize
= st
.st_blksize
;
150 *bufsize
= SM_IO_BUFSIZ
;
151 if ((st
.st_mode
& S_IFMT
) == S_IFREG
&&
152 fp
->f_seek
== sm_stdseek
)