Release 960623
[wine] / win32 / error.c
1 /*
2  * Win32 kernel functions
3  *
4  * Copyright 1995 Martin von Loewis and Cameron Heide
5  */
6
7 #include <stdio.h>
8 #include <errno.h>
9 #include "windows.h"
10 #include "winerror.h"
11 #include "kernel32.h"
12 #include "stddebug.h"
13 #include "debug.h"
14
15 /* WIN32_LastError contains the last error that occurred in the
16  * Win32 API.  This value should be stored separately for each
17  * thread, when we eventually get thread support.
18  */
19 static int WIN32_LastError;
20
21 /* The errno_xlat_table contains the errno-to-Win32 error
22  * mapping.  Since this is a single table, it can't easily
23  * take into account function-specific differences, so there
24  * will probably be quite a few points where we don't exactly
25  * match what NT would return.  Then again, neither does
26  * Windows 95. :-)
27  */
28 typedef struct {
29     int         errno;
30     DWORD       win32err;
31 } ERRNO_XLAT_TABLE;
32
33 /* The table looks pretty ugly due to the preprocessor stuff,
34  * but I honestly have no idea how many of these values are
35  * portable.  I'm not even sure how many of them are even
36  * used at all. :-)
37  */
38 static ERRNO_XLAT_TABLE errno_xlat_table[] = {
39 #if defined(EPERM)
40     {   EPERM,          ERROR_ACCESS_DENIED             },
41 #endif
42 #if defined(ENOENT)
43     {   ENOENT,         ERROR_FILE_NOT_FOUND            },
44 #endif
45 #if defined(ESRCH)
46     {   ESRCH,          ERROR_INVALID_PARAMETER         },
47 #endif
48 #if defined(EIO)
49     {   EIO,            ERROR_IO_DEVICE                 },
50 #endif
51 #if defined(ENOEXEC)
52     {   ENOEXEC,        ERROR_BAD_FORMAT                },
53 #endif
54 #if defined(EBADF)
55     {   EBADF,          ERROR_INVALID_HANDLE            },
56 #endif
57 #if defined(ENOMEM)
58     {   ENOMEM,         ERROR_OUTOFMEMORY               },
59 #endif
60 #if defined(EACCES)
61     {   EACCES,         ERROR_ACCESS_DENIED             },
62 #endif
63 #if defined(EBUSY)
64     {   EBUSY,          ERROR_BUSY                      },
65 #endif
66 #if defined(EEXIST)
67     {   EEXIST,         ERROR_FILE_EXISTS               },
68 #endif
69 #if defined(ENODEV)
70     {   ENODEV,         ERROR_BAD_DEVICE                },
71 #endif
72 #if defined(EINVAL)
73     {   EINVAL,         ERROR_INVALID_PARAMETER         },
74 #endif
75 #if defined(EMFILE)
76     {   EMFILE,         ERROR_TOO_MANY_OPEN_FILES       },
77 #endif
78 #if defined(ETXTBSY)
79     {   ETXTBSY,        ERROR_BUSY,                     },
80 #endif
81 #if defined(ENOSPC)
82     {   ENOSPC,         ERROR_DISK_FULL                 },
83 #endif
84 #if defined(ESPIPE)
85     {   ESPIPE,         ERROR_SEEK_ON_DEVICE            },
86 #endif
87 #if defined(EPIPE)
88     {   EPIPE,          ERROR_BROKEN_PIPE               },
89 #endif
90 #if defined(EDEADLK)
91     {   EDEADLK,        ERROR_POSSIBLE_DEADLOCK         },
92 #endif
93 #if defined(ENAMETOOLONG)
94     {   ENAMETOOLONG,   ERROR_FILENAME_EXCED_RANGE      },
95 #endif
96 #if defined(ENOTEMPTY)
97     {   ENOTEMPTY,      ERROR_DIR_NOT_EMPTY             },
98 #endif
99     {   -1,             0                               }
100 };
101
102 /**********************************************************************
103  *              GetLastError            (KERNEL32.227)
104  */
105 DWORD GetLastError(void)
106 {
107     return WIN32_LastError;
108 }
109
110 /**********************************************************************
111  *              SetLastError            (KERNEL32.497)
112  *
113  * This is probably not used by apps too much, but it's useful for
114  * our own internal use.
115  */
116 void SetLastError(DWORD error)
117 {
118     WIN32_LastError = error;
119 }
120
121 DWORD ErrnoToLastError(int errno_num)
122 {
123     DWORD rc = ERROR_UNKNOWN;
124     int i = 0;
125
126     while(errno_xlat_table[i].errno != -1)
127     {
128         if(errno_xlat_table[i].errno == errno_num)
129         {
130             rc = errno_xlat_table[i].win32err;
131             break;
132         }
133         i++;
134     }
135
136     return rc;
137 }