filedoalloc.c 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Copyright (C) 1993-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>.
  14. As a special exception, if you link the code in this file with
  15. files compiled with a GNU compiler to produce an executable,
  16. that does not cause the resulting executable to be covered by
  17. the GNU Lesser General Public License. This exception does not
  18. however invalidate any other reasons why the executable file
  19. might be covered by the GNU Lesser General Public License.
  20. This exception applies to code released by its copyright holders
  21. in files containing the exception. */
  22. /*
  23. Copyright (C) 1990 The Regents of the University of California.
  24. All rights reserved.
  25. Redistribution and use in source and binary forms, with or without
  26. modification, are permitted provided that the following conditions
  27. are met:
  28. 1. Redistributions of source code must retain the above copyright
  29. notice, this list of conditions and the following disclaimer.
  30. 2. Redistributions in binary form must reproduce the above copyright
  31. notice, this list of conditions and the following disclaimer in the
  32. documentation and/or other materials provided with the distribution.
  33. 4. Neither the name of the University nor the names of its contributors
  34. may be used to endorse or promote products derived from this software
  35. without specific prior written permission.
  36. THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  37. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  39. ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  40. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  41. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  42. OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  44. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  45. OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. SUCH DAMAGE.*/
  47. /* Modified for GNU iostream by Per Bothner 1991, 1992. */
  48. #include "libioP.h"
  49. #include <device-nrs.h>
  50. #include <sys/stat.h>
  51. #include <stdlib.h>
  52. #include <unistd.h>
  53. /* Allocate a file buffer, or switch to unbuffered I/O. Streams for
  54. TTY devices default to line buffered. */
  55. int
  56. _IO_file_doallocate (FILE *fp)
  57. {
  58. size_t size;
  59. char *p;
  60. struct __stat64_t64 st;
  61. size = BUFSIZ;
  62. if (fp->_fileno >= 0 && __builtin_expect (_IO_SYSSTAT (fp, &st), 0) >= 0)
  63. {
  64. if (S_ISCHR (st.st_mode))
  65. {
  66. /* Possibly a tty. */
  67. if (
  68. #ifdef DEV_TTY_P
  69. DEV_TTY_P (&st) ||
  70. #endif
  71. __isatty_nostatus (fp->_fileno))
  72. fp->_flags |= _IO_LINE_BUF;
  73. }
  74. #if defined _STATBUF_ST_BLKSIZE
  75. if (st.st_blksize > 0 && st.st_blksize < BUFSIZ)
  76. size = st.st_blksize;
  77. #endif
  78. }
  79. p = malloc (size);
  80. if (__glibc_unlikely (p == NULL))
  81. return EOF;
  82. _IO_setb (fp, p, p + size, 1);
  83. return 1;
  84. }
  85. libc_hidden_def (_IO_file_doallocate)