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