Update NEWS
[glibc.git] / libio / iofclose.c
blobcfc0ba1aff461e8de9bf2e6fa4105effdc6c1e1c
1 /* Copyright (C) 1993-2013 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, see
16 <http://www.gnu.org/licenses/>.
18 As a special exception, if you link the code in this file with
19 files compiled with a GNU compiler to produce an executable,
20 that does not cause the resulting executable to be covered by
21 the GNU Lesser General Public License. This exception does not
22 however invalidate any other reasons why the executable file
23 might be covered by the GNU Lesser General Public License.
24 This exception applies to code released by its copyright holders
25 in files containing the exception. */
27 #include "libioP.h"
28 #include <stdlib.h>
29 #if _LIBC
30 # include "../iconv/gconv_int.h"
31 # include <shlib-compat.h>
32 #else
33 # define SHLIB_COMPAT(a, b, c) 0
34 # define _IO_new_fclose fclose
35 #endif
37 int
38 _IO_new_fclose (fp)
39 _IO_FILE *fp;
41 int status;
43 CHECK_FILE(fp, EOF);
45 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
46 /* We desperately try to help programs which are using streams in a
47 strange way and mix old and new functions. Detect old streams
48 here. */
49 if (_IO_vtable_offset (fp) != 0)
50 return _IO_old_fclose (fp);
51 #endif
53 /* First unlink the stream. */
54 if (fp->_IO_file_flags & _IO_IS_FILEBUF)
55 _IO_un_link ((struct _IO_FILE_plus *) fp);
57 _IO_acquire_lock (fp);
58 if (fp->_IO_file_flags & _IO_IS_FILEBUF)
59 status = _IO_file_close_it (fp);
60 else
61 status = fp->_flags & _IO_ERR_SEEN ? -1 : 0;
62 _IO_release_lock (fp);
63 _IO_FINISH (fp);
64 if (fp->_mode > 0)
66 #if _LIBC
67 /* This stream has a wide orientation. This means we have to free
68 the conversion functions. */
69 struct _IO_codecvt *cc = fp->_codecvt;
71 __libc_lock_lock (__gconv_lock);
72 __gconv_release_step (cc->__cd_in.__cd.__steps);
73 __gconv_release_step (cc->__cd_out.__cd.__steps);
74 __libc_lock_unlock (__gconv_lock);
75 #endif
77 else
79 if (_IO_have_backup (fp))
80 _IO_free_backup_area (fp);
82 if (fp != _IO_stdin && fp != _IO_stdout && fp != _IO_stderr)
84 fp->_IO_file_flags = 0;
85 free(fp);
88 return status;
91 #ifdef _LIBC
92 versioned_symbol (libc, _IO_new_fclose, _IO_fclose, GLIBC_2_1);
93 strong_alias (_IO_new_fclose, __new_fclose)
94 versioned_symbol (libc, __new_fclose, fclose, GLIBC_2_1);
95 #endif