]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/windows/c.rs
tests: prefer edition: directives to compile-flags:--edition.
[rust.git] / src / libstd / sys / windows / c.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! C definitions used by libnative that don't belong in liblibc
12
13 #![allow(bad_style)]
14 #![cfg_attr(test, allow(dead_code))]
15 #![unstable(issue = "0", feature = "windows_c")]
16
17 use os::raw::{c_int, c_uint, c_ulong, c_long, c_longlong, c_ushort, c_char};
18 #[cfg(target_arch = "x86_64")]
19 use os::raw::c_ulonglong;
20 use libc::{wchar_t, size_t, c_void};
21 use ptr;
22
23 pub use self::FILE_INFO_BY_HANDLE_CLASS::*;
24 pub use self::EXCEPTION_DISPOSITION::*;
25
26 pub type DWORD = c_ulong;
27 pub type HANDLE = LPVOID;
28 pub type HINSTANCE = HANDLE;
29 pub type HMODULE = HINSTANCE;
30 pub type HRESULT = LONG;
31 pub type BOOL = c_int;
32 pub type BYTE = u8;
33 pub type BOOLEAN = BYTE;
34 pub type GROUP = c_uint;
35 pub type LARGE_INTEGER = c_longlong;
36 pub type LONG = c_long;
37 pub type UINT = c_uint;
38 pub type WCHAR = u16;
39 pub type USHORT = c_ushort;
40 pub type SIZE_T = usize;
41 pub type WORD = u16;
42 pub type CHAR = c_char;
43 pub type ULONG_PTR = usize;
44 pub type ULONG = c_ulong;
45 #[cfg(target_arch = "x86_64")]
46 pub type ULONGLONG = u64;
47 #[cfg(target_arch = "x86_64")]
48 pub type DWORDLONG = ULONGLONG;
49
50 pub type LPBOOL = *mut BOOL;
51 pub type LPBYTE = *mut BYTE;
52 pub type LPBY_HANDLE_FILE_INFORMATION = *mut BY_HANDLE_FILE_INFORMATION;
53 pub type LPCSTR = *const CHAR;
54 pub type LPCVOID = *const c_void;
55 pub type LPCWSTR = *const WCHAR;
56 pub type LPDWORD = *mut DWORD;
57 pub type LPHANDLE = *mut HANDLE;
58 pub type LPOVERLAPPED = *mut OVERLAPPED;
59 pub type LPPROCESS_INFORMATION = *mut PROCESS_INFORMATION;
60 pub type LPSECURITY_ATTRIBUTES = *mut SECURITY_ATTRIBUTES;
61 pub type LPSTARTUPINFO = *mut STARTUPINFO;
62 pub type LPVOID = *mut c_void;
63 pub type LPWCH = *mut WCHAR;
64 pub type LPWIN32_FIND_DATAW = *mut WIN32_FIND_DATAW;
65 pub type LPWSADATA = *mut WSADATA;
66 pub type LPWSAPROTOCOL_INFO = *mut WSAPROTOCOL_INFO;
67 pub type LPSTR = *mut CHAR;
68 pub type LPWSTR = *mut WCHAR;
69 pub type LPFILETIME = *mut FILETIME;
70
71 pub type PCONDITION_VARIABLE = *mut CONDITION_VARIABLE;
72 pub type PLARGE_INTEGER = *mut c_longlong;
73 pub type PSRWLOCK = *mut SRWLOCK;
74
75 pub type SOCKET = ::os::windows::raw::SOCKET;
76 pub type socklen_t = c_int;
77 pub type ADDRESS_FAMILY = USHORT;
78
79 pub const TRUE: BOOL = 1;
80 pub const FALSE: BOOL = 0;
81
82 pub const FILE_ATTRIBUTE_READONLY: DWORD = 0x1;
83 pub const FILE_ATTRIBUTE_DIRECTORY: DWORD = 0x10;
84 pub const FILE_ATTRIBUTE_REPARSE_POINT: DWORD = 0x400;
85
86 pub const FILE_SHARE_DELETE: DWORD = 0x4;
87 pub const FILE_SHARE_READ: DWORD = 0x1;
88 pub const FILE_SHARE_WRITE: DWORD = 0x2;
89
90 pub const CREATE_ALWAYS: DWORD = 2;
91 pub const CREATE_NEW: DWORD = 1;
92 pub const OPEN_ALWAYS: DWORD = 4;
93 pub const OPEN_EXISTING: DWORD = 3;
94 pub const TRUNCATE_EXISTING: DWORD = 5;
95
96 pub const FILE_WRITE_DATA: DWORD = 0x00000002;
97 pub const FILE_APPEND_DATA: DWORD = 0x00000004;
98 pub const FILE_WRITE_EA: DWORD = 0x00000010;
99 pub const FILE_WRITE_ATTRIBUTES: DWORD = 0x00000100;
100 pub const READ_CONTROL: DWORD = 0x00020000;
101 pub const SYNCHRONIZE: DWORD = 0x00100000;
102 pub const GENERIC_READ: DWORD = 0x80000000;
103 pub const GENERIC_WRITE: DWORD = 0x40000000;
104 pub const STANDARD_RIGHTS_WRITE: DWORD = READ_CONTROL;
105 pub const FILE_GENERIC_WRITE: DWORD = STANDARD_RIGHTS_WRITE | FILE_WRITE_DATA |
106                                       FILE_WRITE_ATTRIBUTES |
107                                       FILE_WRITE_EA |
108                                       FILE_APPEND_DATA |
109                                       SYNCHRONIZE;
110
111 pub const FILE_FLAG_OPEN_REPARSE_POINT: DWORD = 0x00200000;
112 pub const FILE_FLAG_BACKUP_SEMANTICS: DWORD = 0x02000000;
113 pub const SECURITY_SQOS_PRESENT: DWORD = 0x00100000;
114
115 pub const FIONBIO: c_ulong = 0x8004667e;
116
117 #[repr(C)]
118 #[derive(Copy)]
119 pub struct WIN32_FIND_DATAW {
120     pub dwFileAttributes: DWORD,
121     pub ftCreationTime: FILETIME,
122     pub ftLastAccessTime: FILETIME,
123     pub ftLastWriteTime: FILETIME,
124     pub nFileSizeHigh: DWORD,
125     pub nFileSizeLow: DWORD,
126     pub dwReserved0: DWORD,
127     pub dwReserved1: DWORD,
128     pub cFileName: [wchar_t; 260], // #define MAX_PATH 260
129     pub cAlternateFileName: [wchar_t; 14],
130 }
131 impl Clone for WIN32_FIND_DATAW {
132     fn clone(&self) -> Self { *self }
133 }
134
135 pub const WSA_FLAG_OVERLAPPED: DWORD = 0x01;
136
137 pub const WSADESCRIPTION_LEN: usize = 256;
138 pub const WSASYS_STATUS_LEN: usize = 128;
139 pub const WSAPROTOCOL_LEN: DWORD = 255;
140 pub const INVALID_SOCKET: SOCKET = !0;
141
142 pub const WSAEACCES: c_int = 10013;
143 pub const WSAEINVAL: c_int = 10022;
144 pub const WSAEWOULDBLOCK: c_int = 10035;
145 pub const WSAEADDRINUSE: c_int = 10048;
146 pub const WSAEADDRNOTAVAIL: c_int = 10049;
147 pub const WSAECONNABORTED: c_int = 10053;
148 pub const WSAECONNRESET: c_int = 10054;
149 pub const WSAENOTCONN: c_int = 10057;
150 pub const WSAESHUTDOWN: c_int = 10058;
151 pub const WSAETIMEDOUT: c_int = 10060;
152 pub const WSAECONNREFUSED: c_int = 10061;
153
154 pub const MAX_PROTOCOL_CHAIN: DWORD = 7;
155
156 pub const TOKEN_READ: DWORD = 0x20008;
157 pub const MAXIMUM_REPARSE_DATA_BUFFER_SIZE: usize = 16 * 1024;
158 pub const FSCTL_GET_REPARSE_POINT: DWORD = 0x900a8;
159 pub const IO_REPARSE_TAG_SYMLINK: DWORD = 0xa000000c;
160 pub const IO_REPARSE_TAG_MOUNT_POINT: DWORD = 0xa0000003;
161 pub const SYMLINK_FLAG_RELATIVE: DWORD = 0x00000001;
162 pub const FSCTL_SET_REPARSE_POINT: DWORD = 0x900a4;
163
164 pub const SYMBOLIC_LINK_FLAG_DIRECTORY: DWORD = 0x1;
165 pub const SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE: DWORD = 0x2;
166
167 // Note that these are not actually HANDLEs, just values to pass to GetStdHandle
168 pub const STD_INPUT_HANDLE: DWORD = -10i32 as DWORD;
169 pub const STD_OUTPUT_HANDLE: DWORD = -11i32 as DWORD;
170 pub const STD_ERROR_HANDLE: DWORD = -12i32 as DWORD;
171
172 pub const HANDLE_FLAG_INHERIT: DWORD = 0x00000001;
173
174 pub const PROGRESS_CONTINUE: DWORD = 0;
175
176 pub const ERROR_FILE_NOT_FOUND: DWORD = 2;
177 pub const ERROR_PATH_NOT_FOUND: DWORD = 3;
178 pub const ERROR_ACCESS_DENIED: DWORD = 5;
179 pub const ERROR_INVALID_HANDLE: DWORD = 6;
180 pub const ERROR_NO_MORE_FILES: DWORD = 18;
181 pub const ERROR_HANDLE_EOF: DWORD = 38;
182 pub const ERROR_FILE_EXISTS: DWORD = 80;
183 pub const ERROR_INVALID_PARAMETER: DWORD = 87;
184 pub const ERROR_BROKEN_PIPE: DWORD = 109;
185 pub const ERROR_CALL_NOT_IMPLEMENTED: DWORD = 120;
186 pub const ERROR_INSUFFICIENT_BUFFER: DWORD = 122;
187 pub const ERROR_ALREADY_EXISTS: DWORD = 183;
188 pub const ERROR_NO_DATA: DWORD = 232;
189 pub const ERROR_ENVVAR_NOT_FOUND: DWORD = 203;
190 pub const ERROR_OPERATION_ABORTED: DWORD = 995;
191 pub const ERROR_IO_PENDING: DWORD = 997;
192 pub const ERROR_TIMEOUT: DWORD = 0x5B4;
193
194 pub const E_NOTIMPL: HRESULT = 0x80004001u32 as HRESULT;
195
196 pub const INVALID_HANDLE_VALUE: HANDLE = !0 as HANDLE;
197
198 pub const FACILITY_NT_BIT: DWORD = 0x1000_0000;
199
200 pub const FORMAT_MESSAGE_FROM_SYSTEM: DWORD = 0x00001000;
201 pub const FORMAT_MESSAGE_FROM_HMODULE: DWORD = 0x00000800;
202 pub const FORMAT_MESSAGE_IGNORE_INSERTS: DWORD = 0x00000200;
203
204 pub const TLS_OUT_OF_INDEXES: DWORD = 0xFFFFFFFF;
205
206 pub const DLL_THREAD_DETACH: DWORD = 3;
207 pub const DLL_PROCESS_DETACH: DWORD = 0;
208
209 pub const INFINITE: DWORD = !0;
210
211 pub const DUPLICATE_SAME_ACCESS: DWORD = 0x00000002;
212
213 pub const CONDITION_VARIABLE_INIT: CONDITION_VARIABLE = CONDITION_VARIABLE {
214     ptr: ptr::null_mut(),
215 };
216 pub const SRWLOCK_INIT: SRWLOCK = SRWLOCK { ptr: ptr::null_mut() };
217
218 pub const DETACHED_PROCESS: DWORD = 0x00000008;
219 pub const CREATE_NEW_PROCESS_GROUP: DWORD = 0x00000200;
220 pub const CREATE_UNICODE_ENVIRONMENT: DWORD = 0x00000400;
221 pub const STARTF_USESTDHANDLES: DWORD = 0x00000100;
222
223 pub const AF_INET: c_int = 2;
224 pub const AF_INET6: c_int = 23;
225 pub const SD_BOTH: c_int = 2;
226 pub const SD_RECEIVE: c_int = 0;
227 pub const SD_SEND: c_int = 1;
228 pub const SOCK_DGRAM: c_int = 2;
229 pub const SOCK_STREAM: c_int = 1;
230 pub const SOL_SOCKET: c_int = 0xffff;
231 pub const SO_RCVTIMEO: c_int = 0x1006;
232 pub const SO_SNDTIMEO: c_int = 0x1005;
233 pub const SO_REUSEADDR: c_int = 0x0004;
234 pub const IPPROTO_IP: c_int = 0;
235 pub const IPPROTO_TCP: c_int = 6;
236 pub const IPPROTO_IPV6: c_int = 41;
237 pub const TCP_NODELAY: c_int = 0x0001;
238 pub const IP_TTL: c_int = 4;
239 pub const IPV6_V6ONLY: c_int = 27;
240 pub const SO_ERROR: c_int = 0x1007;
241 pub const SO_BROADCAST: c_int = 0x0020;
242 pub const IP_MULTICAST_LOOP: c_int = 11;
243 pub const IPV6_MULTICAST_LOOP: c_int = 11;
244 pub const IP_MULTICAST_TTL: c_int = 10;
245 pub const IP_ADD_MEMBERSHIP: c_int = 12;
246 pub const IP_DROP_MEMBERSHIP: c_int = 13;
247 pub const IPV6_ADD_MEMBERSHIP: c_int = 12;
248 pub const IPV6_DROP_MEMBERSHIP: c_int = 13;
249 pub const MSG_PEEK: c_int = 0x2;
250
251 #[repr(C)]
252 pub struct ip_mreq {
253     pub imr_multiaddr: in_addr,
254     pub imr_interface: in_addr,
255 }
256
257 #[repr(C)]
258 pub struct ipv6_mreq {
259     pub ipv6mr_multiaddr: in6_addr,
260     pub ipv6mr_interface: c_uint,
261 }
262
263 pub const VOLUME_NAME_DOS: DWORD = 0x0;
264 pub const MOVEFILE_REPLACE_EXISTING: DWORD = 1;
265
266 pub const FILE_BEGIN: DWORD = 0;
267 pub const FILE_CURRENT: DWORD = 1;
268 pub const FILE_END: DWORD = 2;
269
270 pub const WAIT_OBJECT_0: DWORD = 0x00000000;
271 pub const WAIT_TIMEOUT: DWORD = 258;
272 pub const WAIT_FAILED: DWORD = 0xFFFFFFFF;
273
274 #[cfg(target_env = "msvc")]
275 #[cfg(feature = "backtrace")]
276 pub const MAX_SYM_NAME: usize = 2000;
277 #[cfg(target_arch = "x86")]
278 #[cfg(feature = "backtrace")]
279 pub const IMAGE_FILE_MACHINE_I386: DWORD = 0x014c;
280 #[cfg(target_arch = "x86_64")]
281 #[cfg(feature = "backtrace")]
282 pub const IMAGE_FILE_MACHINE_AMD64: DWORD = 0x8664;
283
284 pub const EXCEPTION_CONTINUE_SEARCH: LONG = 0;
285 pub const EXCEPTION_STACK_OVERFLOW: DWORD = 0xc00000fd;
286 pub const EXCEPTION_MAXIMUM_PARAMETERS: usize = 15;
287
288 pub const PIPE_ACCESS_INBOUND: DWORD = 0x00000001;
289 pub const PIPE_ACCESS_OUTBOUND: DWORD = 0x00000002;
290 pub const FILE_FLAG_FIRST_PIPE_INSTANCE: DWORD = 0x00080000;
291 pub const FILE_FLAG_OVERLAPPED: DWORD = 0x40000000;
292 pub const PIPE_WAIT: DWORD = 0x00000000;
293 pub const PIPE_TYPE_BYTE: DWORD = 0x00000000;
294 pub const PIPE_REJECT_REMOTE_CLIENTS: DWORD = 0x00000008;
295 pub const PIPE_READMODE_BYTE: DWORD = 0x00000000;
296
297 pub const FD_SETSIZE: usize = 64;
298
299 pub const STACK_SIZE_PARAM_IS_A_RESERVATION: DWORD = 0x00010000;
300
301 #[repr(C)]
302 #[cfg(not(target_pointer_width = "64"))]
303 pub struct WSADATA {
304     pub wVersion: WORD,
305     pub wHighVersion: WORD,
306     pub szDescription: [u8; WSADESCRIPTION_LEN + 1],
307     pub szSystemStatus: [u8; WSASYS_STATUS_LEN + 1],
308     pub iMaxSockets: u16,
309     pub iMaxUdpDg: u16,
310     pub lpVendorInfo: *mut u8,
311 }
312 #[repr(C)]
313 #[cfg(target_pointer_width = "64")]
314 pub struct WSADATA {
315     pub wVersion: WORD,
316     pub wHighVersion: WORD,
317     pub iMaxSockets: u16,
318     pub iMaxUdpDg: u16,
319     pub lpVendorInfo: *mut u8,
320     pub szDescription: [u8; WSADESCRIPTION_LEN + 1],
321     pub szSystemStatus: [u8; WSASYS_STATUS_LEN + 1],
322 }
323
324 #[repr(C)]
325 pub struct WSAPROTOCOL_INFO {
326     pub dwServiceFlags1: DWORD,
327     pub dwServiceFlags2: DWORD,
328     pub dwServiceFlags3: DWORD,
329     pub dwServiceFlags4: DWORD,
330     pub dwProviderFlags: DWORD,
331     pub ProviderId: GUID,
332     pub dwCatalogEntryId: DWORD,
333     pub ProtocolChain: WSAPROTOCOLCHAIN,
334     pub iVersion: c_int,
335     pub iAddressFamily: c_int,
336     pub iMaxSockAddr: c_int,
337     pub iMinSockAddr: c_int,
338     pub iSocketType: c_int,
339     pub iProtocol: c_int,
340     pub iProtocolMaxOffset: c_int,
341     pub iNetworkByteOrder: c_int,
342     pub iSecurityScheme: c_int,
343     pub dwMessageSize: DWORD,
344     pub dwProviderReserved: DWORD,
345     pub szProtocol: [u16; (WSAPROTOCOL_LEN as usize) + 1],
346 }
347
348 #[repr(C)]
349 #[derive(Copy, Clone)]
350 pub struct WIN32_FILE_ATTRIBUTE_DATA {
351     pub dwFileAttributes: DWORD,
352     pub ftCreationTime: FILETIME,
353     pub ftLastAccessTime: FILETIME,
354     pub ftLastWriteTime: FILETIME,
355     pub nFileSizeHigh: DWORD,
356     pub nFileSizeLow: DWORD,
357 }
358
359 #[repr(C)]
360 pub struct BY_HANDLE_FILE_INFORMATION {
361     pub dwFileAttributes: DWORD,
362     pub ftCreationTime: FILETIME,
363     pub ftLastAccessTime: FILETIME,
364     pub ftLastWriteTime: FILETIME,
365     pub dwVolumeSerialNumber: DWORD,
366     pub nFileSizeHigh: DWORD,
367     pub nFileSizeLow: DWORD,
368     pub nNumberOfLinks: DWORD,
369     pub nFileIndexHigh: DWORD,
370     pub nFileIndexLow: DWORD,
371 }
372
373 #[repr(C)]
374 #[allow(dead_code)] // we only use some variants
375 pub enum FILE_INFO_BY_HANDLE_CLASS {
376     FileBasicInfo                   = 0,
377     FileStandardInfo                = 1,
378     FileNameInfo                    = 2,
379     FileRenameInfo                  = 3,
380     FileDispositionInfo             = 4,
381     FileAllocationInfo              = 5,
382     FileEndOfFileInfo               = 6,
383     FileStreamInfo                  = 7,
384     FileCompressionInfo             = 8,
385     FileAttributeTagInfo            = 9,
386     FileIdBothDirectoryInfo         = 10, // 0xA
387     FileIdBothDirectoryRestartInfo  = 11, // 0xB
388     FileIoPriorityHintInfo          = 12, // 0xC
389     FileRemoteProtocolInfo          = 13, // 0xD
390     FileFullDirectoryInfo           = 14, // 0xE
391     FileFullDirectoryRestartInfo    = 15, // 0xF
392     FileStorageInfo                 = 16, // 0x10
393     FileAlignmentInfo               = 17, // 0x11
394     FileIdInfo                      = 18, // 0x12
395     FileIdExtdDirectoryInfo         = 19, // 0x13
396     FileIdExtdDirectoryRestartInfo  = 20, // 0x14
397     MaximumFileInfoByHandlesClass
398 }
399
400 #[repr(C)]
401 pub struct FILE_BASIC_INFO {
402     pub CreationTime: LARGE_INTEGER,
403     pub LastAccessTime: LARGE_INTEGER,
404     pub LastWriteTime: LARGE_INTEGER,
405     pub ChangeTime: LARGE_INTEGER,
406     pub FileAttributes: DWORD,
407 }
408
409 #[repr(C)]
410 pub struct FILE_END_OF_FILE_INFO {
411     pub EndOfFile: LARGE_INTEGER,
412 }
413
414 #[repr(C)]
415 pub struct REPARSE_DATA_BUFFER {
416     pub ReparseTag: c_uint,
417     pub ReparseDataLength: c_ushort,
418     pub Reserved: c_ushort,
419     pub rest: (),
420 }
421
422 #[repr(C)]
423 pub struct SYMBOLIC_LINK_REPARSE_BUFFER {
424     pub SubstituteNameOffset: c_ushort,
425     pub SubstituteNameLength: c_ushort,
426     pub PrintNameOffset: c_ushort,
427     pub PrintNameLength: c_ushort,
428     pub Flags: c_ulong,
429     pub PathBuffer: WCHAR,
430 }
431
432 #[repr(C)]
433 pub struct MOUNT_POINT_REPARSE_BUFFER {
434     pub SubstituteNameOffset: c_ushort,
435     pub SubstituteNameLength: c_ushort,
436     pub PrintNameOffset: c_ushort,
437     pub PrintNameLength: c_ushort,
438     pub PathBuffer: WCHAR,
439 }
440
441 pub type LPPROGRESS_ROUTINE = ::option::Option<unsafe extern "system" fn(
442     TotalFileSize: LARGE_INTEGER,
443     TotalBytesTransferred: LARGE_INTEGER,
444     StreamSize: LARGE_INTEGER,
445     StreamBytesTransferred: LARGE_INTEGER,
446     dwStreamNumber: DWORD,
447     dwCallbackReason: DWORD,
448     hSourceFile: HANDLE,
449     hDestinationFile: HANDLE,
450     lpData: LPVOID,
451 ) -> DWORD>;
452
453 #[repr(C)]
454 pub struct CONDITION_VARIABLE { pub ptr: LPVOID }
455 #[repr(C)]
456 pub struct SRWLOCK { pub ptr: LPVOID }
457 #[repr(C)]
458 pub struct CRITICAL_SECTION {
459     CriticalSectionDebug: LPVOID,
460     LockCount: LONG,
461     RecursionCount: LONG,
462     OwningThread: HANDLE,
463     LockSemaphore: HANDLE,
464     SpinCount: ULONG_PTR
465 }
466
467 #[repr(C)]
468 pub struct REPARSE_MOUNTPOINT_DATA_BUFFER {
469     pub ReparseTag: DWORD,
470     pub ReparseDataLength: DWORD,
471     pub Reserved: WORD,
472     pub ReparseTargetLength: WORD,
473     pub ReparseTargetMaximumLength: WORD,
474     pub Reserved1: WORD,
475     pub ReparseTarget: WCHAR,
476 }
477
478 #[repr(C)]
479 pub struct EXCEPTION_RECORD {
480     pub ExceptionCode: DWORD,
481     pub ExceptionFlags: DWORD,
482     pub ExceptionRecord: *mut EXCEPTION_RECORD,
483     pub ExceptionAddress: LPVOID,
484     pub NumberParameters: DWORD,
485     pub ExceptionInformation: [LPVOID; EXCEPTION_MAXIMUM_PARAMETERS]
486 }
487
488 #[repr(C)]
489 pub struct EXCEPTION_POINTERS {
490     pub ExceptionRecord: *mut EXCEPTION_RECORD,
491     pub ContextRecord: *mut CONTEXT,
492 }
493
494 pub type PVECTORED_EXCEPTION_HANDLER = extern "system"
495         fn(ExceptionInfo: *mut EXCEPTION_POINTERS) -> LONG;
496
497 #[repr(C)]
498 pub struct GUID {
499     pub Data1: DWORD,
500     pub Data2: WORD,
501     pub Data3: WORD,
502     pub Data4: [BYTE; 8],
503 }
504
505 #[repr(C)]
506 pub struct WSAPROTOCOLCHAIN {
507     pub ChainLen: c_int,
508     pub ChainEntries: [DWORD; MAX_PROTOCOL_CHAIN as usize],
509 }
510
511 #[repr(C)]
512 pub struct SECURITY_ATTRIBUTES {
513     pub nLength: DWORD,
514     pub lpSecurityDescriptor: LPVOID,
515     pub bInheritHandle: BOOL,
516 }
517
518 #[repr(C)]
519 pub struct PROCESS_INFORMATION {
520     pub hProcess: HANDLE,
521     pub hThread: HANDLE,
522     pub dwProcessId: DWORD,
523     pub dwThreadId: DWORD,
524 }
525
526 #[repr(C)]
527 pub struct STARTUPINFO {
528     pub cb: DWORD,
529     pub lpReserved: LPWSTR,
530     pub lpDesktop: LPWSTR,
531     pub lpTitle: LPWSTR,
532     pub dwX: DWORD,
533     pub dwY: DWORD,
534     pub dwXSize: DWORD,
535     pub dwYSize: DWORD,
536     pub dwXCountChars: DWORD,
537     pub dwYCountCharts: DWORD,
538     pub dwFillAttribute: DWORD,
539     pub dwFlags: DWORD,
540     pub wShowWindow: WORD,
541     pub cbReserved2: WORD,
542     pub lpReserved2: LPBYTE,
543     pub hStdInput: HANDLE,
544     pub hStdOutput: HANDLE,
545     pub hStdError: HANDLE,
546 }
547
548 #[repr(C)]
549 pub struct SOCKADDR {
550     pub sa_family: ADDRESS_FAMILY,
551     pub sa_data: [CHAR; 14],
552 }
553
554 #[repr(C)]
555 #[derive(Copy, Clone)]
556 pub struct FILETIME {
557     pub dwLowDateTime: DWORD,
558     pub dwHighDateTime: DWORD,
559 }
560
561 #[repr(C)]
562 pub struct OVERLAPPED {
563     pub Internal: *mut c_ulong,
564     pub InternalHigh: *mut c_ulong,
565     pub Offset: DWORD,
566     pub OffsetHigh: DWORD,
567     pub hEvent: HANDLE,
568 }
569
570 #[repr(C)]
571 #[cfg(target_env = "msvc")]
572 #[cfg(feature = "backtrace")]
573 pub struct SYMBOL_INFO {
574     pub SizeOfStruct: c_ulong,
575     pub TypeIndex: c_ulong,
576     pub Reserved: [u64; 2],
577     pub Index: c_ulong,
578     pub Size: c_ulong,
579     pub ModBase: u64,
580     pub Flags: c_ulong,
581     pub Value: u64,
582     pub Address: u64,
583     pub Register: c_ulong,
584     pub Scope: c_ulong,
585     pub Tag: c_ulong,
586     pub NameLen: c_ulong,
587     pub MaxNameLen: c_ulong,
588     // note that windows has this as 1, but it basically just means that
589     // the name is inline at the end of the struct. For us, we just bump
590     // the struct size up to MAX_SYM_NAME.
591     pub Name: [c_char; MAX_SYM_NAME],
592 }
593
594 #[repr(C)]
595 #[cfg(target_env = "msvc")]
596 #[cfg(feature = "backtrace")]
597 pub struct IMAGEHLP_LINE64 {
598     pub SizeOfStruct: u32,
599     pub Key: *const c_void,
600     pub LineNumber: u32,
601     pub Filename: *const c_char,
602     pub Address: u64,
603 }
604
605 #[repr(C)]
606 #[allow(dead_code)] // we only use some variants
607 pub enum ADDRESS_MODE {
608     AddrMode1616,
609     AddrMode1632,
610     AddrModeReal,
611     AddrModeFlat,
612 }
613
614 #[repr(C)]
615 #[cfg(feature = "backtrace")]
616 pub struct ADDRESS64 {
617     pub Offset: u64,
618     pub Segment: u16,
619     pub Mode: ADDRESS_MODE,
620 }
621
622 #[repr(C)]
623 #[cfg(feature = "backtrace")]
624 pub struct STACKFRAME_EX {
625     pub AddrPC: ADDRESS64,
626     pub AddrReturn: ADDRESS64,
627     pub AddrFrame: ADDRESS64,
628     pub AddrStack: ADDRESS64,
629     pub AddrBStore: ADDRESS64,
630     pub FuncTableEntry: *mut c_void,
631     pub Params: [u64; 4],
632     pub Far: BOOL,
633     pub Virtual: BOOL,
634     pub Reserved: [u64; 3],
635     pub KdHelp: KDHELP64,
636     pub StackFrameSize: DWORD,
637     pub InlineFrameContext: DWORD,
638 }
639
640 #[repr(C)]
641 #[cfg(feature = "backtrace")]
642 pub struct STACKFRAME64 {
643     pub AddrPC: ADDRESS64,
644     pub AddrReturn: ADDRESS64,
645     pub AddrFrame: ADDRESS64,
646     pub AddrStack: ADDRESS64,
647     pub AddrBStore: ADDRESS64,
648     pub FuncTableEntry: *mut c_void,
649     pub Params: [u64; 4],
650     pub Far: BOOL,
651     pub Virtual: BOOL,
652     pub Reserved: [u64; 3],
653     pub KdHelp: KDHELP64,
654 }
655
656 #[repr(C)]
657 #[cfg(feature = "backtrace")]
658 pub struct KDHELP64 {
659     pub Thread: u64,
660     pub ThCallbackStack: DWORD,
661     pub ThCallbackBStore: DWORD,
662     pub NextCallback: DWORD,
663     pub FramePointer: DWORD,
664     pub KiCallUserMode: u64,
665     pub KeUserCallbackDispatcher: u64,
666     pub SystemRangeStart: u64,
667     pub KiUserExceptionDispatcher: u64,
668     pub StackBase: u64,
669     pub StackLimit: u64,
670     pub Reserved: [u64; 5],
671 }
672
673 #[cfg(target_arch = "x86")]
674 #[repr(C)]
675 pub struct CONTEXT {
676     pub ContextFlags: DWORD,
677     pub Dr0: DWORD,
678     pub Dr1: DWORD,
679     pub Dr2: DWORD,
680     pub Dr3: DWORD,
681     pub Dr6: DWORD,
682     pub Dr7: DWORD,
683     pub FloatSave: FLOATING_SAVE_AREA,
684     pub SegGs: DWORD,
685     pub SegFs: DWORD,
686     pub SegEs: DWORD,
687     pub SegDs: DWORD,
688     pub Edi: DWORD,
689     pub Esi: DWORD,
690     pub Ebx: DWORD,
691     pub Edx: DWORD,
692     pub Ecx: DWORD,
693     pub Eax: DWORD,
694     pub Ebp: DWORD,
695     pub Eip: DWORD,
696     pub SegCs: DWORD,
697     pub EFlags: DWORD,
698     pub Esp: DWORD,
699     pub SegSs: DWORD,
700     pub ExtendedRegisters: [u8; 512],
701 }
702
703 #[cfg(target_arch = "x86")]
704 #[repr(C)]
705 pub struct FLOATING_SAVE_AREA {
706     pub ControlWord: DWORD,
707     pub StatusWord: DWORD,
708     pub TagWord: DWORD,
709     pub ErrorOffset: DWORD,
710     pub ErrorSelector: DWORD,
711     pub DataOffset: DWORD,
712     pub DataSelector: DWORD,
713     pub RegisterArea: [u8; 80],
714     pub Cr0NpxState: DWORD,
715 }
716
717 #[cfg(target_arch = "x86_64")]
718 #[repr(C, align(16))]
719 pub struct CONTEXT {
720     pub P1Home: DWORDLONG,
721     pub P2Home: DWORDLONG,
722     pub P3Home: DWORDLONG,
723     pub P4Home: DWORDLONG,
724     pub P5Home: DWORDLONG,
725     pub P6Home: DWORDLONG,
726
727     pub ContextFlags: DWORD,
728     pub MxCsr: DWORD,
729
730     pub SegCs: WORD,
731     pub SegDs: WORD,
732     pub SegEs: WORD,
733     pub SegFs: WORD,
734     pub SegGs: WORD,
735     pub SegSs: WORD,
736     pub EFlags: DWORD,
737
738     pub Dr0: DWORDLONG,
739     pub Dr1: DWORDLONG,
740     pub Dr2: DWORDLONG,
741     pub Dr3: DWORDLONG,
742     pub Dr6: DWORDLONG,
743     pub Dr7: DWORDLONG,
744
745     pub Rax: DWORDLONG,
746     pub Rcx: DWORDLONG,
747     pub Rdx: DWORDLONG,
748     pub Rbx: DWORDLONG,
749     pub Rsp: DWORDLONG,
750     pub Rbp: DWORDLONG,
751     pub Rsi: DWORDLONG,
752     pub Rdi: DWORDLONG,
753     pub R8:  DWORDLONG,
754     pub R9:  DWORDLONG,
755     pub R10: DWORDLONG,
756     pub R11: DWORDLONG,
757     pub R12: DWORDLONG,
758     pub R13: DWORDLONG,
759     pub R14: DWORDLONG,
760     pub R15: DWORDLONG,
761
762     pub Rip: DWORDLONG,
763
764     pub FltSave: FLOATING_SAVE_AREA,
765
766     pub VectorRegister: [M128A; 26],
767     pub VectorControl: DWORDLONG,
768
769     pub DebugControl: DWORDLONG,
770     pub LastBranchToRip: DWORDLONG,
771     pub LastBranchFromRip: DWORDLONG,
772     pub LastExceptionToRip: DWORDLONG,
773     pub LastExceptionFromRip: DWORDLONG,
774 }
775
776 #[cfg(target_arch = "x86_64")]
777 #[repr(C, align(16))]
778 pub struct M128A {
779     pub Low:  c_ulonglong,
780     pub High: c_longlong
781 }
782
783 #[cfg(target_arch = "x86_64")]
784 #[repr(C, align(16))]
785 pub struct FLOATING_SAVE_AREA {
786     _Dummy: [u8; 512] // FIXME: Fill this out
787 }
788
789 // FIXME(#43348): This structure is used for backtrace only, and a fake
790 // definition is provided here only to allow rustdoc to pass type-check. This
791 // will not appear in the final documentation. This should be also defined for
792 // other architectures supported by Windows such as ARM, and for historical
793 // interest, maybe MIPS and PowerPC as well.
794 #[cfg(all(dox, not(any(target_arch = "x86_64", target_arch = "x86"))))]
795 pub enum CONTEXT {}
796
797 #[repr(C)]
798 pub struct SOCKADDR_STORAGE_LH {
799     pub ss_family: ADDRESS_FAMILY,
800     pub __ss_pad1: [CHAR; 6],
801     pub __ss_align: i64,
802     pub __ss_pad2: [CHAR; 112],
803 }
804
805 #[repr(C)]
806 pub struct ADDRINFOA {
807     pub ai_flags: c_int,
808     pub ai_family: c_int,
809     pub ai_socktype: c_int,
810     pub ai_protocol: c_int,
811     pub ai_addrlen: size_t,
812     pub ai_canonname: *mut c_char,
813     pub ai_addr: *mut SOCKADDR,
814     pub ai_next: *mut ADDRINFOA,
815 }
816
817 #[repr(C)]
818 #[derive(Copy, Clone)]
819 pub struct sockaddr_in {
820     pub sin_family: ADDRESS_FAMILY,
821     pub sin_port: USHORT,
822     pub sin_addr: in_addr,
823     pub sin_zero: [CHAR; 8],
824 }
825
826 #[repr(C)]
827 #[derive(Copy, Clone)]
828 pub struct sockaddr_in6 {
829     pub sin6_family: ADDRESS_FAMILY,
830     pub sin6_port: USHORT,
831     pub sin6_flowinfo: c_ulong,
832     pub sin6_addr: in6_addr,
833     pub sin6_scope_id: c_ulong,
834 }
835
836 #[repr(C)]
837 #[derive(Copy, Clone)]
838 pub struct in_addr {
839     pub s_addr: u32,
840 }
841
842 #[repr(C)]
843 #[derive(Copy, Clone)]
844 pub struct in6_addr {
845     pub s6_addr: [u8; 16],
846 }
847
848 #[repr(C)]
849 #[derive(Copy, Clone)]
850 #[allow(dead_code)] // we only use some variants
851 pub enum EXCEPTION_DISPOSITION {
852     ExceptionContinueExecution,
853     ExceptionContinueSearch,
854     ExceptionNestedException,
855     ExceptionCollidedUnwind
856 }
857
858 #[repr(C)]
859 #[derive(Copy, Clone)]
860 pub struct CONSOLE_READCONSOLE_CONTROL {
861     pub nLength: ULONG,
862     pub nInitialChars: ULONG,
863     pub dwCtrlWakeupMask: ULONG,
864     pub dwControlKeyState: ULONG,
865 }
866 pub type PCONSOLE_READCONSOLE_CONTROL = *mut CONSOLE_READCONSOLE_CONTROL;
867
868 #[repr(C)]
869 #[derive(Copy)]
870 pub struct fd_set {
871     pub fd_count: c_uint,
872     pub fd_array: [SOCKET; FD_SETSIZE],
873 }
874
875 impl Clone for fd_set {
876     fn clone(&self) -> fd_set {
877         *self
878     }
879 }
880
881 #[repr(C)]
882 #[derive(Copy, Clone)]
883 pub struct timeval {
884     pub tv_sec: c_long,
885     pub tv_usec: c_long,
886 }
887
888 extern "system" {
889     pub fn WSAStartup(wVersionRequested: WORD,
890                       lpWSAData: LPWSADATA) -> c_int;
891     pub fn WSACleanup() -> c_int;
892     pub fn WSAGetLastError() -> c_int;
893     pub fn WSADuplicateSocketW(s: SOCKET,
894                                dwProcessId: DWORD,
895                                lpProtocolInfo: LPWSAPROTOCOL_INFO)
896                                -> c_int;
897     pub fn GetCurrentProcessId() -> DWORD;
898     pub fn WSASocketW(af: c_int,
899                       kind: c_int,
900                       protocol: c_int,
901                       lpProtocolInfo: LPWSAPROTOCOL_INFO,
902                       g: GROUP,
903                       dwFlags: DWORD) -> SOCKET;
904     pub fn ioctlsocket(s: SOCKET, cmd: c_long, argp: *mut c_ulong) -> c_int;
905     pub fn InitializeCriticalSection(CriticalSection: *mut CRITICAL_SECTION);
906     pub fn EnterCriticalSection(CriticalSection: *mut CRITICAL_SECTION);
907     pub fn TryEnterCriticalSection(CriticalSection: *mut CRITICAL_SECTION) -> BOOLEAN;
908     pub fn LeaveCriticalSection(CriticalSection: *mut CRITICAL_SECTION);
909     pub fn DeleteCriticalSection(CriticalSection: *mut CRITICAL_SECTION);
910
911     pub fn ReadConsoleW(hConsoleInput: HANDLE,
912                         lpBuffer: LPVOID,
913                         nNumberOfCharsToRead: DWORD,
914                         lpNumberOfCharsRead: LPDWORD,
915                         pInputControl: PCONSOLE_READCONSOLE_CONTROL) -> BOOL;
916
917     pub fn WriteConsoleW(hConsoleOutput: HANDLE,
918                          lpBuffer: LPCVOID,
919                          nNumberOfCharsToWrite: DWORD,
920                          lpNumberOfCharsWritten: LPDWORD,
921                          lpReserved: LPVOID) -> BOOL;
922
923     pub fn GetConsoleMode(hConsoleHandle: HANDLE,
924                           lpMode: LPDWORD) -> BOOL;
925     pub fn RemoveDirectoryW(lpPathName: LPCWSTR) -> BOOL;
926     pub fn SetFileAttributesW(lpFileName: LPCWSTR,
927                               dwFileAttributes: DWORD) -> BOOL;
928     pub fn GetFileInformationByHandle(hFile: HANDLE,
929                             lpFileInformation: LPBY_HANDLE_FILE_INFORMATION)
930                             -> BOOL;
931
932     pub fn SetLastError(dwErrCode: DWORD);
933     pub fn GetCommandLineW() -> *mut LPCWSTR;
934     pub fn LocalFree(ptr: *mut c_void);
935     pub fn CommandLineToArgvW(lpCmdLine: *mut LPCWSTR,
936                               pNumArgs: *mut c_int) -> *mut *mut u16;
937     pub fn GetTempPathW(nBufferLength: DWORD,
938                         lpBuffer: LPCWSTR) -> DWORD;
939     pub fn OpenProcessToken(ProcessHandle: HANDLE,
940                             DesiredAccess: DWORD,
941                             TokenHandle: *mut HANDLE) -> BOOL;
942     pub fn GetCurrentProcess() -> HANDLE;
943     pub fn GetCurrentThread() -> HANDLE;
944     pub fn GetStdHandle(which: DWORD) -> HANDLE;
945     pub fn ExitProcess(uExitCode: c_uint) -> !;
946     pub fn DeviceIoControl(hDevice: HANDLE,
947                            dwIoControlCode: DWORD,
948                            lpInBuffer: LPVOID,
949                            nInBufferSize: DWORD,
950                            lpOutBuffer: LPVOID,
951                            nOutBufferSize: DWORD,
952                            lpBytesReturned: LPDWORD,
953                            lpOverlapped: LPOVERLAPPED) -> BOOL;
954     pub fn CreateThread(lpThreadAttributes: LPSECURITY_ATTRIBUTES,
955                         dwStackSize: SIZE_T,
956                         lpStartAddress: extern "system" fn(*mut c_void)
957                                                            -> DWORD,
958                         lpParameter: LPVOID,
959                         dwCreationFlags: DWORD,
960                         lpThreadId: LPDWORD) -> HANDLE;
961     pub fn WaitForSingleObject(hHandle: HANDLE,
962                                dwMilliseconds: DWORD) -> DWORD;
963     pub fn SwitchToThread() -> BOOL;
964     pub fn Sleep(dwMilliseconds: DWORD);
965     pub fn GetProcessId(handle: HANDLE) -> DWORD;
966     pub fn GetUserProfileDirectoryW(hToken: HANDLE,
967                                     lpProfileDir: LPWSTR,
968                                     lpcchSize: *mut DWORD) -> BOOL;
969     pub fn SetHandleInformation(hObject: HANDLE,
970                                 dwMask: DWORD,
971                                 dwFlags: DWORD) -> BOOL;
972     pub fn CopyFileExW(lpExistingFileName: LPCWSTR,
973                        lpNewFileName: LPCWSTR,
974                        lpProgressRoutine: LPPROGRESS_ROUTINE,
975                        lpData: LPVOID,
976                        pbCancel: LPBOOL,
977                        dwCopyFlags: DWORD) -> BOOL;
978     pub fn AddVectoredExceptionHandler(FirstHandler: ULONG,
979                                        VectoredHandler: PVECTORED_EXCEPTION_HANDLER)
980                                        -> LPVOID;
981     pub fn FormatMessageW(flags: DWORD,
982                           lpSrc: LPVOID,
983                           msgId: DWORD,
984                           langId: DWORD,
985                           buf: LPWSTR,
986                           nsize: DWORD,
987                           args: *const c_void)
988                           -> DWORD;
989     pub fn TlsAlloc() -> DWORD;
990     pub fn TlsGetValue(dwTlsIndex: DWORD) -> LPVOID;
991     pub fn TlsSetValue(dwTlsIndex: DWORD, lpTlsvalue: LPVOID) -> BOOL;
992     pub fn GetLastError() -> DWORD;
993     pub fn QueryPerformanceFrequency(lpFrequency: *mut LARGE_INTEGER) -> BOOL;
994     pub fn QueryPerformanceCounter(lpPerformanceCount: *mut LARGE_INTEGER)
995                                    -> BOOL;
996     pub fn GetExitCodeProcess(hProcess: HANDLE, lpExitCode: LPDWORD) -> BOOL;
997     pub fn TerminateProcess(hProcess: HANDLE, uExitCode: UINT) -> BOOL;
998     pub fn CreateProcessW(lpApplicationName: LPCWSTR,
999                           lpCommandLine: LPWSTR,
1000                           lpProcessAttributes: LPSECURITY_ATTRIBUTES,
1001                           lpThreadAttributes: LPSECURITY_ATTRIBUTES,
1002                           bInheritHandles: BOOL,
1003                           dwCreationFlags: DWORD,
1004                           lpEnvironment: LPVOID,
1005                           lpCurrentDirectory: LPCWSTR,
1006                           lpStartupInfo: LPSTARTUPINFO,
1007                           lpProcessInformation: LPPROCESS_INFORMATION)
1008                           -> BOOL;
1009     pub fn GetEnvironmentVariableW(n: LPCWSTR, v: LPWSTR, nsize: DWORD) -> DWORD;
1010     pub fn SetEnvironmentVariableW(n: LPCWSTR, v: LPCWSTR) -> BOOL;
1011     pub fn GetEnvironmentStringsW() -> LPWCH;
1012     pub fn FreeEnvironmentStringsW(env_ptr: LPWCH) -> BOOL;
1013     pub fn GetModuleFileNameW(hModule: HMODULE,
1014                               lpFilename: LPWSTR,
1015                               nSize: DWORD)
1016                               -> DWORD;
1017     pub fn CreateDirectoryW(lpPathName: LPCWSTR,
1018                             lpSecurityAttributes: LPSECURITY_ATTRIBUTES)
1019                             -> BOOL;
1020     pub fn DeleteFileW(lpPathName: LPCWSTR) -> BOOL;
1021     pub fn GetCurrentDirectoryW(nBufferLength: DWORD, lpBuffer: LPWSTR) -> DWORD;
1022     pub fn SetCurrentDirectoryW(lpPathName: LPCWSTR) -> BOOL;
1023     pub fn WideCharToMultiByte(CodePage: UINT,
1024                                dwFlags: DWORD,
1025                                lpWideCharStr: LPCWSTR,
1026                                cchWideChar: c_int,
1027                                lpMultiByteStr: LPSTR,
1028                                cbMultiByte: c_int,
1029                                lpDefaultChar: LPCSTR,
1030                                lpUsedDefaultChar: LPBOOL) -> c_int;
1031
1032     pub fn closesocket(socket: SOCKET) -> c_int;
1033     pub fn recv(socket: SOCKET, buf: *mut c_void, len: c_int,
1034                 flags: c_int) -> c_int;
1035     pub fn send(socket: SOCKET, buf: *const c_void, len: c_int,
1036                 flags: c_int) -> c_int;
1037     pub fn recvfrom(socket: SOCKET,
1038                     buf: *mut c_void,
1039                     len: c_int,
1040                     flags: c_int,
1041                     addr: *mut SOCKADDR,
1042                     addrlen: *mut c_int)
1043                     -> c_int;
1044     pub fn sendto(socket: SOCKET,
1045                   buf: *const c_void,
1046                   len: c_int,
1047                   flags: c_int,
1048                   addr: *const SOCKADDR,
1049                   addrlen: c_int)
1050                   -> c_int;
1051     pub fn shutdown(socket: SOCKET, how: c_int) -> c_int;
1052     pub fn accept(socket: SOCKET,
1053                   address: *mut SOCKADDR,
1054                   address_len: *mut c_int)
1055                   -> SOCKET;
1056     pub fn DuplicateHandle(hSourceProcessHandle: HANDLE,
1057                            hSourceHandle: HANDLE,
1058                            hTargetProcessHandle: HANDLE,
1059                            lpTargetHandle: LPHANDLE,
1060                            dwDesiredAccess: DWORD,
1061                            bInheritHandle: BOOL,
1062                            dwOptions: DWORD)
1063                            -> BOOL;
1064     pub fn ReadFile(hFile: HANDLE,
1065                     lpBuffer: LPVOID,
1066                     nNumberOfBytesToRead: DWORD,
1067                     lpNumberOfBytesRead: LPDWORD,
1068                     lpOverlapped: LPOVERLAPPED)
1069                     -> BOOL;
1070     pub fn WriteFile(hFile: HANDLE,
1071                      lpBuffer: LPVOID,
1072                      nNumberOfBytesToWrite: DWORD,
1073                      lpNumberOfBytesWritten: LPDWORD,
1074                      lpOverlapped: LPOVERLAPPED)
1075                      -> BOOL;
1076     pub fn CloseHandle(hObject: HANDLE) -> BOOL;
1077     pub fn CreateHardLinkW(lpSymlinkFileName: LPCWSTR,
1078                            lpTargetFileName: LPCWSTR,
1079                            lpSecurityAttributes: LPSECURITY_ATTRIBUTES)
1080                            -> BOOL;
1081     pub fn MoveFileExW(lpExistingFileName: LPCWSTR,
1082                        lpNewFileName: LPCWSTR,
1083                        dwFlags: DWORD)
1084                        -> BOOL;
1085     pub fn SetFilePointerEx(hFile: HANDLE,
1086                             liDistanceToMove: LARGE_INTEGER,
1087                             lpNewFilePointer: PLARGE_INTEGER,
1088                             dwMoveMethod: DWORD)
1089                             -> BOOL;
1090     pub fn FlushFileBuffers(hFile: HANDLE) -> BOOL;
1091     pub fn CreateFileW(lpFileName: LPCWSTR,
1092                        dwDesiredAccess: DWORD,
1093                        dwShareMode: DWORD,
1094                        lpSecurityAttributes: LPSECURITY_ATTRIBUTES,
1095                        dwCreationDisposition: DWORD,
1096                        dwFlagsAndAttributes: DWORD,
1097                        hTemplateFile: HANDLE)
1098                        -> HANDLE;
1099
1100     pub fn FindFirstFileW(fileName: LPCWSTR,
1101                           findFileData: LPWIN32_FIND_DATAW)
1102                           -> HANDLE;
1103     pub fn FindNextFileW(findFile: HANDLE, findFileData: LPWIN32_FIND_DATAW)
1104                          -> BOOL;
1105     pub fn FindClose(findFile: HANDLE) -> BOOL;
1106     #[cfg(feature = "backtrace")]
1107     pub fn RtlCaptureContext(ctx: *mut CONTEXT);
1108     pub fn getsockopt(s: SOCKET,
1109                       level: c_int,
1110                       optname: c_int,
1111                       optval: *mut c_char,
1112                       optlen: *mut c_int)
1113                       -> c_int;
1114     pub fn setsockopt(s: SOCKET,
1115                       level: c_int,
1116                       optname: c_int,
1117                       optval: *const c_void,
1118                       optlen: c_int)
1119                       -> c_int;
1120     pub fn getsockname(socket: SOCKET,
1121                        address: *mut SOCKADDR,
1122                        address_len: *mut c_int)
1123                        -> c_int;
1124     pub fn getpeername(socket: SOCKET,
1125                        address: *mut SOCKADDR,
1126                        address_len: *mut c_int)
1127                        -> c_int;
1128     pub fn bind(socket: SOCKET, address: *const SOCKADDR,
1129                 address_len: socklen_t) -> c_int;
1130     pub fn listen(socket: SOCKET, backlog: c_int) -> c_int;
1131     pub fn connect(socket: SOCKET, address: *const SOCKADDR, len: c_int)
1132                    -> c_int;
1133     pub fn getaddrinfo(node: *const c_char, service: *const c_char,
1134                        hints: *const ADDRINFOA,
1135                        res: *mut *mut ADDRINFOA) -> c_int;
1136     pub fn freeaddrinfo(res: *mut ADDRINFOA);
1137
1138     #[cfg(feature = "backtrace")]
1139     pub fn LoadLibraryW(name: LPCWSTR) -> HMODULE;
1140     #[cfg(feature = "backtrace")]
1141     pub fn FreeLibrary(handle: HMODULE) -> BOOL;
1142     pub fn GetProcAddress(handle: HMODULE,
1143                           name: LPCSTR) -> *mut c_void;
1144     pub fn GetModuleHandleW(lpModuleName: LPCWSTR) -> HMODULE;
1145
1146     pub fn GetSystemTimeAsFileTime(lpSystemTimeAsFileTime: LPFILETIME);
1147
1148     pub fn CreateEventW(lpEventAttributes: LPSECURITY_ATTRIBUTES,
1149                         bManualReset: BOOL,
1150                         bInitialState: BOOL,
1151                         lpName: LPCWSTR) -> HANDLE;
1152     pub fn WaitForMultipleObjects(nCount: DWORD,
1153                                   lpHandles: *const HANDLE,
1154                                   bWaitAll: BOOL,
1155                                   dwMilliseconds: DWORD) -> DWORD;
1156     pub fn CreateNamedPipeW(lpName: LPCWSTR,
1157                             dwOpenMode: DWORD,
1158                             dwPipeMode: DWORD,
1159                             nMaxInstances: DWORD,
1160                             nOutBufferSize: DWORD,
1161                             nInBufferSize: DWORD,
1162                             nDefaultTimeOut: DWORD,
1163                             lpSecurityAttributes: LPSECURITY_ATTRIBUTES)
1164                             -> HANDLE;
1165     pub fn CancelIo(handle: HANDLE) -> BOOL;
1166     pub fn GetOverlappedResult(hFile: HANDLE,
1167                                lpOverlapped: LPOVERLAPPED,
1168                                lpNumberOfBytesTransferred: LPDWORD,
1169                                bWait: BOOL) -> BOOL;
1170     pub fn select(nfds: c_int,
1171                   readfds: *mut fd_set,
1172                   writefds: *mut fd_set,
1173                   exceptfds: *mut fd_set,
1174                   timeout: *const timeval) -> c_int;
1175
1176     #[link_name = "SystemFunction036"]
1177     pub fn RtlGenRandom(RandomBuffer: *mut u8, RandomBufferLength: ULONG) -> BOOLEAN;
1178 }
1179
1180 // Functions that aren't available on every version of Windows that we support,
1181 // but we still use them and just provide some form of a fallback implementation.
1182 compat_fn! {
1183     kernel32:
1184
1185     pub fn CreateSymbolicLinkW(_lpSymlinkFileName: LPCWSTR,
1186                                _lpTargetFileName: LPCWSTR,
1187                                _dwFlags: DWORD) -> BOOLEAN {
1188         SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); 0
1189     }
1190     pub fn GetFinalPathNameByHandleW(_hFile: HANDLE,
1191                                      _lpszFilePath: LPCWSTR,
1192                                      _cchFilePath: DWORD,
1193                                      _dwFlags: DWORD) -> DWORD {
1194         SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); 0
1195     }
1196     pub fn SetThreadStackGuarantee(_size: *mut c_ulong) -> BOOL {
1197         SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); 0
1198     }
1199     pub fn SetThreadDescription(hThread: HANDLE,
1200                                 lpThreadDescription: LPCWSTR) -> HRESULT {
1201         SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); E_NOTIMPL
1202     }
1203     pub fn SetFileInformationByHandle(_hFile: HANDLE,
1204                     _FileInformationClass: FILE_INFO_BY_HANDLE_CLASS,
1205                     _lpFileInformation: LPVOID,
1206                     _dwBufferSize: DWORD) -> BOOL {
1207         SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); 0
1208     }
1209     pub fn SleepConditionVariableSRW(ConditionVariable: PCONDITION_VARIABLE,
1210                                      SRWLock: PSRWLOCK,
1211                                      dwMilliseconds: DWORD,
1212                                      Flags: ULONG) -> BOOL {
1213         panic!("condition variables not available")
1214     }
1215     pub fn WakeConditionVariable(ConditionVariable: PCONDITION_VARIABLE)
1216                                  -> () {
1217         panic!("condition variables not available")
1218     }
1219     pub fn WakeAllConditionVariable(ConditionVariable: PCONDITION_VARIABLE)
1220                                     -> () {
1221         panic!("condition variables not available")
1222     }
1223     pub fn AcquireSRWLockExclusive(SRWLock: PSRWLOCK) -> () {
1224         panic!("rwlocks not available")
1225     }
1226     pub fn AcquireSRWLockShared(SRWLock: PSRWLOCK) -> () {
1227         panic!("rwlocks not available")
1228     }
1229     pub fn ReleaseSRWLockExclusive(SRWLock: PSRWLOCK) -> () {
1230         panic!("rwlocks not available")
1231     }
1232     pub fn ReleaseSRWLockShared(SRWLock: PSRWLOCK) -> () {
1233         panic!("rwlocks not available")
1234     }
1235     pub fn TryAcquireSRWLockExclusive(SRWLock: PSRWLOCK) -> BOOLEAN {
1236         panic!("rwlocks not available")
1237     }
1238     pub fn TryAcquireSRWLockShared(SRWLock: PSRWLOCK) -> BOOLEAN {
1239         panic!("rwlocks not available")
1240     }
1241 }
1242
1243 #[cfg(all(target_env = "gnu", feature = "backtrace"))]
1244 mod gnu {
1245     use super::*;
1246
1247     pub const PROCESS_QUERY_INFORMATION: DWORD = 0x0400;
1248
1249     pub const CP_ACP: UINT = 0;
1250
1251     pub const WC_NO_BEST_FIT_CHARS: DWORD = 0x00000400;
1252
1253     extern "system" {
1254         pub fn OpenProcess(dwDesiredAccess: DWORD,
1255                            bInheritHandle: BOOL,
1256                            dwProcessId: DWORD) -> HANDLE;
1257     }
1258
1259     compat_fn! {
1260         kernel32:
1261
1262         pub fn QueryFullProcessImageNameW(_hProcess: HANDLE,
1263                                           _dwFlags: DWORD,
1264                                           _lpExeName: LPWSTR,
1265                                           _lpdwSize: LPDWORD) -> BOOL {
1266             SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); 0
1267         }
1268     }
1269 }
1270
1271 #[cfg(all(target_env = "gnu", feature = "backtrace"))]
1272 pub use self::gnu::*;