Blame libio/iosetvbuf.c

Packit 6c4009
/* Copyright (C) 1993-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.
Packit 6c4009
Packit 6c4009
   As a special exception, if you link the code in this file with
Packit 6c4009
   files compiled with a GNU compiler to produce an executable,
Packit 6c4009
   that does not cause the resulting executable to be covered by
Packit 6c4009
   the GNU Lesser General Public License.  This exception does not
Packit 6c4009
   however invalidate any other reasons why the executable file
Packit 6c4009
   might be covered by the GNU Lesser General Public License.
Packit 6c4009
   This exception applies to code released by its copyright holders
Packit 6c4009
   in files containing the exception.  */
Packit 6c4009
Packit 6c4009
#include "libioP.h"
Packit 6c4009
Packit 6c4009
#define _IOFBF 0 /* Fully buffered. */
Packit 6c4009
#define _IOLBF 1 /* Line buffered. */
Packit 6c4009
#define _IONBF 2 /* No buffering. */
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
_IO_setvbuf (FILE *fp, char *buf, int mode, size_t size)
Packit 6c4009
{
Packit 6c4009
  int result;
Packit 6c4009
  CHECK_FILE (fp, EOF);
Packit 6c4009
  _IO_acquire_lock (fp);
Packit 6c4009
  switch (mode)
Packit 6c4009
    {
Packit 6c4009
    case _IOFBF:
Packit 6c4009
      fp->_flags &= ~(_IO_LINE_BUF|_IO_UNBUFFERED);
Packit 6c4009
      if (buf == NULL)
Packit 6c4009
	{
Packit 6c4009
	  if (fp->_IO_buf_base == NULL)
Packit 6c4009
	    {
Packit 6c4009
	      /* There is no flag to distinguish between "fully buffered
Packit 6c4009
		 mode has been explicitly set" as opposed to "line
Packit 6c4009
		 buffering has not been explicitly set".  In both
Packit 6c4009
		 cases, _IO_LINE_BUF is off.  If this is a tty, and
Packit 6c4009
		 _IO_filedoalloc later gets called, it cannot know if
Packit 6c4009
		 it should set the _IO_LINE_BUF flag (because that is
Packit 6c4009
		 the default), or not (because we have explicitly asked
Packit 6c4009
		 for fully buffered mode).  So we make sure a buffer
Packit 6c4009
		 gets allocated now, and explicitly turn off line
Packit 6c4009
		 buffering.
Packit 6c4009
Packit 6c4009
		 A possibly cleaner alternative would be to add an
Packit 6c4009
		 extra flag, but then flags are a finite resource.  */
Packit 6c4009
	      if (_IO_DOALLOCATE (fp) < 0)
Packit 6c4009
		{
Packit 6c4009
		  result = EOF;
Packit 6c4009
		  goto unlock_return;
Packit 6c4009
		}
Packit 6c4009
	      fp->_flags &= ~_IO_LINE_BUF;
Packit 6c4009
	    }
Packit 6c4009
	  result = 0;
Packit 6c4009
	  goto unlock_return;
Packit 6c4009
	}
Packit 6c4009
      break;
Packit 6c4009
    case _IOLBF:
Packit 6c4009
      fp->_flags &= ~_IO_UNBUFFERED;
Packit 6c4009
      fp->_flags |= _IO_LINE_BUF;
Packit 6c4009
      if (buf == NULL)
Packit 6c4009
	{
Packit 6c4009
	  result = 0;
Packit 6c4009
	  goto unlock_return;
Packit 6c4009
	}
Packit 6c4009
      break;
Packit 6c4009
    case _IONBF:
Packit 6c4009
      fp->_flags &= ~_IO_LINE_BUF;
Packit 6c4009
      fp->_flags |= _IO_UNBUFFERED;
Packit 6c4009
      buf = NULL;
Packit 6c4009
      size = 0;
Packit 6c4009
      break;
Packit 6c4009
    default:
Packit 6c4009
      result = EOF;
Packit 6c4009
      goto unlock_return;
Packit 6c4009
    }
Packit 6c4009
  result = _IO_SETBUF (fp, buf, size) == NULL ? EOF : 0;
Packit 6c4009
Packit 6c4009
unlock_return:
Packit 6c4009
  _IO_release_lock (fp);
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (_IO_setvbuf)
Packit 6c4009
Packit 6c4009
weak_alias (_IO_setvbuf, setvbuf)