xref: /reactos/sdk/lib/crt/stdlib/errno.c (revision 0ea48e79)
1 /*
2  * COPYRIGHT:   See COPYING in the top level directory
3  * PROJECT:     ReactOS system libraries
4  * FILE:        lib/sdk/crt/stdlib/errno.c
5  * PURPOSE:     Unknown
6  * PROGRAMER:   Unknown
7  *
8  */
9 #include <precomp.h>
10 #include "doserrmap.h"
11 #include <errno.h>
12 #include <internal/wine/msvcrt.h>
13 
14 /*********************************************************************
15  *		_errno (MSVCRT.@)
16  */
_errno(void)17 int* CDECL _errno(void)
18 {
19     return &(msvcrt_get_thread_data()->thread_errno);
20 }
21 
22 /*********************************************************************
23  *		__doserrno (MSVCRT.@)
24  */
__doserrno(void)25 unsigned long* CDECL __doserrno(void)
26 {
27     return &(msvcrt_get_thread_data()->thread_doserrno);
28 }
29 
30 /*********************************************************************
31  *		_get_errno (MSVCRT.@)
32  */
_get_errno(int * pValue)33 errno_t CDECL _get_errno(int *pValue)
34 {
35     if (!pValue)
36         return EINVAL;
37 
38     *pValue = *_errno();
39     return 0;
40 }
41 
42 /*********************************************************************
43  *		_get_doserrno (MSVCRT.@)
44  */
_get_doserrno(unsigned long * pValue)45 errno_t CDECL _get_doserrno(unsigned long *pValue)
46 {
47     if (!pValue)
48         return EINVAL;
49 
50     *pValue = *__doserrno();
51     return 0;
52 }
53 
54 /*********************************************************************
55  *		_set_errno (MSVCRT.@)
56  */
_set_errno(int value)57 errno_t CDECL _set_errno(int value)
58 {
59     *_errno() = value;
60     return 0;
61 }
62 
63 /*********************************************************************
64  *		_set_doserrno (MSVCRT.@)
65  */
_set_doserrno(unsigned long value)66 errno_t CDECL _set_doserrno(unsigned long value)
67 {
68     *__doserrno() = value;
69     return 0;
70 }
71 
72 /*
73  * This function sets both doserrno to the passed in OS error code
74  * and also maps this to an appropriate errno code.  The mapping
75  * has been deduced automagically by running this functions, which
76  * exists in MSVCRT but is undocumented, on all the error codes in
77  * winerror.h.
78  */
_dosmaperr(unsigned long oserror)79 void CDECL _dosmaperr(unsigned long oserror)
80 {
81 	int pos, base, lim;
82 
83 	_set_doserrno(oserror);
84 
85 	/* Use binary chop to find the corresponding errno code */
86 	for (base=0, lim=sizeof(doserrmap)/sizeof(doserrmap[0]); lim; lim >>= 1) {
87 		pos = base+(lim >> 1);
88 		if (doserrmap[pos].winerr == oserror) {
89 			_set_errno(doserrmap[pos].en);
90 			return;
91 		} else if (doserrmap[pos].winerr < oserror) {
92 			base = pos + 1;
93 			--lim;
94 		}
95 	}
96 	/* EINVAL appears to be the default */
97 	_set_errno(EINVAL);
98 }
99 
100 /******************************************************************************
101 *              _set_error_mode (MSVCRT.@)
102 *
103 * Set the error mode, which describes where the C run-time writes error
104 * messages.
105 *
106 * PARAMS
107 *   mode - the new error mode
108 *
109 * RETURNS
110 *   The old error mode.
111 *
112 */
113 int msvcrt_error_mode = MSVCRT__OUT_TO_DEFAULT;
114 
_set_error_mode(int mode)115 int CDECL _set_error_mode(int mode)
116 {
117     const int old = msvcrt_error_mode;
118     if ( MSVCRT__REPORT_ERRMODE != mode ) {
119         msvcrt_error_mode = mode;
120     }
121     return old;
122 }
123 
124 /******************************************************************************
125  *		_seterrormode (MSVCRT.@)
126  */
_seterrormode(int mode)127 void CDECL _seterrormode(int mode)
128 {
129     SetErrorMode( mode );
130 }
131