Blame libio/filedoalloc.c

Packit Service 82fcde
/* Copyright (C) 1993-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.
Packit Service 82fcde
Packit Service 82fcde
   As a special exception, if you link the code in this file with
Packit Service 82fcde
   files compiled with a GNU compiler to produce an executable,
Packit Service 82fcde
   that does not cause the resulting executable to be covered by
Packit Service 82fcde
   the GNU Lesser General Public License.  This exception does not
Packit Service 82fcde
   however invalidate any other reasons why the executable file
Packit Service 82fcde
   might be covered by the GNU Lesser General Public License.
Packit Service 82fcde
   This exception applies to code released by its copyright holders
Packit Service 82fcde
   in files containing the exception.  */
Packit Service 82fcde
Packit Service 82fcde
/*
Packit Service 82fcde
   Copyright (C) 1990 The Regents of the University of California.
Packit Service 82fcde
   All rights reserved.
Packit Service 82fcde
Packit Service 82fcde
   Redistribution and use in source and binary forms, with or without
Packit Service 82fcde
   modification, are permitted provided that the following conditions
Packit Service 82fcde
   are met:
Packit Service 82fcde
Packit Service 82fcde
   1. Redistributions of source code must retain the above copyright
Packit Service 82fcde
      notice, this list of conditions and the following disclaimer.
Packit Service 82fcde
   2. Redistributions in binary form must reproduce the above copyright
Packit Service 82fcde
      notice, this list of conditions and the following disclaimer in the
Packit Service 82fcde
      documentation and/or other materials provided with the distribution.
Packit Service 82fcde
   4. Neither the name of the University nor the names of its contributors
Packit Service 82fcde
      may be used to endorse or promote products derived from this software
Packit Service 82fcde
      without specific prior written permission.
Packit Service 82fcde
Packit Service 82fcde
   THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit Service 82fcde
   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit Service 82fcde
   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit Service 82fcde
   ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit Service 82fcde
   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit Service 82fcde
   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit Service 82fcde
   OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit Service 82fcde
   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit Service 82fcde
   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit Service 82fcde
   OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit Service 82fcde
   SUCH DAMAGE.*/
Packit Service 82fcde
Packit Service 82fcde
/* Modified for GNU iostream by Per Bothner 1991, 1992. */
Packit Service 82fcde
Packit Service 82fcde
#include "libioP.h"
Packit Service 82fcde
#include <device-nrs.h>
Packit Service 82fcde
#include <sys/stat.h>
Packit Service 82fcde
#include <stdlib.h>
Packit Service 82fcde
#include <unistd.h>
Packit Service 82fcde
Packit Service 82fcde
/* Return the result of isatty, without changing errno.  */
Packit Service 82fcde
static int
Packit Service 82fcde
local_isatty (int fd)
Packit Service 82fcde
{
Packit Service 82fcde
  int save_errno = errno;
Packit Service 82fcde
  int res = __isatty (fd);
Packit Service 82fcde
  __set_errno (save_errno);
Packit Service 82fcde
  return res;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
/* Allocate a file buffer, or switch to unbuffered I/O.  Streams for
Packit Service 82fcde
   TTY devices default to line buffered.  */
Packit Service 82fcde
int
Packit Service 82fcde
_IO_file_doallocate (FILE *fp)
Packit Service 82fcde
{
Packit Service 82fcde
  size_t size;
Packit Service 82fcde
  char *p;
Packit Service 82fcde
  struct stat64 st;
Packit Service 82fcde
Packit Service 82fcde
  size = BUFSIZ;
Packit Service 82fcde
  if (fp->_fileno >= 0 && __builtin_expect (_IO_SYSSTAT (fp, &st), 0) >= 0)
Packit Service 82fcde
    {
Packit Service 82fcde
      if (S_ISCHR (st.st_mode))
Packit Service 82fcde
	{
Packit Service 82fcde
	  /* Possibly a tty.  */
Packit Service 82fcde
	  if (
Packit Service 82fcde
#ifdef DEV_TTY_P
Packit Service 82fcde
	      DEV_TTY_P (&st) ||
Packit Service 82fcde
#endif
Packit Service 82fcde
	      local_isatty (fp->_fileno))
Packit Service 82fcde
	    fp->_flags |= _IO_LINE_BUF;
Packit Service 82fcde
	}
Packit Service 82fcde
#if defined _STATBUF_ST_BLKSIZE
Packit Service 82fcde
      if (st.st_blksize > 0 && st.st_blksize < BUFSIZ)
Packit Service 82fcde
	size = st.st_blksize;
Packit Service 82fcde
#endif
Packit Service 82fcde
    }
Packit Service 82fcde
  p = malloc (size);
Packit Service 82fcde
  if (__glibc_unlikely (p == NULL))
Packit Service 82fcde
    return EOF;
Packit Service 82fcde
  _IO_setb (fp, p, p + size, 1);
Packit Service 82fcde
  return 1;
Packit Service 82fcde
}
Packit Service 82fcde
libc_hidden_def (_IO_file_doallocate)