Release 980329
[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 "debug.h"
12
13 /* The errno_xlat_table contains the errno-to-Win32 error
14  * mapping.  Since this is a single table, it can't easily
15  * take into account function-specific differences, so there
16  * will probably be quite a few points where we don't exactly
17  * match what NT would return.  Then again, neither does
18  * Windows 95. :-)
19  */
20 typedef struct {
21     int         err;
22     DWORD       win32err;
23 } ERRNO_XLAT_TABLE;
24
25 /* The table looks pretty ugly due to the preprocessor stuff,
26  * but I honestly have no idea how many of these values are
27  * portable.  I'm not even sure how many of them are even
28  * used at all. :-)
29  */
30 static ERRNO_XLAT_TABLE errno_xlat_table[] = {
31 #if defined(EPERM)
32     {   EPERM,          ERROR_ACCESS_DENIED             },
33 #endif
34 #if defined(ENOENT)
35     {   ENOENT,         ERROR_FILE_NOT_FOUND            },
36 #endif
37 #if defined(ESRCH)
38     {   ESRCH,          ERROR_INVALID_PARAMETER         },
39 #endif
40 #if defined(EIO)
41     {   EIO,            ERROR_IO_DEVICE                 },
42 #endif
43 #if defined(ENOEXEC)
44     {   ENOEXEC,        ERROR_BAD_FORMAT                },
45 #endif
46 #if defined(EBADF)
47     {   EBADF,          ERROR_INVALID_HANDLE            },
48 #endif
49 #if defined(ENOMEM)
50     {   ENOMEM,         ERROR_OUTOFMEMORY               },
51 #endif
52 #if defined(EACCES)
53     {   EACCES,         ERROR_ACCESS_DENIED             },
54 #endif
55 #if defined(EBUSY)
56     {   EBUSY,          ERROR_BUSY                      },
57 #endif
58 #if defined(EEXIST)
59     {   EEXIST,         ERROR_FILE_EXISTS               },
60 #endif
61 #if defined(ENODEV)
62     {   ENODEV,         ERROR_BAD_DEVICE                },
63 #endif
64 #if defined(EINVAL)
65     {   EINVAL,         ERROR_INVALID_PARAMETER         },
66 #endif
67 #if defined(EMFILE)
68     {   EMFILE,         ERROR_TOO_MANY_OPEN_FILES       },
69 #endif
70 #if defined(ETXTBSY)
71     {   ETXTBSY,        ERROR_BUSY,                     },
72 #endif
73 #if defined(ENOSPC)
74     {   ENOSPC,         ERROR_DISK_FULL                 },
75 #endif
76 #if defined(ESPIPE)
77     {   ESPIPE,         ERROR_SEEK_ON_DEVICE            },
78 #endif
79 #if defined(EPIPE)
80     {   EPIPE,          ERROR_BROKEN_PIPE               },
81 #endif
82 #if defined(EDEADLK)
83     {   EDEADLK,        ERROR_POSSIBLE_DEADLOCK         },
84 #endif
85 #if defined(ENAMETOOLONG)
86     {   ENAMETOOLONG,   ERROR_FILENAME_EXCED_RANGE      },
87 #endif
88 #if defined(ENOTEMPTY)
89     {   ENOTEMPTY,      ERROR_DIR_NOT_EMPTY             },
90 #endif
91     {   -1,             0                               }
92 };
93
94 DWORD ErrnoToLastError(int errno_num)
95 {
96     DWORD rc = ERROR_UNKNOWN;
97     int i = 0;
98
99     while(errno_xlat_table[i].err != -1)
100     {
101         if(errno_xlat_table[i].err == errno_num)
102         {
103             rc = errno_xlat_table[i].win32err;
104             break;
105         }
106         i++;
107     }
108
109     return rc;
110 }
111
112 int LastErrorToErrno(DWORD lasterror)
113 {
114     int rc = 0; /* no error */
115     int i = 0;
116
117     while(errno_xlat_table[i].err != -1)
118     {
119         if(errno_xlat_table[i].win32err == lasterror )
120         {
121             rc = errno_xlat_table[i].err;
122             break;
123         }
124         i++;
125     }
126     return rc;
127 }