ReactOS 0.4.16-dev-852-gcfcc8d8
setmode.cpp
Go to the documentation of this file.
1//
2// setmode.cpp
3//
4// Copyright (c) Microsoft Corporation. All rights reserved.
5//
6// Defines _setmode(), which sets the translation mode for a file, and
7// _set_fmode() and _get_fmode(), which control the global default translation
8// mode.
9//
11#include <stdlib.h>
12
13
14
15// Sets the file translation mode. This changes the file mode to text or binary,
16// depending on the mode argument. This affects whether reads and writes on the
17// file translate between CRLF and LF. Returns the old file translation mode on
18// success, or -1 on failure.
19extern "C" int __cdecl _setmode(int const fh, int const mode)
20{
22 mode == _O_BINARY ||
23 mode == _O_WTEXT ||
24 mode == _O_U8TEXT ||
26 EINVAL, -1);
27
28 _CHECK_FH_RETURN(fh, EBADF, -1);
29 _VALIDATE_RETURN((fh >= 0 && (unsigned)fh < (unsigned)_nhandle), EBADF, -1);
30 _VALIDATE_RETURN((_osfile(fh) & FOPEN), EBADF, -1);
31
33 int result = -1;
34 __try
35 {
36 if ((_osfile(fh) & FOPEN) == 0)
37 {
38 errno = EBADF;
39 _ASSERTE(("Invalid file descriptor. File possibly closed by a different thread",0));
40 __leave;
41 }
42
44 }
46 {
48 }
50 return result;
51}
52
53
54
55extern "C" int __cdecl _setmode_nolock(int const fh, int const mode)
56{
57 int const old_mode = _osfile(fh) & FTEXT;
58 __crt_lowio_text_mode const old_textmode = _textmode(fh);
59
60 switch (mode)
61 {
62 case _O_BINARY:
63 _osfile(fh) &= ~FTEXT;
64 break;
65
66 case _O_TEXT:
67 _osfile(fh) |= FTEXT;
69 break;
70
71 case _O_U8TEXT:
72 _osfile(fh) |= FTEXT;
74 break;
75
76 case _O_U16TEXT:
77 case _O_WTEXT:
78 _osfile(fh) |= FTEXT;
80 break;
81 }
82
83 if (old_mode == 0)
84 return _O_BINARY;
85
86 if (old_textmode == __crt_lowio_text_mode::ansi)
87 {
88 return _O_TEXT;
89 }
90 else if (old_textmode == __crt_lowio_text_mode::utf8)
91 {
92 return _O_U8TEXT;
93 }
94
95 return _O_WTEXT;
96}
97
98
99
100extern "C" errno_t __cdecl _set_fmode(int const mode)
101{
103
105 _InterlockedExchange(reinterpret_cast<long*>(&_fmode.value()), mode);
107
108 return 0;
109}
110
111
112
113extern "C" errno_t __cdecl _get_fmode(int* const pMode)
114{
115 _VALIDATE_RETURN_ERRCODE(pMode != nullptr, EINVAL);
116
118 *pMode = __crt_interlocked_read(&_fmode.value());
120
121 return 0;
122}
#define EINVAL
Definition: acclib.h:90
#define EBADF
Definition: acclib.h:82
#define __cdecl
Definition: accygwin.h:79
#define _setmode(fd, mode)
Definition: cat.c:21
#define _CHECK_FH_RETURN(handle, errorcode, retexpr)
int _nhandle
Definition: ioinit.cpp:34
__crt_lowio_text_mode
void __cdecl __acrt_lowio_lock_fh(_In_ int _FileHandle)
void __cdecl __acrt_lowio_unlock_fh(_In_ int _FileHandle)
#define _VALIDATE_RETURN(expr, errorcode, retexpr)
#define _ASSERTE(expr)
Definition: crtdbg.h:114
#define _O_BINARY
Definition: cabinet.h:51
#define _O_TEXT
Definition: cabinet.h:50
#define FOPEN
GLenum mode
Definition: glext.h:6217
GLuint64EXT * result
Definition: glext.h:11304
#define _O_WTEXT
Definition: fcntl.h:20
#define _O_U8TEXT
Definition: fcntl.h:22
#define _O_U16TEXT
Definition: fcntl.h:21
#define _fmode
Definition: stdlib.h:255
#define _BEGIN_SECURE_CRT_DEPRECATION_DISABLE
#define _VALIDATE_RETURN_ERRCODE(expr, errorcode)
#define _END_SECURE_CRT_DEPRECATION_DISABLE
long __cdecl _InterlockedExchange(_Interlocked_operand_ long volatile *_Target, long _Value)
#define __try
Definition: pseh2_64.h:172
#define __leave
Definition: pseh2_64.h:176
#define __endtry
Definition: pseh2_64.h:175
#define __finally
Definition: pseh2_64.h:174
#define errno
Definition: errno.h:18
#define _osfile(i)
Definition: internal.h:72
#define _textmode(i)
Definition: internal.h:74
errno_t __cdecl _set_fmode(int const mode)
Definition: setmode.cpp:100
errno_t __cdecl _get_fmode(int *const pMode)
Definition: setmode.cpp:113
int __cdecl _setmode_nolock(int const fh, int const mode)
Definition: setmode.cpp:55
char * value
Definition: compiler.c:67
int errno_t
Definition: corecrt.h:615