xref: /minix3/lib/libc/stdio/makebuf.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: makebuf.c,v 1.18 2015/07/15 19:08:43 christos Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras  * Copyright (c) 1990, 1993
52fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * This code is derived from software contributed to Berkeley by
82fe8fb19SBen Gras  * Chris Torek.
92fe8fb19SBen Gras  *
102fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
122fe8fb19SBen Gras  * are met:
132fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
192fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
202fe8fb19SBen Gras  *    without specific prior written permission.
212fe8fb19SBen Gras  *
222fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
232fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
252fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
262fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322fe8fb19SBen Gras  * SUCH DAMAGE.
332fe8fb19SBen Gras  */
342fe8fb19SBen Gras 
352fe8fb19SBen Gras #include <sys/cdefs.h>
362fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
372fe8fb19SBen Gras #if 0
382fe8fb19SBen Gras static char sccsid[] = "@(#)makebuf.c	8.1 (Berkeley) 6/4/93";
392fe8fb19SBen Gras #else
40*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: makebuf.c,v 1.18 2015/07/15 19:08:43 christos Exp $");
412fe8fb19SBen Gras #endif
422fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
432fe8fb19SBen Gras 
442fe8fb19SBen Gras #include "namespace.h"
452fe8fb19SBen Gras 
462fe8fb19SBen Gras #include <sys/types.h>
472fe8fb19SBen Gras #include <sys/stat.h>
482fe8fb19SBen Gras #include <assert.h>
492fe8fb19SBen Gras #include <stdio.h>
502fe8fb19SBen Gras #include <stdlib.h>
512fe8fb19SBen Gras #include <unistd.h>
52*0a6a1f1dSLionel Sambuc #include <inttypes.h>
53*0a6a1f1dSLionel Sambuc #include <ctype.h>
542fe8fb19SBen Gras #include "reentrant.h"
552fe8fb19SBen Gras #include "local.h"
562fe8fb19SBen Gras 
572fe8fb19SBen Gras /*
58*0a6a1f1dSLionel Sambuc  * Override the file buffering based on the environment setting STDBUF%d
59*0a6a1f1dSLionel Sambuc  * (for the specific file descriptor) and STDBUF (for all descriptors).
60*0a6a1f1dSLionel Sambuc  * the setting is ULB<num> standing for "Unbuffered", "Linebuffered",
61*0a6a1f1dSLionel Sambuc  * and Fullybuffered", and <num> is a value from 0 to 1M.
62*0a6a1f1dSLionel Sambuc  */
63*0a6a1f1dSLionel Sambuc static int
__senvbuf(FILE * fp,size_t * size,int * couldbetty)64*0a6a1f1dSLionel Sambuc __senvbuf(FILE *fp, size_t *size, int *couldbetty)
65*0a6a1f1dSLionel Sambuc {
66*0a6a1f1dSLionel Sambuc 	char evb[64], *evp;
67*0a6a1f1dSLionel Sambuc 	int flags, e;
68*0a6a1f1dSLionel Sambuc 	intmax_t s;
69*0a6a1f1dSLionel Sambuc 
70*0a6a1f1dSLionel Sambuc 	flags = 0;
71*0a6a1f1dSLionel Sambuc 	if (snprintf(evb, sizeof(evb), "STDBUF%d", fp->_file) < 0)
72*0a6a1f1dSLionel Sambuc 		return flags;
73*0a6a1f1dSLionel Sambuc 
74*0a6a1f1dSLionel Sambuc 	if ((evp = getenv(evb)) == NULL && (evp = getenv("STDBUF")) == NULL)
75*0a6a1f1dSLionel Sambuc 		return flags;
76*0a6a1f1dSLionel Sambuc 
77*0a6a1f1dSLionel Sambuc 	switch (*evp) {
78*0a6a1f1dSLionel Sambuc 	case 'u':
79*0a6a1f1dSLionel Sambuc 	case 'U':
80*0a6a1f1dSLionel Sambuc 		evp++;
81*0a6a1f1dSLionel Sambuc 		flags |= __SNBF;
82*0a6a1f1dSLionel Sambuc 		break;
83*0a6a1f1dSLionel Sambuc 	case 'l':
84*0a6a1f1dSLionel Sambuc 	case 'L':
85*0a6a1f1dSLionel Sambuc 		evp++;
86*0a6a1f1dSLionel Sambuc 		flags |= __SLBF;
87*0a6a1f1dSLionel Sambuc 		break;
88*0a6a1f1dSLionel Sambuc 	case 'f':
89*0a6a1f1dSLionel Sambuc 	case 'F':
90*0a6a1f1dSLionel Sambuc 		evp++;
91*0a6a1f1dSLionel Sambuc 		*couldbetty = 0;
92*0a6a1f1dSLionel Sambuc 		break;
93*0a6a1f1dSLionel Sambuc 	}
94*0a6a1f1dSLionel Sambuc 
95*0a6a1f1dSLionel Sambuc 	if (!isdigit((unsigned char)*evp))
96*0a6a1f1dSLionel Sambuc 		return flags;
97*0a6a1f1dSLionel Sambuc 
98*0a6a1f1dSLionel Sambuc 	s = strtoi(evp, NULL, 0, 0, 1024 * 1024, &e);
99*0a6a1f1dSLionel Sambuc 	if (e != 0)
100*0a6a1f1dSLionel Sambuc 		return flags;
101*0a6a1f1dSLionel Sambuc 
102*0a6a1f1dSLionel Sambuc 	*size = (size_t)s;
103*0a6a1f1dSLionel Sambuc 	if (*size == 0)
104*0a6a1f1dSLionel Sambuc 		return __SNBF;
105*0a6a1f1dSLionel Sambuc 
106*0a6a1f1dSLionel Sambuc 	return flags;
107*0a6a1f1dSLionel Sambuc }
108*0a6a1f1dSLionel Sambuc 
109*0a6a1f1dSLionel Sambuc /*
1102fe8fb19SBen Gras  * Allocate a file buffer, or switch to unbuffered I/O.
1112fe8fb19SBen Gras  * Per the ANSI C standard, ALL tty devices default to line buffered.
1122fe8fb19SBen Gras  *
1132fe8fb19SBen Gras  * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek
1142fe8fb19SBen Gras  * optimisation) right after the fstat() that finds the buffer size.
1152fe8fb19SBen Gras  */
1162fe8fb19SBen Gras void
__smakebuf(FILE * fp)117f14fb602SLionel Sambuc __smakebuf(FILE *fp)
1182fe8fb19SBen Gras {
1192fe8fb19SBen Gras 	void *p;
1202fe8fb19SBen Gras 	int flags;
1212fe8fb19SBen Gras 	size_t size;
1222fe8fb19SBen Gras 	int couldbetty;
1232fe8fb19SBen Gras 
1242fe8fb19SBen Gras 	_DIAGASSERT(fp != NULL);
1252fe8fb19SBen Gras 
126*0a6a1f1dSLionel Sambuc 	if (fp->_flags & __SNBF)
127*0a6a1f1dSLionel Sambuc 		goto unbuf;
128*0a6a1f1dSLionel Sambuc 
1292fe8fb19SBen Gras 	flags = __swhatbuf(fp, &size, &couldbetty);
130*0a6a1f1dSLionel Sambuc 
131*0a6a1f1dSLionel Sambuc 	if ((fp->_flags & (__SLBF|__SNBF|__SMBF)) == 0
132*0a6a1f1dSLionel Sambuc 	    && fp->_cookie == fp && fp->_file >= 0) {
133*0a6a1f1dSLionel Sambuc 		flags |= __senvbuf(fp, &size, &couldbetty);
134*0a6a1f1dSLionel Sambuc 		if (flags & __SNBF)
135*0a6a1f1dSLionel Sambuc 			goto unbuf;
1362fe8fb19SBen Gras 	}
137*0a6a1f1dSLionel Sambuc 
138*0a6a1f1dSLionel Sambuc 	if ((p = malloc(size)) == NULL)
139*0a6a1f1dSLionel Sambuc 		goto unbuf;
140*0a6a1f1dSLionel Sambuc 
1412fe8fb19SBen Gras 	__cleanup = _cleanup;
1422fe8fb19SBen Gras 	flags |= __SMBF;
1432fe8fb19SBen Gras 	fp->_bf._base = fp->_p = p;
144f14fb602SLionel Sambuc 	_DIAGASSERT(__type_fit(int, size));
145f14fb602SLionel Sambuc 	fp->_bf._size = (int)size;
1462fe8fb19SBen Gras 	if (couldbetty && isatty(__sfileno(fp)))
1472fe8fb19SBen Gras 		flags |= __SLBF;
1482fe8fb19SBen Gras 	fp->_flags |= flags;
149*0a6a1f1dSLionel Sambuc 	return;
150*0a6a1f1dSLionel Sambuc unbuf:
151*0a6a1f1dSLionel Sambuc 	fp->_flags |= __SNBF;
152*0a6a1f1dSLionel Sambuc 	fp->_bf._base = fp->_p = fp->_nbuf;
153*0a6a1f1dSLionel Sambuc 	fp->_bf._size = 1;
1542fe8fb19SBen Gras }
1552fe8fb19SBen Gras 
1562fe8fb19SBen Gras /*
1572fe8fb19SBen Gras  * Internal routine to determine `proper' buffering for a file.
1582fe8fb19SBen Gras  */
1592fe8fb19SBen Gras int
__swhatbuf(FILE * fp,size_t * bufsize,int * couldbetty)160f14fb602SLionel Sambuc __swhatbuf(FILE *fp, size_t *bufsize, int *couldbetty)
1612fe8fb19SBen Gras {
1622fe8fb19SBen Gras 	struct stat st;
1632fe8fb19SBen Gras 
1642fe8fb19SBen Gras 	_DIAGASSERT(fp != NULL);
1652fe8fb19SBen Gras 	_DIAGASSERT(bufsize != NULL);
1662fe8fb19SBen Gras 	_DIAGASSERT(couldbetty != NULL);
1672fe8fb19SBen Gras 
1682fe8fb19SBen Gras 	if (__sfileno(fp) == -1 || fstat(__sfileno(fp), &st) < 0) {
1692fe8fb19SBen Gras 		*couldbetty = 0;
1702fe8fb19SBen Gras 		*bufsize = BUFSIZ;
171f14fb602SLionel Sambuc 		return __SNPT;
1722fe8fb19SBen Gras 	}
1732fe8fb19SBen Gras 
1742fe8fb19SBen Gras 	/* could be a tty iff it is a character device */
1752fe8fb19SBen Gras 	*couldbetty = S_ISCHR(st.st_mode);
176f14fb602SLionel Sambuc 	if (st.st_blksize == 0) {
177f14fb602SLionel Sambuc 		*bufsize = BUFSIZ;
178f14fb602SLionel Sambuc 		return __SNPT;
179f14fb602SLionel Sambuc 	}
1802fe8fb19SBen Gras 
1812fe8fb19SBen Gras 	/*
1822fe8fb19SBen Gras 	 * Optimise fseek() only if it is a regular file.  (The test for
1832fe8fb19SBen Gras 	 * __sseek is mainly paranoia.)  It is safe to set _blksize
1842fe8fb19SBen Gras 	 * unconditionally; it will only be used if __SOPT is also set.
1852fe8fb19SBen Gras 	 */
18684d9c625SLionel Sambuc 	*bufsize = st.st_blksize;
18784d9c625SLionel Sambuc 	fp->_blksize = st.st_blksize;
188f14fb602SLionel Sambuc 	return (st.st_mode & S_IFMT) == S_IFREG && fp->_seek == __sseek ?
189f14fb602SLionel Sambuc 	    __SOPT : __SNPT;
1902fe8fb19SBen Gras }
191