home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / mag&info / msjv7_4.zip / OPEN.ARJ / COMMIT.C < prev    next >
Text File  |  1992-07-01  |  3KB  |  88 lines

  1. /*
  2.       Copyright 1991 by David Thielen, All Rights Reserved.
  3.  
  4.       This code example is from a commercial product and has restricted
  5.       rights.  This code, or any code derived from this code may be
  6.       incorporated into any programs with the following restrictions;
  7.       1) It cannot be sold as source code, and 2) It cannot be sold in a
  8.       product which provides this code as an API.
  9. */
  10.  
  11.  
  12. #include "file_io.h"
  13.  
  14.  
  15. // Keep in mind that a disk caching program may ignore the below so
  16. // that while the file is flushed from DOS, it may still be in the
  17. // buffers of the cache.  Unfortunately, there is no way around this.
  18.  
  19. // fOpenCommit is true if the file was opened with the commit bit set.
  20.  
  21. unsigned FileCommit (int hFil,int fOpenCommit)
  22. {
  23. int hTmp;
  24. unsigned uRtn=0;
  25.  
  26.    // If we opened with commit & we are on DOS 4.0+, we don't need to
  27.    // do anything.
  28.    // OS/2 is DOS version 10 - we treat that like DOS 3.3
  29.    if ((fOpenCommit) && (DosMajVer >= 4) && (DosMajVer != 10))
  30.       return (0);
  31.  
  32.    // If we are on version 3.3+, we can use file commit
  33.    if ((DosMajVer > 3) || ((DosMajVer == 3) && (DosMinVer >= 3)))
  34.       {
  35.       _asm
  36.          {
  37.          mov      ah, 68h
  38.          mov      bx, [hFil]
  39.          int      21h
  40.          jnc      Ok
  41.          mov      [uRtn], ax
  42. Ok:
  43.          }
  44.       return (uRtn);
  45.       }
  46.  
  47.    // We now have 3 options.  The first is to create a dup handle & then
  48.    // close it.  However, this will fail if there are no available
  49.    // handles.  The second option is to close the file & then re-open it
  50.    // but that requires we store the file name - which we will only need
  51.    // for DOS < 3.3 AND no available handles.  Finally, we can reset the
  52.    // disk which will flush all buffers but won't update the directory
  53.    // entry (which means the size & date of the file MAY be wrong).
  54.  
  55.    // If you are VERY concerned about this, there are 3 changes you can
  56.    // make in this code.  1) You can require DOS 3.3 or greater.  2) You
  57.    // can store the FULL name of the file.  3) You could close handle 0
  58.    // (which is generally CON) and then perform the dup/close, then re-open
  59.    // CON.  Be careful on the final choice that you re-open whatever was
  60.    // truly on handle 0 - which in the case of re-directed I/O, may be
  61.    // a file.
  62.  
  63.    // Lets try a dup
  64.    if ((hTmp = FileDup (hFil)) >= 0)
  65.       {
  66.       // ok close the dup & return
  67.       uRtn = FileClose (hTmp);
  68.  
  69.       return (uRtn);
  70.       }
  71.  
  72.    // We're fucked - all we can do is flush the buffers
  73.    // The directory entry WON'T be updated (which is only a problem
  74.    // if the file has grown since it was last closed/flushed).
  75.    // If you are using a file for transaction processing & it has a
  76.    // maximum size that's reasonable, open the file, write 0 bytes at
  77.    // the limit to size it, close & re-open it & then the below flush
  78.    // will always be sufficient.
  79.    _asm
  80.       {
  81.       mov      ah, 0Dh
  82.       int      21h
  83.       }
  84.  
  85.    return (0);
  86. }
  87.  
  88.