Leaked source code of windows server 2003
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.

146 lines
5.1 KiB

  1. /***
  2. *rmtmp.c - remove temporary files created by tmpfile.
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *
  8. *Revision History:
  9. * 09-15-83 TC initial version
  10. * 11-02-87 JCR Multi-thread support
  11. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  12. * 05-27-88 PHG Merged normal and DLL versions
  13. * 06-10-88 JCR Use near pointer to reference _iob[] entries
  14. * 08-18-89 GJF Clean up, now specific to OS/2 2.0 (i.e., 386 flat
  15. * model). Also fixed copyright and indents.
  16. * 02-15-90 GJF Fixed copyright
  17. * 03-19-90 GJF Made calling type _CALLTYPE1, added #include
  18. * <cruntime.h> and removed #include <register.h>.
  19. * 10-03-90 GJF New-style function declarator.
  20. * 01-21-91 GJF ANSI naming.
  21. * 07-30-91 GJF Added support for termination scheme used on
  22. * non-Cruiser targets [_WIN32_].
  23. * 03-11-92 GJF Replaced _tmpnum(stream) with stream->_tmpfname for
  24. * Win32.
  25. * 03-17-92 GJF Got rid of definition of _tmpoff.
  26. * 03-31-92 GJF Merged with Stevesa's changes.
  27. * 04-16-92 GJF Merged with Darekm's changes.
  28. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  29. * 10-29-93 GJF Define entry for termination section (used to be in
  30. * i386\cinittmp.asm). Also, replaced MTHREAD with _MT
  31. * and deleted old Cruiser support.
  32. * 04-04-94 GJF #ifdef-ed out definition _tmpoff for msvcrt*.dll, it
  33. * is unnecessary. Made definitions of _tempoff and
  34. * _old_pfxlen conditional on ndef DLL_FOR_WIN32S.
  35. * 02-20-95 GJF Merged in Mac version.
  36. * 03-07-95 GJF Converted to walk the __piob[] table (rather than
  37. * the _iob[] table).
  38. * 03-16-95 GJF Must be sure __piob[i]!=NULL before trying to lock it!
  39. * 03-28-95 SKS Fix declaration of _prmtmp (__cdecl goes BEFORE the *)
  40. * 08-01-96 RDK For PMac, change termination pointer data type to static.
  41. * 03-02-98 GJF Exception-safe locking.
  42. * 04-28-99 PML Wrap __declspec(allocate()) in _CRTALLOC macro.
  43. * 05-13-99 PML Remove Win32s
  44. * 05-17-99 PML Remove all Macintosh support.
  45. * 02-19-01 PML Avoid allocating unnecessary locks in _rmtmp, part of
  46. * vs7#172586.
  47. *
  48. *******************************************************************************/
  49. #include <sect_attribs.h>
  50. #include <cruntime.h>
  51. #include <stdio.h>
  52. #include <file2.h>
  53. #include <internal.h>
  54. #include <mtdll.h>
  55. #pragma data_seg(".CRT$XPX")
  56. _CRTALLOC(".CRT$XPX") static _PVFV pterm = _rmtmp;
  57. #pragma data_seg()
  58. /*
  59. * Definitions for _tmpoff, _tempoff and _old_pfxlen. These will cause this
  60. * module to be linked in whenever the termination code needs it.
  61. */
  62. #ifndef CRTDLL
  63. unsigned _tmpoff = 1;
  64. #endif /* CRTDLL */
  65. unsigned _tempoff = 1;
  66. unsigned _old_pfxlen = 0;
  67. /***
  68. *int _rmtmp() - closes and removes temp files created by tmpfile
  69. *
  70. *Purpose:
  71. * closes and deletes all open files that were created by tmpfile.
  72. *
  73. *Entry:
  74. * None.
  75. *
  76. *Exit:
  77. * returns number of streams closed
  78. *
  79. *Exceptions:
  80. *
  81. *******************************************************************************/
  82. int __cdecl _rmtmp (
  83. void
  84. )
  85. {
  86. REG2 int count = 0;
  87. REG1 int i;
  88. #ifdef _MT
  89. _mlock(_IOB_SCAN_LOCK);
  90. __try {
  91. #endif
  92. for ( i = 0 ; i < _nstream ; i++)
  93. if ( __piob[i] != NULL && inuse( (FILE *)__piob[i] )) {
  94. #ifdef _MT
  95. /*
  96. * lock the stream. this is not done until testing
  97. * the stream is in use to avoid unnecessarily creating
  98. * a lock for every stream. the price is having to
  99. * retest the stream after the lock has been asserted.
  100. */
  101. _lock_str2(i, __piob[i]);
  102. __try {
  103. /*
  104. * if the stream is STILL in use (it may have
  105. * been closed before the lock was asserted),
  106. * see about flushing it.
  107. */
  108. if ( inuse( (FILE *)__piob[i] )) {
  109. #endif
  110. if ( ((FILE *)__piob[i])->_tmpfname != NULL )
  111. {
  112. _fclose_lk( __piob[i] );
  113. count++;
  114. }
  115. #ifdef _MT
  116. }
  117. }
  118. __finally {
  119. _unlock_str2(i, __piob[i]);
  120. }
  121. #endif
  122. }
  123. #ifdef _MT
  124. }
  125. __finally {
  126. _munlock(_IOB_SCAN_LOCK);
  127. }
  128. #endif
  129. return(count);
  130. }