]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - libio/filedoalloc.c
NEWS: Mention Minguo calendar support added [BZ #24293]
[thirdparty/glibc.git] / libio / filedoalloc.c
index c9b0f01722a1c5b65f4e2df24e951154d133ae21..292ab40f7c058e3eaaebfecc2673b9304d9da28a 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1993, 1997, 2001-2002, 2011-2012 Free Software Foundation, Inc.
+/* Copyright (C) 1993-2019 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
 
 /* Modified for GNU iostream by Per Bothner 1991, 1992. */
 
-#ifndef _POSIX_SOURCE
-# define _POSIX_SOURCE
-#endif
 #include "libioP.h"
-#include <sys/types.h>
+#include <device-nrs.h>
 #include <sys/stat.h>
 #include <stdlib.h>
 #include <unistd.h>
 
-#ifdef _LIBC
-# undef isatty
-# define isatty(Fd) __isatty (Fd)
-
-# include <device-nrs.h>
-#endif
-
-
+/* Return the result of isatty, without changing errno.  */
 static int
 local_isatty (int fd)
 {
   int save_errno = errno;
-  int res = isatty (fd);
+  int res = __isatty (fd);
   __set_errno (save_errno);
   return res;
 }
 
-
-/*
- * Allocate a file buffer, or switch to unbuffered I/O.
- * Per the ANSI C standard, ALL tty devices default to line buffered.
- *
- * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek
- * optimisation) right after the _fstat() that finds the buffer size.
- */
-
+/* Allocate a file buffer, or switch to unbuffered I/O.  Streams for
+   TTY devices default to line buffered.  */
 int
-_IO_file_doallocate (fp)
-     _IO_FILE *fp;
+_IO_file_doallocate (FILE *fp)
 {
-  _IO_size_t size;
+  size_t size;
   char *p;
   struct stat64 st;
 
-#ifndef _LIBC
-  /* If _IO_cleanup_registration_needed is non-zero, we should call the
-     function it points to.  This is to make sure _IO_cleanup gets called
-     on exit.  We call it from _IO_file_doallocate, since that is likely
-     to get called by any program that does buffered I/O. */
-  if (__builtin_expect (_IO_cleanup_registration_needed != NULL, 0))
-    (*_IO_cleanup_registration_needed) ();
-#endif
-
-  size = _IO_BUFSIZ;
+  size = BUFSIZ;
   if (fp->_fileno >= 0 && __builtin_expect (_IO_SYSSTAT (fp, &st), 0) >= 0)
     {
       if (S_ISCHR (st.st_mode))
@@ -120,12 +93,14 @@ _IO_file_doallocate (fp)
              local_isatty (fp->_fileno))
            fp->_flags |= _IO_LINE_BUF;
        }
-#if _IO_HAVE_ST_BLKSIZE
-      if (st.st_blksize > 0)
+#if defined _STATBUF_ST_BLKSIZE
+      if (st.st_blksize > 0 && st.st_blksize < BUFSIZ)
        size = st.st_blksize;
 #endif
     }
-  ALLOC_BUF (p, size, EOF);
+  p = malloc (size);
+  if (__glibc_unlikely (p == NULL))
+    return EOF;
   _IO_setb (fp, p, p + size, 1);
   return 1;
 }