1/* Copyright (C) 1993, 1997, 2001, 2002 Free Software Foundation, Inc.
2   This file is part of the GNU C Library.
3
4   The GNU C Library is free software; you can redistribute it and/or
5   modify it under the terms of the GNU Lesser General Public
6   License as published by the Free Software Foundation; either
7   version 2.1 of the License, or (at your option) any later version.
8
9   The GNU C Library is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with the GNU C Library; if not, write to the Free
16   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17   02111-1307 USA.
18
19   As a special exception, if you link the code in this file with
20   files compiled with a GNU compiler to produce an executable,
21   that does not cause the resulting executable to be covered by
22   the GNU Lesser General Public License.  This exception does not
23   however invalidate any other reasons why the executable file
24   might be covered by the GNU Lesser General Public License.
25   This exception applies to code released by its copyright holders
26   in files containing the exception.  */
27
28/*
29   Copyright (C) 1990 The Regents of the University of California.
30   All rights reserved.
31
32   Redistribution and use in source and binary forms, with or without
33   modification, are permitted provided that the following conditions
34   are met:
35
36   1. Redistributions of source code must retain the above copyright
37      notice, this list of conditions and the following disclaimer.
38   2. Redistributions in binary form must reproduce the above copyright
39      notice, this list of conditions and the following disclaimer in the
40      documentation and/or other materials provided with the distribution.
41   4. Neither the name of the University nor the names of its contributors
42      may be used to endorse or promote products derived from this software
43      without specific prior written permission.
44
45   THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48   ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51   OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54   OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55   SUCH DAMAGE.*/
56
57/* Modified for GNU iostream by Per Bothner 1991, 1992. */
58
59#ifndef _POSIX_SOURCE
60# define _POSIX_SOURCE
61#endif
62#include "libioP.h"
63#include <sys/types.h>
64#include <sys/stat.h>
65#ifdef __STDC__
66#include <stdlib.h>
67#include <unistd.h>
68#endif
69
70#ifdef _LIBC
71# undef isatty
72# define isatty(Fd) __isatty (Fd)
73
74# include <device-nrs.h>
75#endif
76
77/*
78 * Allocate a file buffer, or switch to unbuffered I/O.
79 * Per the ANSI C standard, ALL tty devices default to line buffered.
80 *
81 * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek
82 * optimisation) right after the _fstat() that finds the buffer size.
83 */
84
85int
86_IO_file_doallocate (fp)
87     _IO_FILE *fp;
88{
89  _IO_size_t size;
90  char *p;
91  struct _G_stat64 st;
92
93#ifndef _LIBC
94  /* If _IO_cleanup_registration_needed is non-zero, we should call the
95     function it points to.  This is to make sure _IO_cleanup gets called
96     on exit.  We call it from _IO_file_doallocate, since that is likely
97     to get called by any program that does buffered I/O. */
98  if (__builtin_expect (_IO_cleanup_registration_needed != NULL, 0))
99    (*_IO_cleanup_registration_needed) ();
100#endif
101
102  size = _IO_BUFSIZ;
103  if (fp->_fileno >= 0 && __builtin_expect (_IO_SYSSTAT (fp, &st), 0) >= 0)
104    {
105      if (S_ISCHR (st.st_mode))
106	{
107	  /* Possibly a tty.  */
108	  if (
109#ifdef DEV_TTY_P
110	      DEV_TTY_P (&st) ||
111#endif
112	      isatty (fp->_fileno))
113	    fp->_flags |= _IO_LINE_BUF;
114	}
115#if _IO_HAVE_ST_BLKSIZE
116      if (st.st_blksize > 0)
117	size = st.st_blksize;
118#endif
119    }
120  ALLOC_BUF (p, size, EOF);
121  INTUSE(_IO_setb) (fp, p, p + size, 1);
122  return 1;
123}
124INTDEF(_IO_file_doallocate)
125