Back to index

glibc  2.9
wfiledoalloc.c
Go to the documentation of this file.
00001 /* Copyright (C) 1993, 1997, 1999, 2000, 2002, 2006
00002    Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.
00019 
00020    As a special exception, if you link the code in this file with
00021    files compiled with a GNU compiler to produce an executable,
00022    that does not cause the resulting executable to be covered by
00023    the GNU Lesser General Public License.  This exception does not
00024    however invalidate any other reasons why the executable file
00025    might be covered by the GNU Lesser General Public License.
00026    This exception applies to code released by its copyright holders
00027    in files containing the exception.  */
00028 
00029 /*
00030    Copyright (C) 1990 The Regents of the University of California.
00031    All rights reserved.
00032  
00033    Redistribution and use in source and binary forms, with or without
00034    modification, are permitted provided that the following conditions
00035    are met:
00036 
00037    1. Redistributions of source code must retain the above copyright
00038       notice, this list of conditions and the following disclaimer.
00039    2. Redistributions in binary form must reproduce the above copyright
00040       notice, this list of conditions and the following disclaimer in the
00041       documentation and/or other materials provided with the distribution.
00042    4. Neither the name of the University nor the names of its contributors
00043       may be used to endorse or promote products derived from this software
00044       without specific prior written permission.
00045    
00046    THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00047    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00048    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00049    ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00050    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00051    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00052    OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00053    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00054    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00055    OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00056    SUCH DAMAGE.*/
00057 
00058 /* Modified for GNU iostream by Per Bothner 1991, 1992. */
00059 
00060 #ifndef _POSIX_SOURCE
00061 # define _POSIX_SOURCE
00062 #endif
00063 #include "libioP.h"
00064 #include <sys/types.h>
00065 #include <sys/stat.h>
00066 #ifdef __STDC__
00067 #include <stdlib.h>
00068 #include <unistd.h>
00069 #endif
00070 
00071 #ifdef _LIBC
00072 # undef isatty
00073 # define isatty(Fd) __isatty (Fd)
00074 #endif
00075 
00076 /*
00077  * Allocate a file buffer, or switch to unbuffered I/O.
00078  * Per the ANSI C standard, ALL tty devices default to line buffered.
00079  *
00080  * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek
00081  * optimisation) right after the _fstat() that finds the buffer size.
00082  */
00083 
00084 int
00085 _IO_wfile_doallocate (fp)
00086      _IO_FILE *fp;
00087 {
00088   _IO_size_t size;
00089   wchar_t *p;
00090 
00091   /* Allocate room for the external buffer.  */
00092   if (fp->_IO_buf_base == NULL)
00093     INTUSE(_IO_file_doallocate) (fp);
00094 
00095   /* If narrow buffer is user allocated (set by setvbuf etc.),
00096      use that size as the size of the wide buffer, when it is
00097      allocated by _IO_file_doallocate, multiply that by size
00098      of the wide character.  */
00099   size = fp->_IO_buf_end - fp->_IO_buf_base;
00100   if ((fp->_flags & _IO_USER_BUF))
00101     size = (size + sizeof (wchar_t) - 1) / sizeof (wchar_t);
00102   ALLOC_WBUF (p, size * sizeof (wchar_t), EOF);
00103   INTUSE(_IO_wsetb) (fp, p, p + size, 1);
00104   return 1;
00105 }