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.

107 lines
3.0 KiB

  1. /***
  2. *closeall.c - close all open files
  3. *
  4. * Copyright (c) 1985-2001, 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. *Revision History:
  11. * 09-19-83 RN initial version
  12. * 06-26-87 JCR Stream search starts with _iob[3] for OS/2
  13. * 11-02-87 JCR Multi-thread support
  14. * 11-08-87 SKS Changed PROTMODE to OS2
  15. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  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 Added check that OS2 is defined whenever M_I386 is.
  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. * 10-03-90 GJF New-style function declarator.
  25. * 01-21-91 GJF ANSI naming.
  26. * 03-25-92 DJM POSIX support
  27. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  28. * 03-06-95 GJF Converted to walk the __piob[] table (rather than
  29. * the _iob[] table).
  30. * 02-25-98 GJF Exception-safe locking.
  31. * 05-17-99 PML Remove all Macintosh support.
  32. *
  33. *******************************************************************************/
  34. #include <cruntime.h>
  35. #include <windows.h>
  36. #include <stdio.h>
  37. #include <file2.h>
  38. #include <internal.h>
  39. #include <malloc.h>
  40. #include <mtdll.h>
  41. #include <dbgint.h>
  42. /***
  43. *int _fcloseall() - close all open streams
  44. *
  45. *Purpose:
  46. * Closes all streams currently open except for stdin/out/err/aux/prn.
  47. * tmpfile() files are among those closed.
  48. *
  49. *Entry:
  50. * None.
  51. *
  52. *Exit:
  53. * returns number of streams closed if OK
  54. * returns EOF if fails.
  55. *
  56. *Exceptions:
  57. *
  58. *******************************************************************************/
  59. int __cdecl _fcloseall (
  60. void
  61. )
  62. {
  63. REG2 int count = 0;
  64. REG1 i;
  65. #ifdef _MT
  66. _mlock(_IOB_SCAN_LOCK);
  67. __try {
  68. #endif
  69. for ( i = 3 ; i < _nstream ; i++ ) {
  70. if ( __piob[i] != NULL ) {
  71. /*
  72. * if the stream is in use, close it
  73. */
  74. if ( inuse( (FILE *)__piob[i] ) && (fclose( __piob[i] ) !=
  75. EOF) )
  76. count++;
  77. /*
  78. * if stream is part of a _FILEX we allocated, free it.
  79. */
  80. if ( i >= _IOB_ENTRIES ) {
  81. #ifdef _MT
  82. DeleteCriticalSection( &(((_FILEX *)__piob[i])->lock) );
  83. #endif
  84. _free_crt( __piob[i] );
  85. __piob[i] = NULL;
  86. }
  87. }
  88. }
  89. #ifdef _MT
  90. }
  91. __finally {
  92. _munlock(_IOB_SCAN_LOCK);
  93. }
  94. #endif
  95. return(count);
  96. }