e2ae388324ff9a59d354478a553b6f40fc2f6447
[reactos.git] / reactos / lib / crt / stdlib / errno.c
1 /* $Id$
2 *
3 */
4 #include "precomp.h"
5 #include <internal/tls.h>
6
7 #include "doserrmap.h"
8
9 /*
10 * @implemented
11 */
12 int* __doserrno(void)
13 {
14 return (int*)(&GetThreadData()->tdoserrno);
15 }
16
17 /*
18 * @implemented
19 */
20 int *_errno(void)
21 {
22 return(&GetThreadData()->terrno);
23 }
24
25
26 int __set_doserrno(int error)
27 {
28 PTHREADDATA ThreadData;
29
30 ThreadData = GetThreadData();
31 if (ThreadData)
32 ThreadData->tdoserrno = error;
33
34 return(error);
35 }
36
37 int __set_errno(int error)
38 {
39 PTHREADDATA ThreadData;
40
41 ThreadData = GetThreadData();
42 if (ThreadData)
43 ThreadData->terrno = error;
44
45 return(error);
46 }
47
48 /*
49 * This function sets both doserrno to the passed in OS error code
50 * and also maps this to an appropriate errno code. The mapping
51 * has been deduced automagically by running this functions, which
52 * exists in MSVCRT but is undocumented, on all the error codes in
53 * winerror.h.
54 */
55 void _dosmaperr(unsigned long oserror)
56 {
57 int pos, base, lim;
58
59 __set_doserrno(oserror);
60
61 /* Use binary chop to find the corresponding errno code */
62 for (base=0, lim=sizeof(doserrmap)/sizeof(doserrmap[0]); lim; lim >>= 1) {
63 pos = base+(lim >> 1);
64 if (doserrmap[pos].winerr == oserror) {
65 __set_errno(doserrmap[pos].en);
66 return;
67 } else if (doserrmap[pos].winerr < oserror) {
68 base = pos + 1;
69 --lim;
70 }
71 }
72 /* EINVAL appears to be the default */
73 __set_errno(EINVAL);
74 }
75
76 /* EOF */