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.

121 lines
3.6 KiB

  1. /***
  2. *freopen.c - close a stream and assign it to a new file
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines freopen() - close and reopen file, typically used to redirect
  8. * stdin/out/err/prn/aux.
  9. *
  10. *Revision History:
  11. * 09-02-83 RN initial version
  12. * 04-13-87 JCR added const to declarations
  13. * 11-02-87 JCR Multi-thread support
  14. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  15. * 05-27-88 PHG Merged DLL and normal versions
  16. * 06-15-88 JCR Near reference to _iob[] entries; improve REG variables
  17. * 08-25-88 GJF Don't use FP_OFF() macro for the 386
  18. * 11-14-88 GJF _openfile() now takes a file sharing flag, also some
  19. * cleanup (now specific to the 386)
  20. * 08-17-89 GJF Clean up, now specific to OS/2 2.0 (i.e., 386 flat
  21. * model). Also fixed copyright and indents.
  22. * 02-15-90 GJF Fixed copyright
  23. * 03-19-90 GJF Made calling type _CALLTYPE1, added #include
  24. * <cruntime.h> and removed #include <register.h>.
  25. * 07-23-90 SBM Replaced <assertm.h> by <assert.h>
  26. * 10-02-90 GJF New-style function declarator.
  27. * 01-22-91 GJF ANSI naming.
  28. * 03-27-92 DJM POSIX support.
  29. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  30. * 11-01-93 CFW Enable Unicode variant.
  31. * 01-17-94 GJF Ignore possible failure of _fclose_lk (ANSI 4.9.5.4)
  32. * 04-11-94 CFW Remove unused 'done' label to avoid warnings.
  33. * 02-06-94 CFW assert -> _ASSERTE.
  34. * 02-20-95 GJF Replaced WPRFLAG with _UNICODE.
  35. * 03-07-95 GJF _[un]lock_str macros now take FILE * arg.
  36. * 03-02-98 GJF Exception-safe locking.
  37. *
  38. *******************************************************************************/
  39. #include <cruntime.h>
  40. #include <stdio.h>
  41. #include <file2.h>
  42. #include <share.h>
  43. #include <dbgint.h>
  44. #include <internal.h>
  45. #include <mtdll.h>
  46. #include <tchar.h>
  47. /***
  48. *FILE *freopen(filename, mode, stream) - reopen stream as new file
  49. *
  50. *Purpose:
  51. * Closes the file associated with stream and assigns stream to a new
  52. * file with current mode. Usually used to redirect a standard file
  53. * handle.
  54. *
  55. *Entry:
  56. * char *filename - new file to open
  57. * char *mode - new file mode, as in fopen()
  58. * FILE *stream - stream to close and reassign
  59. *
  60. *Exit:
  61. * returns stream if successful
  62. * return NULL if fails
  63. *
  64. *Exceptions:
  65. *
  66. *******************************************************************************/
  67. FILE * __cdecl _tfreopen (
  68. const _TSCHAR *filename,
  69. const _TSCHAR *mode,
  70. FILE *str
  71. )
  72. {
  73. REG1 FILE *stream;
  74. FILE *retval;
  75. _ASSERTE(filename != NULL);
  76. _ASSERTE(*filename != _T('\0'));
  77. _ASSERTE(mode != NULL);
  78. _ASSERTE(str != NULL);
  79. /* Init stream pointer */
  80. stream = str;
  81. #ifdef _MT
  82. _lock_str(stream);
  83. __try {
  84. #endif
  85. /* If the stream is in use, try to close it. Ignore possible
  86. * error (ANSI 4.9.5.4). */
  87. if ( inuse(stream) )
  88. _fclose_lk(stream);
  89. stream->_ptr = stream->_base = NULL;
  90. stream->_cnt = stream->_flag = 0;
  91. #ifdef _POSIX_
  92. #ifdef _UNICODE
  93. retval = _wopenfile(filename,mode,stream);
  94. #else
  95. retval = _openfile(filename,mode,stream);
  96. #endif
  97. #else
  98. #ifdef _UNICODE
  99. retval = _wopenfile(filename,mode,_SH_DENYNO,stream);
  100. #else
  101. retval = _openfile(filename,mode,_SH_DENYNO,stream);
  102. #endif
  103. #endif
  104. #ifdef _MT
  105. }
  106. __finally {
  107. _unlock_str(stream);
  108. }
  109. #endif
  110. return(retval);
  111. }