xref: /netbsd-src/lib/libc/stdio/setvbuf.c (revision 4b30c543a0b21e3ba94f2c569e9a82b4fdb2075f)
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  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #if defined(LIBC_SCCS) && !defined(lint)
38 /*static char *sccsid = "from: @(#)setvbuf.c	5.5 (Berkeley) 5/6/93";*/
39 static char *rcsid = "$Id: setvbuf.c,v 1.5 1993/08/26 00:47:23 jtc Exp $";
40 #endif /* LIBC_SCCS and not lint */
41 
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include "local.h"
45 
46 /*
47  * Set one of the three kinds of buffering, optionally including
48  * a buffer.
49  */
50 setvbuf(fp, buf, mode, size)
51 	register FILE *fp;
52 	char *buf;
53 	register int mode;
54 	register size_t size;
55 {
56 	register int ret, flags;
57 	size_t iosize;
58 	int ttyflag;
59 
60 	/*
61 	 * Verify arguments.  The `int' limit on `size' is due to this
62 	 * particular implementation.  Note, buf and size are ignored
63 	 * when setting _IONBF.
64 	 */
65 	if (mode != _IONBF)
66 		if ((mode != _IOFBF && mode != _IOLBF) || (int)size < 0)
67 			return (EOF);
68 
69 	/*
70 	 * Write current buffer, if any.  Discard unread input, cancel
71 	 * line buffering, and free old buffer if malloc()ed.
72 	 */
73 	ret = 0;
74 	(void)__sflush(fp);
75 	fp->_r = fp->_lbfsize = 0;
76 	flags = fp->_flags;
77 	if (flags & __SMBF)
78 		free((void *)fp->_bf._base);
79 	flags &= ~(__SLBF | __SNBF | __SMBF | __SOPT | __SNPT);
80 
81 	/* If setting unbuffered mode, skip all the hard work. */
82 	if (mode == _IONBF)
83 		goto nbf;
84 
85 	/*
86 	 * Find optimal I/O size for seek optimization.  This also returns
87 	 * a `tty flag' to suggest that we check isatty(fd), but we do not
88 	 * care since our caller told us how to buffer.
89 	 */
90 	flags |= __swhatbuf(fp, &iosize, &ttyflag);
91 	if (size == 0) {
92 		buf = NULL;	/* force local allocation */
93 		size = iosize;
94 	}
95 
96 	/* Allocate buffer if needed. */
97 	if (buf == NULL) {
98 		if ((buf = malloc(size)) == NULL) {
99 			/*
100 			 * Unable to honor user's request.  We will return
101 			 * failure, but try again with file system size.
102 			 */
103 			ret = EOF;
104 			if (size != iosize) {
105 				size = iosize;
106 				buf = malloc(size);
107 			}
108 		}
109 		if (buf == NULL) {
110 			/* No luck; switch to unbuffered I/O. */
111 nbf:
112 			fp->_flags = flags | __SNBF;
113 			fp->_w = 0;
114 			fp->_bf._base = fp->_p = fp->_nbuf;
115 			fp->_bf._size = 1;
116 			return (ret);
117 		}
118 		flags |= __SMBF;
119 	}
120 
121 	/*
122 	 * Kill any seek optimization if the buffer is not the
123 	 * right size.
124 	 *
125 	 * SHOULD WE ALLOW MULTIPLES HERE (i.e., ok iff (size % iosize) == 0)?
126 	 */
127 	if (size != iosize)
128 		flags |= __SNPT;
129 
130 	/*
131 	 * Fix up the FILE fields, and set __cleanup for output flush on
132 	 * exit (since we are buffered in some way).  If in r/w mode, go
133 	 * to the intermediate state, so that everyone has to call
134 	 * __srefill or __swsetup on the first operation -- it is more
135 	 * trouble than it is worth to set things up correctly here.
136 	 */
137 	if (mode == _IOLBF)
138 		flags |= __SLBF;
139 	if (flags & __SRW)
140 		flags &= ~(__SRD | __SWR);
141 	fp->_w = 0;
142 	fp->_flags = flags;
143 	fp->_bf._base = fp->_p = (unsigned char *)buf;
144 	fp->_bf._size = size;
145 	fp->_lbfsize = 0;
146 	__cleanup = _cleanup;
147 
148 	return (ret);
149 }
150