xref: /reactos/sdk/lib/ucrt/stdio/fgetc.cpp (revision fe93a3f9)
1 //
2 // fgetc.cpp
3 //
4 //      Copyright (c) Microsoft Corporation.  All rights reserved.
5 //
6 // Functions that read the next character from a stream and return it.  If the
7 // read causes the stream to reach EOF, EOF is returned and the EOF bit is set
8 // on the stream.
9 //
10 #include <corecrt_internal_stdio.h>
11 
12 
13 
14 extern "C" int __cdecl _fgetc_nolock(FILE* const public_stream)
15 {
16     __crt_stdio_stream const stream(public_stream);
17 
18     _VALIDATE_RETURN(stream.valid(), EINVAL, EOF);
19 
20     --stream->_cnt;
21 
22     if (stream->_cnt < 0)
23         return __acrt_stdio_refill_and_read_narrow_nolock(stream.public_stream());
24 
25     char const c = *stream->_ptr;
26     ++stream->_ptr;
27     return c & 0xff;
28 }
29 
30 
31 
32 extern "C" int __cdecl _getc_nolock(FILE* const stream)
33 {
34     return _fgetc_nolock(stream);
35 }
36 
37 
38 
39 extern "C" int __cdecl fgetc(FILE* const public_stream)
40 {
41     __crt_stdio_stream const stream(public_stream);
42 
43     _VALIDATE_RETURN(stream.valid(), EINVAL, EOF);
44 
45     int return_value = 0;
46 
47     _lock_file(stream.public_stream());
48     __try
49     {
50         _VALIDATE_STREAM_ANSI_RETURN(stream, EINVAL, EOF);
51 
52         return_value = _fgetc_nolock(stream.public_stream());
53     }
54     __finally
55     {
56         _unlock_file(stream.public_stream());
57     }
58     __endtry
59 
60     return return_value;
61 }
62 
63 
64 
65 extern "C" int __cdecl getc(FILE* const stream)
66 {
67     return fgetc(stream);
68 }
69 
70 
71 
72 extern "C" int __cdecl _fgetchar()
73 {
74     return fgetc(stdin);
75 }
76 
77 
78 
79 extern "C" int __cdecl getchar()
80 {
81     return _fgetchar();
82 }
83