makebuf.c revision 92889
1802Ssundar/*- 2802Ssundar * Copyright (c) 1990, 1993 3802Ssundar * The Regents of the University of California. All rights reserved. 4802Ssundar * 5970Sattila * This code is derived from software contributed to Berkeley by 6802Ssundar * Chris Torek. 7802Ssundar * 8802Ssundar * Redistribution and use in source and binary forms, with or without 9970Sattila * modification, are permitted provided that the following conditions 10802Ssundar * are met: 11802Ssundar * 1. Redistributions of source code must retain the above copyright 12970Sattila * notice, this list of conditions and the following disclaimer. 13802Ssundar * 2. Redistributions in binary form must reproduce the above copyright 14802Ssundar * notice, this list of conditions and the following disclaimer in the 15802Ssundar * documentation and/or other materials provided with the distribution. 16970Sattila * 3. All advertising materials mentioning features or use of this software 17802Ssundar * must display the following acknowledgement: 18802Ssundar * This product includes software developed by the University of 19802Ssundar * California, Berkeley and its contributors. 20970Sattila * 4. Neither the name of the University nor the names of its contributors 21802Ssundar * may be used to endorse or promote products derived from this software 22802Ssundar * without specific prior written permission. 23802Ssundar * 24802Ssundar * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25802Ssundar * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26802Ssundar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27802Ssundar * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28802Ssundar * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29802Ssundar * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30802Ssundar * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31802Ssundar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32802Ssundar * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33802Ssundar * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34802Ssundar * SUCH DAMAGE. 35802Ssundar * 36802Ssundar * $FreeBSD: head/lib/libc/stdio/makebuf.c 92889 2002-03-21 18:49:23Z obrien $ 37802Ssundar */ 38802Ssundar 39802Ssundar#if defined(LIBC_SCCS) && !defined(lint) 40802Ssundarstatic char sccsid[] = "@(#)makebuf.c 8.1 (Berkeley) 6/4/93"; 41802Ssundar#endif /* LIBC_SCCS and not lint */ 42802Ssundar 43802Ssundar#include "namespace.h" 44802Ssundar#include <sys/types.h> 45802Ssundar#include <sys/stat.h> 46802Ssundar#include <unistd.h> 47802Ssundar#include <stdio.h> 48802Ssundar#include <stdlib.h> 49802Ssundar#include "local.h" 50802Ssundar#include "un-namespace.h" 51802Ssundar 52802Ssundar/* 53802Ssundar * Allocate a file buffer, or switch to unbuffered I/O. 54802Ssundar * Per the ANSI C standard, ALL tty devices default to line buffered. 55802Ssundar * 56802Ssundar * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek 57802Ssundar * optimisation) right after the _fstat() that finds the buffer size. 58802Ssundar */ 59802Ssundarvoid 60802Ssundar__smakebuf(fp) 61802Ssundar FILE *fp; 62802Ssundar{ 63802Ssundar void *p; 64 int flags; 65 size_t size; 66 int couldbetty; 67 68 if (fp->_flags & __SNBF) { 69 fp->_bf._base = fp->_p = fp->_nbuf; 70 fp->_bf._size = 1; 71 return; 72 } 73 flags = __swhatbuf(fp, &size, &couldbetty); 74 if ((p = malloc(size)) == NULL) { 75 fp->_flags |= __SNBF; 76 fp->_bf._base = fp->_p = fp->_nbuf; 77 fp->_bf._size = 1; 78 return; 79 } 80 __cleanup = _cleanup; 81 flags |= __SMBF; 82 fp->_bf._base = fp->_p = p; 83 fp->_bf._size = size; 84 if (couldbetty && isatty(fp->_file)) 85 flags |= __SLBF; 86 fp->_flags |= flags; 87} 88 89/* 90 * Internal routine to determine `proper' buffering for a file. 91 */ 92int 93__swhatbuf(fp, bufsize, couldbetty) 94 FILE *fp; 95 size_t *bufsize; 96 int *couldbetty; 97{ 98 struct stat st; 99 100 if (fp->_file < 0 || _fstat(fp->_file, &st) < 0) { 101 *couldbetty = 0; 102 *bufsize = BUFSIZ; 103 return (__SNPT); 104 } 105 106 /* could be a tty iff it is a character device */ 107 *couldbetty = (st.st_mode & S_IFMT) == S_IFCHR; 108 if (st.st_blksize <= 0) { 109 *bufsize = BUFSIZ; 110 return (__SNPT); 111 } 112 113 /* 114 * Optimise fseek() only if it is a regular file. (The test for 115 * __sseek is mainly paranoia.) It is safe to set _blksize 116 * unconditionally; it will only be used if __SOPT is also set. 117 */ 118 *bufsize = st.st_blksize; 119 fp->_blksize = st.st_blksize; 120 return ((st.st_mode & S_IFMT) == S_IFREG && fp->_seek == __sseek ? 121 __SOPT : __SNPT); 122} 123