xref: /original-bsd/lib/libc/stdio/makebuf.c (revision 51e389b0)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Chris Torek.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #if defined(LIBC_SCCS) && !defined(lint)
12 static char sccsid[] = "@(#)makebuf.c	5.2 (Berkeley) 02/24/91";
13 #endif /* LIBC_SCCS and not lint */
14 
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <unistd.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include "local.h"
21 
22 /*
23  * Allocate a file buffer, or switch to unbuffered I/O.
24  * Per the ANSI C standard, ALL tty devices default to line buffered.
25  *
26  * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek
27  * optimisation) right after the fstat() that finds the buffer size.
28  */
29 void
30 __smakebuf(fp)
31 	register FILE *fp;
32 {
33 	register size_t size, couldbetty;
34 	register void *p;
35 	struct stat st;
36 
37 	if (fp->_flags & __SNBF) {
38 		fp->_bf._base = fp->_p = fp->_nbuf;
39 		fp->_bf._size = 1;
40 		return;
41 	}
42 	if (fp->_file < 0 || fstat(fp->_file, &st) < 0) {
43 		couldbetty = 0;
44 		size = BUFSIZ;
45 		/* do not try to optimise fseek() */
46 		fp->_flags |= __SNPT;
47 	} else {
48 		couldbetty = (st.st_mode & S_IFMT) == S_IFCHR;
49 		size = st.st_blksize <= 0 ? BUFSIZ : st.st_blksize;
50 		/*
51 		 * Optimise fseek() only if it is a regular file.
52 		 * (The test for __sseek is mainly paranoia.)
53 		 */
54 		if ((st.st_mode & S_IFMT) == S_IFREG &&
55 		    fp->_seek == __sseek) {
56 			fp->_flags |= __SOPT;
57 			fp->_blksize = st.st_blksize;
58 		} else
59 			fp->_flags |= __SNPT;
60 	}
61 	if ((p = malloc(size)) == NULL) {
62 		fp->_flags |= __SNBF;
63 		fp->_bf._base = fp->_p = fp->_nbuf;
64 		fp->_bf._size = 1;
65 	} else {
66 		__cleanup = _cleanup;
67 		fp->_flags |= __SMBF;
68 		fp->_bf._base = fp->_p = p;
69 		fp->_bf._size = size;
70 		if (couldbetty && isatty(fp->_file))
71 			fp->_flags |= __SLBF;
72 	}
73 }
74