Subversion Repositories Kolibri OS

Rev

Rev 359 | 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.  
  48. #ifndef __UNIX__
  49. void    (*__RmTmpFileFn)( FILE *fp );
  50. #endif
  51.  
  52.  
  53. int __doclose( FILE *fp, int close_handle )
  54. {
  55.     int         ret;
  56.  
  57.     if( fp->_flag == 0 ) {
  58.         return( -1 );                       /* file already closed */
  59.     }
  60.     ret = 0;
  61.     if( fp->_flag & _DIRTY ) {
  62.         ret = __flush( fp );
  63.     }
  64.     _AccessFile( fp );
  65. /*
  66.  *      02-nov-92 G.Turcotte  Syncronize buffer pointer with the file pointer
  67.  *                        IEEE Std 1003.1-1988 B.8.2.3.2
  68.  *      03-nov-03 B.Oldeman Inlined ftell; we already know the buffer isn't
  69.  *                dirty (because of the flush), so only a "get" applies
  70.  */
  71.     if( fp->_cnt != 0 ) {                   /* if something in buffer */
  72.         __lseek( fileno( fp ), -fp->_cnt, SEEK_CUR );
  73.     }
  74.  
  75.     if( close_handle ) {
  76. #if defined( __UNIX__ ) || defined( __NETWARE__ )
  77.         // we don't get to implement the close function on these systems
  78.         ret |= close( fileno( fp ) );
  79. #else
  80.         ret |= __close( fileno( fp ) );
  81. #endif
  82.     }
  83.     if( fp->_flag & _BIGBUF ) {     /* if we allocated the buffer */
  84.         lib_free( _FP_BASE(fp) );
  85.         _FP_BASE(fp) = NULL;
  86.     }
  87. #ifndef __UNIX__
  88.     /* this never happens under UNIX */
  89.     if( fp->_flag & _TMPFIL ) {     /* if this is a temporary file */
  90.         __RmTmpFileFn( fp );
  91.     }
  92. #endif
  93.     _ReleaseFile( fp );
  94.     return( ret );
  95. }
  96.  
  97. int __shutdown_stream( FILE *fp, int close_handle )
  98. {
  99.     int         ret;
  100.  
  101.     ret = __doclose( fp, close_handle );
  102.     __freefp( fp );
  103.     return( ret );
  104. }
  105.  
  106. _WCRTLINK int fclose( FILE *fp )
  107. {
  108.     __stream_link       *link;
  109.  
  110.     _AccessIOB();
  111.     link = _RWD_ostream;
  112.     for( ;; ) {
  113.         if( link == NULL ) {
  114.             _ReleaseIOB();
  115.             return( -1 );     /* file not open */
  116.         }
  117.         if( link->stream == fp ) break;
  118.         link = link->next;
  119.     }
  120.     _ReleaseIOB();
  121.     return( __shutdown_stream( fp, 1 ) );
  122. }
  123.