xref: /netbsd-src/lib/libc/stdio/makebuf.c (revision e8277867b9cf63cdf758fd2ee8fbacb18d7d5133)
1*e8277867Suwe /*	$NetBSD: makebuf.c,v 1.19 2018/12/14 03:29:54 uwe Exp $	*/
2255db7b2Sjtc 
361f28255Scgd /*-
4255db7b2Sjtc  * Copyright (c) 1990, 1993
5255db7b2Sjtc  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * This code is derived from software contributed to Berkeley by
861f28255Scgd  * Chris Torek.
961f28255Scgd  *
1061f28255Scgd  * Redistribution and use in source and binary forms, with or without
1161f28255Scgd  * modification, are permitted provided that the following conditions
1261f28255Scgd  * are met:
1361f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1461f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1561f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1761f28255Scgd  *    documentation and/or other materials provided with the distribution.
18eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd  *    may be used to endorse or promote products derived from this software
2061f28255Scgd  *    without specific prior written permission.
2161f28255Scgd  *
2261f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd  * SUCH DAMAGE.
3361f28255Scgd  */
3461f28255Scgd 
3523312f88Schristos #include <sys/cdefs.h>
3661f28255Scgd #if defined(LIBC_SCCS) && !defined(lint)
37255db7b2Sjtc #if 0
38255db7b2Sjtc static char sccsid[] = "@(#)makebuf.c	8.1 (Berkeley) 6/4/93";
3923312f88Schristos #else
40*e8277867Suwe __RCSID("$NetBSD: makebuf.c,v 1.19 2018/12/14 03:29:54 uwe Exp $");
41255db7b2Sjtc #endif
4261f28255Scgd #endif /* LIBC_SCCS and not lint */
4361f28255Scgd 
44f5092a6cSkleink #include "namespace.h"
45b48252f3Slukem 
4661f28255Scgd #include <sys/types.h>
4761f28255Scgd #include <sys/stat.h>
48b48252f3Slukem #include <assert.h>
4961f28255Scgd #include <stdio.h>
5061f28255Scgd #include <stdlib.h>
51b48252f3Slukem #include <unistd.h>
5290b9d61dSchristos #include <inttypes.h>
5390b9d61dSchristos #include <ctype.h>
543fdac2b8Sthorpej #include "reentrant.h"
5561f28255Scgd #include "local.h"
5661f28255Scgd 
5761f28255Scgd /*
5890b9d61dSchristos  * Override the file buffering based on the environment setting STDBUF%d
5990b9d61dSchristos  * (for the specific file descriptor) and STDBUF (for all descriptors).
60*e8277867Suwe  * the setting is ULF<num> standing for "Unbuffered", "Linebuffered",
6190b9d61dSchristos  * and Fullybuffered", and <num> is a value from 0 to 1M.
6290b9d61dSchristos  */
6390b9d61dSchristos static int
__senvbuf(FILE * fp,size_t * size,int * couldbetty)6490b9d61dSchristos __senvbuf(FILE *fp, size_t *size, int *couldbetty)
6590b9d61dSchristos {
6690b9d61dSchristos 	char evb[64], *evp;
6790b9d61dSchristos 	int flags, e;
6890b9d61dSchristos 	intmax_t s;
6990b9d61dSchristos 
7090b9d61dSchristos 	flags = 0;
7190b9d61dSchristos 	if (snprintf(evb, sizeof(evb), "STDBUF%d", fp->_file) < 0)
7290b9d61dSchristos 		return flags;
7390b9d61dSchristos 
7490b9d61dSchristos 	if ((evp = getenv(evb)) == NULL && (evp = getenv("STDBUF")) == NULL)
7590b9d61dSchristos 		return flags;
7690b9d61dSchristos 
7790b9d61dSchristos 	switch (*evp) {
7890b9d61dSchristos 	case 'u':
7990b9d61dSchristos 	case 'U':
8090b9d61dSchristos 		evp++;
8190b9d61dSchristos 		flags |= __SNBF;
8290b9d61dSchristos 		break;
8390b9d61dSchristos 	case 'l':
8490b9d61dSchristos 	case 'L':
8590b9d61dSchristos 		evp++;
8690b9d61dSchristos 		flags |= __SLBF;
8790b9d61dSchristos 		break;
8890b9d61dSchristos 	case 'f':
8990b9d61dSchristos 	case 'F':
9090b9d61dSchristos 		evp++;
9190b9d61dSchristos 		*couldbetty = 0;
9290b9d61dSchristos 		break;
9390b9d61dSchristos 	}
9490b9d61dSchristos 
9590b9d61dSchristos 	if (!isdigit((unsigned char)*evp))
9690b9d61dSchristos 		return flags;
9790b9d61dSchristos 
9890b9d61dSchristos 	s = strtoi(evp, NULL, 0, 0, 1024 * 1024, &e);
9990b9d61dSchristos 	if (e != 0)
10090b9d61dSchristos 		return flags;
10190b9d61dSchristos 
10290b9d61dSchristos 	*size = (size_t)s;
10390b9d61dSchristos 	if (*size == 0)
10490b9d61dSchristos 		return __SNBF;
10590b9d61dSchristos 
10690b9d61dSchristos 	return flags;
10790b9d61dSchristos }
10890b9d61dSchristos 
10990b9d61dSchristos /*
11061f28255Scgd  * Allocate a file buffer, or switch to unbuffered I/O.
11161f28255Scgd  * Per the ANSI C standard, ALL tty devices default to line buffered.
11261f28255Scgd  *
11361f28255Scgd  * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek
11461f28255Scgd  * optimisation) right after the fstat() that finds the buffer size.
11561f28255Scgd  */
11661f28255Scgd void
__smakebuf(FILE * fp)117526d9427Schristos __smakebuf(FILE *fp)
11861f28255Scgd {
119c8bafd62Sperry 	void *p;
120c8bafd62Sperry 	int flags;
121e9fa4a0dScgd 	size_t size;
122e9fa4a0dScgd 	int couldbetty;
12361f28255Scgd 
124b48252f3Slukem 	_DIAGASSERT(fp != NULL);
125b48252f3Slukem 
12690b9d61dSchristos 	if (fp->_flags & __SNBF)
12790b9d61dSchristos 		goto unbuf;
12890b9d61dSchristos 
129e9fa4a0dScgd 	flags = __swhatbuf(fp, &size, &couldbetty);
13090b9d61dSchristos 
13190b9d61dSchristos 	if ((fp->_flags & (__SLBF|__SNBF|__SMBF)) == 0
13290b9d61dSchristos 	    && fp->_cookie == fp && fp->_file >= 0) {
13390b9d61dSchristos 		flags |= __senvbuf(fp, &size, &couldbetty);
13490b9d61dSchristos 		if (flags & __SNBF)
13590b9d61dSchristos 			goto unbuf;
136e9fa4a0dScgd 	}
13790b9d61dSchristos 
13890b9d61dSchristos 	if ((p = malloc(size)) == NULL)
13990b9d61dSchristos 		goto unbuf;
14090b9d61dSchristos 
14161f28255Scgd 	__cleanup = _cleanup;
142e9fa4a0dScgd 	flags |= __SMBF;
14361f28255Scgd 	fp->_bf._base = fp->_p = p;
144c5e820caSchristos 	_DIAGASSERT(__type_fit(int, size));
145c5e820caSchristos 	fp->_bf._size = (int)size;
146749de7f2Schristos 	if (couldbetty && isatty(__sfileno(fp)))
147e9fa4a0dScgd 		flags |= __SLBF;
148e9fa4a0dScgd 	fp->_flags |= flags;
14990b9d61dSchristos 	return;
15090b9d61dSchristos unbuf:
15190b9d61dSchristos 	fp->_flags |= __SNBF;
15290b9d61dSchristos 	fp->_bf._base = fp->_p = fp->_nbuf;
15390b9d61dSchristos 	fp->_bf._size = 1;
15461f28255Scgd }
155e9fa4a0dScgd 
156e9fa4a0dScgd /*
157e9fa4a0dScgd  * Internal routine to determine `proper' buffering for a file.
158e9fa4a0dScgd  */
159e9fa4a0dScgd int
__swhatbuf(FILE * fp,size_t * bufsize,int * couldbetty)160526d9427Schristos __swhatbuf(FILE *fp, size_t *bufsize, int *couldbetty)
161e9fa4a0dScgd {
162e9fa4a0dScgd 	struct stat st;
163e9fa4a0dScgd 
164b48252f3Slukem 	_DIAGASSERT(fp != NULL);
165b48252f3Slukem 	_DIAGASSERT(bufsize != NULL);
166b48252f3Slukem 	_DIAGASSERT(couldbetty != NULL);
167b48252f3Slukem 
168749de7f2Schristos 	if (__sfileno(fp) == -1 || fstat(__sfileno(fp), &st) < 0) {
169e9fa4a0dScgd 		*couldbetty = 0;
170e9fa4a0dScgd 		*bufsize = BUFSIZ;
171526d9427Schristos 		return __SNPT;
172e9fa4a0dScgd 	}
173e9fa4a0dScgd 
174e9fa4a0dScgd 	/* could be a tty iff it is a character device */
17515de1c9fSmycroft 	*couldbetty = S_ISCHR(st.st_mode);
1766865d51cSchristos 	if (st.st_blksize == 0) {
177e9fa4a0dScgd 		*bufsize = BUFSIZ;
178526d9427Schristos 		return __SNPT;
179e9fa4a0dScgd 	}
180e9fa4a0dScgd 
181e9fa4a0dScgd 	/*
182e9fa4a0dScgd 	 * Optimise fseek() only if it is a regular file.  (The test for
183e9fa4a0dScgd 	 * __sseek is mainly paranoia.)  It is safe to set _blksize
184e9fa4a0dScgd 	 * unconditionally; it will only be used if __SOPT is also set.
185e9fa4a0dScgd 	 */
186e9fa4a0dScgd 	*bufsize = st.st_blksize;
187e9fa4a0dScgd 	fp->_blksize = st.st_blksize;
188526d9427Schristos 	return (st.st_mode & S_IFMT) == S_IFREG && fp->_seek == __sseek ?
189526d9427Schristos 	    __SOPT : __SNPT;
19061f28255Scgd }
191