Back to index

glibc  2.9
iosetvbuf.c
Go to the documentation of this file.
00001 /* Copyright (C) 1993, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
00002    Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.
00019 
00020    As a special exception, if you link the code in this file with
00021    files compiled with a GNU compiler to produce an executable,
00022    that does not cause the resulting executable to be covered by
00023    the GNU Lesser General Public License.  This exception does not
00024    however invalidate any other reasons why the executable file
00025    might be covered by the GNU Lesser General Public License.
00026    This exception applies to code released by its copyright holders
00027    in files containing the exception.  */
00028 
00029 #include "libioP.h"
00030 
00031 #define _IOFBF 0 /* Fully buffered. */
00032 #define _IOLBF 1 /* Line buffered. */
00033 #define _IONBF 2 /* No buffering. */
00034 
00035 int
00036 _IO_setvbuf (fp, buf, mode, size)
00037      _IO_FILE *fp;
00038      char *buf;
00039      int mode;
00040      _IO_size_t size;
00041 {
00042   int result;
00043   CHECK_FILE (fp, EOF);
00044   _IO_acquire_lock (fp);
00045   switch (mode)
00046     {
00047     case _IOFBF:
00048       fp->_IO_file_flags &= ~(_IO_LINE_BUF|_IO_UNBUFFERED);
00049       if (buf == NULL)
00050        {
00051          if (fp->_IO_buf_base == NULL)
00052            {
00053              /* There is no flag to distinguish between "fully buffered
00054                mode has been explicitly set" as opposed to "line
00055                buffering has not been explicitly set".  In both
00056                cases, _IO_LINE_BUF is off.  If this is a tty, and
00057                _IO_filedoalloc later gets called, it cannot know if
00058                it should set the _IO_LINE_BUF flag (because that is
00059                the default), or not (because we have explicitly asked
00060                for fully buffered mode).  So we make sure a buffer
00061                gets allocated now, and explicitly turn off line
00062                buffering.
00063 
00064                A possibly cleaner alternative would be to add an
00065                extra flag, but then flags are a finite resource.  */
00066              if (_IO_DOALLOCATE (fp) < 0)
00067               {
00068                 result = EOF;
00069                 goto unlock_return;
00070               }
00071              fp->_IO_file_flags &= ~_IO_LINE_BUF;
00072            }
00073          result = 0;
00074          goto unlock_return;
00075        }
00076       break;
00077     case _IOLBF:
00078       fp->_IO_file_flags &= ~_IO_UNBUFFERED;
00079       fp->_IO_file_flags |= _IO_LINE_BUF;
00080       if (buf == NULL)
00081        {
00082          result = 0;
00083          goto unlock_return;
00084        }
00085       break;
00086     case _IONBF:
00087       fp->_IO_file_flags &= ~_IO_LINE_BUF;
00088       fp->_IO_file_flags |= _IO_UNBUFFERED;
00089       buf = NULL;
00090       size = 0;
00091       break;
00092     default:
00093       result = EOF;
00094       goto unlock_return;
00095     }
00096   result = _IO_SETBUF (fp, buf, size) == NULL ? EOF : 0;
00097 
00098 unlock_return:
00099   _IO_release_lock (fp);
00100   return result;
00101 }
00102 INTDEF(_IO_setvbuf)
00103 
00104 #ifdef weak_alias
00105 weak_alias (_IO_setvbuf, setvbuf)
00106 #endif