Back to index

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