/* f4flush.c   (c)Copyright Sequiter Software Inc., 1990-1994.  All rights reserved. */

#include "d4all.h"

#ifdef S4WIN32
#include <windows.h>
#endif

#ifndef S4UNIX
#ifdef __TURBOC__
#pragma hdrstop
#endif
#endif

#ifdef __WATCOMC__
#ifdef M_I386
#define S4USE_EBX
#endif
#endif

#ifdef S4OS2
#define S4USE_DUP
#else
#ifdef S4UNIX
#ifndef S4MACINTOSH
#define S4USE_DUP
#endif
#else
#ifndef S4WIN32
#include <dos.h>
#endif
#endif
#endif

/* returns 1 if no handles available */
int S4FUNCTION file4low_flush( FILE4 *file, int do_free )
{
  int rc ;
#ifndef S4OPTIMIZE_OFF
  long real_len ;
#endif

#ifdef S4USE_DUP
  int handle ;
#else
#ifndef S4MACINTOSH
#ifndef S4WIN32
  union REGS dos_flush ;
#endif
#endif
#endif

#ifdef S4MACINTOSH
  short vRefNum ;
#endif

#ifdef S4DEBUG
  if ( file == 0 || do_free < 0 || do_free > 1 )
    e4severe( e4parm, E4_F4LOW_FLUSH ) ;
#endif

#ifndef S4OPTIMIZE_OFF
  if ( file->do_buffer == 1 )
  {
    rc = opt4file_flush_list( file, opt4file_find_list( file ), do_free ) ;
    if ( file->code_base->opt.write_file == file )  /* flush out the final file changes */
      if ( opt4flush_write_buffer( &file->code_base->opt ) < 0 )
        rc = -1 ;
    if ( file->len != -1 )
    {
      file->do_buffer = 0 ;
      real_len = file->len ;
      if ( file->file_created == 0 )
      {
#ifdef S4DEBUG
        if ( real_len != 0 )
          e4severe( e4opt, "file4low_flush() uncreate file attempted flush" ) ;
#endif
      }
      else
        if ( file4len( file ) != real_len )   /* make sure the length is updated */
          file4len_set( file, real_len ) ;
      file->do_buffer = 1 ;
    }
    if ( rc )
      return rc ;
  }
#endif

  if ( file->hand < 0 )  /* file not open */
    return 0 ;

#ifdef S4USE_DUP
#ifndef S4NO_DUP
  handle = dup( file->hand ) ;
  if ( handle < 0 )  /* means that there are no available handles */
    return e4( file->code_base, e4opt_flush, 0 ) ;
  if ( close( handle ) < 0 )
    return e4( file->code_base, e4opt_flush, 0 ) ;
#endif
#else
#ifdef S4MACINTOSH
  if (GetVRefNum( file->hand, &vRefNum ) != 0 )
    return e4( file->code_base, e4opt_flush, 0 ) ;

  if (FlushVol( 0, vRefNum ) != 0 )
    return e4( file->code_base, e4opt_flush, 0 ) ;
#else
#ifdef S4WIN32
  if ( !FlushFileBuffers( (HANDLE) file->hand ) )
    return e4( file->code_base, e4opt_flush, 0 ) ;
#else
  dos_flush.h.ah = 0x68 ;
#ifdef S4USE_EBX
  dos_flush.x.ebx = file->hand ;
#else
  dos_flush.x.bx = file->hand ;
#endif
  intdos( &dos_flush, &dos_flush ) ;
  if ( dos_flush.x.cflag != 0 )
    return e4( file->code_base, e4opt_flush, 0 ) ;
#endif
#endif
#endif

  return 0 ;
}

int S4FUNCTION file4flush( FILE4 *file )
{
  int rc, err ;

  err = e4set( file->code_base, 0 ) ;
#ifdef S4DEBUG
  if ( file == 0 )
    e4severe( e4parm, E4_F4FLUSH ) ;
#endif
  rc = file4low_flush( file, 0 ) ;
  if ( err != 0 )
    e4set( file->code_base, err ) ;

  return rc ;
}

int S4FUNCTION file4refresh( FILE4 *file )
{
#ifndef S4OPTIMIZE_OFF
#ifndef S4SINGLE
  int rc ;
#ifdef S4DEBUG
  if ( file == 0 )
    e4severe( e4parm, E4_F4REFRESH ) ;
#endif

  if ( file->do_buffer == 0 || file->is_exclusive == 1 || file->is_read_only == 1 )
    return 0 ;

  rc = file4low_flush( file, 1 ) ;
  if ( rc < 0 )
    return rc ;
  file->len = -1 ;
#endif
#endif
  return 0 ;
}


