Setup LOCPATH for tst-ftell-active-handler and tst-ftell-partial-wide in libio
[platform/upstream/glibc.git] / libio / iofclose.c
1 /* Copyright (C) 1993-2014 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
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.
8
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.
13
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/>.
17
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.  */
26
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
36
37 int
38 _IO_new_fclose (fp)
39      _IO_FILE *fp;
40 {
41   int status;
42
43   CHECK_FILE(fp, EOF);
44
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
52
53   /* First unlink the stream.  */
54   if (fp->_IO_file_flags & _IO_IS_FILEBUF)
55     _IO_un_link ((struct _IO_FILE_plus *) fp);
56
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)
65     {
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;
70
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
76     }
77   else
78     {
79       if (_IO_have_backup (fp))
80         _IO_free_backup_area (fp);
81     }
82   if (fp != _IO_stdin && fp != _IO_stdout && fp != _IO_stderr)
83     {
84       fp->_IO_file_flags = 0;
85       free(fp);
86     }
87
88   return status;
89 }
90
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