Merge branch 'master' of ssh://sourceware.org/git/glibc
[glibc.git] / libio / wfiledoalloc.c
blob67a05175b2367fd1cc7247cd591596c22031322c
1 /* Copyright (C) 1993, 1997, 1999, 2000, 2002, 2006
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA.
20 As a special exception, if you link the code in this file with
21 files compiled with a GNU compiler to produce an executable,
22 that does not cause the resulting executable to be covered by
23 the GNU Lesser General Public License. This exception does not
24 however invalidate any other reasons why the executable file
25 might be covered by the GNU Lesser General Public License.
26 This exception applies to code released by its copyright holders
27 in files containing the exception. */
30 Copyright (C) 1990 The Regents of the University of California.
31 All rights reserved.
33 Redistribution and use in source and binary forms, with or without
34 modification, are permitted provided that the following conditions
35 are met:
37 1. Redistributions of source code must retain the above copyright
38 notice, this list of conditions and the following disclaimer.
39 2. Redistributions in binary form must reproduce the above copyright
40 notice, this list of conditions and the following disclaimer in the
41 documentation and/or other materials provided with the distribution.
42 4. Neither the name of the University nor the names of its contributors
43 may be used to endorse or promote products derived from this software
44 without specific prior written permission.
46 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 SUCH DAMAGE.*/
58 /* Modified for GNU iostream by Per Bothner 1991, 1992. */
60 #ifndef _POSIX_SOURCE
61 # define _POSIX_SOURCE
62 #endif
63 #include "libioP.h"
64 #include <sys/types.h>
65 #include <sys/stat.h>
66 #ifdef __STDC__
67 #include <stdlib.h>
68 #include <unistd.h>
69 #endif
71 #ifdef _LIBC
72 # undef isatty
73 # define isatty(Fd) __isatty (Fd)
74 #endif
77 * Allocate a file buffer, or switch to unbuffered I/O.
78 * Per the ANSI C standard, ALL tty devices default to line buffered.
80 * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek
81 * optimisation) right after the _fstat() that finds the buffer size.
84 int
85 _IO_wfile_doallocate (fp)
86 _IO_FILE *fp;
88 _IO_size_t size;
89 wchar_t *p;
91 /* Allocate room for the external buffer. */
92 if (fp->_IO_buf_base == NULL)
93 INTUSE(_IO_file_doallocate) (fp);
95 /* If narrow buffer is user allocated (set by setvbuf etc.),
96 use that size as the size of the wide buffer, when it is
97 allocated by _IO_file_doallocate, multiply that by size
98 of the wide character. */
99 size = fp->_IO_buf_end - fp->_IO_buf_base;
100 if ((fp->_flags & _IO_USER_BUF))
101 size = (size + sizeof (wchar_t) - 1) / sizeof (wchar_t);
102 ALLOC_WBUF (p, size * sizeof (wchar_t), EOF);
103 INTUSE(_IO_wsetb) (fp, p, p + size, 1);
104 return 1;