OSDN Git Service

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