Delete _MSC_VER when not necessary, fix #10511
[ghc.git] / libraries / base / cbits / Win32Utils.c
1 /* ----------------------------------------------------------------------------
2 (c) The University of Glasgow 2006
3
4 Useful Win32 bits
5 ------------------------------------------------------------------------- */
6
7 #if defined(_WIN32)
8
9 #include "HsBase.h"
10
11 /* This is the error table that defines the mapping between OS error
12 codes and errno values */
13
14 struct errentry {
15 unsigned long oscode; /* OS return value */
16 int errnocode; /* System V error code */
17 };
18
19 static struct errentry errtable[] = {
20 { ERROR_INVALID_FUNCTION, EINVAL }, /* 1 */
21 { ERROR_FILE_NOT_FOUND, ENOENT }, /* 2 */
22 { ERROR_PATH_NOT_FOUND, ENOENT }, /* 3 */
23 { ERROR_TOO_MANY_OPEN_FILES, EMFILE }, /* 4 */
24 { ERROR_ACCESS_DENIED, EACCES }, /* 5 */
25 { ERROR_INVALID_HANDLE, EBADF }, /* 6 */
26 { ERROR_ARENA_TRASHED, ENOMEM }, /* 7 */
27 { ERROR_NOT_ENOUGH_MEMORY, ENOMEM }, /* 8 */
28 { ERROR_INVALID_BLOCK, ENOMEM }, /* 9 */
29 { ERROR_BAD_ENVIRONMENT, E2BIG }, /* 10 */
30 { ERROR_BAD_FORMAT, ENOEXEC }, /* 11 */
31 { ERROR_INVALID_ACCESS, EINVAL }, /* 12 */
32 { ERROR_INVALID_DATA, EINVAL }, /* 13 */
33 { ERROR_INVALID_DRIVE, ENOENT }, /* 15 */
34 { ERROR_CURRENT_DIRECTORY, EACCES }, /* 16 */
35 { ERROR_NOT_SAME_DEVICE, EXDEV }, /* 17 */
36 { ERROR_NO_MORE_FILES, ENOENT }, /* 18 */
37 { ERROR_LOCK_VIOLATION, EACCES }, /* 33 */
38 { ERROR_BAD_NETPATH, ENOENT }, /* 53 */
39 { ERROR_NETWORK_ACCESS_DENIED, EACCES }, /* 65 */
40 { ERROR_BAD_NET_NAME, ENOENT }, /* 67 */
41 { ERROR_FILE_EXISTS, EEXIST }, /* 80 */
42 { ERROR_CANNOT_MAKE, EACCES }, /* 82 */
43 { ERROR_FAIL_I24, EACCES }, /* 83 */
44 { ERROR_INVALID_PARAMETER, EINVAL }, /* 87 */
45 { ERROR_NO_PROC_SLOTS, EAGAIN }, /* 89 */
46 { ERROR_DRIVE_LOCKED, EACCES }, /* 108 */
47 { ERROR_BROKEN_PIPE, EPIPE }, /* 109 */
48 { ERROR_DISK_FULL, ENOSPC }, /* 112 */
49 { ERROR_INVALID_TARGET_HANDLE, EBADF }, /* 114 */
50 { ERROR_INVALID_HANDLE, EINVAL }, /* 124 */
51 { ERROR_WAIT_NO_CHILDREN, ECHILD }, /* 128 */
52 { ERROR_CHILD_NOT_COMPLETE, ECHILD }, /* 129 */
53 { ERROR_DIRECT_ACCESS_HANDLE, EBADF }, /* 130 */
54 { ERROR_NEGATIVE_SEEK, EINVAL }, /* 131 */
55 { ERROR_SEEK_ON_DEVICE, EACCES }, /* 132 */
56 { ERROR_DIR_NOT_EMPTY, ENOTEMPTY }, /* 145 */
57 { ERROR_NOT_LOCKED, EACCES }, /* 158 */
58 { ERROR_BAD_PATHNAME, ENOENT }, /* 161 */
59 { ERROR_MAX_THRDS_REACHED, EAGAIN }, /* 164 */
60 { ERROR_LOCK_FAILED, EACCES }, /* 167 */
61 { ERROR_ALREADY_EXISTS, EEXIST }, /* 183 */
62 { ERROR_FILENAME_EXCED_RANGE, ENOENT }, /* 206 */
63 { ERROR_NESTING_NOT_ALLOWED, EAGAIN }, /* 215 */
64 /* Windows returns this when the read end of a pipe is
65 * closed (or closing) and we write to it. */
66 { ERROR_NO_DATA, EPIPE }, /* 232 */
67 { ERROR_NOT_ENOUGH_QUOTA, ENOMEM } /* 1816 */
68 };
69
70 /* size of the table */
71 #define ERRTABLESIZE (sizeof(errtable)/sizeof(errtable[0]))
72
73 /* The following two constants must be the minimum and maximum
74 values in the (contiguous) range of Exec Failure errors. */
75 #define MIN_EXEC_ERROR ERROR_INVALID_STARTING_CODESEG
76 #define MAX_EXEC_ERROR ERROR_INFLOOP_IN_RELOC_CHAIN
77
78 /* These are the low and high value in the range of errors that are
79 access violations */
80 #define MIN_EACCES_RANGE ERROR_WRITE_PROTECT
81 #define MAX_EACCES_RANGE ERROR_SHARING_BUFFER_EXCEEDED
82
83 void maperrno(void)
84 {
85 errno = maperrno_func(GetLastError());
86 }
87
88 int maperrno_func(DWORD dwErrorCode)
89 {
90 int i;
91
92 /* check the table for the OS error code */
93 for (i = 0; i < ERRTABLESIZE; ++i)
94 if (dwErrorCode == errtable[i].oscode)
95 return errtable[i].errnocode;
96
97 /* The error code wasn't in the table. We check for a range of */
98 /* EACCES errors or exec failure errors (ENOEXEC). Otherwise */
99 /* EINVAL is returned. */
100
101 if (dwErrorCode >= MIN_EACCES_RANGE && dwErrorCode <= MAX_EACCES_RANGE)
102 return EACCES;
103 else if (dwErrorCode >= MIN_EXEC_ERROR && dwErrorCode <= MAX_EXEC_ERROR)
104 return ENOEXEC;
105 else
106 return EINVAL;
107 }
108
109 LPWSTR base_getErrorMessage(DWORD err)
110 {
111 LPWSTR what;
112 DWORD res;
113
114 res = FormatMessageW(
115 (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER),
116 NULL,
117 err,
118 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
119 (LPWSTR) &what,
120 0,
121 NULL
122 );
123 if (res == 0)
124 return NULL;
125 return what;
126 }
127
128 int get_unique_file_info(int fd, HsWord64 *dev, HsWord64 *ino)
129 {
130 HANDLE h = (HANDLE)_get_osfhandle(fd);
131 BY_HANDLE_FILE_INFORMATION info;
132
133 if (GetFileInformationByHandle(h, &info))
134 {
135 *dev = info.dwVolumeSerialNumber;
136 *ino = info.nFileIndexLow
137 | ((HsWord64)info.nFileIndexHigh << 32);
138
139 return 0;
140 }
141
142 return -1;
143 }
144
145 BOOL file_exists(LPCTSTR path)
146 {
147 DWORD r = GetFileAttributes(path);
148 return r != INVALID_FILE_ATTRIBUTES;
149 }
150
151 #endif