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: makebuf.c,v 1.26 2001/10/31 16:04:08 ca Exp $")
19 #include <sys/types.h>
27 ** SM_MAKEBUF -- make a buffer for the file
30 ** fp -- the file to be buffered
35 ** Allocate a file buffer, or switch to unbuffered I/O.
36 ** By default tty devices default to line buffered.
41 register SM_FILE_T
*fp
;
48 if (fp
->f_flags
& SMNBF
)
50 fp
->f_bf
.smb_base
= fp
->f_p
= fp
->f_nbuf
;
51 fp
->f_bf
.smb_size
= 1;
54 flags
= sm_whatbuf(fp
, &size
, &couldbetty
);
55 if ((p
= sm_malloc(size
)) == NULL
)
58 fp
->f_bf
.smb_base
= fp
->f_p
= fp
->f_nbuf
;
59 fp
->f_bf
.smb_size
= 1;
65 fp
->f_bf
.smb_base
= fp
->f_p
= p
;
66 fp
->f_bf
.smb_size
= size
;
67 if (couldbetty
&& isatty(fp
->f_file
))
73 ** SM_WHATBUF -- determine proper buffer for a file (internal)
75 ** Plus it fills in 'bufsize' for recommended buffer size and
76 ** fills in flag to indicate if 'fp' could be a tty (nothing
77 ** to do with "betty" :-) ).
80 ** fp -- file pointer to be buffered
81 ** bufsize -- new buffer size (a return)
82 ** couldbetty -- could be a tty (returns)
87 ** SMNPT -- not seek opimized
88 ** SMOPT -- seek opimized
92 sm_whatbuf(fp
, bufsize
, couldbetty
)
93 register SM_FILE_T
*fp
;
99 if (fp
->f_file
< 0 || fstat(fp
->f_file
, &st
) < 0)
102 *bufsize
= SM_IO_BUFSIZ
;
106 /* could be a tty iff it is a character device */
107 *couldbetty
= S_ISCHR(st
.st_mode
);
108 if (st
.st_blksize
== 0)
110 *bufsize
= SM_IO_BUFSIZ
;
114 #if SM_IO_MAX_BUF_FILE > 0
115 if (S_ISREG(st
.st_mode
) && st
.st_blksize
> SM_IO_MAX_BUF_FILE
)
116 st
.st_blksize
= SM_IO_MAX_BUF_FILE
;
117 #endif /* SM_IO_MAX_BUF_FILE > 0 */
119 #if SM_IO_MAX_BUF > 0 || SM_IO_MIN_BUF > 0
120 if (!S_ISREG(st
.st_mode
))
122 # if SM_IO_MAX_BUF > 0
123 if (st
.st_blksize
> SM_IO_MAX_BUF
)
124 st
.st_blksize
= SM_IO_MAX_BUF
;
125 # if SM_IO_MIN_BUF > 0
127 # endif /* SM_IO_MIN_BUF > 0 */
128 # endif /* SM_IO_MAX_BUF > 0 */
129 # if SM_IO_MIN_BUF > 0
130 if (st
.st_blksize
< SM_IO_MIN_BUF
)
131 st
.st_blksize
= SM_IO_MIN_BUF
;
132 # endif /* SM_IO_MIN_BUF > 0 */
134 #endif /* SM_IO_MAX_BUF > 0 || SM_IO_MIN_BUF > 0 */
137 ** Optimise fseek() only if it is a regular file. (The test for
138 ** sm_std_seek is mainly paranoia.) It is safe to set _blksize
139 ** unconditionally; it will only be used if SMOPT is also set.
142 if ((fp
->f_flags
& SMSTR
) == 0)
144 *bufsize
= st
.st_blksize
;
145 fp
->f_blksize
= st
.st_blksize
;
148 *bufsize
= SM_IO_BUFSIZ
;
149 if ((st
.st_mode
& S_IFMT
) == S_IFREG
&&
150 fp
->f_seek
== sm_stdseek
)