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