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.

193 lines
5.6 KiB

  1. /***
  2. *fclose.c - close a file
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines fclose() - close an open file
  8. *
  9. *Revision History:
  10. * 09-02-83 RN initial version
  11. * 05-14-87 SKS error return from fflush must not be clobbered
  12. * 08-10-87 JCR Added code to support P_tmpdir with or without trailing '\'
  13. * 11-01-87 JCR Multi-thread support
  14. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  15. * 01-13-88 JCR Removed unnecessary calls to mthread fileno/feof/ferror
  16. * 05-31-88 PHG Merged DLL and normal versions
  17. * 06-14-88 JCR Use near pointer to reference _iob[] entries
  18. * 08-24-88 GJF Don't use FP_OFF() macro for the 386
  19. * 08-17-89 GJF Clean up, now specific to OS/2 2.0 (i.e., 386 flat
  20. * model). Also fixed copyright and indents.
  21. * 02-15-90 GJF Fixed copyright
  22. * 03-16-90 GJF Made calling type _CALLTYPE1, added #include
  23. * <cruntime.h> and removed #include <register.h>.
  24. * 05-29-90 SBM Use _flush, not [_]fflush[_lk]
  25. * 07-25-90 SBM Replaced <assertm.h> by <assert.h>
  26. * 10-02-90 GJF New-style function declarator.
  27. * 01-21-91 GJF ANSI naming.
  28. * 03-11-92 GJF For Win32, revised temporary file cleanup.
  29. * 03-25-92 DJM POSIX support.
  30. * 08-26-92 GJF Include unistd.h for POSIX build.
  31. * 01-13-93 GJF Don't need/want to remove() tmp files on Windows NT
  32. * (file is removed by the OS when handle is closed).
  33. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  34. * 09-06-94 CFW Remove Cruiser support.
  35. * 09-06-94 CFW Replace MTHREAD with _MT.
  36. * 01-10-95 CFW Debug CRT allocs.
  37. * 02-06-94 CFW assert -> _ASSERTE.
  38. * 02-17-95 GJF Merged in Mac version. Removed some useless #ifdef-s.
  39. * 03-07-95 GJF _[un]lock_str macros now take FILE * arg.
  40. * 05-12-95 CFW Paranoia: set _tmpfname field to NULL.
  41. * 02-25-98 GJF Exception-safe locking.
  42. * 05-17-99 PML Remove all Macintosh support.
  43. *
  44. *******************************************************************************/
  45. #include <cruntime.h>
  46. #ifdef _POSIX_
  47. #include <unistd.h>
  48. #endif
  49. #include <stdio.h>
  50. #include <file2.h>
  51. #include <string.h>
  52. #include <io.h>
  53. #include <stdlib.h>
  54. #include <internal.h>
  55. #include <mtdll.h>
  56. #include <dbgint.h>
  57. /***
  58. *int fclose(stream) - close a stream
  59. *
  60. *Purpose:
  61. * Flushes and closes a stream and frees any buffer associated
  62. * with that stream, unless it was set with setbuf.
  63. *
  64. *Entry:
  65. * FILE *stream - stream to close
  66. *
  67. *Exit:
  68. * returns 0 if OK, EOF if fails (can't _flush, not a FILE, not open, etc.)
  69. * closes the file -- affecting FILE structure
  70. *
  71. *Exceptions:
  72. *
  73. *******************************************************************************/
  74. #ifdef _MT /* multi-thread; define both fclose and _fclose_lk */
  75. int __cdecl fclose (
  76. FILE *stream
  77. )
  78. {
  79. int result = EOF;
  80. _ASSERTE(stream != NULL);
  81. /* If stream is a string, simply clear flag and return EOF */
  82. if (stream->_flag & _IOSTRG)
  83. stream->_flag = 0; /* IS THIS REALLY NEEDED ??? */
  84. /* Stream is a real file. */
  85. else {
  86. _lock_str(stream);
  87. __try {
  88. result = _fclose_lk(stream);
  89. }
  90. __finally {
  91. _unlock_str(stream);
  92. }
  93. }
  94. return(result);
  95. }
  96. /***
  97. *int _fclose_lk() - close a stream (lock already held)
  98. *
  99. *Purpose:
  100. * Core fclose() routine; assumes caller has stream lock held.
  101. *
  102. * [See fclose() above for more information.]
  103. *
  104. *Entry:
  105. *
  106. *Exit:
  107. *
  108. *Exceptions:
  109. *
  110. *******************************************************************************/
  111. int __cdecl _fclose_lk (
  112. FILE *str
  113. )
  114. {
  115. REG1 FILE *stream;
  116. REG2 int result = EOF;
  117. /* Init near stream pointer */
  118. stream = str;
  119. #else /* non multi-thread; just define fclose() */
  120. int __cdecl fclose (
  121. FILE *str
  122. )
  123. {
  124. REG1 FILE *stream;
  125. REG2 int result = EOF;
  126. /* Init near stream pointer */
  127. stream = str;
  128. if (stream->_flag & _IOSTRG) {
  129. stream->_flag = 0;
  130. return(EOF);
  131. }
  132. #endif
  133. _ASSERTE(str != NULL);
  134. if (inuse(stream)) {
  135. /* Stream is in use:
  136. (1) flush stream
  137. (2) free the buffer
  138. (3) close the file
  139. (4) delete the file if temporary
  140. */
  141. result = _flush(stream);
  142. _freebuf(stream);
  143. #ifdef _POSIX_
  144. if (close(fileno(stream)) <0)
  145. #else
  146. if (_close(_fileno(stream)) < 0)
  147. #endif
  148. result = EOF;
  149. else if ( stream->_tmpfname != NULL ) {
  150. /*
  151. * temporary file (i.e., one created by tmpfile()
  152. * call). delete, if necessary (don't have to on
  153. * Windows NT because it was done by the system when
  154. * the handle was closed). also, free up the heap
  155. * block holding the pathname.
  156. */
  157. #ifdef _POSIX_
  158. if ( unlink(stream->_tmpfname) )
  159. result = EOF;
  160. #endif
  161. _free_crt(stream->_tmpfname);
  162. stream->_tmpfname = NULL;
  163. }
  164. }
  165. stream->_flag = 0;
  166. return(result);
  167. }