ReactOS 0.4.16-dev-979-g79f281e
_file.cpp
Go to the documentation of this file.
1//
2// _file.cpp
3//
4// Copyright (c) Microsoft Corporation. All rights reserved.
5//
6// Defines the _iob array, the global __piob pointer, the stdio initialization
7// and termination code, and the stream locking routines.
8//
10
11
12
13// FILE descriptors for stdin, stdout, and stderr
15{
16 // ptr _base, _cnt, _flag, _file, _charbuf, _bufsiz
17 { nullptr, nullptr, 0, _IOALLOCATED | _IOREAD, 0, 0, 0}, // stdin
18 { nullptr, nullptr, 0, _IOALLOCATED | _IOWRITE, 1, 0, 0}, // stdout
19 { nullptr, nullptr, 0, _IOALLOCATED | _IOWRITE, 2, 0, 0}, // stderr
20}; }
21
22extern "C" FILE* __cdecl __acrt_iob_func(unsigned const id)
23{
24 return &_iob[id]._public_file;
25}
26
27
28
29// Pointer to the array of FILE objects:
31
32
33
34// Number of open streams (set to _NSTREAM by default):
35#ifdef CRTDLL
36 extern "C" { int _nstream = _NSTREAM_; }
37#else
38 extern "C" { int _nstream; }
39#endif
40
41
42
43// Initializer and terminator for the stdio library:
44_CRT_LINKER_FORCE_INCLUDE(__acrt_stdio_initializer);
45_CRT_LINKER_FORCE_INCLUDE(__acrt_stdio_terminator);
46
47
48
49#ifndef CRTDLL
50 // This variable is used by the statically linked CRT to ensure that if any
51 // stdio functionality is used, the terminate_stdio() function will be
52 // registered for call during CRT termination.
53 extern "C" { int _cflush = 0; }
54#endif
55
56
57
58// Initializes the stdio library. Returns 0 on success; -1 on failure.
60{
61 #ifndef CRTDLL
62 // If the user has not supplied a definition of _nstream, set it to _NSTREAM_.
63 // If the user has supplied a value that is too small, set _nstream to the
64 // minimum acceptable value (_IOB_ENTRIES):
65 if (_nstream == 0)
66 {
68 }
69 else if (_nstream < _IOB_ENTRIES)
70 {
72 }
73 #endif
74
75 // Allocate the __piob array. Try for _nstream entries first. If this
76 // fails, then reset _nstream to _IOB_ENTRIES an try again. If it still
77 // fails, bail out and cause CRT initialization to fail:
78 __piob = _calloc_crt_t(__crt_stdio_stream_data*, _nstream).detach();
79 if (!__piob)
80 {
82
83 __piob = _calloc_crt_t(__crt_stdio_stream_data*, _nstream).detach();
84 if (!__piob)
85 {
86 return -1;
87 }
88 }
89
90 // Initialize the first _IOB_ENTRIES to point to the corresponding entries
91 // in _iob[]:
92 for (int i = 0; i != _IOB_ENTRIES; ++i)
93 {
95 __piob[i] = &_iob[i];
96
97 // For stdin, stdout, and stderr, we use _NO_CONSOLE_FILENO to allow
98 // callers to distinguish between failure to open a file (-1) and a
99 // program run without a console.
100 intptr_t const os_handle = _osfhnd(i);
101 bool const has_no_console =
102 os_handle == reinterpret_cast<intptr_t>(INVALID_HANDLE_VALUE) ||
103 os_handle == _NO_CONSOLE_FILENO ||
104 os_handle == 0;
105
106 if (has_no_console)
107 {
109 }
110 }
111
112 return 0;
113}
114
115
116
117// Terminates the stdio library. All streams are flushed (this is done even if
118// we are going to close them since that stream won't do anything to the standard
119// streams) and closed.
121{
122 _flushall();
123 _fcloseall();
124
125 for (int i = 0; i != _IOB_ENTRIES; ++i)
126 {
129 }
130
132 __piob = nullptr;
133}
134
135
136
137// Locks a stdio stream.
138extern "C" void __cdecl _lock_file(FILE* const stream)
139{
141}
142
143
144
145// Unlocks a stdio stream.
146extern "C" void __cdecl _unlock_file(FILE* const stream)
147{
149}
150
151
152
154 FILE* const public_stream,
155 char*** const base,
156 char*** const ptr,
157 int** const count
158 )
159{
160 _VALIDATE_RETURN_ERRCODE(public_stream != nullptr, EINVAL);
161
162 __crt_stdio_stream const stream(public_stream);
163 if (base)
164 {
165 *base = &stream->_base;
166 }
167
168 if (ptr)
169 {
170 *ptr = &stream->_ptr;
171 }
172
173 if (count)
174 {
175 *count = &stream->_cnt;
176 }
177
178 return 0;
179}
FILE *__cdecl __acrt_iob_func(unsigned const id)
Definition: _file.cpp:22
__crt_stdio_stream_data _iob[_IOB_ENTRIES]
Definition: _file.cpp:14
void __cdecl _unlock_file(FILE *const stream)
Definition: _file.cpp:146
void __cdecl __acrt_uninitialize_stdio()
Definition: _file.cpp:120
int _nstream
Definition: _file.cpp:38
void __cdecl _lock_file(FILE *const stream)
Definition: _file.cpp:138
int _cflush
Definition: _file.cpp:53
int __cdecl __acrt_initialize_stdio()
Definition: _file.cpp:59
errno_t __cdecl _get_stream_buffer_pointers(FILE *const public_stream, char ***const base, char ***const ptr, int **const count)
Definition: _file.cpp:153
__crt_stdio_stream_data ** __piob
Definition: _file.cpp:30
void __cdecl __acrt_stdio_free_buffer_nolock(FILE *const public_stream)
Definition: _freebuf.cpp:15
#define EINVAL
Definition: acclib.h:90
#define __cdecl
Definition: accygwin.h:79
#define _CORECRT_SPINCOUNT
#define _CRT_LINKER_FORCE_INCLUDE(name)
BOOL WINAPI __acrt_InitializeCriticalSectionEx(_Out_ LPCRITICAL_SECTION critical_section, _In_ DWORD spin_count, _In_ DWORD flags)
#define _osfhnd(i)
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
GLuint GLuint GLsizei count
Definition: gl.h:1545
GLuint id
Definition: glext.h:5910
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
_Check_return_opt_ _CRTIMP int __cdecl _fcloseall(void)
Definition: file.c:1081
#define _IOB_ENTRIES
Definition: stdio.h:23
#define _IOREAD
Definition: stdio.h:124
_Check_return_opt_ _CRTIMP int __cdecl _flushall(void)
Definition: file.c:893
#define _NSTREAM_
Definition: stdio.h:22
#define _free_crt
#define _VALIDATE_RETURN_ERRCODE(expr, errorcode)
static PVOID ptr
Definition: dispmode.c:27
#define _NO_CONSOLE_FILENO
Definition: internal.h:92
void _lock(int locknum)
Definition: lock.c:97
Definition: parse.h:23
int errno_t
Definition: corecrt.h:615
int intptr_t
Definition: vcruntime.h:134
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)
void WINAPI EnterCriticalSection(LPCRITICAL_SECTION)
void WINAPI DeleteCriticalSection(PCRITICAL_SECTION)