1: /*-
   2:  * Copyright (c) 1990, 1993
   3:  *	The Regents of the University of California.  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[] = "@(#)setvbuf.c	8.1.1 (2.11BSD) 1997/7/27";
  39: #endif /* LIBC_SCCS and not lint */
  40: 
  41: #include <stdio.h>
  42: #include <stdlib.h>
  43: #include <sys/types.h>
  44: 
  45: /*
  46:  * This has been slightly trimmed from the 4.4BSD version for use with 2.11BSD.
  47:  * In particular 1) the flag names were changed back to the original ones
  48:  * since I didn't feel like porting all of 4.4's stdio package right now and
  49:  * 2) The constant BUFSIZ is used rather than importing the "optimum buffer
  50:  * size selection" logic from 4.4 (besides, a PDP11 can't afford more than 1kb
  51:  * most of the time anyhow).
  52:  *
  53:  * Set one of the three kinds of buffering, optionally including
  54:  * a buffer.
  55:  */
  56: setvbuf(fp, buf, mode, size)
  57:     register FILE *fp;
  58:     char *buf;
  59:     register int mode;
  60:     size_t size;
  61: {
  62:     int ret;
  63:     register int flags;
  64: 
  65: /*
  66:  * Verify arguments. Note, buf and size are ignored when setting _IONBF.
  67:  */
  68:     if (mode != _IONBF)
  69:         if ((mode != _IOFBF && mode != _IOLBF) || (int)size < 0)
  70:             return (EOF);
  71: 
  72:     /*
  73: 	 * Write current buffer, if any.  Discard unread input, cancel
  74: 	 * line buffering, and free old buffer if malloc()ed.
  75: 	 */
  76:     (void)fflush(fp);
  77:     fp->_cnt = fp->_bufsiz = 0;
  78:     flags = fp->_flag;
  79:     if (flags & _IOMYBUF)
  80:         free((void *)fp->_base);
  81:     flags &= ~(_IOLBF | _IONBF | _IOMYBUF);
  82:     ret = 0;
  83: 
  84:     /* If setting unbuffered mode, skip all the hard work. */
  85:     if (mode == _IONBF)
  86:         goto nbf;
  87: 
  88:     if (size == 0) {
  89:         buf = NULL; /* force local allocation */
  90:         size = BUFSIZ;
  91:     }
  92: 
  93:     /* Allocate buffer if needed. */
  94:     if (buf == NULL) {
  95:         if ((buf = (char *)malloc(size)) == NULL) {
  96:             /*
  97: 			 * Unable to honor user's request.  We will return
  98: 			 * failure, but try again with file system size.
  99: 			 */
 100:             ret = EOF;
 101:             if (size != BUFSIZ) {
 102:                 size = BUFSIZ;
 103:                 buf = (char *)malloc(size);
 104:             }
 105:         }
 106:         if (buf == NULL) {
 107:             /* No luck; switch to unbuffered I/O. */
 108: nbf:
 109:             fp->_flag = flags | _IONBF;
 110:             fp->_base = fp->_ptr = NULL;
 111:             return (ret);
 112:         }
 113:         flags |= _IOMYBUF;
 114:     }
 115: 
 116:     /*
 117: 	 * Fix up the FILE fields.  If in r/w mode, go to the unknown state
 118: 	 * so that the the first read performs its initial call to _filbuf and
 119: 	 * the first write has an empty buffer to fill.
 120: 	 */
 121:     if (mode == _IOLBF)
 122:         flags |= _IOLBF;
 123:     if (flags & _IORW)
 124:         flags &= ~(_IOREAD | _IOWRT);
 125:     fp->_flag = flags;
 126:     fp->_base = fp->_ptr = (char *)buf;
 127:     fp->_bufsiz = size;
 128:     return (ret);
 129: }

Defined functions

Defined variables

sccsid defined in line 38; never used
Last modified: 1997-07-30
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 2059
Valid CSS Valid XHTML 1.0 Strict