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.

153 lines
4.2 KiB

  1. /***
  2. *fopen.c - open a file
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines fopen() and _fsopen() - open a file as a stream and open a file
  8. * with a specified sharing mode as a stream
  9. *
  10. *Revision History:
  11. * 09-02-83 RN initial version
  12. * 04-13-87 JCR added const to declarations
  13. * 11-01-87 JCR Multi-thread support
  14. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  15. * 05-31-88 PHG Merged DLL and normal versions
  16. * 11-14-88 GJF Added _fsopen().
  17. * 02-15-90 GJF Fixed copyright and indents
  18. * 03-19-90 GJF Replaced _LOAD_DS with _CALLTYPE1, added #include
  19. * <cruntime.h> and removed #include <register.h>.
  20. * 07-23-90 SBM Replaced <assertm.h> by <assert.h>
  21. * 10-02-90 GJF New-style function declarators.
  22. * 01-21-91 GJF ANSI naming.
  23. * 03-26-92 DJM POSIX support
  24. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  25. * 11-01-93 CFW Enable Unicode variant.
  26. * 02-06-94 CFW assert -> _ASSERTE.
  27. * 02-20-95 GJF Replaced WPRFLAG with _UNICODE.
  28. * 03-07-95 GJF _[un]lock_str macros now take FILE * arg.
  29. * 02-27-98 GJF Exception-safe locking.
  30. * 10-06-99 PML Set errno EMFILE when out of streams.
  31. *
  32. *******************************************************************************/
  33. #include <cruntime.h>
  34. #include <stdio.h>
  35. #include <share.h>
  36. #include <dbgint.h>
  37. #include <internal.h>
  38. #include <mtdll.h>
  39. #include <file2.h>
  40. #include <tchar.h>
  41. #include <errno.h>
  42. /***
  43. *FILE *_fsopen(file, mode, shflag) - open a file
  44. *
  45. *Purpose:
  46. * Opens the file specified as a stream. mode determines file mode:
  47. * "r": read "w": write "a": append
  48. * "r+": read/write "w+": open empty for read/write
  49. * "a+": read/append
  50. * Append "t" or "b" for text and binary mode. shflag determines the
  51. * sharing mode. Values are the same as for sopen().
  52. *
  53. *Entry:
  54. * char *file - file name to open
  55. * char *mode - mode of file access
  56. *
  57. *Exit:
  58. * returns pointer to stream
  59. * returns NULL if fails
  60. *
  61. *Exceptions:
  62. *
  63. *******************************************************************************/
  64. FILE * __cdecl _tfsopen (
  65. const _TSCHAR *file,
  66. const _TSCHAR *mode
  67. #ifndef _POSIX_
  68. ,int shflag
  69. #endif
  70. )
  71. {
  72. REG1 FILE *stream;
  73. REG2 FILE *retval;
  74. _ASSERTE(file != NULL);
  75. _ASSERTE(*file != _T('\0'));
  76. _ASSERTE(mode != NULL);
  77. _ASSERTE(*mode != _T('\0'));
  78. /* Get a free stream */
  79. /* [NOTE: _getstream() returns a locked stream.] */
  80. if ((stream = _getstream()) == NULL) {
  81. errno = EMFILE;
  82. return(NULL);
  83. }
  84. #ifdef _MT
  85. __try {
  86. #endif
  87. /* open the stream */
  88. #ifdef _POSIX_
  89. #ifdef _UNICODE
  90. retval = _wopenfile(file,mode, stream);
  91. #else
  92. retval = _openfile(file,mode, stream);
  93. #endif
  94. #else
  95. #ifdef _UNICODE
  96. retval = _wopenfile(file,mode,shflag,stream);
  97. #else
  98. retval = _openfile(file,mode,shflag,stream);
  99. #endif
  100. #endif
  101. #ifdef _MT
  102. }
  103. __finally {
  104. _unlock_str(stream);
  105. }
  106. #endif
  107. return(retval);
  108. }
  109. /***
  110. *FILE *fopen(file, mode) - open a file
  111. *
  112. *Purpose:
  113. * Opens the file specified as a stream. mode determines file mode:
  114. * "r": read "w": write "a": append
  115. * "r+": read/write "w+": open empty for read/write
  116. * "a+": read/append
  117. * Append "t" or "b" for text and binary mode
  118. *
  119. *Entry:
  120. * char *file - file name to open
  121. * char *mode - mode of file access
  122. *
  123. *Exit:
  124. * returns pointer to stream
  125. * returns NULL if fails
  126. *
  127. *Exceptions:
  128. *
  129. *******************************************************************************/
  130. FILE * __cdecl _tfopen (
  131. const _TSCHAR *file,
  132. const _TSCHAR *mode
  133. )
  134. {
  135. #ifdef _POSIX_
  136. return( _tfsopen(file, mode) );
  137. #else
  138. return( _tfsopen(file, mode, _SH_DENYNO) );
  139. #endif
  140. }