ReactOS 0.4.16-dev-716-g2b2bdab
errno.cpp
Go to the documentation of this file.
1//
2// errno.cpp
3//
4// Copyright (c) Microsoft Corporation. All rights reserved.
5//
6// Defines _errno, _doserrno, and related functions
7//
8#define _ALLOW_OLD_VALIDATE_MACROS
9#include <corecrt_internal.h>
11#include <errno.h>
12
13
14
15// This is the error table that defines the mapping between OS error codes and
16// errno values.
17namespace
18{
19 struct errentry
20 {
21 unsigned long oscode; // OS return value
22 int errnocode; // System V error code
23 };
24}
25
26static errentry const errtable[]
27{
38 { ERROR_BAD_FORMAT, ENOEXEC }, // 11
40 { ERROR_INVALID_DATA, EINVAL }, // 13
41 { ERROR_INVALID_DRIVE, ENOENT }, // 15
44 { ERROR_NO_MORE_FILES, ENOENT }, // 18
46 { ERROR_BAD_NETPATH, ENOENT }, // 53
48 { ERROR_BAD_NET_NAME, ENOENT }, // 67
49 { ERROR_FILE_EXISTS, EEXIST }, // 80
50 { ERROR_CANNOT_MAKE, EACCES }, // 82
51 { ERROR_FAIL_I24, EACCES }, // 83
53 { ERROR_NO_PROC_SLOTS, EAGAIN }, // 89
54 { ERROR_DRIVE_LOCKED, EACCES }, // 108
55 { ERROR_BROKEN_PIPE, EPIPE }, // 109
56 { ERROR_DISK_FULL, ENOSPC }, // 112
61 { ERROR_NEGATIVE_SEEK, EINVAL }, // 131
62 { ERROR_SEEK_ON_DEVICE, EACCES }, // 132
64 { ERROR_NOT_LOCKED, EACCES }, // 158
65 { ERROR_BAD_PATHNAME, ENOENT }, // 161
67 { ERROR_LOCK_FAILED, EACCES }, // 167
68 { ERROR_ALREADY_EXISTS, EEXIST }, // 183
73};
74
75// Number of elements in the table
76#define ERRTABLECOUNT (sizeof(errtable) / sizeof(errtable[0]))
77
78// The following two constants must be the minimum and maximum
79// values in the (contiguous) range of Exec Failure errors.
80#define MIN_EXEC_ERROR ERROR_INVALID_STARTING_CODESEG
81#define MAX_EXEC_ERROR ERROR_INFLOOP_IN_RELOC_CHAIN
82
83// These are the low and high value in the range of errors that are
84// access violations
85#define MIN_EACCES_RANGE ERROR_WRITE_PROTECT
86#define MAX_EACCES_RANGE ERROR_SHARING_BUFFER_EXCEEDED
87
88
89
90// These map Windows error codes into errno error codes
91extern "C" void __cdecl __acrt_errno_map_os_error(unsigned long const oserrno)
92{
93 _doserrno = oserrno;
95}
96
97extern "C" void __cdecl __acrt_errno_map_os_error_ptd(unsigned long const oserrno, __crt_cached_ptd_host& ptd)
98{
99 ptd.get_doserrno().set(oserrno);
100 ptd.get_errno().set(__acrt_errno_from_os_error(oserrno));
101}
102
103extern "C" int __cdecl __acrt_errno_from_os_error(unsigned long const oserrno)
104{
105 // Check the table for the OS error code
106 for (unsigned i{0}; i < ERRTABLECOUNT; ++i)
107 {
108 if (oserrno == errtable[i].oscode)
109 return errtable[i].errnocode;
110 }
111
112 // The error code wasn't in the table. We check for a range of
113 // EACCES errors or exec failure errors (ENOEXEC). Otherwise
114 // EINVAL is returned.
115 if (oserrno >= MIN_EACCES_RANGE && oserrno <= MAX_EACCES_RANGE)
116 {
117 return EACCES;
118 }
119 else if (oserrno >= MIN_EXEC_ERROR && oserrno <= MAX_EXEC_ERROR)
120 {
121 return ENOEXEC;
122 }
123 else
124 {
125 return EINVAL;
126 }
127}
128
129
130
131// These safely set and get the value of the calling thread's errno
132extern "C" errno_t _set_errno(int const value)
133{
135 if (!ptd)
136 return ENOMEM;
137
138 errno = value;
139 return 0;
140}
141
142extern "C" errno_t _get_errno(int* const result)
143{
145
146 // Unlike most of our globals, this one is guaranteed to give some answer
147 *result = errno;
148 return 0;
149}
150
151
152
153// These safely set and get the value of the calling thread's doserrno
154extern "C" errno_t _set_doserrno(unsigned long const value)
155{
157 if (!ptd)
158 return ENOMEM;
159
161 return 0;
162}
163
164extern "C" errno_t _get_doserrno(unsigned long* const result)
165{
167
168 // Unlike most of our globals, this one is guaranteed to give some answer:
169 *result = _doserrno;
170 return 0;
171}
172
173
174
175// These return pointers to the calling thread's errno and doserrno values,
176// respectively, and are used to implement errno and _doserrno in the header.
179
180extern "C" int* __cdecl _errno()
181{
183 if (!ptd)
184 {
185 return &errno_no_memory;
186 }
187
188 return &ptd->_terrno;
189}
190
191extern "C" unsigned long* __cdecl __doserrno()
192{
194 if (!ptd)
195 {
196 return &doserrno_no_memory;
197 }
198
199 return &ptd->_tdoserrno;
200}
#define ENOENT
Definition: acclib.h:79
#define EEXIST
Definition: acclib.h:88
#define EINVAL
Definition: acclib.h:90
#define EPIPE
Definition: acclib.h:91
#define ENOMEM
Definition: acclib.h:84
#define EACCES
Definition: acclib.h:85
#define EBADF
Definition: acclib.h:82
#define EAGAIN
Definition: acclib.h:83
#define __cdecl
Definition: accygwin.h:79
__acrt_ptd *__cdecl __acrt_getptd_noexit(void)
_In_ size_t const _In_ int _In_ bool const _In_ unsigned const _In_ __acrt_rounding_mode const _Inout_ __crt_cached_ptd_host & ptd
Definition: cvt.cpp:355
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
#define ERROR_INVALID_FUNCTION
Definition: dderror.h:6
#define ERROR_INVALID_PARAMETER
Definition: compat.h:101
#define ERROR_INVALID_HANDLE
Definition: compat.h:98
#define ERROR_ACCESS_DENIED
Definition: compat.h:97
#define ENOSPC
Definition: errno.h:34
#define EXDEV
Definition: errno.h:24
#define ECHILD
Definition: errno.h:16
#define ENOEXEC
Definition: errno.h:14
#define EMFILE
Definition: errno.h:30
#define E2BIG
Definition: errno.h:13
#define ERRTABLECOUNT
Definition: errno.cpp:76
errno_t _set_doserrno(unsigned long const value)
Definition: errno.cpp:154
static unsigned long doserrno_no_memory
Definition: errno.cpp:178
errno_t _set_errno(int const value)
Definition: errno.cpp:132
int __cdecl __acrt_errno_from_os_error(unsigned long const oserrno)
Definition: errno.cpp:103
#define MAX_EXEC_ERROR
Definition: errno.cpp:81
void __cdecl __acrt_errno_map_os_error(unsigned long const oserrno)
Definition: errno.cpp:91
errno_t _get_errno(int *const result)
Definition: errno.cpp:142
static int errno_no_memory
Definition: errno.cpp:177
#define MIN_EACCES_RANGE
Definition: errno.cpp:85
int *__cdecl _errno()
Definition: errno.cpp:180
static errentry const errtable[]
Definition: errno.cpp:27
unsigned long *__cdecl __doserrno()
Definition: errno.cpp:191
void __cdecl __acrt_errno_map_os_error_ptd(unsigned long const oserrno, __crt_cached_ptd_host &ptd)
Definition: errno.cpp:97
#define MIN_EXEC_ERROR
Definition: errno.cpp:80
#define MAX_EACCES_RANGE
Definition: errno.cpp:86
errno_t _get_doserrno(unsigned long *const result)
Definition: errno.cpp:164
GLuint64EXT * result
Definition: glext.h:11304
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
#define _doserrno
Definition: stdlib.h:131
#define _VALIDATE_RETURN_NOERRNO(expr, retexpr)
#define ERROR_ALREADY_EXISTS
Definition: disk.h:80
#define ERROR_FILE_NOT_FOUND
Definition: disk.h:79
#define ENOTEMPTY
Definition: errno.h:58
#define errno
Definition: errno.h:18
#define EILSEQ
Definition: errno.h:109
int errno_t
Definition: corecrt.h:615
Definition: pdh_main.c:94
#define ERROR_BAD_NETPATH
Definition: winerror.h:145
#define ERROR_NOT_ENOUGH_QUOTA
Definition: winerror.h:1123
#define ERROR_CURRENT_DIRECTORY
Definition: winerror.h:119
#define ERROR_DIRECT_ACCESS_HANDLE
Definition: winerror.h:202
#define ERROR_NESTING_NOT_ALLOWED
Definition: winerror.h:270
#define ERROR_BAD_ENVIRONMENT
Definition: winerror.h:113
#define ERROR_TOO_MANY_OPEN_FILES
Definition: winerror.h:107
#define ERROR_INVALID_BLOCK
Definition: winerror.h:112
#define ERROR_PATH_NOT_FOUND
Definition: winerror.h:106
#define ERROR_NEGATIVE_SEEK
Definition: winerror.h:203
#define ERROR_BAD_PATHNAME
Definition: winerror.h:233
#define ERROR_NO_PROC_SLOTS
Definition: winerror.h:173
#define ERROR_DISK_FULL
Definition: winerror.h:186
#define ERROR_INVALID_TARGET_HANDLE
Definition: winerror.h:188
#define ERROR_BROKEN_PIPE
Definition: winerror.h:183
#define ERROR_NOT_LOCKED
Definition: winerror.h:230
#define ERROR_CHILD_NOT_COMPLETE
Definition: winerror.h:201
#define ERROR_DIR_NOT_EMPTY
Definition: winerror.h:217
#define ERROR_FILE_EXISTS
Definition: winerror.h:165
#define ERROR_LOCK_VIOLATION
Definition: winerror.h:136
#define ERROR_SEEK_ON_DEVICE
Definition: winerror.h:204
#define ERROR_CANNOT_MAKE
Definition: winerror.h:166
#define ERROR_FAIL_I24
Definition: winerror.h:167
#define ERROR_NO_MORE_FILES
Definition: winerror.h:121
#define ERROR_WAIT_NO_CHILDREN
Definition: winerror.h:200
#define ERROR_NOT_SAME_DEVICE
Definition: winerror.h:120
#define ERROR_BAD_FORMAT
Definition: winerror.h:114
#define ERROR_NO_UNICODE_TRANSLATION
Definition: winerror.h:649
#define ERROR_FILENAME_EXCED_RANGE
Definition: winerror.h:263
#define ERROR_DRIVE_LOCKED
Definition: winerror.h:182
#define ERROR_LOCK_FAILED
Definition: winerror.h:236
#define ERROR_MAX_THRDS_REACHED
Definition: winerror.h:235
#define ERROR_INVALID_ACCESS
Definition: winerror.h:115
#define ERROR_NETWORK_ACCESS_DENIED
Definition: winerror.h:157
#define ERROR_ARENA_TRASHED
Definition: winerror.h:110
#define ERROR_INVALID_DRIVE
Definition: winerror.h:118
#define ERROR_INVALID_DATA
Definition: winerror.h:116
#define ERROR_BAD_NET_NAME
Definition: winerror.h:159