Update.
[glibc.git] / libio / wfiledoalloc.c
blob3317fb7f678d10bbf876c428b37a09ba2b257955
1 /* Copyright (C) 1993, 1997, 1999, 2000 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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.
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. */
29 * Copyright (c) 1990 The Regents of the University of California.
30 * All rights reserved.
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.
45 /* Modified for GNU iostream by Per Bothner 1991, 1992. */
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
58 #ifdef _LIBC
59 # undef isatty
60 # define isatty(Fd) __isatty (Fd)
61 #endif
64 * Allocate a file buffer, or switch to unbuffered I/O.
65 * Per the ANSI C standard, ALL tty devices default to line buffered.
67 * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek
68 * optimisation) right after the _fstat() that finds the buffer size.
71 int
72 _IO_wfile_doallocate (fp)
73 _IO_FILE *fp;
75 _IO_size_t size;
76 int couldbetty;
77 wchar_t *p;
78 struct _G_stat64 st;
80 /* Allocate room for the external buffer. */
81 if (fp->_IO_buf_base == NULL)
82 _IO_file_doallocate (fp);
84 if (fp->_fileno < 0 || _IO_SYSSTAT (fp, &st) < 0)
86 couldbetty = 0;
87 size = _IO_BUFSIZ;
88 #if 0
89 /* do not try to optimise fseek() */
90 fp->_flags |= __SNPT;
91 #endif
93 else
95 couldbetty = S_ISCHR (st.st_mode);
96 #if _IO_HAVE_ST_BLKSIZE
97 size = st.st_blksize <= 0 ? _IO_BUFSIZ : st.st_blksize;
98 #else
99 size = _IO_BUFSIZ;
100 #endif
102 ALLOC_WBUF (p, size * sizeof (wchar_t), EOF);
103 _IO_wsetb (fp, p, p + size, 1);
104 if (couldbetty && isatty (fp->_fileno))
105 fp->_flags |= _IO_LINE_BUF;
106 return 1;