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.1 (Berkeley) 01/20/91"; 13 #endif /* LIBC_SCCS and not lint */ 14 15 #include <sys/types.h> 16 #include <sys/stat.h> 17 #include <stdio.h> 18 #include <stdlib.h> 19 #include "local.h" 20 21 /* 22 * Allocate a file buffer, or switch to unbuffered I/O. 23 * Per the ANSI C standard, ALL tty devices default to line buffered. 24 * 25 * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek 26 * optimisation) right after the fstat() that finds the buffer size. 27 */ 28 void 29 __smakebuf(fp) 30 register FILE *fp; 31 { 32 register size_t size, couldbetty; 33 register void *p; 34 struct stat st; 35 36 if (fp->_flags & __SNBF) { 37 fp->_bf._base = fp->_p = fp->_nbuf; 38 fp->_bf._size = 1; 39 return; 40 } 41 if (fp->_file < 0 || fstat(fp->_file, &st) < 0) { 42 couldbetty = 0; 43 size = BUFSIZ; 44 /* do not try to optimise fseek() */ 45 fp->_flags |= __SNPT; 46 } else { 47 couldbetty = (st.st_mode & S_IFMT) == S_IFCHR; 48 size = st.st_blksize <= 0 ? BUFSIZ : st.st_blksize; 49 /* 50 * Optimise fseek() only if it is a regular file. 51 * (The test for __sseek is mainly paranoia.) 52 */ 53 if ((st.st_mode & S_IFMT) == S_IFREG && 54 fp->_seek == __sseek) { 55 fp->_flags |= __SOPT; 56 fp->_blksize = st.st_blksize; 57 } else 58 fp->_flags |= __SNPT; 59 } 60 if ((p = malloc(size)) == NULL) { 61 fp->_flags |= __SNBF; 62 fp->_bf._base = fp->_p = fp->_nbuf; 63 fp->_bf._size = 1; 64 } else { 65 __cleanup = _cleanup; 66 fp->_flags |= __SMBF; 67 fp->_bf._base = fp->_p = p; 68 fp->_bf._size = size; 69 if (couldbetty && isatty(fp->_file)) 70 fp->_flags |= __SLBF; 71 } 72 } 73