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 //! Implementation of `std::os` functionality for Windows
15 use os::windows::prelude::*;
17 use error::Error as StdError;
18 use ffi::{OsString, OsStr};
21 use libc::{c_int, c_void};
23 use os::windows::ffi::EncodeWide;
24 use path::{self, PathBuf};
28 use sys::handle::Handle;
32 pub fn errno() -> i32 {
33 unsafe { c::GetLastError() as i32 }
36 /// Gets a detailed string description for the given error number.
37 pub fn error_string(errnum: i32) -> String {
38 // This value is calculated from the macro
39 // MAKELANGID(LANG_SYSTEM_DEFAULT, SUBLANG_SYS_DEFAULT)
40 let langId = 0x0800 as c::DWORD;
42 let mut buf = [0 as c::WCHAR; 2048];
45 let res = c::FormatMessageW(c::FORMAT_MESSAGE_FROM_SYSTEM |
46 c::FORMAT_MESSAGE_IGNORE_INSERTS,
51 buf.len() as c::DWORD,
52 ptr::null()) as usize;
54 // Sometimes FormatMessageW can fail e.g. system doesn't like langId,
56 return format!("OS Error {} (FormatMessageW() returned error {})",
60 match String::from_utf16(&buf[..res]) {
62 // Trim trailing CRLF inserted by FormatMessageW
63 let len = msg.trim_right().len();
67 Err(..) => format!("OS Error {} (FormatMessageW() returned \
68 invalid UTF-16)", errnum),
78 impl Iterator for Env {
79 type Item = (OsString, OsString);
81 fn next(&mut self) -> Option<(OsString, OsString)> {
84 if *self.cur == 0 { return None }
85 let p = &*self.cur as *const u16;
87 while *p.offset(len) != 0 {
90 let s = slice::from_raw_parts(p, len as usize);
91 self.cur = self.cur.offset(len + 1);
93 // Windows allows environment variables to start with an equals
94 // symbol (in any other position, this is the separator between
95 // variable name and value). Since`s` has at least length 1 at
96 // this point (because the empty string terminates the array of
97 // environment variables), we can safely slice.
98 let pos = match s[1..].iter().position(|&u| u == b'=' as u16).map(|p| p + 1) {
103 OsStringExt::from_wide(&s[..pos]),
104 OsStringExt::from_wide(&s[pos+1..]),
113 unsafe { c::FreeEnvironmentStringsW(self.base); }
117 pub fn env() -> Env {
119 let ch = c::GetEnvironmentStringsW();
120 if ch as usize == 0 {
121 panic!("failure getting env string from OS: {}",
122 io::Error::last_os_error());
124 Env { base: ch, cur: ch }
128 pub struct SplitPaths<'a> {
129 data: EncodeWide<'a>,
133 pub fn split_paths(unparsed: &OsStr) -> SplitPaths {
135 data: unparsed.encode_wide(),
140 impl<'a> Iterator for SplitPaths<'a> {
142 fn next(&mut self) -> Option<PathBuf> {
143 // On Windows, the PATH environment variable is semicolon separated.
144 // Double quotes are used as a way of introducing literal semicolons
145 // (since c:\some;dir is a valid Windows path). Double quotes are not
146 // themselves permitted in path names, so there is no way to escape a
147 // double quote. Quoted regions can appear in arbitrary locations, so
149 // c:\foo;c:\som"e;di"r;c:\bar
151 // Should parse as [c:\foo, c:\some;dir, c:\bar].
153 // (The above is based on testing; there is no clear reference available
157 let must_yield = self.must_yield;
158 self.must_yield = false;
160 let mut in_progress = Vec::new();
161 let mut in_quote = false;
162 for b in self.data.by_ref() {
164 in_quote = !in_quote;
165 } else if b == ';' as u16 && !in_quote {
166 self.must_yield = true;
173 if !must_yield && in_progress.is_empty() {
176 Some(super::os2path(&in_progress))
182 pub struct JoinPathsError;
184 pub fn join_paths<I, T>(paths: I) -> Result<OsString, JoinPathsError>
185 where I: Iterator<Item=T>, T: AsRef<OsStr>
187 let mut joined = Vec::new();
188 let sep = b';' as u16;
190 for (i, path) in paths.enumerate() {
191 let path = path.as_ref();
192 if i > 0 { joined.push(sep) }
193 let v = path.encode_wide().collect::<Vec<u16>>();
194 if v.contains(&(b'"' as u16)) {
195 return Err(JoinPathsError)
196 } else if v.contains(&sep) {
197 joined.push(b'"' as u16);
198 joined.extend_from_slice(&v[..]);
199 joined.push(b'"' as u16);
201 joined.extend_from_slice(&v[..]);
205 Ok(OsStringExt::from_wide(&joined[..]))
208 impl fmt::Display for JoinPathsError {
209 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
210 "path segment contains `\"`".fmt(f)
214 impl StdError for JoinPathsError {
215 fn description(&self) -> &str { "failed to join paths" }
218 pub fn current_exe() -> io::Result<PathBuf> {
219 super::fill_utf16_buf(|buf, sz| unsafe {
220 c::GetModuleFileNameW(ptr::null_mut(), buf, sz)
224 pub fn getcwd() -> io::Result<PathBuf> {
225 super::fill_utf16_buf(|buf, sz| unsafe {
226 c::GetCurrentDirectoryW(sz, buf)
230 pub fn chdir(p: &path::Path) -> io::Result<()> {
231 let p: &OsStr = p.as_ref();
232 let mut p = p.encode_wide().collect::<Vec<_>>();
236 c::SetCurrentDirectoryW(p.as_ptr())
240 pub fn getenv(k: &OsStr) -> io::Result<Option<OsString>> {
242 let res = super::fill_utf16_buf(|buf, sz| unsafe {
243 c::GetEnvironmentVariableW(k.as_ptr(), buf, sz)
245 OsStringExt::from_wide(buf)
248 Ok(value) => Ok(Some(value)),
250 if e.raw_os_error() == Some(c::ERROR_ENVVAR_NOT_FOUND as i32) {
259 pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
264 c::SetEnvironmentVariableW(k.as_ptr(), v.as_ptr())
268 pub fn unsetenv(n: &OsStr) -> io::Result<()> {
271 c::SetEnvironmentVariableW(v.as_ptr(), ptr::null())
280 unsafe fn os_string_from_ptr(ptr: *mut u16) -> OsString {
282 while *ptr.offset(len) != 0 { len += 1; }
284 // Push it onto the list.
285 let ptr = ptr as *const u16;
286 let buf = slice::from_raw_parts(ptr, len as usize);
287 OsStringExt::from_wide(buf)
290 impl Iterator for Args {
291 type Item = OsString;
292 fn next(&mut self) -> Option<OsString> {
293 self.range.next().map(|i| unsafe { os_string_from_ptr(*self.cur.offset(i)) } )
295 fn size_hint(&self) -> (usize, Option<usize>) { self.range.size_hint() }
298 impl DoubleEndedIterator for Args {
299 fn next_back(&mut self) -> Option<OsString> {
300 self.range.next_back().map(|i| unsafe { os_string_from_ptr(*self.cur.offset(i)) } )
304 impl ExactSizeIterator for Args {
305 fn len(&self) -> usize { self.range.len() }
310 // self.cur can be null if CommandLineToArgvW previously failed,
311 // but LocalFree ignores NULL pointers
312 unsafe { c::LocalFree(self.cur as *mut c_void); }
316 pub fn args() -> Args {
318 let mut nArgs: c_int = 0;
319 let lpCmdLine = c::GetCommandLineW();
320 let szArgList = c::CommandLineToArgvW(lpCmdLine, &mut nArgs);
322 // szArcList can be NULL if CommandLinToArgvW failed,
323 // but in that case nArgs is 0 so we won't actually
324 // try to read a null pointer
325 Args { cur: szArgList, range: 0..(nArgs as isize) }
329 pub fn temp_dir() -> PathBuf {
330 super::fill_utf16_buf(|buf, sz| unsafe {
331 c::GetTempPathW(sz, buf)
332 }, super::os2path).unwrap()
335 pub fn home_dir() -> Option<PathBuf> {
336 ::env::var_os("HOME").or_else(|| {
337 ::env::var_os("USERPROFILE")
338 }).map(PathBuf::from).or_else(|| unsafe {
339 let me = c::GetCurrentProcess();
340 let mut token = ptr::null_mut();
341 if c::OpenProcessToken(me, c::TOKEN_READ, &mut token) == 0 {
344 let _handle = Handle::new(token);
345 super::fill_utf16_buf(|buf, mut sz| {
346 match c::GetUserProfileDirectoryW(token, buf, &mut sz) {
347 0 if c::GetLastError() != c::ERROR_INSUFFICIENT_BUFFER => 0,
349 _ => sz - 1, // sz includes the null terminator
351 }, super::os2path).ok()
355 pub fn exit(code: i32) -> ! {
356 unsafe { c::ExitProcess(code as c::UINT) }