Blame libio/filedoalloc.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
/*
Packit 6c4009
   Copyright (C) 1990 The Regents of the University of California.
Packit 6c4009
   All rights reserved.
Packit 6c4009
Packit 6c4009
   Redistribution and use in source and binary forms, with or without
Packit 6c4009
   modification, are permitted provided that the following conditions
Packit 6c4009
   are met:
Packit 6c4009
Packit 6c4009
   1. Redistributions of source code must retain the above copyright
Packit 6c4009
      notice, this list of conditions and the following disclaimer.
Packit 6c4009
   2. Redistributions in binary form must reproduce the above copyright
Packit 6c4009
      notice, this list of conditions and the following disclaimer in the
Packit 6c4009
      documentation and/or other materials provided with the distribution.
Packit 6c4009
   4. Neither the name of the University nor the names of its contributors
Packit 6c4009
      may be used to endorse or promote products derived from this software
Packit 6c4009
      without specific prior written permission.
Packit 6c4009
Packit 6c4009
   THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit 6c4009
   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 6c4009
   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 6c4009
   ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit 6c4009
   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 6c4009
   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit 6c4009
   OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit 6c4009
   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit 6c4009
   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit 6c4009
   OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit 6c4009
   SUCH DAMAGE.*/
Packit 6c4009
Packit 6c4009
/* Modified for GNU iostream by Per Bothner 1991, 1992. */
Packit 6c4009
Packit 6c4009
#include "libioP.h"
Packit 6c4009
#include <device-nrs.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
/* Return the result of isatty, without changing errno.  */
Packit 6c4009
static int
Packit 6c4009
local_isatty (int fd)
Packit 6c4009
{
Packit 6c4009
  int save_errno = errno;
Packit 6c4009
  int res = __isatty (fd);
Packit 6c4009
  __set_errno (save_errno);
Packit 6c4009
  return res;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Allocate a file buffer, or switch to unbuffered I/O.  Streams for
Packit 6c4009
   TTY devices default to line buffered.  */
Packit 6c4009
int
Packit 6c4009
_IO_file_doallocate (FILE *fp)
Packit 6c4009
{
Packit 6c4009
  size_t size;
Packit 6c4009
  char *p;
Packit 6c4009
  struct stat64 st;
Packit 6c4009
Packit 6c4009
  size = BUFSIZ;
Packit 6c4009
  if (fp->_fileno >= 0 && __builtin_expect (_IO_SYSSTAT (fp, &st), 0) >= 0)
Packit 6c4009
    {
Packit 6c4009
      if (S_ISCHR (st.st_mode))
Packit 6c4009
	{
Packit 6c4009
	  /* Possibly a tty.  */
Packit 6c4009
	  if (
Packit 6c4009
#ifdef DEV_TTY_P
Packit 6c4009
	      DEV_TTY_P (&st) ||
Packit 6c4009
#endif
Packit 6c4009
	      local_isatty (fp->_fileno))
Packit 6c4009
	    fp->_flags |= _IO_LINE_BUF;
Packit 6c4009
	}
Packit 6c4009
#if defined _STATBUF_ST_BLKSIZE
Packit 6c4009
      if (st.st_blksize > 0 && st.st_blksize < BUFSIZ)
Packit 6c4009
	size = st.st_blksize;
Packit 6c4009
#endif
Packit 6c4009
    }
Packit 6c4009
  p = malloc (size);
Packit 6c4009
  if (__glibc_unlikely (p == NULL))
Packit 6c4009
    return EOF;
Packit 6c4009
  _IO_setb (fp, p, p + size, 1);
Packit 6c4009
  return 1;
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (_IO_file_doallocate)