Add tests for strfrom functions
[glibc.git] / libio / iofclose.c
blob33b130eb252cfa41797d09bde46ef8e0a5d1c9b8
1 /* Copyright (C) 1993-2016 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 (_IO_FILE *fp)
40 int status;
42 CHECK_FILE(fp, EOF);
44 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
45 /* We desperately try to help programs which are using streams in a
46 strange way and mix old and new functions. Detect old streams
47 here. */
48 if (_IO_vtable_offset (fp) != 0)
49 return _IO_old_fclose (fp);
50 #endif
52 /* First unlink the stream. */
53 if (fp->_IO_file_flags & _IO_IS_FILEBUF)
54 _IO_un_link ((struct _IO_FILE_plus *) fp);
56 _IO_acquire_lock (fp);
57 if (fp->_IO_file_flags & _IO_IS_FILEBUF)
58 status = _IO_file_close_it (fp);
59 else
60 status = fp->_flags & _IO_ERR_SEEN ? -1 : 0;
61 _IO_release_lock (fp);
62 _IO_FINISH (fp);
63 if (fp->_mode > 0)
65 #if _LIBC
66 /* This stream has a wide orientation. This means we have to free
67 the conversion functions. */
68 struct _IO_codecvt *cc = fp->_codecvt;
70 __libc_lock_lock (__gconv_lock);
71 __gconv_release_step (cc->__cd_in.__cd.__steps);
72 __gconv_release_step (cc->__cd_out.__cd.__steps);
73 __libc_lock_unlock (__gconv_lock);
74 #endif
76 else
78 if (_IO_have_backup (fp))
79 _IO_free_backup_area (fp);
81 if (fp != _IO_stdin && fp != _IO_stdout && fp != _IO_stderr)
83 fp->_IO_file_flags = 0;
84 free(fp);
87 return status;
90 #ifdef _LIBC
91 versioned_symbol (libc, _IO_new_fclose, _IO_fclose, GLIBC_2_1);
92 strong_alias (_IO_new_fclose, __new_fclose)
93 versioned_symbol (libc, __new_fclose, fclose, GLIBC_2_1);
94 #endif