|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #include <corecrt_internal_stdio.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| template <typename Character>
|
| static FILE* __cdecl common_fsopen(
|
| Character const* const file_name,
|
| Character const* const mode,
|
| int const share_flag
|
| ) throw()
|
| {
|
| typedef __acrt_stdio_char_traits<Character> stdio_traits;
|
|
|
| _VALIDATE_RETURN(file_name != nullptr, EINVAL, nullptr);
|
| _VALIDATE_RETURN(mode != nullptr, EINVAL, nullptr);
|
| _VALIDATE_RETURN(*mode != 0, EINVAL, nullptr);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| _VALIDATE_RETURN_NOEXC(*file_name != 0, EINVAL, nullptr);
|
|
|
|
|
| __crt_stdio_stream stream = __acrt_stdio_allocate_stream();
|
| if (!stream.valid())
|
| {
|
| errno = EMFILE;
|
| return nullptr;
|
| }
|
|
|
| FILE* return_value = nullptr;
|
| __try
|
| {
|
| return_value = stdio_traits::open_file(file_name, mode, share_flag, stream.public_stream());
|
| }
|
| __finally
|
| {
|
| if (return_value == nullptr)
|
| __acrt_stdio_free_stream(stream);
|
|
|
| stream.unlock();
|
| }
|
|
|
| return return_value;
|
| }
|
|
|
|
|
|
|
|
|
|
|
| template <typename Character>
|
| static errno_t __cdecl common_fopen_s(
|
| FILE** const result,
|
| Character const* const file_name,
|
| Character const* const mode
|
| ) throw()
|
| {
|
| _VALIDATE_RETURN_ERRCODE(result != nullptr, EINVAL);
|
|
|
| *result = common_fsopen(file_name, mode, _SH_SECURE);
|
| if (*result == nullptr)
|
| return errno;
|
|
|
| return 0;
|
| }
|
|
|
|
|
|
|
| extern "C" FILE* __cdecl _fsopen(
|
| char const* const file,
|
| char const* const mode,
|
| int const share_flag
|
| )
|
| {
|
| return common_fsopen(file, mode, share_flag);
|
| }
|
|
|
| extern "C" FILE* __cdecl fopen(
|
| char const* const file,
|
| char const* const mode
|
| )
|
| {
|
| return common_fsopen(file, mode, _SH_DENYNO);
|
| }
|
|
|
| extern "C" errno_t __cdecl fopen_s(
|
| FILE** const result,
|
| char const* const file,
|
| char const* const mode
|
| )
|
| {
|
| return common_fopen_s(result, file, mode);
|
| }
|
|
|
| extern "C" FILE* __cdecl _wfsopen(
|
| wchar_t const* const file,
|
| wchar_t const* const mode,
|
| int const share_flag
|
| )
|
| {
|
| return common_fsopen(file, mode, share_flag);
|
| }
|
|
|
| extern "C" FILE* __cdecl _wfopen(
|
| wchar_t const* const file,
|
| wchar_t const* const mode
|
| )
|
| {
|
| return common_fsopen(file, mode, _SH_DENYNO);
|
| }
|
|
|
| extern "C" errno_t __cdecl _wfopen_s(
|
| FILE** const result,
|
| wchar_t const* const file,
|
| wchar_t const* const mode
|
| )
|
| {
|
| return common_fopen_s(result, file, mode);
|
| }
|
|
|