1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
3*7c478bd9Sstevel@tonic-gate  *      All rights reserved.
4*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1990, 1993
5*7c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
6*7c478bd9Sstevel@tonic-gate  *
7*7c478bd9Sstevel@tonic-gate  * This code is derived from software contributed to Berkeley by
8*7c478bd9Sstevel@tonic-gate  * Chris Torek.
9*7c478bd9Sstevel@tonic-gate  *
10*7c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
11*7c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
12*7c478bd9Sstevel@tonic-gate  * the sendmail distribution.
13*7c478bd9Sstevel@tonic-gate  */
14*7c478bd9Sstevel@tonic-gate 
15*7c478bd9Sstevel@tonic-gate #include <sm/gen.h>
16*7c478bd9Sstevel@tonic-gate SM_RCSID("@(#)$Id: makebuf.c,v 1.26 2001/10/31 16:04:08 ca Exp $")
17*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
18*7c478bd9Sstevel@tonic-gate #include <unistd.h>
19*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
20*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
21*7c478bd9Sstevel@tonic-gate #include <sm/io.h>
22*7c478bd9Sstevel@tonic-gate #include <sm/heap.h>
23*7c478bd9Sstevel@tonic-gate #include <sm/conf.h>
24*7c478bd9Sstevel@tonic-gate #include "local.h"
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate **  SM_MAKEBUF -- make a buffer for the file
28*7c478bd9Sstevel@tonic-gate **
29*7c478bd9Sstevel@tonic-gate **	Parameters:
30*7c478bd9Sstevel@tonic-gate **		fp -- the file to be buffered
31*7c478bd9Sstevel@tonic-gate **
32*7c478bd9Sstevel@tonic-gate **	Returns:
33*7c478bd9Sstevel@tonic-gate **		nothing
34*7c478bd9Sstevel@tonic-gate **
35*7c478bd9Sstevel@tonic-gate **	Allocate a file buffer, or switch to unbuffered I/O.
36*7c478bd9Sstevel@tonic-gate **	By default tty devices default to line buffered.
37*7c478bd9Sstevel@tonic-gate */
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate void
40*7c478bd9Sstevel@tonic-gate sm_makebuf(fp)
41*7c478bd9Sstevel@tonic-gate 	register SM_FILE_T *fp;
42*7c478bd9Sstevel@tonic-gate {
43*7c478bd9Sstevel@tonic-gate 	register void *p;
44*7c478bd9Sstevel@tonic-gate 	register int flags;
45*7c478bd9Sstevel@tonic-gate 	size_t size;
46*7c478bd9Sstevel@tonic-gate 	int couldbetty;
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate 	if (fp->f_flags & SMNBF)
49*7c478bd9Sstevel@tonic-gate 	{
50*7c478bd9Sstevel@tonic-gate 		fp->f_bf.smb_base = fp->f_p = fp->f_nbuf;
51*7c478bd9Sstevel@tonic-gate 		fp->f_bf.smb_size = 1;
52*7c478bd9Sstevel@tonic-gate 		return;
53*7c478bd9Sstevel@tonic-gate 	}
54*7c478bd9Sstevel@tonic-gate 	flags = sm_whatbuf(fp, &size, &couldbetty);
55*7c478bd9Sstevel@tonic-gate 	if ((p = sm_malloc(size)) == NULL)
56*7c478bd9Sstevel@tonic-gate 	{
57*7c478bd9Sstevel@tonic-gate 		fp->f_flags |= SMNBF;
58*7c478bd9Sstevel@tonic-gate 		fp->f_bf.smb_base = fp->f_p = fp->f_nbuf;
59*7c478bd9Sstevel@tonic-gate 		fp->f_bf.smb_size = 1;
60*7c478bd9Sstevel@tonic-gate 		return;
61*7c478bd9Sstevel@tonic-gate 	}
62*7c478bd9Sstevel@tonic-gate 	if (!Sm_IO_DidInit)
63*7c478bd9Sstevel@tonic-gate 		sm_init();
64*7c478bd9Sstevel@tonic-gate 	flags |= SMMBF;
65*7c478bd9Sstevel@tonic-gate 	fp->f_bf.smb_base = fp->f_p = p;
66*7c478bd9Sstevel@tonic-gate 	fp->f_bf.smb_size = size;
67*7c478bd9Sstevel@tonic-gate 	if (couldbetty && isatty(fp->f_file))
68*7c478bd9Sstevel@tonic-gate 		flags |= SMLBF;
69*7c478bd9Sstevel@tonic-gate 	fp->f_flags |= flags;
70*7c478bd9Sstevel@tonic-gate }
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate /*
73*7c478bd9Sstevel@tonic-gate **  SM_WHATBUF -- determine proper buffer for a file (internal)
74*7c478bd9Sstevel@tonic-gate **
75*7c478bd9Sstevel@tonic-gate **  Plus it fills in 'bufsize' for recommended buffer size and
76*7c478bd9Sstevel@tonic-gate **  fills in flag to indicate if 'fp' could be a tty (nothing
77*7c478bd9Sstevel@tonic-gate **  to do with "betty" :-) ).
78*7c478bd9Sstevel@tonic-gate **
79*7c478bd9Sstevel@tonic-gate **	Parameters:
80*7c478bd9Sstevel@tonic-gate **		fp -- file pointer to be buffered
81*7c478bd9Sstevel@tonic-gate **		bufsize -- new buffer size (a return)
82*7c478bd9Sstevel@tonic-gate **		couldbetty -- could be a tty (returns)
83*7c478bd9Sstevel@tonic-gate **
84*7c478bd9Sstevel@tonic-gate **	Returns:
85*7c478bd9Sstevel@tonic-gate **		Success:
86*7c478bd9Sstevel@tonic-gate **		on error:
87*7c478bd9Sstevel@tonic-gate **			SMNPT -- not seek opimized
88*7c478bd9Sstevel@tonic-gate **			SMOPT -- seek opimized
89*7c478bd9Sstevel@tonic-gate */
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate int
sm_whatbuf(fp,bufsize,couldbetty)92*7c478bd9Sstevel@tonic-gate sm_whatbuf(fp, bufsize, couldbetty)
93*7c478bd9Sstevel@tonic-gate 	register SM_FILE_T *fp;
94*7c478bd9Sstevel@tonic-gate 	size_t *bufsize;
95*7c478bd9Sstevel@tonic-gate 	int *couldbetty;
96*7c478bd9Sstevel@tonic-gate {
97*7c478bd9Sstevel@tonic-gate 	struct stat st;
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	if (fp->f_file < 0 || fstat(fp->f_file, &st) < 0)
100*7c478bd9Sstevel@tonic-gate 	{
101*7c478bd9Sstevel@tonic-gate 		*couldbetty = 0;
102*7c478bd9Sstevel@tonic-gate 		*bufsize = SM_IO_BUFSIZ;
103*7c478bd9Sstevel@tonic-gate 		return SMNPT;
104*7c478bd9Sstevel@tonic-gate 	}
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	/* could be a tty iff it is a character device */
107*7c478bd9Sstevel@tonic-gate 	*couldbetty = S_ISCHR(st.st_mode);
108*7c478bd9Sstevel@tonic-gate 	if (st.st_blksize == 0)
109*7c478bd9Sstevel@tonic-gate 	{
110*7c478bd9Sstevel@tonic-gate 		*bufsize = SM_IO_BUFSIZ;
111*7c478bd9Sstevel@tonic-gate 		return SMNPT;
112*7c478bd9Sstevel@tonic-gate 	}
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate #if SM_IO_MAX_BUF_FILE > 0
115*7c478bd9Sstevel@tonic-gate 	if (S_ISREG(st.st_mode) && st.st_blksize > SM_IO_MAX_BUF_FILE)
116*7c478bd9Sstevel@tonic-gate 		st.st_blksize = SM_IO_MAX_BUF_FILE;
117*7c478bd9Sstevel@tonic-gate #endif /* SM_IO_MAX_BUF_FILE > 0 */
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate #if SM_IO_MAX_BUF > 0 || SM_IO_MIN_BUF > 0
120*7c478bd9Sstevel@tonic-gate 	if (!S_ISREG(st.st_mode))
121*7c478bd9Sstevel@tonic-gate 	{
122*7c478bd9Sstevel@tonic-gate # if SM_IO_MAX_BUF > 0
123*7c478bd9Sstevel@tonic-gate 		if (st.st_blksize > SM_IO_MAX_BUF)
124*7c478bd9Sstevel@tonic-gate 			st.st_blksize = SM_IO_MAX_BUF;
125*7c478bd9Sstevel@tonic-gate #  if SM_IO_MIN_BUF > 0
126*7c478bd9Sstevel@tonic-gate 		else
127*7c478bd9Sstevel@tonic-gate #  endif /* SM_IO_MIN_BUF > 0 */
128*7c478bd9Sstevel@tonic-gate # endif /* SM_IO_MAX_BUF > 0 */
129*7c478bd9Sstevel@tonic-gate # if SM_IO_MIN_BUF > 0
130*7c478bd9Sstevel@tonic-gate 		if (st.st_blksize < SM_IO_MIN_BUF)
131*7c478bd9Sstevel@tonic-gate 			st.st_blksize = SM_IO_MIN_BUF;
132*7c478bd9Sstevel@tonic-gate # endif /* SM_IO_MIN_BUF > 0 */
133*7c478bd9Sstevel@tonic-gate 	}
134*7c478bd9Sstevel@tonic-gate #endif /* SM_IO_MAX_BUF > 0 || SM_IO_MIN_BUF > 0 */
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	/*
137*7c478bd9Sstevel@tonic-gate 	**  Optimise fseek() only if it is a regular file.  (The test for
138*7c478bd9Sstevel@tonic-gate 	**  sm_std_seek is mainly paranoia.)  It is safe to set _blksize
139*7c478bd9Sstevel@tonic-gate 	**  unconditionally; it will only be used if SMOPT is also set.
140*7c478bd9Sstevel@tonic-gate 	*/
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	if ((fp->f_flags & SMSTR) == 0)
143*7c478bd9Sstevel@tonic-gate 	{
144*7c478bd9Sstevel@tonic-gate 		*bufsize = st.st_blksize;
145*7c478bd9Sstevel@tonic-gate 		fp->f_blksize = st.st_blksize;
146*7c478bd9Sstevel@tonic-gate 	}
147*7c478bd9Sstevel@tonic-gate 	else
148*7c478bd9Sstevel@tonic-gate 		*bufsize = SM_IO_BUFSIZ;
149*7c478bd9Sstevel@tonic-gate 	if ((st.st_mode & S_IFMT) == S_IFREG &&
150*7c478bd9Sstevel@tonic-gate 	    fp->f_seek == sm_stdseek)
151*7c478bd9Sstevel@tonic-gate 		return SMOPT;
152*7c478bd9Sstevel@tonic-gate 	else
153*7c478bd9Sstevel@tonic-gate 		return SMNPT;
154*7c478bd9Sstevel@tonic-gate }
155