Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.4 KiB

  1. /***
  2. *commit.c - flush buffer to disk
  3. *
  4. * Copyright (c) 1990-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * contains _commit() - flush buffer to disk
  8. *
  9. *Revision History:
  10. * 05-25-90 SBM initial version
  11. * 07-24-90 SBM Removed '32' from API names
  12. * 09-28-90 GJF New-style function declarator.
  13. * 12-03-90 GJF Appended Win32 version onto the source with #ifdef-s.
  14. * It is close enough to the Cruiser version that it
  15. * should be more closely merged with it later on (much
  16. * later on).
  17. * 12-04-90 SRW Changed to include <oscalls.h> instead of <doscalls.h>
  18. * 12-06-90 SRW Changed to use _osfile and _osfhnd instead of _osfinfo
  19. * 02-07-91 SRW Changed to call _get_osfhandle [_WIN32_]
  20. * 04-09-91 PNT Added _MAC_ conditional
  21. * 02-13-92 GJF Replaced _nfile by _nhandle for Win32.
  22. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  23. * 09-06-94 CFW Remove Cruiser support.
  24. * 01-04-95 GJF _WIN32_ -> _WIN32
  25. * 02-15-95 GJF Appended Mac version of source file (somewhat cleaned
  26. * up), with appropriate #ifdef-s.
  27. * 06-11-95 GJF Replaced _osfile[] with __osfile() (macro referencing
  28. * field in ioinfo struct).
  29. * 06-26-95 GJF Added initial check that the file handle is open.
  30. * 07-08-96 GJF Replaced defined(_WIN32) with !defined(_MAC) and
  31. * defined(_M_M68K) || defined(_M_MPPC) with
  32. * defined(_MAC). Removed REG1 and REG2 (old register
  33. * macros). Replaced oscalls.h with windows.h. Also,
  34. * detab-ed and cleaned up the format a bit.
  35. * 12-17-97 GJF Exception-safe locking.
  36. * 05-17-99 PML Remove all Macintosh support.
  37. *
  38. *******************************************************************************/
  39. #include <cruntime.h>
  40. #include <windows.h>
  41. #include <errno.h>
  42. #include <io.h>
  43. #include <internal.h>
  44. #include <msdos.h> /* for FOPEN */
  45. #include <mtdll.h>
  46. #include <stdlib.h> /* for _doserrno */
  47. /***
  48. *int _commit(filedes) - flush buffer to disk
  49. *
  50. *Purpose:
  51. * Flushes cache buffers for the specified file handle to disk
  52. *
  53. *Entry:
  54. * int filedes - file handle of file
  55. /*
  56. *Exit:
  57. * returns success code
  58. *
  59. *Exceptions:
  60. *
  61. *******************************************************************************/
  62. int __cdecl _commit (
  63. int filedes
  64. )
  65. {
  66. int retval;
  67. /* if filedes out of range, complain */
  68. if ( ((unsigned)filedes >= (unsigned)_nhandle) ||
  69. !(_osfile(filedes) & FOPEN) )
  70. {
  71. errno = EBADF;
  72. return (-1);
  73. }
  74. #ifdef _MT
  75. _lock_fh(filedes);
  76. __try {
  77. if (_osfile(filedes) & FOPEN) {
  78. #endif /* _MT */
  79. if ( !FlushFileBuffers((HANDLE)_get_osfhandle(filedes)) ) {
  80. retval = GetLastError();
  81. }
  82. else {
  83. retval = 0; /* return success */
  84. }
  85. /* map the OS return code to C errno value and return code */
  86. if (retval == 0)
  87. goto good;
  88. _doserrno = retval;
  89. #ifdef _MT
  90. }
  91. #endif
  92. errno = EBADF;
  93. retval = -1;
  94. good :
  95. #ifdef _MT
  96. ; }
  97. __finally {
  98. _unlock_fh(filedes);
  99. }
  100. #endif /* _MT */
  101. return (retval);
  102. }