sl@0: /* MAKEBUF.C sl@0: * sl@0: * Portions Copyright (c) 1990-1999 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: */ sl@0: sl@0: /* No user fns here. Pesch 15apr92. */ sl@0: sl@0: /* sl@0: * Copyright (c) 1990 The Regents of the University of California. sl@0: * All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms are permitted sl@0: * provided that the above copyright notice and this paragraph are sl@0: * duplicated in all such forms and that any documentation, sl@0: * advertising materials, and other materials related to such sl@0: * distribution and use acknowledge that the software was developed sl@0: * by the University of California, Berkeley. The name of the sl@0: * University may not be used to endorse or promote products derived sl@0: * from this software without specific prior written permission. sl@0: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR sl@0: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED sl@0: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #include "LOCAL.H" sl@0: sl@0: /* sl@0: * Allocate a file buffer, or switch to unbuffered I/O. sl@0: * Per the ANSI C standard, ALL tty devices default to line buffered. sl@0: * sl@0: * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek sl@0: * optimization) right after the _fstat() that finds the buffer size. sl@0: */ sl@0: sl@0: void sl@0: __smakebuf (register FILE *fp) sl@0: { sl@0: register size_t size, couldbetty; sl@0: register void* p; sl@0: struct stat st; sl@0: sl@0: if (fp->_flags & __SNBF) sl@0: { sl@0: fp->_bf._base = fp->_p = fp->_nbuf; sl@0: fp->_bf._size = 1; sl@0: return; sl@0: } sl@0: if (fp->_file < 0 || _fstat_r (fp->_data, fp->_file, &st) < 0) sl@0: { sl@0: couldbetty = 0; sl@0: size = BUFSIZ; sl@0: /* do not try to optimise fseek() */ sl@0: fp->_flags |= __SNPT; sl@0: } sl@0: else sl@0: { sl@0: couldbetty = (st.st_mode & S_IFMT) == S_IFCHR; sl@0: size = st.st_blksize <= 0 ? BUFSIZ : st.st_blksize; sl@0: /* sl@0: * Optimize fseek() only if it is a regular file. sl@0: * (The test for __sseek is mainly paranoia.) sl@0: */ sl@0: if ((st.st_mode & S_IFMT) == S_IFREG && fp->_seek == __sseek) sl@0: { sl@0: fp->_flags |= __SOPT; sl@0: fp->_blksize = st.st_blksize; sl@0: } sl@0: else sl@0: fp->_flags |= __SNPT; sl@0: } sl@0: if ((p = _malloc_r (fp->_data, size)) == NULL) sl@0: { sl@0: fp->_flags |= __SNBF; sl@0: fp->_bf._base = fp->_p = fp->_nbuf; sl@0: fp->_bf._size = 1; sl@0: } sl@0: else sl@0: { sl@0: fp->_data->__cleanup = _cleanup_r; sl@0: fp->_flags |= __SMBF; sl@0: fp->_bf._base = fp->_p = (unsigned char *) p; sl@0: fp->_bf._size = size; sl@0: if (couldbetty && isatty (fp->_file)) sl@0: fp->_flags |= __SLBF; sl@0: } sl@0: }