3621e4307362d0c463a4d4e0e21823e90fcf495d
[platform/upstream/glibc.git] / libio / filedoalloc.c
1 /* Copyright (C) 1993, 1997, 2001 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 are permitted
33  * provided that the above copyright notice and this paragraph are
34  * duplicated in all such forms and that any documentation,
35  * advertising materials, and other materials related to such
36  * distribution and use acknowledge that the software was developed
37  * by the University of California, Berkeley.  The name of the
38  * University may not be used to endorse or promote products derived
39  * from this software without specific prior written permission.
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
41  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
42  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
43  */
44
45 /* Modified for GNU iostream by Per Bothner 1991, 1992. */
46
47 #ifndef _POSIX_SOURCE
48 # define _POSIX_SOURCE
49 #endif
50 #include "libioP.h"
51 #include <sys/types.h>
52 #include <sys/stat.h>
53 #ifdef __STDC__
54 #include <stdlib.h>
55 #include <unistd.h>
56 #endif
57
58 #ifdef _LIBC
59 # undef isatty
60 # define isatty(Fd) __isatty (Fd)
61
62 # include <device-nrs.h>
63 #endif
64
65 /*
66  * Allocate a file buffer, or switch to unbuffered I/O.
67  * Per the ANSI C standard, ALL tty devices default to line buffered.
68  *
69  * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek
70  * optimisation) right after the _fstat() that finds the buffer size.
71  */
72
73 int
74 _IO_file_doallocate (fp)
75      _IO_FILE *fp;
76 {
77   _IO_size_t size;
78   char *p;
79   struct _G_stat64 st;
80
81 #ifndef _LIBC
82   /* If _IO_cleanup_registration_needed is non-zero, we should call the
83      function it points to.  This is to make sure _IO_cleanup gets called
84      on exit.  We call it from _IO_file_doallocate, since that is likely
85      to get called by any program that does buffered I/O. */
86   if (__builtin_expect (_IO_cleanup_registration_needed != NULL, 0))
87     (*_IO_cleanup_registration_needed) ();
88 #endif
89
90   size = _IO_BUFSIZ;
91   if (fp->_fileno >= 0 && __builtin_expect (_IO_SYSSTAT (fp, &st), 0) >= 0)
92     {
93       if (S_ISCHR (st.st_mode))
94         {
95           /* Possibly a tty.  */
96           if (
97 #ifdef DEV_TTY_P
98               DEV_TTY_P (&st) ||
99 #endif
100               isatty (fp->_fileno))
101             fp->_flags |= _IO_LINE_BUF;
102         }
103 #if _IO_HAVE_ST_BLKSIZE
104       if (st.st_blksize > 0)
105         size = st.st_blksize;
106 #endif
107     }
108   ALLOC_BUF (p, size, EOF);
109   _IO_setb (fp, p, p + size, 1);
110   return 1;
111 }