1
/* Copyright (c) 2008 MySQL AB, 2009 Sun Microsystems, Inc.
2
Use is subject to license terms.
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation; version 2 of the License.
8
This program is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
GNU General Public License for more details.
13
You should have received a copy of the GNU General Public License
14
along with this program; if not, write to the Free Software
15
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
18
Convert Windows API error (GetLastError() to Posix equivalent (errno)
19
The exported function my_osmaperr() is modelled after and borrows
20
heavily from undocumented _dosmaperr()(found of the static Microsoft C runtime).
23
#include <my_global.h>
29
unsigned long oscode; /* OS return value */
30
int sysv_errno; /* System V error code */
33
static struct errentry errtable[]= {
34
{ ERROR_INVALID_FUNCTION, EINVAL }, /* 1 */
35
{ ERROR_FILE_NOT_FOUND, ENOENT }, /* 2 */
36
{ ERROR_PATH_NOT_FOUND, ENOENT }, /* 3 */
37
{ ERROR_TOO_MANY_OPEN_FILES, EMFILE }, /* 4 */
38
{ ERROR_ACCESS_DENIED, EACCES }, /* 5 */
39
{ ERROR_INVALID_HANDLE, EBADF }, /* 6 */
40
{ ERROR_ARENA_TRASHED, ENOMEM }, /* 7 */
41
{ ERROR_NOT_ENOUGH_MEMORY, ENOMEM }, /* 8 */
42
{ ERROR_INVALID_BLOCK, ENOMEM }, /* 9 */
43
{ ERROR_BAD_ENVIRONMENT, E2BIG }, /* 10 */
44
{ ERROR_BAD_FORMAT, ENOEXEC }, /* 11 */
45
{ ERROR_INVALID_ACCESS, EINVAL }, /* 12 */
46
{ ERROR_INVALID_DATA, EINVAL }, /* 13 */
47
{ ERROR_INVALID_DRIVE, ENOENT }, /* 15 */
48
{ ERROR_CURRENT_DIRECTORY, EACCES }, /* 16 */
49
{ ERROR_NOT_SAME_DEVICE, EXDEV }, /* 17 */
50
{ ERROR_NO_MORE_FILES, ENOENT }, /* 18 */
51
{ ERROR_LOCK_VIOLATION, EACCES }, /* 33 */
52
{ ERROR_BAD_NETPATH, ENOENT }, /* 53 */
53
{ ERROR_NETWORK_ACCESS_DENIED, EACCES }, /* 65 */
54
{ ERROR_BAD_NET_NAME, ENOENT }, /* 67 */
55
{ ERROR_FILE_EXISTS, EEXIST }, /* 80 */
56
{ ERROR_CANNOT_MAKE, EACCES }, /* 82 */
57
{ ERROR_FAIL_I24, EACCES }, /* 83 */
58
{ ERROR_INVALID_PARAMETER, EINVAL }, /* 87 */
59
{ ERROR_NO_PROC_SLOTS, EAGAIN }, /* 89 */
60
{ ERROR_DRIVE_LOCKED, EACCES }, /* 108 */
61
{ ERROR_BROKEN_PIPE, EPIPE }, /* 109 */
62
{ ERROR_DISK_FULL, ENOSPC }, /* 112 */
63
{ ERROR_INVALID_TARGET_HANDLE, EBADF }, /* 114 */
64
{ ERROR_INVALID_HANDLE, EINVAL }, /* 124 */
65
{ ERROR_WAIT_NO_CHILDREN, ECHILD }, /* 128 */
66
{ ERROR_CHILD_NOT_COMPLETE, ECHILD }, /* 129 */
67
{ ERROR_DIRECT_ACCESS_HANDLE, EBADF }, /* 130 */
68
{ ERROR_NEGATIVE_SEEK, EINVAL }, /* 131 */
69
{ ERROR_SEEK_ON_DEVICE, EACCES }, /* 132 */
70
{ ERROR_DIR_NOT_EMPTY, ENOTEMPTY }, /* 145 */
71
{ ERROR_NOT_LOCKED, EACCES }, /* 158 */
72
{ ERROR_BAD_PATHNAME, ENOENT }, /* 161 */
73
{ ERROR_MAX_THRDS_REACHED, EAGAIN }, /* 164 */
74
{ ERROR_LOCK_FAILED, EACCES }, /* 167 */
75
{ ERROR_ALREADY_EXISTS, EEXIST }, /* 183 */
76
{ ERROR_FILENAME_EXCED_RANGE, ENOENT }, /* 206 */
77
{ ERROR_NESTING_NOT_ALLOWED, EAGAIN }, /* 215 */
78
{ ERROR_NOT_ENOUGH_QUOTA, ENOMEM } /* 1816 */
81
/* size of the table */
82
#define ERRTABLESIZE (sizeof(errtable)/sizeof(errtable[0]))
84
/* The following two constants must be the minimum and maximum
85
values in the (contiguous) range of Exec Failure errors. */
86
#define MIN_EXEC_ERROR ERROR_INVALID_STARTING_CODESEG
87
#define MAX_EXEC_ERROR ERROR_INFLOOP_IN_RELOC_CHAIN
89
/* These are the low and high value in the range of errors that are
91
#define MIN_EACCES_RANGE ERROR_WRITE_PROTECT
92
#define MAX_EACCES_RANGE ERROR_SHARING_BUFFER_EXCEEDED
95
static int get_errno_from_oserr(unsigned long oserrno)
99
/* check the table for the OS error code */
100
for (i= 0; i < ERRTABLESIZE; ++i)
102
if (oserrno == errtable[i].oscode)
104
return errtable[i].sysv_errno;
108
/* The error code wasn't in the table. We check for a range of */
109
/* EACCES errors or exec failure errors (ENOEXEC). Otherwise */
110
/* EINVAL is returned. */
112
if (oserrno >= MIN_EACCES_RANGE && oserrno <= MAX_EACCES_RANGE)
114
else if (oserrno >= MIN_EXEC_ERROR && oserrno <= MAX_EXEC_ERROR)
120
/* Set errno corresponsing to GetLastError() value */
121
void my_osmaperr ( unsigned long oserrno)
123
errno= get_errno_from_oserr(oserrno);