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