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

  1. /***
  2. *rmtmp.c - remove temporary files created by tmpfile.
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *
  8. *******************************************************************************/
  9.  
  10. #include <cruntime.h>
  11. #include <stdio.h>
  12. #include <file2.h>
  13. #include <internal.h>
  14. #include <mtdll.h>
  15.  
  16.  
  17. #ifdef _MSC_VER
  18.  
  19. #pragma data_seg(".CRT$XPX")
  20.  
  21. #ifdef _WIN32
  22.  
  23. static _PVFV pterm = _rmtmp;
  24.  
  25. #else  /* _WIN32 */
  26. #if defined (_M_MPPC) || defined (_M_M68K)
  27.  
  28. static _PVFV __prmtmp = _rmtmp;
  29.  
  30. #endif  /* defined (_M_MPPC) || defined (_M_M68K) */
  31. #endif  /* _WIN32 */
  32.  
  33. #pragma data_seg()
  34.  
  35. #endif  /* _MSC_VER */
  36.  
  37. #ifdef _WIN32
  38.  
  39. /*
  40.  * Definitions for _tmpoff, _tempoff and _old_pfxlen. These will cause this
  41.  * module to be linked in whenever the termination code needs it.
  42.  */
  43. #ifndef CRTDLL
  44. unsigned _tmpoff = 1;
  45. #endif  /* CRTDLL */
  46.  
  47. unsigned _tempoff = 1;
  48. unsigned _old_pfxlen = 0;
  49.  
  50.  
  51. #else  /* _WIN32 */
  52. #if defined (_M_MPPC) || defined (_M_M68K)
  53.  
  54. /*
  55.  * Definitions for _tmpoff, _tempoff and _old_pfxlen. These will cause this
  56.  * module to be linked in whenever the termination code needs it.
  57.  */
  58. unsigned int _tmpoff = 1;
  59. unsigned int _tempoff = 1;
  60. unsigned int _old_pfxlen = 0;
  61.  
  62. /*
  63.  * Define _prmtmp, the function pointer used in the termination code.
  64.  */
  65. void (__cdecl * _prmtmp)(void) = _rmtmp;
  66.  
  67. #endif  /* defined (_M_MPPC) || defined (_M_M68K) */
  68. #endif  /* _WIN32 */
  69.  
  70.  
  71. /***
  72. *int _rmtmp() - closes and removes temp files created by tmpfile
  73. *
  74. *Purpose:
  75. *       closes and deletes all open files that were created by tmpfile.
  76. *
  77. *Entry:
  78. *       None.
  79. *
  80. *Exit:
  81. *       returns number of streams closed
  82. *
  83. *Exceptions:
  84. *
  85. *******************************************************************************/
  86.  
  87. int __cdecl _rmtmp (
  88.         void
  89.         )
  90. {
  91.         REG2 int count = 0;
  92.  
  93. #ifdef _WIN32
  94.  
  95.         REG1 int i;
  96.  
  97.         _mlock(_IOB_SCAN_LOCK);
  98.  
  99.         for ( i = 0 ; i < _nstream ; i++)
  100.  
  101.                 if ( __piob[i] != NULL ) {
  102.  
  103.                         _lock_str2(i, __piob[i]);
  104.  
  105.                         if ( inuse( (FILE *)__piob[i] ) &&
  106.                              (((FILE *)__piob[i])->_tmpfname != NULL) )
  107.                         {
  108.                                 _fclose_lk( __piob[i] );
  109.                                 count++;
  110.                         }
  111.  
  112.                         _unlock_str2(i, __piob[i]);
  113.                 }
  114.  
  115.         _munlock(_IOB_SCAN_LOCK);
  116.  
  117. #else  /* _WIN32 */
  118. #if defined (_M_MPPC) || defined (_M_M68K)
  119.  
  120.         REG1 FILE *stream = _iob;
  121.  
  122.         for (; stream <= _lastiob; stream++) {
  123.  
  124.                 if (inuse(stream) && (stream->_tmpfname != NULL) ) {
  125.                         fclose(stream);
  126.                         count++;
  127.                 }
  128.         }
  129.  
  130. #endif  /* defined (_M_MPPC) || defined (_M_M68K) */
  131. #endif  /* _WIN32 */
  132.  
  133.         return(count);
  134. }
  135.