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

  1. /***
  2. *closeall.c - close all open files
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines _fcloseall() - closes all open files except stdin, stdout
  8. *       stdprn, stderr, and stdaux.
  9. *
  10. *******************************************************************************/
  11.  
  12. #include <cruntime.h>
  13. #ifdef _WIN32
  14. #include <windows.h>
  15. #endif  /* _WIN32 */
  16. #include <stdio.h>
  17. #include <file2.h>
  18. #include <internal.h>
  19. #include <malloc.h>
  20. #include <mtdll.h>
  21. #include <dbgint.h>
  22.  
  23.  
  24. /***
  25. *int _fcloseall() - close all open streams
  26. *
  27. *Purpose:
  28. *       Closes all streams currently open except for stdin/out/err/aux/prn.
  29. *       tmpfile() files are among those closed.
  30. *
  31. *Entry:
  32. *       None.
  33. *
  34. *Exit:
  35. *       returns number of streams closed if OK
  36. *       returns EOF if fails.
  37. *
  38. *Exceptions:
  39. *
  40. *******************************************************************************/
  41.  
  42. int __cdecl _fcloseall (
  43.         void
  44.         )
  45. {
  46.         REG2 int count = 0;
  47.  
  48. #ifdef _WIN32
  49.  
  50.         REG1 i;
  51.  
  52.         _mlock(_IOB_SCAN_LOCK);
  53.  
  54.         for ( i = 3 ; i < _nstream ; i++ ) {
  55.  
  56.             if ( __piob[i] != NULL ) {
  57.                 /*
  58.                  * if the stream is in use, close it
  59.                  */
  60.                 if ( inuse( (FILE *)__piob[i] ) && (fclose( __piob[i] ) !=
  61.                      EOF) )
  62.                         count++;
  63.  
  64.                 /*
  65.                  * if stream is part of a _FILEX we allocated, free it.
  66.                  */
  67.                 if ( i >= _IOB_ENTRIES ) {
  68.  
  69. #if defined (_MT)
  70.                     DeleteCriticalSection( &(((_FILEX *)__piob[i])->lock) );
  71. #endif  /* defined (_MT) */
  72.                     _free_crt( __piob[i] );
  73.                     __piob[i] = NULL;
  74.                 }
  75.             }
  76.         }
  77.  
  78.         _munlock(_IOB_SCAN_LOCK);
  79.  
  80. #else  /* _WIN32 */
  81. #if defined (_M_MPPC) || defined (_M_M68K)
  82.  
  83.         REG1 FILE *stream = &_iob[3];
  84.  
  85.         for (; stream <= _lastiob; stream++)
  86.                 if (fclose(stream) != EOF)
  87.                         count++;
  88.  
  89. #endif  /* defined (_M_MPPC) || defined (_M_M68K) */
  90. #endif  /* _WIN32 */
  91.  
  92.         return(count);
  93. }
  94.