Reactos
1//
2// getw.cpp
3//
4// Copyright (c) Microsoft Corporation. All rights reserved.
5//
6// Defines _getw(), which reads a binary integer to a stream.
7//
8#include <corecrt_internal_stdio.h>
9
10
11
12// Reads a binary int value from the stream, byte-by-byte. On success, returns
13// the value written; on failure (error or eof), returns EOF. Note, however,
14// that the value may be EOF--that is, after all, a valid integer--so be sure to
15// test ferror() and feof() to check for error conditions.
16extern "C" int __cdecl _getw(FILE* const stream)
17{
18 _VALIDATE_RETURN(stream != nullptr, EINVAL, EOF);
19
20 int return_value = EOF;
21
22 _lock_file(stream);
23 __try
24 {
25 int value = 0;
26 char* const first = reinterpret_cast<char*>(&value);
27 char* const last = first + sizeof(value);
28
29 for (char* it = first; it != last; ++it)
30 {
31 *it = static_cast<char>(_getc_nolock(stream));
32 }
33
34 if (feof(stream))
35 __leave;
36
37 if (ferror(stream))
38 __leave;
39
40 return_value = value;
41 }
42 __finally
43 {
44 _unlock_file(stream);
45 }
46 __endtry
47
48 return return_value;
49}