Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /****************************************************************************
  2. *
  3. *                            Open Watcom Project
  4. *
  5. *    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
  6. *
  7. *  ========================================================================
  8. *
  9. *    This file contains Original Code and/or Modifications of Original
  10. *    Code as defined in and that are subject to the Sybase Open Watcom
  11. *    Public License version 1.0 (the 'License'). You may not use this file
  12. *    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
  13. *    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
  14. *    provided with the Original Code and Modifications, and is also
  15. *    available at www.sybase.com/developer/opensource.
  16. *
  17. *    The Original Code and all software distributed under the License are
  18. *    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  19. *    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
  20. *    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
  21. *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
  22. *    NON-INFRINGEMENT. Please see the License for the specific language
  23. *    governing rights and limitations under the License.
  24. *
  25. *  ========================================================================
  26. *
  27. * Description:  Platform independent fclose() implementation.
  28. *
  29. ****************************************************************************/
  30.  
  31.  
  32. #include "variety.h"
  33. #include <stdio.h>
  34. #include <unistd.h>
  35. #include <errno.h>
  36. #include "liballoc.h"
  37. #include "mf.h"
  38. #include <string.h>
  39. #include "fileacc.h"
  40. #include "tmpfname.h"
  41. #include "rtdata.h"
  42. #include "lseek.h"
  43. #include "streamio.h"
  44. #include "close.h"
  45. #include "flush.h"
  46.  
  47. #ifdef DLHEAP
  48.  
  49. void* _cdecl dlmalloc(size_t);
  50. void  _cdecl dlfree(void*);
  51. void _cdecl mf_init();
  52.  
  53. #define malloc dlmalloc
  54. #define free dlfree
  55. #define realloc dlrealloc
  56.  
  57. #define lib_free dlfree
  58. #endif
  59.  
  60. #ifndef __UNIX__
  61. void    (*__RmTmpFileFn)( FILE *fp );
  62. #endif
  63.  
  64.  
  65. int __doclose( FILE *fp, int close_handle )
  66. {
  67.     int         ret;
  68.  
  69.     if( fp->_flag == 0 ) {
  70.         return( -1 );                       /* file already closed */
  71.     }
  72.     ret = 0;
  73.     if( fp->_flag & _DIRTY ) {
  74.         ret = __flush( fp );
  75.     }
  76.     _AccessFile( fp );
  77. /*
  78.  *      02-nov-92 G.Turcotte  Syncronize buffer pointer with the file pointer
  79.  *                        IEEE Std 1003.1-1988 B.8.2.3.2
  80.  *      03-nov-03 B.Oldeman Inlined ftell; we already know the buffer isn't
  81.  *                dirty (because of the flush), so only a "get" applies
  82.  */
  83.     if( fp->_cnt != 0 ) {                   /* if something in buffer */
  84.         __lseek( fileno( fp ), -fp->_cnt, SEEK_CUR );
  85.     }
  86.  
  87.     if( close_handle ) {
  88. #if defined( __UNIX__ ) || defined( __NETWARE__ )
  89.         // we don't get to implement the close function on these systems
  90.         ret |= close( fileno( fp ) );
  91. #else
  92.         ret |= __close( fileno( fp ) );
  93. #endif
  94.     }
  95.     if( fp->_flag & _BIGBUF ) {     /* if we allocated the buffer */
  96.         lib_free( _FP_BASE(fp) );
  97.         _FP_BASE(fp) = NULL;
  98.     }
  99. #ifndef __UNIX__
  100.     /* this never happens under UNIX */
  101.     if( fp->_flag & _TMPFIL ) {     /* if this is a temporary file */
  102.         __RmTmpFileFn( fp );
  103.     }
  104. #endif
  105.     _ReleaseFile( fp );
  106.     return( ret );
  107. }
  108.  
  109. int __shutdown_stream( FILE *fp, int close_handle )
  110. {
  111.     int         ret;
  112.  
  113.     ret = __doclose( fp, close_handle );
  114.     __freefp( fp );
  115.     return( ret );
  116. }
  117.  
  118. _WCRTLINK int fclose( FILE *fp )
  119. {
  120.     __stream_link       *link;
  121.  
  122.     _AccessIOB();
  123.     link = _RWD_ostream;
  124.     for( ;; ) {
  125.         if( link == NULL ) {
  126.             _ReleaseIOB();
  127.             return( -1 );     /* file not open */
  128.         }
  129.         if( link->stream == fp ) break;
  130.         link = link->next;
  131.     }
  132.     _ReleaseIOB();
  133.     return( __shutdown_stream( fp, 1 ) );
  134. }
  135.