Back to index

glibc  2.9
iofclose.c
Go to the documentation of this file.
00001 /* Copyright (C) 1993,1995,1997-2004,2005 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 #include "libioP.h"
00029 #ifdef __STDC__
00030 #include <stdlib.h>
00031 #endif
00032 #if _LIBC
00033 # include "../iconv/gconv_int.h"
00034 # include <shlib-compat.h>
00035 #else
00036 # define SHLIB_COMPAT(a, b, c) 0
00037 # define _IO_new_fclose fclose
00038 #endif
00039 
00040 int
00041 _IO_new_fclose (fp)
00042      _IO_FILE *fp;
00043 {
00044   int status;
00045 
00046   CHECK_FILE(fp, EOF);
00047 
00048 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
00049   /* We desperately try to help programs which are using streams in a
00050      strange way and mix old and new functions.  Detect old streams
00051      here.  */
00052   if (_IO_vtable_offset (fp) != 0)
00053     return _IO_old_fclose (fp);
00054 #endif
00055 
00056   /* First unlink the stream.  */
00057   if (fp->_IO_file_flags & _IO_IS_FILEBUF)
00058     INTUSE(_IO_un_link) ((struct _IO_FILE_plus *) fp);
00059 
00060   _IO_acquire_lock (fp);
00061   if (fp->_IO_file_flags & _IO_IS_FILEBUF)
00062     status = INTUSE(_IO_file_close_it) (fp);
00063   else
00064     status = fp->_flags & _IO_ERR_SEEN ? -1 : 0;
00065   _IO_release_lock (fp);
00066   _IO_FINISH (fp);
00067   if (fp->_mode > 0)
00068     {
00069 #if _LIBC
00070       /* This stream has a wide orientation.  This means we have to free
00071         the conversion functions.  */
00072       struct _IO_codecvt *cc = fp->_codecvt;
00073 
00074       __libc_lock_lock (__gconv_lock);
00075       __gconv_release_step (cc->__cd_in.__cd.__steps);
00076       __gconv_release_step (cc->__cd_out.__cd.__steps);
00077       __libc_lock_unlock (__gconv_lock);
00078 #endif
00079     }
00080   else
00081     {
00082       if (_IO_have_backup (fp))
00083        INTUSE(_IO_free_backup_area) (fp);
00084     }
00085   if (fp != _IO_stdin && fp != _IO_stdout && fp != _IO_stderr)
00086     {
00087       fp->_IO_file_flags = 0;
00088       free(fp);
00089     }
00090 
00091   return status;
00092 }
00093 
00094 #ifdef _LIBC
00095 versioned_symbol (libc, _IO_new_fclose, _IO_fclose, GLIBC_2_1);
00096 strong_alias (_IO_new_fclose, __new_fclose)
00097 versioned_symbol (libc, __new_fclose, fclose, GLIBC_2_1);
00098 #endif