home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / commit.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  4KB  |  155 lines

  1. /***
  2. *commit.c - flush buffer to disk
  3. *
  4. *       Copyright (c) 1990-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       contains _commit() - flush buffer to disk
  8. *
  9. *******************************************************************************/
  10.  
  11. #ifndef _MAC
  12.  
  13.  
  14. #include <cruntime.h>
  15. #include <windows.h>
  16. #include <errno.h>
  17. #include <io.h>
  18. #include <internal.h>
  19. #include <msdos.h>      /* for FOPEN */
  20. #include <mtdll.h>
  21. #include <stdlib.h>     /* for _doserrno */
  22.  
  23. /***
  24. *int _commit(filedes) - flush buffer to disk
  25. *
  26. *Purpose:
  27. *       Flushes cache buffers for the specified file handle to disk
  28. *
  29. *Entry:
  30. *       int filedes - file handle of file
  31. /*
  32. *Exit:
  33. *       returns success code
  34. *
  35. *Exceptions:
  36. *
  37. *******************************************************************************/
  38.  
  39. int __cdecl _commit (
  40.         int filedes
  41.         )
  42. {
  43.         int retval;
  44.  
  45.         /* if filedes out of range, complain */
  46.         if ( ((unsigned)filedes >= (unsigned)_nhandle) ||
  47.              !(_osfile(filedes) & FOPEN) )
  48.         {
  49.                 errno = EBADF;
  50.                 return (-1);
  51.         }
  52.  
  53.         _lock_fh(filedes);
  54.  
  55.         /* if filedes open, try to commit, else fall through to bad */
  56.         if (_osfile(filedes) & FOPEN) {
  57.  
  58.                 if ( !FlushFileBuffers((HANDLE)_get_osfhandle(filedes)) ) {
  59.                         retval = GetLastError();
  60.                 } else {
  61.                         retval = 0;     /* return success */
  62.                 }
  63.  
  64.                 /* map the OS return code to C errno value and return code */
  65.                 if (retval == 0) {
  66.                         goto good;
  67.                 } else {
  68.                         _doserrno = retval;
  69.                         goto bad;
  70.                 }
  71.  
  72.         }
  73.  
  74. bad :
  75.         errno = EBADF;
  76.         retval = -1;
  77. good :
  78.         _unlock_fh(filedes);
  79.         return (retval);
  80. }
  81.  
  82.  
  83. #else  /* _MAC */
  84.  
  85.  
  86. #include <cruntime.h>
  87. #include <errno.h>
  88. #include <io.h>
  89. #include <internal.h>
  90. #include <memory.h>
  91. #include <msdos.h>      /* for FOPEN */
  92. #include <stdlib.h>     /* for _doserrno */
  93. #include <macos\files.h>
  94. #include <macos\errors.h>
  95.  
  96. /***
  97. *int _commit(fh) - flush buffer to disk
  98. *
  99. *Purpose:
  100. *       Flushes cache buffers for the specified file handle to disk
  101. *
  102. *Entry:
  103. *       int filedes - file handle of file
  104. /*
  105. *Exit:
  106. *       returns success code
  107. *
  108. *Exceptions:
  109. *
  110. *******************************************************************************/
  111.  
  112. int __cdecl _commit (
  113.         int fh
  114.         )
  115. {
  116.         ParamBlockRec parm;
  117.         OSErr osErr = 0;
  118.  
  119.         if ((unsigned)fh >= (unsigned)_nfile || !(_osfile[fh] & FOPEN))
  120.         {
  121.                 /* out of range -- return error */
  122.                 errno = EBADF;
  123.                 _macerrno = 0;
  124.                 return -1;
  125.         }
  126.  
  127.         if (!(_osfile[fh] & FDEV))
  128.         {
  129.                 memset(&parm, 0, sizeof(ParamBlockRec));
  130.                 parm.ioParam.ioRefNum = _osfhnd[fh];
  131.                 osErr = PBFlushFileSync(&parm);
  132.                 switch (osErr)
  133.                 {
  134.                         case noErr:
  135.                                 memset(&parm, 0, sizeof(ParamBlockRec));
  136.                                 parm.ioParam.ioVRefNum = _osVRefNum[fh];
  137.                                 osErr =  PBFlushVolSync(&parm);
  138.                                 if (osErr)
  139.                                 {
  140.                                         _dosmaperr(osErr);
  141.                                         return -1;
  142.                                 }
  143.                                 return 0;
  144.  
  145.                         default:
  146.                                 errno = EIO;
  147.                                 return -1;
  148.                 }
  149.         }
  150.         return 0;
  151. }
  152.  
  153.  
  154. #endif  /* _MAC */
  155.