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.
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.
11 //! C definitions used by libnative that don't belong in liblibc
13 #![allow(type_overflow)]
17 pub static WSADESCRIPTION_LEN: uint = 256;
18 pub static WSASYS_STATUS_LEN: uint = 128;
19 pub static FIONBIO: libc::c_long = 0x8004667e;
20 static FD_SETSIZE: uint = 64;
21 pub static MSG_DONTWAIT: libc::c_int = 0;
25 pub wVersion: libc::WORD,
26 pub wHighVersion: libc::WORD,
27 pub szDescription: [u8, ..WSADESCRIPTION_LEN + 1],
28 pub szSystemStatus: [u8, ..WSASYS_STATUS_LEN + 1],
31 pub lpVendorInfo: *u8,
34 pub type LPWSADATA = *mut WSADATA;
38 fd_count: libc::c_uint,
39 fd_array: [libc::SOCKET, ..FD_SETSIZE],
42 pub fn fd_set(set: &mut fd_set, s: libc::SOCKET) {
43 set.fd_array[set.fd_count as uint] = s;
47 #[link(name = "ws2_32")]
49 pub fn WSAStartup(wVersionRequested: libc::WORD,
50 lpWSAData: LPWSADATA) -> libc::c_int;
51 pub fn WSAGetLastError() -> libc::c_int;
53 pub fn ioctlsocket(s: libc::SOCKET, cmd: libc::c_long,
54 argp: *mut libc::c_ulong) -> libc::c_int;
55 pub fn select(nfds: libc::c_int,
59 timeout: *libc::timeval) -> libc::c_int;
60 pub fn getsockopt(sockfd: libc::SOCKET,
63 optval: *mut libc::c_char,
64 optlen: *mut libc::c_int) -> libc::c_int;
66 pub fn CancelIo(hFile: libc::HANDLE) -> libc::BOOL;
67 pub fn CancelIoEx(hFile: libc::HANDLE,
68 lpOverlapped: libc::LPOVERLAPPED) -> libc::BOOL;
72 use std::intrinsics::{atomic_store_relaxed, transmute};
73 use libc::types::os::arch::extra::{LPCWSTR, HMODULE, LPCSTR, LPVOID};
76 fn GetModuleHandleW(lpModuleName: LPCWSTR) -> HMODULE;
77 fn GetProcAddress(hModule: HMODULE, lpProcName: LPCSTR) -> LPVOID;
80 // store_func() is idempotent, so using relaxed ordering for the atomics
81 // should be enough. This way, calling a function in this compatibility
82 // layer (after it's loaded) shouldn't be any slower than a regular DLL
84 unsafe fn store_func(ptr: *mut uint, module: &str, symbol: &str, fallback: uint) {
85 let module = module.to_utf16().append_one(0);
86 symbol.with_c_str(|symbol| {
87 let handle = GetModuleHandleW(module.as_ptr());
88 let func: uint = transmute(GetProcAddress(handle, symbol));
89 atomic_store_relaxed(ptr, if func == 0 {
97 /// Macro for creating a compatibility fallback for a Windows function
101 /// compat_fn!(adll32::SomeFunctionW(_arg: LPCWSTR) {
102 /// // Fallback implementation
106 /// Note that arguments unused by the fallback implementation should not be called `_` as
107 /// they are used to be passed to the real function if available.
108 macro_rules! compat_fn(
109 ($module:ident::$symbol:ident($($argname:ident: $argtype:ty),*)
110 -> $rettype:ty $fallback:block) => (
112 pub unsafe fn $symbol($($argname: $argtype),*) -> $rettype {
113 static mut ptr: extern "system" fn($($argname: $argtype),*) -> $rettype = thunk;
115 extern "system" fn thunk($($argname: $argtype),*) -> $rettype {
117 ::io::c::compat::store_func(&mut ptr as *mut _ as *mut uint,
121 ::std::intrinsics::atomic_load_relaxed(&ptr)($($argname),*)
125 extern "system" fn fallback($($argname: $argtype),*) -> $rettype $fallback
127 ::std::intrinsics::atomic_load_relaxed(&ptr)($($argname),*)
131 ($module:ident::$symbol:ident($($argname:ident: $argtype:ty),*) $fallback:block) => (
132 compat_fn!($module::$symbol($($argname: $argtype),*) -> () $fallback)
136 /// Compatibility layer for functions in `kernel32.dll`
138 /// Latest versions of Windows this is needed for:
140 /// * `CreateSymbolicLinkW`: Windows XP, Windows Server 2003
141 /// * `GetFinalPathNameByHandleW`: Windows XP, Windows Server 2003
143 use libc::types::os::arch::extra::{DWORD, LPCWSTR, BOOLEAN, HANDLE};
144 use libc::consts::os::extra::ERROR_CALL_NOT_IMPLEMENTED;
147 fn SetLastError(dwErrCode: DWORD);
150 compat_fn!(kernel32::CreateSymbolicLinkW(_lpSymlinkFileName: LPCWSTR,
151 _lpTargetFileName: LPCWSTR,
152 _dwFlags: DWORD) -> BOOLEAN {
153 unsafe { SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); }
157 compat_fn!(kernel32::GetFinalPathNameByHandleW(_hFile: HANDLE,
158 _lpszFilePath: LPCWSTR,
160 _dwFlags: DWORD) -> DWORD {
161 unsafe { SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); }