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