ReactOS 0.4.16-dev-1067-ge98bba2
osfinfo.cpp File Reference
Include dependency graph for osfinfo.cpp:

Go to the source code of this file.

Functions

__crt_lowio_handle_data *__cdecl __acrt_lowio_create_handle_array ()
 
void __cdecl __acrt_lowio_destroy_handle_array (__crt_lowio_handle_data *const array)
 
errno_t __cdecl __acrt_lowio_ensure_fh_exists (int const fh)
 
int __cdecl _alloc_osfhnd ()
 
int __cdecl __acrt_lowio_set_os_handle (int const fh, intptr_t const value)
 
int __cdecl _free_osfhnd (int const fh)
 
intptr_t __cdecl _get_osfhandle (int const fh)
 
int __cdecl _open_osfhandle (intptr_t const osfhandle, int const source_flags)
 
void __cdecl __acrt_lowio_lock_fh (int const fh)
 
void __cdecl __acrt_lowio_unlock_fh (int const fh)
 

Function Documentation

◆ __acrt_lowio_create_handle_array()

__crt_lowio_handle_data *__cdecl __acrt_lowio_create_handle_array ( )

Definition at line 13 of file osfinfo.cpp.

14{
15 __crt_unique_heap_ptr<__crt_lowio_handle_data> array(_calloc_crt_t(
18
19 if (!array)
20 return nullptr;
21
22 __crt_lowio_handle_data* const first = array.get();
24 for (auto it = first; it != last; ++it)
25 {
27 it->osfhnd = reinterpret_cast<intptr_t>(INVALID_HANDLE_VALUE);
28 it->startpos = 0;
29 it->osfile = 0;
30 it->textmode = __crt_lowio_text_mode::ansi;
31 it->_pipe_lookahead[0] = LF;
32 it->_pipe_lookahead[1] = LF;
33 it->_pipe_lookahead[2] = LF;
34 it->unicode = false;
35 it->utf8translations = false;
36 it->dbcsBufferUsed = false;
37 for (int i = 0; i < sizeof(it->mbBuffer); ++i)
38 {
39 it->mbBuffer[i] = '\0';
40 }
41 }
42
43 return array.detach();
44}
#define _CORECRT_SPINCOUNT
BOOL WINAPI __acrt_InitializeCriticalSectionEx(_Out_ LPCRITICAL_SECTION critical_section, _In_ DWORD spin_count, _In_ DWORD flags)
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
const GLint * first
Definition: glext.h:5794
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
static UINT UINT last
Definition: font.c:45
#define IOINFO_ARRAY_ELTS
Definition: internal.h:69
#define LF
Definition: telnetd.h:24
int intptr_t
Definition: vcruntime.h:134

Referenced by __acrt_lowio_ensure_fh_exists(), and _alloc_osfhnd().

◆ __acrt_lowio_destroy_handle_array()

void __cdecl __acrt_lowio_destroy_handle_array ( __crt_lowio_handle_data *const  array)

Definition at line 46 of file osfinfo.cpp.

47{
48 if (!array)
49 return;
50
53 for (auto it = first; it != last; ++it)
54 {
55 DeleteCriticalSection(&it->lock);
56 }
57
59}
#define _free_crt
void WINAPI DeleteCriticalSection(PCRITICAL_SECTION)

◆ __acrt_lowio_ensure_fh_exists()

errno_t __cdecl __acrt_lowio_ensure_fh_exists ( int const  fh)

Definition at line 66 of file osfinfo.cpp.

67{
68 _VALIDATE_RETURN_ERRCODE(static_cast<unsigned>(fh) < _NHANDLE_, EBADF);
69
70 errno_t status = 0;
71
73 __try
74 {
75 for (size_t i = 0; fh >= _nhandle; ++i)
76 {
77 if (__pioinfo[i])
78 {
79 continue;
80 }
81
83 if (!__pioinfo[i])
84 {
85 status = ENOMEM;
86 __leave;
87 }
88
90 }
91 }
93 {
95 }
97
98 return status;
99}
#define ENOMEM
Definition: acclib.h:84
#define EBADF
Definition: acclib.h:82
void __cdecl __acrt_unlock(_In_ __acrt_lock_id lock)
Definition: locks.cpp:57
@ __acrt_lowio_index_lock
int _nhandle
Definition: ioinit.cpp:34
#define _NHANDLE_
__acrt_lock(__acrt_heap_lock)
#define _VALIDATE_RETURN_ERRCODE(expr, errorcode)
static ioinfo ** __pioinfo
Definition: file.c:66
__crt_lowio_handle_data *__cdecl __acrt_lowio_create_handle_array()
Definition: osfinfo.cpp:13
#define __try
Definition: pseh2_64.h:188
#define __leave
Definition: pseh2_64.h:192
#define __endtry
Definition: pseh2_64.h:191
#define __finally
Definition: pseh2_64.h:190
Definition: ps.c:97
int errno_t
Definition: corecrt.h:615

◆ __acrt_lowio_lock_fh()

void __cdecl __acrt_lowio_lock_fh ( int const  fh)

Definition at line 340 of file osfinfo.cpp.

341{
343}
#define _pioinfo(i)
Definition: internal.h:71
rwlock_t lock
Definition: tcpcore.h:0
void WINAPI EnterCriticalSection(LPCRITICAL_SECTION)

Referenced by _alloc_osfhnd().

◆ __acrt_lowio_set_os_handle()

int __cdecl __acrt_lowio_set_os_handle ( int const  fh,
intptr_t const  value 
)

Definition at line 195 of file osfinfo.cpp.

196{
197 if (fh >= 0 &&
198 static_cast<unsigned>(fh) < static_cast<unsigned>(_nhandle) &&
199 _osfhnd(fh) == reinterpret_cast<intptr_t>(INVALID_HANDLE_VALUE))
200 {
202 {
203 HANDLE const handle_value = reinterpret_cast<HANDLE>(value);
204 switch (fh)
205 {
206 case 0: SetStdHandle(STD_INPUT_HANDLE, handle_value); break;
207 case 1: SetStdHandle(STD_OUTPUT_HANDLE, handle_value); break;
208 case 2: SetStdHandle(STD_ERROR_HANDLE, handle_value); break;
209 }
210 }
211
212 _osfhnd(fh) = value;
213 return 0 ;
214 }
215 else
216 {
217 errno = EBADF; // Bad handle
218 _doserrno = 0; // This is not an OS error
219 return -1;
220 }
221}
#define _osfhnd(i)
@ _crt_console_app
_ACRTIMP _crt_app_type __cdecl _query_app_type(void)
BOOL WINAPI DECLSPEC_HOTPATCH SetStdHandle(DWORD nStdHandle, HANDLE hHandle)
Definition: console.c:1213
#define _doserrno
Definition: stdlib.h:131
#define errno
Definition: errno.h:18
Definition: pdh_main.c:96
#define STD_OUTPUT_HANDLE
Definition: winbase.h:294
#define STD_INPUT_HANDLE
Definition: winbase.h:293
#define STD_ERROR_HANDLE
Definition: winbase.h:295

Referenced by _dup2_nolock_internal(), _open_osfhandle(), _pipe(), _wsopen_nolock(), and duplicate_osfhnd().

◆ __acrt_lowio_unlock_fh()

void __cdecl __acrt_lowio_unlock_fh ( int const  fh)

Definition at line 348 of file osfinfo.cpp.

349{
351}
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)

Referenced by _open_osfhandle().

◆ _alloc_osfhnd()

int __cdecl _alloc_osfhnd ( void  )

Definition at line 116 of file osfinfo.cpp.

117{
119 int result = -1;
120 __try
121 {
122 // Search the arrays of file objects, in order, looking for the first
123 // free entry. The compound index of this free entry is the return
124 // value.
125 //
126 // The compound index of the file object entry *(__pioinfo[i] + j) is
127 // k = i * IOINFO_ARRAY_ELTS + j.
128 for (int i = 0; i < IOINFO_ARRAYS; ++i)
129 {
130 // If this __crt_lowio_handle_data array does not yet exist, create a new one:
131 if (!__pioinfo[i])
132 {
134 if (!__pioinfo[i])
135 __leave;
136
138
139 // The first element of the newly allocated array of handle data
140 // objects is our first free entry. Note that since we hold the
141 // index lock, no one else can allocate this handle.
142 int const fh = i * IOINFO_ARRAY_ELTS;
143
145 _osfile(fh) = FOPEN;
146 result = fh;
147 __leave;
148 }
149
150 // Otherwise, this file object array already exists. Search it looking
151 // for the first free entry:
154 for (__crt_lowio_handle_data* pio = first; pio != last; ++pio)
155 {
156 if (pio->osfile & FOPEN)
157 continue;
158
159 // Another thread may have grabbed this file handle out from
160 // under us while we waited for the lock. If so, continue on
161 // searching through the array.
162 //
163 // CRT_REFACTOR TODO: Resolve lowio synchronization issues.
164 EnterCriticalSection(&pio->lock);
165 if ((pio->osfile & FOPEN) != 0)
166 {
167 LeaveCriticalSection(&pio->lock);
168 continue;
169 }
170
171 // Otherwise, this entry is ours: we hold the lock, so we can
172 // initialize it and return its handle:
173 int const fh = i * IOINFO_ARRAY_ELTS + static_cast<int>(pio - first);
174 _osfile(fh) = FOPEN;
175 _osfhnd(fh) = reinterpret_cast<intptr_t>(INVALID_HANDLE_VALUE);
176 result = fh;
177 __leave;
178 }
179 }
180
181 // All entries are in use if we fall out of the loop. return -1 in this case (which result is already set to)
182 }
184 {
186 }
188 return result;
189}
#define IOINFO_ARRAYS
#define FOPEN
GLuint64EXT * result
Definition: glext.h:11304
void __cdecl __acrt_lowio_lock_fh(int const fh)
Definition: osfinfo.cpp:340
#define _osfile(i)
Definition: internal.h:72

Referenced by _dup_nolock_internal(), _open_osfhandle(), _pipe(), and _wsopen_nolock().

◆ _free_osfhnd()

int __cdecl _free_osfhnd ( int const  fh)

Definition at line 227 of file osfinfo.cpp.

228{
229 if (fh >= 0 &&
230 static_cast<unsigned>(fh) < static_cast<unsigned>(_nhandle) &&
231 (_osfile(fh) & FOPEN) &&
232 _osfhnd(fh) != reinterpret_cast<intptr_t>(INVALID_HANDLE_VALUE))
233 {
235 {
236 switch (fh)
237 {
238 case 0: SetStdHandle(STD_INPUT_HANDLE, nullptr); break;
239 case 1: SetStdHandle(STD_OUTPUT_HANDLE, nullptr); break;
240 case 2: SetStdHandle(STD_ERROR_HANDLE, nullptr); break;
241 }
242 }
243
244 _osfhnd(fh) = reinterpret_cast<intptr_t>(INVALID_HANDLE_VALUE);
245 return 0;
246 }
247 else
248 {
249 errno = EBADF; // Bad handle
250 _doserrno = 0; // This is not an OS error
251 return -1;
252 }
253}

Referenced by _close_nolock_internal(), and _wsopen_nolock().

◆ _get_osfhandle()

intptr_t __cdecl _get_osfhandle ( int const  fh)

Definition at line 259 of file osfinfo.cpp.

260{
262 _VALIDATE_CLEAR_OSSERR_RETURN(fh >= 0 && (unsigned)fh < (unsigned)_nhandle, EBADF, -1);
264
265 return _osfhnd(fh);
266}
#define _CHECK_FH_CLEAR_OSSERR_RETURN(handle, errorcode, retexpr)
#define _VALIDATE_CLEAR_OSSERR_RETURN(expr, errorcode, retexpr)

◆ _open_osfhandle()

int __cdecl _open_osfhandle ( intptr_t const  osfhandle,
int const  source_flags 
)

Definition at line 273 of file osfinfo.cpp.

274{
275 // Copy relevant source_flags from second parameter
276 unsigned char file_flags = 0;
277
278 if (source_flags & _O_APPEND)
279 file_flags |= FAPPEND;
280
281 if (source_flags & _O_TEXT)
282 file_flags |= FTEXT;
283
284 if (source_flags & _O_NOINHERIT)
285 file_flags |= FNOINHERIT;
286
287 // Find out what type of file (file/device/pipe):
288 DWORD const file_type = GetFileType(reinterpret_cast<HANDLE>(osfhandle));
290 {
292 return -1;
293 }
294
296 file_flags |= FDEV;
297
298 else if (file_type == FILE_TYPE_PIPE)
299 file_flags |= FPIPE;
300
301 // Attempt to allocate a CRT file handle:
302 int const fh = _alloc_osfhnd();
303 if (fh == -1)
304 {
305 errno = EMFILE; // Too many open files
306 _doserrno = 0L; // This is not an OS error
307 return -1;
308 }
309
310 bool success = false;
311 __try
312 {
313 // The file is open. now set the info in _osfhnd array:
314 __acrt_lowio_set_os_handle(fh, osfhandle);
315
316 file_flags |= FOPEN;
317
318 _osfile(fh) = file_flags;
320 _tm_unicode(fh) = false;
321
322 success = true;
323 }
325 {
326 if (!success)
327 {
328 _osfile(fh) &= ~FOPEN;
329 }
330
332 }
334 return fh;
335}
void __cdecl __acrt_errno_map_os_error(unsigned long)
Definition: errno.cpp:91
#define _O_NOINHERIT
Definition: cabinet.h:45
#define _O_APPEND
Definition: cabinet.h:41
#define _O_TEXT
Definition: cabinet.h:50
DWORD WINAPI GetFileType(HANDLE hFile)
Definition: fileinfo.c:269
#define EMFILE
Definition: errno.h:30
unsigned long DWORD
Definition: ntddk_ex.h:95
#define L(x)
Definition: ntvdm.h:50
int __cdecl _alloc_osfhnd()
Definition: osfinfo.cpp:116
int __cdecl __acrt_lowio_set_os_handle(int const fh, intptr_t const value)
Definition: osfinfo.cpp:195
void __cdecl __acrt_lowio_unlock_fh(int const fh)
Definition: osfinfo.cpp:348
#define _tm_unicode(i)
Definition: internal.h:75
#define _textmode(i)
Definition: internal.h:74
FD_TYPE file_type(FDSC **curr, char *fixed)
Definition: file.c:221
#define success(from, fromstr, to, tostr)
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define FILE_TYPE_UNKNOWN
Definition: winbase.h:284
#define FILE_TYPE_CHAR
Definition: winbase.h:286
#define FILE_TYPE_PIPE
Definition: winbase.h:287