xref: /openbsd-src/gnu/lib/libstdc++/libstdc++/libio/wfiledoalloc.c (revision c2fb321235c8dbfba0909bc43913400e90646ae7)
1 /* Copyright (C) 1993, 1997, 1999, 2000 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 are permitted
31  * provided that the above copyright notice and this paragraph are
32  * duplicated in all such forms and that any documentation,
33  * advertising materials, and other materials related to such
34  * distribution and use acknowledge that the software was developed
35  * by the University of California, Berkeley.  The name of the
36  * University may not be used to endorse or promote products derived
37  * from this software without specific prior written permission.
38  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
39  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
40  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
41  */
42 
43 /* Modified for GNU iostream by Per Bothner 1991, 1992. */
44 
45 #ifndef _POSIX_SOURCE
46 # define _POSIX_SOURCE
47 #endif
48 #include "libioP.h"
49 #if defined(_GLIBCPP_USE_WCHAR_T) || defined(_GLIBCPP_USE_TYPE_WCHAR_T)
50 #include <sys/types.h>
51 #include <sys/stat.h>
52 #ifdef __STDC__
53 #include <stdlib.h>
54 #include <unistd.h>
55 #endif
56 
57 #ifdef _LIBC
58 # undef isatty
59 # define isatty(Fd) __isatty (Fd)
60 #endif
61 
62 /*
63  * Allocate a file buffer, or switch to unbuffered I/O.
64  * Per the ANSI C standard, ALL tty devices default to line buffered.
65  *
66  * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek
67  * optimisation) right after the _fstat() that finds the buffer size.
68  */
69 
70 int
_IO_wfile_doallocate(fp)71 _IO_wfile_doallocate (fp)
72      _IO_FILE *fp;
73 {
74   _IO_size_t size;
75   int couldbetty;
76   wchar_t *p;
77   struct _G_stat64 st;
78 
79   /* Allocate room for the external buffer.  */
80   if (fp->_IO_buf_base == NULL)
81     _IO_file_doallocate (fp);
82 
83   if (fp->_fileno < 0 || _IO_SYSSTAT (fp, &st) < 0)
84     {
85       couldbetty = 0;
86       size = _IO_BUFSIZ;
87 #if 0
88       /* do not try to optimise fseek() */
89       fp->_flags |= __SNPT;
90 #endif
91     }
92   else
93     {
94       couldbetty = S_ISCHR (st.st_mode);
95 #if defined(_IO_HAVE_ST_BLKSIZE)
96       size = st.st_blksize <= 0 ? _IO_BUFSIZ : st.st_blksize;
97 #else
98       size = _IO_BUFSIZ;
99 #endif
100     }
101   ALLOC_WBUF (p, size * sizeof (wchar_t), EOF);
102   _IO_wsetb (fp, p, p + size, 1);
103   if (couldbetty && isatty (fp->_fileno))
104     fp->_flags |= _IO_LINE_BUF;
105   return 1;
106 }
107 
108 #endif /* _GLIBCPP_USE_WCHAR_T */
109