os.rs 54.2 KB
Newer Older
A
Akos Kiss 已提交
1
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 3 4 5 6 7 8 9 10
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

S
Steve Klabnik 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23
//! Higher-level interfaces to libc::* functions and operating system services.
//!
//! In general these take and return rust types, use rust idioms (enums, closures, vectors) rather
//! than C idioms, and do more extensive safety checks.
//!
//! This module is not meant to only contain 1:1 mappings to libc entries; any os-interface code
//! that is reasonably useful and broadly applicable can go here. Including utility routines that
//! merely build on other os code.
//!
//! We assume the general case is that users do not care, and do not want to be made to care, which
//! operating system they are on. While they may want to special case various special cases -- and
//! so we will not _hide_ the facts of which OS the user is on -- they should be given the
//! opportunity to write OS-ignorant code by default.
24

25 26
#![experimental]

A
Aaron Turon 已提交
27
#![allow(missing_docs)]
28
#![allow(non_snake_case)]
29
#![allow(unused_imports)]
30

C
Corey Farwell 已提交
31 32 33
use self::MemoryMapKind::*;
use self::MapOption::*;
use self::MapError::*;
S
Steven Fackler 已提交
34

35
use clone::Clone;
36
use error::{FromError, Error};
37
use fmt;
38
use io::{IoResult, IoError};
A
Aaron Turon 已提交
39
use iter::{Iterator, IteratorExt};
N
Niko Matsakis 已提交
40
use kinds::Copy;
41
use libc::{c_void, c_int, c_char};
42
use libc;
43
use boxed::Box;
J
Jorge Aparicio 已提交
44
use ops::{Drop, FnOnce};
C
Corey Farwell 已提交
45 46
use option::Option;
use option::Option::{Some, None};
A
Aaron Turon 已提交
47
use path::{Path, GenericPath, BytesContainer};
48
use sys;
49 50
use sys::os as os_imp;
use ptr::PtrExt;
51
use ptr;
C
Corey Farwell 已提交
52 53
use result::Result;
use result::Result::{Err, Ok};
A
Aaron Turon 已提交
54
use slice::{AsSlice, SliceExt};
A
Alex Crichton 已提交
55
use str::{Str, StrExt};
56
use string::{String, ToString};
57
use sync::atomic::{AtomicInt, ATOMIC_INT_INIT, SeqCst};
58
use vec::Vec;
59

60
#[cfg(unix)] use c_str::ToCStr;
61

A
Aaron Turon 已提交
62 63 64 65 66
#[cfg(unix)]
pub use sys::ext as unix;
#[cfg(windows)]
pub use sys::ext as windows;

67 68 69
/// Get the number of cores available
pub fn num_cpus() -> uint {
    unsafe {
70
        return rust_get_num_cpus() as uint;
71 72 73 74 75 76
    }

    extern {
        fn rust_get_num_cpus() -> libc::uintptr_t;
    }
}
77

A
Alex Crichton 已提交
78
pub const TMPBUF_SZ : uint = 1000u;
79

80
/// Returns the current working directory as a `Path`.
81
///
82
/// # Errors
83
///
84
/// Returns an `Err` if the current working directory value is invalid.
J
jmu303 已提交
85
/// Possible cases:
86 87 88
///
/// * Current directory does not exist.
/// * There are insufficient permissions to access the current directory.
89
/// * The internal buffer is not large enough to hold the path.
90 91 92 93
///
/// # Example
///
/// ```rust
A
Axel Viala 已提交
94 95
/// use std::os;
///
A
Aaron Turon 已提交
96
/// // We assume that we are in a valid directory.
97
/// let current_working_directory = os::getcwd().unwrap();
98 99
/// println!("The current directory is {}", current_working_directory.display());
/// ```
100
pub fn getcwd() -> IoResult<Path> {
A
Aaron Turon 已提交
101
    sys::os::getcwd()
102 103
}

104 105
/*
Accessing environment variables is not generally threadsafe.
106
Serialize access through a global lock.
107
*/
J
Jorge Aparicio 已提交
108 109 110
fn with_env_lock<T, F>(f: F) -> T where
    F: FnOnce() -> T,
{
A
Alex Crichton 已提交
111
    use sync::{StaticMutex, MUTEX_INIT};
112

A
Alex Crichton 已提交
113
    static LOCK: StaticMutex = MUTEX_INIT;
114

A
Alex Crichton 已提交
115 116
    let _guard = LOCK.lock();
    f()
B
Ben Blum 已提交
117 118
}

A
Axel Viala 已提交
119 120
/// Returns a vector of (variable, value) pairs, for all the environment
/// variables of the current process.
121
///
122
/// Invalid UTF-8 bytes are replaced with \uFFFD. See `String::from_utf8_lossy()`
123
/// for details.
124 125 126 127
///
/// # Example
///
/// ```rust
A
Axel Viala 已提交
128 129 130 131
/// use std::os;
///
/// // We will iterate through the references to the element returned by os::env();
/// for &(ref key, ref value) in os::env().iter() {
132 133 134
///     println!("'{}': '{}'", key, value );
/// }
/// ```
135
pub fn env() -> Vec<(String,String)> {
A
Aaron Turon 已提交
136
    env_as_bytes().into_iter().map(|(k,v)| {
A
Alex Crichton 已提交
137 138
        let k = String::from_utf8_lossy(k.as_slice()).into_owned();
        let v = String::from_utf8_lossy(v.as_slice()).into_owned();
139 140 141 142 143 144
        (k,v)
    }).collect()
}

/// Returns a vector of (variable, value) byte-vector pairs for all the
/// environment variables of the current process.
145
pub fn env_as_bytes() -> Vec<(Vec<u8>,Vec<u8>)> {
146
    unsafe {
147
        fn env_convert(input: Vec<Vec<u8>>) -> Vec<(Vec<u8>, Vec<u8>)> {
148
            let mut pairs = Vec::new();
D
Daniel Micay 已提交
149
            for p in input.iter() {
150
                let mut it = p.splitn(1, |b| *b == b'=');
151
                let key = it.next().unwrap().to_vec();
152
                let default: &[u8] = &[];
153
                let val = it.next().unwrap_or(default).to_vec();
154
                pairs.push((key, val));
155
            }
L
Luqman Aden 已提交
156
            pairs
157
        }
158
        with_env_lock(|| {
A
Aaron Turon 已提交
159
            let unparsed_environ = sys::os::get_env_pairs();
K
Kevin Ballard 已提交
160
            env_convert(unparsed_environ)
161
        })
162
    }
163
}
164

165
#[cfg(unix)]
166 167
/// Fetches the environment variable `n` from the current process, returning
/// None if the variable isn't set.
168 169
///
/// Any invalid UTF-8 bytes in the value are replaced by \uFFFD. See
170
/// `String::from_utf8_lossy()` for details.
171
///
172
/// # Panics
173
///
174
/// Panics if `n` has any interior NULs.
175 176 177 178
///
/// # Example
///
/// ```rust
A
Axel Viala 已提交
179 180
/// use std::os;
///
181
/// let key = "HOME";
A
Axel Viala 已提交
182
/// match os::getenv(key) {
183
///     Some(val) => println!("{}: {}", key, val),
J
Joseph Crail 已提交
184
///     None => println!("{} is not defined in the environment.", key)
185 186
/// }
/// ```
187
pub fn getenv(n: &str) -> Option<String> {
A
Alex Crichton 已提交
188
    getenv_as_bytes(n).map(|v| String::from_utf8_lossy(v.as_slice()).into_owned())
189 190 191 192 193 194
}

#[cfg(unix)]
/// Fetches the environment variable `n` byte vector from the current process,
/// returning None if the variable isn't set.
///
195
/// # Panics
196
///
197
/// Panics if `n` has any interior NULs.
198
pub fn getenv_as_bytes(n: &str) -> Option<Vec<u8>> {
199 200
    use c_str::CString;

201
    unsafe {
202 203
        with_env_lock(|| {
            let s = n.with_c_str(|buf| libc::getenv(buf));
204
            if s.is_null() {
205
                None
206
            } else {
A
Akos Kiss 已提交
207
                Some(CString::new(s as *const libc::c_char, false).as_bytes_no_nul().to_vec())
208
            }
209
        })
210 211
    }
}
212

213
#[cfg(windows)]
214 215
/// Fetches the environment variable `n` from the current process, returning
/// None if the variable isn't set.
216
pub fn getenv(n: &str) -> Option<String> {
217
    unsafe {
218
        with_env_lock(|| {
A
Aaron Turon 已提交
219
            use sys::os::fill_utf16_buf_and_decode;
220 221
            let mut n: Vec<u16> = n.utf16_units().collect();
            n.push(0);
222 223
            fill_utf16_buf_and_decode(|buf, sz| {
                libc::GetEnvironmentVariableW(n.as_ptr(), buf, sz)
224 225
            })
        })
226 227
    }
}
228

229 230 231
#[cfg(windows)]
/// Fetches the environment variable `n` byte vector from the current process,
/// returning None if the variable isn't set.
232
pub fn getenv_as_bytes(n: &str) -> Option<Vec<u8>> {
233 234 235
    getenv(n).map(|s| s.into_bytes())
}

236
/// Sets the environment variable `n` to the value `v` for the currently running
A
Axel Viala 已提交
237
/// process.
238
///
A
Axel Viala 已提交
239
/// # Example
240
///
A
Axel Viala 已提交
241 242 243 244 245 246 247 248 249 250
/// ```rust
/// use std::os;
///
/// let key = "KEY";
/// os::setenv(key, "VALUE");
/// match os::getenv(key) {
///     Some(ref val) => println!("{}: {}", key, val),
///     None => println!("{} is not defined in the environment.", key)
/// }
/// ```
251
pub fn setenv<T: BytesContainer>(n: &str, v: T) {
A
Axel Viala 已提交
252
    #[cfg(unix)]
253
    fn _setenv(n: &str, v: &[u8]) {
A
Axel Viala 已提交
254 255 256 257
        unsafe {
            with_env_lock(|| {
                n.with_c_str(|nbuf| {
                    v.with_c_str(|vbuf| {
258 259 260
                        if libc::funcs::posix01::unistd::setenv(nbuf, vbuf, 1) != 0 {
                            panic!(IoError::last_error());
                        }
A
Axel Viala 已提交
261
                    })
262 263
                })
            })
A
Axel Viala 已提交
264
        }
265
    }
266

A
Axel Viala 已提交
267
    #[cfg(windows)]
268
    fn _setenv(n: &str, v: &[u8]) {
269 270 271 272
        let mut n: Vec<u16> = n.utf16_units().collect();
        n.push(0);
        let mut v: Vec<u16> = ::str::from_utf8(v).unwrap().utf16_units().collect();
        v.push(0);
273

A
Axel Viala 已提交
274 275
        unsafe {
            with_env_lock(|| {
276 277 278
                if libc::SetEnvironmentVariableW(n.as_ptr(), v.as_ptr()) == 0 {
                    panic!(IoError::last_error());
                }
A
Axel Viala 已提交
279 280
            })
        }
281
    }
282 283

    _setenv(n, v.container_as_bytes())
284 285
}

A
Axel Viala 已提交
286
/// Remove a variable from the environment entirely.
C
Corey Richardson 已提交
287 288 289 290
pub fn unsetenv(n: &str) {
    #[cfg(unix)]
    fn _unsetenv(n: &str) {
        unsafe {
291 292
            with_env_lock(|| {
                n.with_c_str(|nbuf| {
293 294 295
                    if libc::funcs::posix01::unistd::unsetenv(nbuf) != 0 {
                        panic!(IoError::last_error());
                    }
296 297
                })
            })
C
Corey Richardson 已提交
298 299
        }
    }
A
Axel Viala 已提交
300

C
Corey Richardson 已提交
301 302
    #[cfg(windows)]
    fn _unsetenv(n: &str) {
303 304
        let mut n: Vec<u16> = n.utf16_units().collect();
        n.push(0);
C
Corey Richardson 已提交
305
        unsafe {
306
            with_env_lock(|| {
307 308 309
                if libc::SetEnvironmentVariableW(n.as_ptr(), ptr::null()) == 0 {
                    panic!(IoError::last_error());
                }
310
            })
C
Corey Richardson 已提交
311 312
        }
    }
313 314

    _unsetenv(n)
C
Corey Richardson 已提交
315 316
}

317 318
/// Parses input according to platform conventions for the `PATH`
/// environment variable.
A
Axel Viala 已提交
319 320 321 322 323 324
///
/// # Example
/// ```rust
/// use std::os;
///
/// let key = "PATH";
325
/// match os::getenv_as_bytes(key) {
A
Axel Viala 已提交
326 327 328 329 330
///     Some(paths) => {
///         for path in os::split_paths(paths).iter() {
///             println!("'{}'", path.display());
///         }
///     }
J
Joseph Crail 已提交
331
///     None => println!("{} is not defined in the environment.", key)
A
Axel Viala 已提交
332 333
/// }
/// ```
A
Aaron Turon 已提交
334
pub fn split_paths<T: BytesContainer>(unparsed: T) -> Vec<Path> {
A
Aaron Turon 已提交
335
    sys::os::split_paths(unparsed.container_as_bytes())
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
}

/// Joins a collection of `Path`s appropriately for the `PATH`
/// environment variable.
///
/// Returns a `Vec<u8>` on success, since `Path`s are not utf-8
/// encoded on all platforms.
///
/// Returns an `Err` (containing an error message) if one of the input
/// `Path`s contains an invalid character for constructing the `PATH`
/// variable (a double quote on Windows or a colon on Unix).
///
/// # Example
///
/// ```rust
/// use std::os;
/// use std::path::Path;
///
/// let key = "PATH";
/// let mut paths = os::getenv_as_bytes(key).map_or(Vec::new(), os::split_paths);
/// paths.push(Path::new("/home/xyz/bin"));
/// os::setenv(key, os::join_paths(paths.as_slice()).unwrap());
/// ```
pub fn join_paths<T: BytesContainer>(paths: &[T]) -> Result<Vec<u8>, &'static str> {
A
Aaron Turon 已提交
360
    sys::os::join_paths(paths)
A
Aaron Turon 已提交
361 362
}

363
/// A low-level OS in-memory pipe.
J
Jorge Aparicio 已提交
364
#[deriving(Copy)]
365
pub struct Pipe {
366 367
    /// A file descriptor representing the reading end of the pipe. Data written
    /// on the `out` file descriptor can be read from this file descriptor.
368
    pub reader: c_int,
369 370
    /// A file descriptor representing the write end of the pipe. Data written
    /// to this file descriptor can be read from the `input` file descriptor.
371
    pub writer: c_int,
372
}
373

374 375 376 377 378 379 380 381 382 383
/// Creates a new low-level OS in-memory pipe.
///
/// This function can fail to succeed if there are no more resources available
/// to allocate a pipe.
///
/// This function is also unsafe as there is no destructor associated with the
/// `Pipe` structure will return. If it is not arranged for the returned file
/// descriptors to be closed, the file descriptors will leak. For safe handling
/// of this scenario, use `std::io::PipeStream` instead.
pub unsafe fn pipe() -> IoResult<Pipe> {
384 385 386 387 388
    let (reader, writer) = try!(sys::os::pipe());
    Ok(Pipe {
        reader: reader.unwrap(),
        writer: writer.unwrap(),
    })
389 390
}

A
Axel Viala 已提交
391 392
/// Returns the proper dll filename for the given basename of a file
/// as a String.
V
Valerii Hiora 已提交
393
#[cfg(not(target_os="ios"))]
394
pub fn dll_filename(base: &str) -> String {
A
Alex Crichton 已提交
395
    format!("{}{}{}", consts::DLL_PREFIX, base, consts::DLL_SUFFIX)
396 397
}

A
Axel Viala 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410
/// Optionally returns the filesystem path to the current executable which is
/// running but with the executable name.
///
/// # Examples
///
/// ```rust
/// use std::os;
///
/// match os::self_exe_name() {
///     Some(exe_path) => println!("Path of this executable is: {}", exe_path.display()),
///     None => println!("Unable to get the path of this executable!")
/// };
/// ```
B
Ben Noordhuis 已提交
411
pub fn self_exe_name() -> Option<Path> {
A
Aaron Turon 已提交
412
    sys::os::load_self().and_then(Path::new_opt)
B
Ben Noordhuis 已提交
413 414 415
}

/// Optionally returns the filesystem path to the current executable which is
A
Axel Viala 已提交
416 417 418 419 420 421 422 423 424 425 426 427 428 429
/// running.
///
/// Like self_exe_name() but without the binary's name.
///
/// # Example
///
/// ```rust
/// use std::os;
///
/// match os::self_exe_path() {
///     Some(exe_path) => println!("Executable's Path is: {}", exe_path.display()),
///     None => println!("Impossible to fetch the path of this executable.")
/// };
/// ```
B
Ben Noordhuis 已提交
430 431
pub fn self_exe_path() -> Option<Path> {
    self_exe_name().map(|mut p| { p.pop(); p })
432 433
}

A
Axel Viala 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
/// Optionally returns the path to the current user's home directory if known.
///
/// # Unix
///
/// Returns the value of the 'HOME' environment variable if it is set
/// and not equal to the empty string.
///
/// # Windows
///
/// Returns the value of the 'HOME' environment variable if it is
/// set and not equal to the empty string. Otherwise, returns the value of the
/// 'USERPROFILE' environment variable if it is set and not equal to the empty
/// string.
///
/// # Example
///
/// ```rust
/// use std::os;
///
/// match os::homedir() {
///     Some(ref p) => println!("{}", p.display()),
///     None => println!("Impossible to get your home dir!")
/// }
/// ```
458
pub fn homedir() -> Option<Path> {
A
Axel Viala 已提交
459
    #[inline]
460
    #[cfg(unix)]
A
Axel Viala 已提交
461 462
    fn _homedir() -> Option<Path> {
        aux_homedir("HOME")
463 464
    }

A
Axel Viala 已提交
465
    #[inline]
466
    #[cfg(windows)]
A
Axel Viala 已提交
467 468 469 470 471 472 473 474 475 476 477 478
    fn _homedir() -> Option<Path> {
        aux_homedir("HOME").or(aux_homedir("USERPROFILE"))
    }

    #[inline]
    fn aux_homedir(home_name: &str) -> Option<Path> {
        match getenv_as_bytes(home_name) {
            Some(p)  => {
                if p.is_empty() { None } else { Path::new_opt(p) }
            },
            _ => None
        }
479
    }
A
Axel Viala 已提交
480
    _homedir()
481 482
}

S
Steve Klabnik 已提交
483 484 485 486 487 488 489 490 491 492
/// Returns the path to a temporary directory.
///
/// On Unix, returns the value of the 'TMPDIR' environment variable if it is
/// set, otherwise for non-Android it returns '/tmp'. If Android, since there
/// is no global temporary folder (it is usually allocated per-app), we return
/// '/data/local/tmp'.
///
/// On Windows, returns the value of, in order, the 'TMP', 'TEMP',
/// 'USERPROFILE' environment variable  if any are set and not the empty
/// string. Otherwise, tmpdir returns the path to the Windows directory.
493
pub fn tmpdir() -> Path {
494 495
    return lookup();

B
Brian Anderson 已提交
496
    fn getenv_nonempty(v: &str) -> Option<Path> {
497
        match getenv(v) {
L
Luqman Aden 已提交
498
            Some(x) =>
499
                if x.is_empty() {
B
Brian Anderson 已提交
500
                    None
501
                } else {
502
                    Path::new_opt(x)
503
                },
B
Brian Anderson 已提交
504
            _ => None
505 506 507 508
        }
    }

    #[cfg(unix)]
509
    fn lookup() -> Path {
510 511
        let default = if cfg!(target_os = "android") {
            Path::new("/data/local/tmp")
512
        } else {
513 514 515 516
            Path::new("/tmp")
        };

        getenv_nonempty("TMPDIR").unwrap_or(default)
517 518 519
    }

    #[cfg(windows)]
520
    fn lookup() -> Path {
521 522 523
        getenv_nonempty("TMP").or(
            getenv_nonempty("TEMP").or(
                getenv_nonempty("USERPROFILE").or(
524
                   getenv_nonempty("WINDIR")))).unwrap_or(Path::new("C:\\Windows"))
525 526
    }
}
B
Brian Anderson 已提交
527

A
Anton Lofgren 已提交
528 529 530 531 532 533 534 535 536 537 538 539 540
/// Convert a relative path to an absolute path
///
/// If the given path is relative, return it prepended with the current working
/// directory. If the given path is already an absolute path, return it
/// as is.
///
/// # Example
/// ```rust
/// use std::os;
/// use std::path::Path;
///
/// // Assume we're in a path like /home/someuser
/// let rel_path = Path::new("..");
541
/// let abs_path = os::make_absolute(&rel_path).unwrap();
A
Anton Lofgren 已提交
542 543 544
/// println!("The absolute path is {}", abs_path.display());
/// // Prints "The absolute path is /home"
/// ```
545 546 547
// NB: this is here rather than in path because it is a form of environment
// querying; what it does depends on the process working directory, not just
// the input paths.
548
pub fn make_absolute(p: &Path) -> IoResult<Path> {
549
    if p.is_absolute() {
550
        Ok(p.clone())
551
    } else {
552 553 554 555
        getcwd().map(|mut cwd| {
            cwd.push(p);
            cwd
        })
556
    }
557 558
}

559 560
/// Changes the current working directory to the specified path, returning
/// whether the change was completed successfully or not.
A
Anton Lofgren 已提交
561 562 563 564 565 566 567
///
/// # Example
/// ```rust
/// use std::os;
/// use std::path::Path;
///
/// let root = Path::new("/");
568
/// assert!(os::change_dir(&root).is_ok());
J
Joseph Crail 已提交
569
/// println!("Successfully changed working directory to {}!", root.display());
A
Anton Lofgren 已提交
570
/// ```
571
pub fn change_dir(p: &Path) -> IoResult<()> {
A
Aaron Turon 已提交
572
    return sys::os::chdir(p);
573 574
}

575
/// Returns the platform-specific value of errno
576
pub fn errno() -> uint {
A
Aaron Turon 已提交
577
    sys::os::errno() as uint
578 579
}

580
/// Return the string corresponding to an `errno()` value of `errnum`.
B
Barosl Lee 已提交
581
///
A
Anton Lofgren 已提交
582 583 584 585 586 587 588
/// # Example
/// ```rust
/// use std::os;
///
/// // Same as println!("{}", last_os_error());
/// println!("{}", os::error_string(os::errno() as uint));
/// ```
589
pub fn error_string(errnum: uint) -> String {
A
Aaron Turon 已提交
590
    return sys::os::error_string(errnum as i32);
591
}
592

593
/// Get a string representing the platform-dependent last error
594
pub fn last_os_error() -> String {
595
    error_string(errno() as uint)
596
}
597

598
static EXIT_STATUS: AtomicInt = ATOMIC_INT_INIT;
599

S
Steve Klabnik 已提交
600 601 602 603 604 605 606 607
/// Sets the process exit code
///
/// Sets the exit code returned by the process if all supervised tasks
/// terminate successfully (without panicking). If the current root task panics
/// and is supervised by the scheduler then any user-specified exit status is
/// ignored and the process exits with the default panic status.
///
/// Note that this is not synchronized against modifications of other threads.
608
pub fn set_exit_status(code: int) {
A
Alex Crichton 已提交
609
    EXIT_STATUS.store(code, SeqCst)
610 611 612 613 614
}

/// Fetches the process's current exit code. This defaults to 0 and can change
/// by calling `set_exit_status`.
pub fn get_exit_status() -> int {
A
Alex Crichton 已提交
615
    EXIT_STATUS.load(SeqCst)
616
}
617

K
Kiet Tran 已提交
618
#[cfg(target_os = "macos")]
619 620
unsafe fn load_argc_and_argv(argc: int,
                             argv: *const *const c_char) -> Vec<Vec<u8>> {
621
    use c_str::CString;
A
Aaron Turon 已提交
622
    use iter::range;
623

A
Aaron Turon 已提交
624
    range(0, argc as uint).map(|i| {
625
        CString::new(*argv.offset(i as int), false).as_bytes_no_nul().to_vec()
A
Aaron Turon 已提交
626
    }).collect()
627 628
}

S
Steve Klabnik 已提交
629 630 631
/// Returns the command line arguments
///
/// Returns a list of the command line arguments.
632
#[cfg(target_os = "macos")]
633
fn real_args_as_bytes() -> Vec<Vec<u8>> {
634
    unsafe {
635
        let (argc, argv) = (*_NSGetArgc() as int,
636
                            *_NSGetArgv() as *const *const c_char);
637
        load_argc_and_argv(argc, argv)
638 639 640
    }
}

V
Valerii Hiora 已提交
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
// As _NSGetArgc and _NSGetArgv aren't mentioned in iOS docs
// and use underscores in their names - they're most probably
// are considered private and therefore should be avoided
// Here is another way to get arguments using Objective C
// runtime
//
// In general it looks like:
// res = Vec::new()
// let args = [[NSProcessInfo processInfo] arguments]
// for i in range(0, [args count])
//      res.push([args objectAtIndex:i])
// res
#[cfg(target_os = "ios")]
fn real_args_as_bytes() -> Vec<Vec<u8>> {
    use c_str::CString;
    use iter::range;
    use mem;

    #[link(name = "objc")]
    extern {
661
        fn sel_registerName(name: *const libc::c_uchar) -> Sel;
V
Valerii Hiora 已提交
662
        fn objc_msgSend(obj: NsId, sel: Sel, ...) -> NsId;
663
        fn objc_getClass(class_name: *const libc::c_uchar) -> NsId;
V
Valerii Hiora 已提交
664 665 666 667 668
    }

    #[link(name = "Foundation", kind = "framework")]
    extern {}

669 670
    type Sel = *const libc::c_void;
    type NsId = *const libc::c_void;
V
Valerii Hiora 已提交
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687

    let mut res = Vec::new();

    unsafe {
        let processInfoSel = sel_registerName("processInfo\0".as_ptr());
        let argumentsSel = sel_registerName("arguments\0".as_ptr());
        let utf8Sel = sel_registerName("UTF8String\0".as_ptr());
        let countSel = sel_registerName("count\0".as_ptr());
        let objectAtSel = sel_registerName("objectAtIndex:\0".as_ptr());

        let klass = objc_getClass("NSProcessInfo\0".as_ptr());
        let info = objc_msgSend(klass, processInfoSel);
        let args = objc_msgSend(info, argumentsSel);

        let cnt: int = mem::transmute(objc_msgSend(args, countSel));
        for i in range(0, cnt) {
            let tmp = objc_msgSend(args, objectAtSel, i);
688 689
            let utf_c_str: *const libc::c_char =
                mem::transmute(objc_msgSend(tmp, utf8Sel));
V
Valerii Hiora 已提交
690
            let s = CString::new(utf_c_str, false);
691
            res.push(s.as_bytes_no_nul().to_vec())
V
Valerii Hiora 已提交
692 693 694 695 696 697
        }
    }

    res
}

S
Steven Fackler 已提交
698 699 700 701
#[cfg(any(target_os = "linux",
          target_os = "android",
          target_os = "freebsd",
          target_os = "dragonfly"))]
702
fn real_args_as_bytes() -> Vec<Vec<u8>> {
A
Aaron Turon 已提交
703
    use rt;
704
    rt::args::clone().unwrap_or_else(|| vec![])
705 706
}

707
#[cfg(not(windows))]
708
fn real_args() -> Vec<String> {
A
Aaron Turon 已提交
709
    real_args_as_bytes().into_iter()
710
                        .map(|v| {
A
Alex Crichton 已提交
711
                            String::from_utf8_lossy(v.as_slice()).into_owned()
712
                        }).collect()
713 714
}

715
#[cfg(windows)]
716
fn real_args() -> Vec<String> {
D
Daniel Micay 已提交
717
    use slice;
A
Alex Crichton 已提交
718
    use iter::range;
719

720
    let mut nArgs: c_int = 0;
D
Daniel Micay 已提交
721
    let lpArgCount: *mut c_int = &mut nArgs;
T
Tim Chevalier 已提交
722 723
    let lpCmdLine = unsafe { GetCommandLineW() };
    let szArgList = unsafe { CommandLineToArgvW(lpCmdLine, lpArgCount) };
724

A
Aaron Turon 已提交
725
    let args: Vec<_> = range(0, nArgs as uint).map(|i| unsafe {
726 727 728 729 730 731
        // Determine the length of this argument.
        let ptr = *szArgList.offset(i as int);
        let mut len = 0;
        while *ptr.offset(len as int) != 0 { len += 1; }

        // Push it onto the list.
732 733
        let ptr = ptr as *const u16;
        let buf = slice::from_raw_buf(&ptr, len);
A
Alex Crichton 已提交
734
        let opt_s = String::from_utf16(sys::os::truncate_utf16_at_nul(buf));
A
Alex Crichton 已提交
735
        opt_s.ok().expect("CommandLineToArgvW returned invalid UTF-16")
A
Aaron Turon 已提交
736
    }).collect();
737 738

    unsafe {
739
        LocalFree(szArgList as *mut c_void);
740 741
    }

K
Kevin Ballard 已提交
742
    return args
743 744
}

745
#[cfg(windows)]
746
fn real_args_as_bytes() -> Vec<Vec<u8>> {
A
Aaron Turon 已提交
747
    real_args().into_iter().map(|s| s.into_bytes()).collect()
748 749
}

750
type LPCWSTR = *const u16;
751

A
Alex Crichton 已提交
752
#[cfg(windows)]
K
klutzy 已提交
753
#[link_name="kernel32"]
A
Alex Crichton 已提交
754
extern "system" {
K
klutzy 已提交
755
    fn GetCommandLineW() -> LPCWSTR;
756
    fn LocalFree(ptr: *mut c_void);
K
klutzy 已提交
757 758
}

A
Alex Crichton 已提交
759
#[cfg(windows)]
K
klutzy 已提交
760
#[link_name="shell32"]
A
Alex Crichton 已提交
761
extern "system" {
762 763
    fn CommandLineToArgvW(lpCmdLine: LPCWSTR,
                          pNumArgs: *mut c_int) -> *mut *mut u16;
K
klutzy 已提交
764 765
}

766 767
/// Returns the arguments which this program was started with (normally passed
/// via the command line).
768
///
L
Liigo Zhuang 已提交
769 770
/// The first element is traditionally the path to the executable, but it can be
/// set to arbitrary text, and it may not even exist, so this property should not
771
/// be relied upon for security purposes.
L
Liigo Zhuang 已提交
772
///
773
/// The arguments are interpreted as utf-8, with invalid bytes replaced with \uFFFD.
774
/// See `String::from_utf8_lossy` for details.
A
Anton Lofgren 已提交
775 776 777 778 779 780 781 782 783 784
/// # Example
///
/// ```rust
/// use std::os;
///
/// // Prints each argument on a separate line
/// for argument in os::args().iter() {
///     println!("{}", argument);
/// }
/// ```
785
pub fn args() -> Vec<String> {
786
    real_args()
787 788
}

789 790
/// Returns the arguments which this program was started with (normally passed
/// via the command line) as byte vectors.
791
pub fn args_as_bytes() -> Vec<Vec<u8>> {
792 793 794
    real_args_as_bytes()
}

795 796 797
#[cfg(target_os = "macos")]
extern {
    // These functions are in crt_externs.h.
798 799
    pub fn _NSGetArgc() -> *mut c_int;
    pub fn _NSGetArgv() -> *mut *mut *mut c_char;
800 801
}

802
/// Returns the page size of the current architecture in bytes.
803
pub fn page_size() -> uint {
A
Aaron Turon 已提交
804
    sys::os::page_size()
805 806
}

A
Alex Crichton 已提交
807 808
/// A memory mapped file or chunk of memory. This is a very system-specific
/// interface to the OS's memory mapping facilities (`mmap` on POSIX,
809
/// `VirtualAlloc`/`CreateFileMapping` on Windows). It makes no attempt at
A
Alex Crichton 已提交
810
/// abstracting platform differences, besides in error values returned. Consider
C
Corey Richardson 已提交
811 812
/// yourself warned.
///
A
Alex Crichton 已提交
813 814
/// The memory map is released (unmapped) when the destructor is run, so don't
/// let it leave scope by accident if you want it to stick around.
A
Alex Crichton 已提交
815
#[allow(missing_copy_implementations)]
816
pub struct MemoryMap {
817 818 819
    data: *mut u8,
    len: uint,
    kind: MemoryMapKind,
820 821
}

C
Corey Richardson 已提交
822
/// Type of memory map
823
pub enum MemoryMapKind {
A
Alex Crichton 已提交
824 825
    /// Virtual memory map. Usually used to change the permissions of a given
    /// chunk of memory.  Corresponds to `VirtualAlloc` on Windows.
826
    MapFile(*const u8),
A
Alex Crichton 已提交
827 828 829
    /// Virtual memory map. Usually used to change the permissions of a given
    /// chunk of memory, or for allocation. Corresponds to `VirtualAlloc` on
    /// Windows.
830 831 832
    MapVirtual
}

N
Niko Matsakis 已提交
833 834
impl Copy for MemoryMapKind {}

C
Corey Richardson 已提交
835
/// Options the memory map is created with
836
pub enum MapOption {
C
Corey Richardson 已提交
837
    /// The memory should be readable
838
    MapReadable,
C
Corey Richardson 已提交
839
    /// The memory should be writable
840
    MapWritable,
C
Corey Richardson 已提交
841
    /// The memory should be executable
842
    MapExecutable,
A
Alex Crichton 已提交
843 844
    /// Create a map for a specific address range. Corresponds to `MAP_FIXED` on
    /// POSIX.
845
    MapAddr(*const u8),
846 847 848
    /// Create a memory mapping for a file with a given HANDLE.
    #[cfg(windows)]
    MapFd(libc::HANDLE),
C
Corey Richardson 已提交
849
    /// Create a memory mapping for a file with a given fd.
850
    #[cfg(not(windows))]
851
    MapFd(c_int),
A
Alex Crichton 已提交
852 853
    /// When using `MapFd`, the start of the map is `uint` bytes from the start
    /// of the file.
854
    MapOffset(uint),
A
Alex Crichton 已提交
855 856 857 858
    /// On POSIX, this can be used to specify the default flags passed to
    /// `mmap`. By default it uses `MAP_PRIVATE` and, if not using `MapFd`,
    /// `MAP_ANON`. This will override both of those. This is platform-specific
    /// (the exact values used) and ignored on Windows.
859
    MapNonStandardFlags(c_int),
860 861
}

N
Niko Matsakis 已提交
862 863
impl Copy for MapOption {}

C
Corey Richardson 已提交
864
/// Possible errors when creating a map.
J
Jorge Aparicio 已提交
865
#[deriving(Copy)]
866
pub enum MapError {
S
Steve Klabnik 已提交
867
    /// # The following are POSIX-specific
C
Corey Richardson 已提交
868
    ///
A
Alex Crichton 已提交
869 870
    /// fd was not open for reading or, if using `MapWritable`, was not open for
    /// writing.
871
    ErrFdNotAvail,
C
Corey Richardson 已提交
872
    /// fd was not valid
873
    ErrInvalidFd,
A
Alex Crichton 已提交
874 875
    /// Either the address given by `MapAddr` or offset given by `MapOffset` was
    /// not a multiple of `MemoryMap::granularity` (unaligned to page size).
876
    ErrUnaligned,
C
Corey Richardson 已提交
877
    /// With `MapFd`, the fd does not support mapping.
878
    ErrNoMapSupport,
A
Alex Crichton 已提交
879 880 881
    /// If using `MapAddr`, the address + `min_len` was outside of the process's
    /// address space. If using `MapFd`, the target of the fd didn't have enough
    /// resources to fulfill the request.
882
    ErrNoMem,
C
Corey Richardson 已提交
883
    /// A zero-length map was requested. This is invalid according to
A
Alex Crichton 已提交
884 885
    /// [POSIX](http://pubs.opengroup.org/onlinepubs/9699919799/functions/mmap.html).
    /// Not all platforms obey this, but this wrapper does.
C
Corey Richardson 已提交
886
    ErrZeroLength,
C
Corey Richardson 已提交
887
    /// Unrecognized error. The inner value is the unrecognized errno.
888
    ErrUnknown(int),
S
Steve Klabnik 已提交
889
    /// # The following are Windows-specific
C
Corey Richardson 已提交
890
    ///
A
Alex Crichton 已提交
891 892
    /// Unsupported combination of protection flags
    /// (`MapReadable`/`MapWritable`/`MapExecutable`).
893
    ErrUnsupProt,
A
Alex Crichton 已提交
894 895
    /// When using `MapFd`, `MapOffset` was given (Windows does not support this
    /// at all)
896
    ErrUnsupOffset,
C
Corey Richardson 已提交
897
    /// When using `MapFd`, there was already a mapping to the file.
898
    ErrAlreadyExists,
A
Alex Crichton 已提交
899 900
    /// Unrecognized error from `VirtualAlloc`. The inner value is the return
    /// value of GetLastError.
901
    ErrVirtualAlloc(uint),
A
Alex Crichton 已提交
902 903
    /// Unrecognized error from `CreateFileMapping`. The inner value is the
    /// return value of `GetLastError`.
904
    ErrCreateFileMappingW(uint),
A
Alex Crichton 已提交
905 906
    /// Unrecognized error from `MapViewOfFile`. The inner value is the return
    /// value of `GetLastError`.
907 908 909
    ErrMapViewOfFile(uint)
}

910
impl fmt::Show for MapError {
911 912
    fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result {
        let str = match *self {
913 914
            ErrFdNotAvail => "fd not available for reading or writing",
            ErrInvalidFd => "Invalid fd",
A
Alex Crichton 已提交
915 916 917 918
            ErrUnaligned => {
                "Unaligned address, invalid flags, negative length or \
                 unaligned offset"
            }
919 920 921 922 923
            ErrNoMapSupport=> "File doesn't support mapping",
            ErrNoMem => "Invalid address, or not enough available memory",
            ErrUnsupProt => "Protection mode unsupported",
            ErrUnsupOffset => "Offset in virtual memory mode is unsupported",
            ErrAlreadyExists => "File mapping for specified file already exists",
C
Corey Richardson 已提交
924
            ErrZeroLength => "Zero-length mapping not allowed",
A
Alex Crichton 已提交
925
            ErrUnknown(code) => {
A
Alex Crichton 已提交
926
                return write!(out, "Unknown error = {}", code)
A
Alex Crichton 已提交
927 928
            },
            ErrVirtualAlloc(code) => {
A
Alex Crichton 已提交
929
                return write!(out, "VirtualAlloc failure = {}", code)
A
Alex Crichton 已提交
930
            },
931
            ErrCreateFileMappingW(code) => {
A
Alex Crichton 已提交
932
                return write!(out, "CreateFileMappingW failure = {}", code)
933 934
            },
            ErrMapViewOfFile(code) => {
A
Alex Crichton 已提交
935
                return write!(out, "MapViewOfFile failure = {}", code)
936 937
            }
        };
A
Alex Crichton 已提交
938
        write!(out, "{}", str)
939 940 941
    }
}

942 943 944 945 946 947 948 949 950 951 952
impl Error for MapError {
    fn description(&self) -> &str { "memory map error" }
    fn detail(&self) -> Option<String> { Some(self.to_string()) }
}

impl FromError<MapError> for Box<Error> {
    fn from_error(err: MapError) -> Box<Error> {
        box err
    }
}

A
Aaron Turon 已提交
953 954 955 956 957 958 959 960 961 962 963 964 965 966
// Round up `from` to be divisible by `to`
fn round_up(from: uint, to: uint) -> uint {
    let r = if from % to == 0 {
        from
    } else {
        from + to - (from % to)
    };
    if r == 0 {
        to
    } else {
        r
    }
}

967 968
#[cfg(unix)]
impl MemoryMap {
A
Alex Crichton 已提交
969 970 971
    /// Create a new mapping with the given `options`, at least `min_len` bytes
    /// long. `min_len` must be greater than zero; see the note on
    /// `ErrZeroLength`.
972
    pub fn new(min_len: uint, options: &[MapOption]) -> Result<MemoryMap, MapError> {
973 974
        use libc::off_t;

C
Corey Richardson 已提交
975 976 977
        if min_len == 0 {
            return Err(ErrZeroLength)
        }
978
        let mut addr: *const u8 = ptr::null();
979 980 981 982
        let mut prot = 0;
        let mut flags = libc::MAP_PRIVATE;
        let mut fd = -1;
        let mut offset = 0;
983
        let mut custom_flags = false;
984
        let len = round_up(min_len, page_size());
985

D
Daniel Micay 已提交
986
        for &o in options.iter() {
987 988 989 990 991 992 993 994 995 996 997 998
            match o {
                MapReadable => { prot |= libc::PROT_READ; },
                MapWritable => { prot |= libc::PROT_WRITE; },
                MapExecutable => { prot |= libc::PROT_EXEC; },
                MapAddr(addr_) => {
                    flags |= libc::MAP_FIXED;
                    addr = addr_;
                },
                MapFd(fd_) => {
                    flags |= libc::MAP_FILE;
                    fd = fd_;
                },
999 1000
                MapOffset(offset_) => { offset = offset_ as off_t; },
                MapNonStandardFlags(f) => { custom_flags = true; flags = f },
1001 1002
            }
        }
1003
        if fd == -1 && !custom_flags { flags |= libc::MAP_ANON; }
1004 1005

        let r = unsafe {
1006 1007
            libc::mmap(addr as *mut c_void, len as libc::size_t, prot, flags,
                       fd, offset)
1008
        };
1009
        if r == libc::MAP_FAILED {
1010 1011 1012 1013 1014 1015
            Err(match errno() as c_int {
                libc::EACCES => ErrFdNotAvail,
                libc::EBADF => ErrInvalidFd,
                libc::EINVAL => ErrUnaligned,
                libc::ENODEV => ErrNoMapSupport,
                libc::ENOMEM => ErrNoMem,
1016
                code => ErrUnknown(code as int)
1017 1018
            })
        } else {
1019
            Ok(MemoryMap {
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
               data: r as *mut u8,
               len: len,
               kind: if fd == -1 {
                   MapVirtual
               } else {
                   MapFile(ptr::null())
               }
            })
        }
    }
V
Vadim Chugunov 已提交
1030

A
Alex Crichton 已提交
1031 1032
    /// Granularity that the offset or address must be for `MapOffset` and
    /// `MapAddr` respectively.
V
Vadim Chugunov 已提交
1033 1034 1035
    pub fn granularity() -> uint {
        page_size()
    }
1036 1037 1038 1039
}

#[cfg(unix)]
impl Drop for MemoryMap {
S
Steve Klabnik 已提交
1040
    /// Unmap the mapping. Panics the task if `munmap` panics.
D
Daniel Micay 已提交
1041
    fn drop(&mut self) {
C
Corey Richardson 已提交
1042 1043
        if self.len == 0 { /* workaround for dummy_stack */ return; }

1044
        unsafe {
S
Steve Klabnik 已提交
1045
            // `munmap` only panics due to logic errors
1046
            libc::munmap(self.data as *mut c_void, self.len as libc::size_t);
1047 1048 1049 1050 1051 1052
        }
    }
}

#[cfg(windows)]
impl MemoryMap {
C
Corey Richardson 已提交
1053
    /// Create a new mapping with the given `options`, at least `min_len` bytes long.
1054
    pub fn new(min_len: uint, options: &[MapOption]) -> Result<MemoryMap, MapError> {
1055 1056
        use libc::types::os::arch::extra::{LPVOID, DWORD, SIZE_T, HANDLE};

A
Aaron Turon 已提交
1057
        let mut lpAddress: LPVOID = ptr::null_mut();
1058 1059 1060
        let mut readable = false;
        let mut writable = false;
        let mut executable = false;
1061
        let mut handle: HANDLE = libc::INVALID_HANDLE_VALUE;
1062
        let mut offset: uint = 0;
1063
        let len = round_up(min_len, page_size());
1064

D
Daniel Micay 已提交
1065
        for &o in options.iter() {
1066 1067 1068 1069 1070
            match o {
                MapReadable => { readable = true; },
                MapWritable => { writable = true; },
                MapExecutable => { executable = true; }
                MapAddr(addr_) => { lpAddress = addr_ as LPVOID; },
1071
                MapFd(handle_) => { handle = handle_; },
1072
                MapOffset(offset_) => { offset = offset_; },
A
Alex Crichton 已提交
1073
                MapNonStandardFlags(..) => {}
1074 1075 1076 1077
            }
        }

        let flProtect = match (executable, readable, writable) {
1078
            (false, false, false) if handle == libc::INVALID_HANDLE_VALUE => libc::PAGE_NOACCESS,
1079 1080
            (false, true, false) => libc::PAGE_READONLY,
            (false, true, true) => libc::PAGE_READWRITE,
1081
            (true, false, false) if handle == libc::INVALID_HANDLE_VALUE => libc::PAGE_EXECUTE,
1082 1083 1084 1085 1086
            (true, true, false) => libc::PAGE_EXECUTE_READ,
            (true, true, true) => libc::PAGE_EXECUTE_READWRITE,
            _ => return Err(ErrUnsupProt)
        };

1087
        if handle == libc::INVALID_HANDLE_VALUE {
1088 1089 1090 1091 1092
            if offset != 0 {
                return Err(ErrUnsupOffset);
            }
            let r = unsafe {
                libc::VirtualAlloc(lpAddress,
1093
                                   len as SIZE_T,
1094 1095 1096 1097 1098
                                   libc::MEM_COMMIT | libc::MEM_RESERVE,
                                   flProtect)
            };
            match r as uint {
                0 => Err(ErrVirtualAlloc(errno())),
1099
                _ => Ok(MemoryMap {
1100 1101 1102 1103 1104 1105
                   data: r as *mut u8,
                   len: len,
                   kind: MapVirtual
                })
            }
        } else {
V
Vadim Chugunov 已提交
1106 1107 1108 1109 1110 1111 1112
            let dwDesiredAccess = match (executable, readable, writable) {
                (false, true, false) => libc::FILE_MAP_READ,
                (false, true, true) => libc::FILE_MAP_WRITE,
                (true, true, false) => libc::FILE_MAP_READ | libc::FILE_MAP_EXECUTE,
                (true, true, true) => libc::FILE_MAP_WRITE | libc::FILE_MAP_EXECUTE,
                _ => return Err(ErrUnsupProt) // Actually, because of the check above,
                                              // we should never get here.
1113 1114
            };
            unsafe {
1115
                let hFile = handle;
1116
                let mapping = libc::CreateFileMappingW(hFile,
A
Aaron Turon 已提交
1117
                                                       ptr::null_mut(),
1118
                                                       flProtect,
V
Vadim Chugunov 已提交
1119 1120
                                                       0,
                                                       0,
1121
                                                       ptr::null());
A
Aaron Turon 已提交
1122
                if mapping == ptr::null_mut() {
1123 1124 1125 1126 1127 1128 1129
                    return Err(ErrCreateFileMappingW(errno()));
                }
                if errno() as c_int == libc::ERROR_ALREADY_EXISTS {
                    return Err(ErrAlreadyExists);
                }
                let r = libc::MapViewOfFile(mapping,
                                            dwDesiredAccess,
V
Vadim Chugunov 已提交
1130
                                            ((len as u64) >> 32) as DWORD,
1131 1132 1133 1134
                                            (offset & 0xffff_ffff) as DWORD,
                                            0);
                match r as uint {
                    0 => Err(ErrMapViewOfFile(errno())),
1135
                    _ => Ok(MemoryMap {
1136 1137
                       data: r as *mut u8,
                       len: len,
1138
                       kind: MapFile(mapping as *const u8)
1139 1140 1141 1142 1143
                    })
                }
            }
        }
    }
V
Vadim Chugunov 已提交
1144 1145 1146 1147

    /// Granularity of MapAddr() and MapOffset() parameter values.
    /// This may be greater than the value returned by page_size().
    pub fn granularity() -> uint {
1148
        use mem;
V
Vadim Chugunov 已提交
1149
        unsafe {
1150
            let mut info = mem::zeroed();
V
Vadim Chugunov 已提交
1151 1152 1153 1154 1155
            libc::GetSystemInfo(&mut info);

            return info.dwAllocationGranularity as uint;
        }
    }
1156 1157 1158 1159
}

#[cfg(windows)]
impl Drop for MemoryMap {
1160
    /// Unmap the mapping. Panics the task if any of `VirtualFree`,
A
Alex Crichton 已提交
1161
    /// `UnmapViewOfFile`, or `CloseHandle` fail.
D
Daniel Micay 已提交
1162
    fn drop(&mut self) {
1163
        use libc::types::os::arch::extra::{LPCVOID, HANDLE};
V
Vadim Chugunov 已提交
1164
        use libc::consts::os::extra::FALSE;
A
Alex Crichton 已提交
1165
        if self.len == 0 { return }
1166 1167 1168

        unsafe {
            match self.kind {
V
Vadim Chugunov 已提交
1169
                MapVirtual => {
1170
                    if libc::VirtualFree(self.data as *mut c_void, 0,
A
Alex Crichton 已提交
1171
                                         libc::MEM_RELEASE) == 0 {
A
Alex Crichton 已提交
1172
                        println!("VirtualFree failed: {}", errno());
V
Vadim Chugunov 已提交
1173
                    }
1174 1175
                },
                MapFile(mapping) => {
V
Vadim Chugunov 已提交
1176
                    if libc::UnmapViewOfFile(self.data as LPCVOID) == FALSE {
A
Alex Crichton 已提交
1177
                        println!("UnmapViewOfFile failed: {}", errno());
1178
                    }
V
Vadim Chugunov 已提交
1179
                    if libc::CloseHandle(mapping as HANDLE) == FALSE {
A
Alex Crichton 已提交
1180
                        println!("CloseHandle failed: {}", errno());
1181 1182 1183 1184 1185 1186 1187
                    }
                }
            }
        }
    }
}

1188 1189 1190 1191 1192 1193 1194 1195 1196
impl MemoryMap {
    /// Returns the pointer to the memory created or modified by this map.
    pub fn data(&self) -> *mut u8 { self.data }
    /// Returns the number of bytes this map applies to.
    pub fn len(&self) -> uint { self.len }
    /// Returns the type of mapping this represents.
    pub fn kind(&self) -> MemoryMapKind { self.kind }
}

1197
#[cfg(target_os = "linux")]
1198
pub mod consts {
1199
    pub use os::arch_consts::ARCH;
1200

A
Alex Crichton 已提交
1201
    pub const FAMILY: &'static str = "unix";
1202

1203 1204
    /// A string describing the specific operating system in use: in this
    /// case, `linux`.
A
Alex Crichton 已提交
1205
    pub const SYSNAME: &'static str = "linux";
I
ILyoan 已提交
1206

1207 1208
    /// Specifies the filename prefix used for shared libraries on this
    /// platform: in this case, `lib`.
A
Alex Crichton 已提交
1209
    pub const DLL_PREFIX: &'static str = "lib";
I
ILyoan 已提交
1210

1211 1212
    /// Specifies the filename suffix used for shared libraries on this
    /// platform: in this case, `.so`.
A
Alex Crichton 已提交
1213
    pub const DLL_SUFFIX: &'static str = ".so";
I
ILyoan 已提交
1214

1215 1216
    /// Specifies the file extension used for shared libraries on this
    /// platform that goes after the dot: in this case, `so`.
A
Alex Crichton 已提交
1217
    pub const DLL_EXTENSION: &'static str = "so";
K
kyeongwoon 已提交
1218

1219 1220
    /// Specifies the filename suffix used for executable binaries on this
    /// platform: in this case, the empty string.
A
Alex Crichton 已提交
1221
    pub const EXE_SUFFIX: &'static str = "";
1222

1223 1224
    /// Specifies the file extension, if any, used for executable binaries
    /// on this platform: in this case, the empty string.
A
Alex Crichton 已提交
1225
    pub const EXE_EXTENSION: &'static str = "";
1226
}
1227

1228 1229
#[cfg(target_os = "macos")]
pub mod consts {
1230
    pub use os::arch_consts::ARCH;
1231

A
Alex Crichton 已提交
1232
    pub const FAMILY: &'static str = "unix";
1233

1234 1235
    /// A string describing the specific operating system in use: in this
    /// case, `macos`.
A
Alex Crichton 已提交
1236
    pub const SYSNAME: &'static str = "macos";
1237

1238 1239
    /// Specifies the filename prefix used for shared libraries on this
    /// platform: in this case, `lib`.
A
Alex Crichton 已提交
1240
    pub const DLL_PREFIX: &'static str = "lib";
1241

1242 1243
    /// Specifies the filename suffix used for shared libraries on this
    /// platform: in this case, `.dylib`.
A
Alex Crichton 已提交
1244
    pub const DLL_SUFFIX: &'static str = ".dylib";
1245

1246 1247
    /// Specifies the file extension used for shared libraries on this
    /// platform that goes after the dot: in this case, `dylib`.
A
Alex Crichton 已提交
1248
    pub const DLL_EXTENSION: &'static str = "dylib";
1249

1250 1251
    /// Specifies the filename suffix used for executable binaries on this
    /// platform: in this case, the empty string.
A
Alex Crichton 已提交
1252
    pub const EXE_SUFFIX: &'static str = "";
V
Valerii Hiora 已提交
1253 1254 1255

    /// Specifies the file extension, if any, used for executable binaries
    /// on this platform: in this case, the empty string.
A
Alex Crichton 已提交
1256
    pub const EXE_EXTENSION: &'static str = "";
V
Valerii Hiora 已提交
1257 1258 1259 1260 1261 1262
}

#[cfg(target_os = "ios")]
pub mod consts {
    pub use os::arch_consts::ARCH;

A
Alex Crichton 已提交
1263
    pub const FAMILY: &'static str = "unix";
V
Valerii Hiora 已提交
1264 1265 1266

    /// A string describing the specific operating system in use: in this
    /// case, `ios`.
A
Alex Crichton 已提交
1267
    pub const SYSNAME: &'static str = "ios";
V
Valerii Hiora 已提交
1268 1269 1270

    /// Specifies the filename suffix used for executable binaries on this
    /// platform: in this case, the empty string.
A
Alex Crichton 已提交
1271
    pub const EXE_SUFFIX: &'static str = "";
1272

1273 1274
    /// Specifies the file extension, if any, used for executable binaries
    /// on this platform: in this case, the empty string.
A
Alex Crichton 已提交
1275
    pub const EXE_EXTENSION: &'static str = "";
1276
}
1277

1278 1279
#[cfg(target_os = "freebsd")]
pub mod consts {
1280
    pub use os::arch_consts::ARCH;
1281

A
Alex Crichton 已提交
1282
    pub const FAMILY: &'static str = "unix";
1283

1284 1285
    /// A string describing the specific operating system in use: in this
    /// case, `freebsd`.
A
Alex Crichton 已提交
1286
    pub const SYSNAME: &'static str = "freebsd";
I
ILyoan 已提交
1287

1288 1289
    /// Specifies the filename prefix used for shared libraries on this
    /// platform: in this case, `lib`.
A
Alex Crichton 已提交
1290
    pub const DLL_PREFIX: &'static str = "lib";
1291

1292 1293
    /// Specifies the filename suffix used for shared libraries on this
    /// platform: in this case, `.so`.
A
Alex Crichton 已提交
1294
    pub const DLL_SUFFIX: &'static str = ".so";
1295

1296 1297
    /// Specifies the file extension used for shared libraries on this
    /// platform that goes after the dot: in this case, `so`.
A
Alex Crichton 已提交
1298
    pub const DLL_EXTENSION: &'static str = "so";
1299

1300 1301
    /// Specifies the filename suffix used for executable binaries on this
    /// platform: in this case, the empty string.
A
Alex Crichton 已提交
1302
    pub const EXE_SUFFIX: &'static str = "";
1303

1304 1305
    /// Specifies the file extension, if any, used for executable binaries
    /// on this platform: in this case, the empty string.
A
Alex Crichton 已提交
1306
    pub const EXE_EXTENSION: &'static str = "";
M
Michael Neumann 已提交
1307 1308 1309 1310 1311 1312
}

#[cfg(target_os = "dragonfly")]
pub mod consts {
    pub use os::arch_consts::ARCH;

A
Alex Crichton 已提交
1313
    pub const FAMILY: &'static str = "unix";
M
Michael Neumann 已提交
1314 1315 1316

    /// A string describing the specific operating system in use: in this
    /// case, `dragonfly`.
A
Alex Crichton 已提交
1317
    pub const SYSNAME: &'static str = "dragonfly";
M
Michael Neumann 已提交
1318 1319 1320

    /// Specifies the filename prefix used for shared libraries on this
    /// platform: in this case, `lib`.
A
Alex Crichton 已提交
1321
    pub const DLL_PREFIX: &'static str = "lib";
M
Michael Neumann 已提交
1322 1323 1324

    /// Specifies the filename suffix used for shared libraries on this
    /// platform: in this case, `.so`.
A
Alex Crichton 已提交
1325
    pub const DLL_SUFFIX: &'static str = ".so";
M
Michael Neumann 已提交
1326 1327 1328

    /// Specifies the file extension used for shared libraries on this
    /// platform that goes after the dot: in this case, `so`.
A
Alex Crichton 已提交
1329
    pub const DLL_EXTENSION: &'static str = "so";
M
Michael Neumann 已提交
1330 1331 1332

    /// Specifies the filename suffix used for executable binaries on this
    /// platform: in this case, the empty string.
A
Alex Crichton 已提交
1333
    pub const EXE_SUFFIX: &'static str = "";
M
Michael Neumann 已提交
1334 1335 1336

    /// Specifies the file extension, if any, used for executable binaries
    /// on this platform: in this case, the empty string.
A
Alex Crichton 已提交
1337
    pub const EXE_EXTENSION: &'static str = "";
1338
}
1339

1340 1341
#[cfg(target_os = "android")]
pub mod consts {
1342
    pub use os::arch_consts::ARCH;
I
ILyoan 已提交
1343

A
Alex Crichton 已提交
1344
    pub const FAMILY: &'static str = "unix";
1345

1346 1347
    /// A string describing the specific operating system in use: in this
    /// case, `android`.
A
Alex Crichton 已提交
1348
    pub const SYSNAME: &'static str = "android";
1349

1350 1351
    /// Specifies the filename prefix used for shared libraries on this
    /// platform: in this case, `lib`.
A
Alex Crichton 已提交
1352
    pub const DLL_PREFIX: &'static str = "lib";
1353

1354 1355
    /// Specifies the filename suffix used for shared libraries on this
    /// platform: in this case, `.so`.
A
Alex Crichton 已提交
1356
    pub const DLL_SUFFIX: &'static str = ".so";
1357

1358 1359
    /// Specifies the file extension used for shared libraries on this
    /// platform that goes after the dot: in this case, `so`.
A
Alex Crichton 已提交
1360
    pub const DLL_EXTENSION: &'static str = "so";
1361

1362 1363
    /// Specifies the filename suffix used for executable binaries on this
    /// platform: in this case, the empty string.
A
Alex Crichton 已提交
1364
    pub const EXE_SUFFIX: &'static str = "";
K
kyeongwoon 已提交
1365

1366 1367
    /// Specifies the file extension, if any, used for executable binaries
    /// on this platform: in this case, the empty string.
A
Alex Crichton 已提交
1368
    pub const EXE_EXTENSION: &'static str = "";
1369
}
1370

1371
#[cfg(target_os = "windows")]
1372
pub mod consts {
1373
    pub use os::arch_consts::ARCH;
1374

A
Alex Crichton 已提交
1375
    pub const FAMILY: &'static str = "windows";
1376

1377
    /// A string describing the specific operating system in use: in this
1378
    /// case, `windows`.
A
Alex Crichton 已提交
1379
    pub const SYSNAME: &'static str = "windows";
1380

1381 1382
    /// Specifies the filename prefix used for shared libraries on this
    /// platform: in this case, the empty string.
A
Alex Crichton 已提交
1383
    pub const DLL_PREFIX: &'static str = "";
1384

1385 1386
    /// Specifies the filename suffix used for shared libraries on this
    /// platform: in this case, `.dll`.
A
Alex Crichton 已提交
1387
    pub const DLL_SUFFIX: &'static str = ".dll";
1388

1389 1390
    /// Specifies the file extension used for shared libraries on this
    /// platform that goes after the dot: in this case, `dll`.
A
Alex Crichton 已提交
1391
    pub const DLL_EXTENSION: &'static str = "dll";
1392

1393 1394
    /// Specifies the filename suffix used for executable binaries on this
    /// platform: in this case, `.exe`.
A
Alex Crichton 已提交
1395
    pub const EXE_SUFFIX: &'static str = ".exe";
1396

1397 1398
    /// Specifies the file extension, if any, used for executable binaries
    /// on this platform: in this case, `exe`.
A
Alex Crichton 已提交
1399
    pub const EXE_EXTENSION: &'static str = "exe";
1400
}
1401

1402 1403
#[cfg(target_arch = "x86")]
mod arch_consts {
A
Alex Crichton 已提交
1404
    pub const ARCH: &'static str = "x86";
1405
}
1406

1407 1408
#[cfg(target_arch = "x86_64")]
mod arch_consts {
A
Alex Crichton 已提交
1409
    pub const ARCH: &'static str = "x86_64";
1410
}
1411

1412 1413
#[cfg(target_arch = "arm")]
mod arch_consts {
A
Alex Crichton 已提交
1414
    pub const ARCH: &'static str = "arm";
1415
}
I
ILyoan 已提交
1416

A
Akos Kiss 已提交
1417 1418 1419 1420 1421
#[cfg(target_arch = "aarch64")]
mod arch_consts {
    pub const ARCH: &'static str = "aarch64";
}

1422 1423
#[cfg(target_arch = "mips")]
mod arch_consts {
A
Alex Crichton 已提交
1424
    pub const ARCH: &'static str = "mips";
I
ILyoan 已提交
1425
}
1426

1427 1428
#[cfg(target_arch = "mipsel")]
mod arch_consts {
A
Alex Crichton 已提交
1429
    pub const ARCH: &'static str = "mipsel";
1430
}
1431

1432 1433
#[cfg(test)]
mod tests {
1434
    use prelude::v1::*;
1435 1436

    use iter::repeat;
K
Kevin Ballard 已提交
1437
    use os::{env, getcwd, getenv, make_absolute};
1438
    use os::{split_paths, join_paths, setenv, unsetenv};
1439
    use os;
1440
    use rand::Rng;
1441
    use rand;
1442

1443
    #[test]
1444
    pub fn last_os_error() {
1445
        debug!("{}", os::last_os_error());
1446
    }
1447

1448
    fn make_rand_name() -> String {
S
Simonas Kazlauskas 已提交
1449
        let mut rng = rand::thread_rng();
A
Alex Crichton 已提交
1450 1451
        let n = format!("TEST{}", rng.gen_ascii_chars().take(10u)
                                     .collect::<String>());
1452
        assert!(getenv(n.as_slice()).is_none());
1453
        n
1454 1455
    }

1456 1457 1458 1459 1460
    #[test]
    fn test_num_cpus() {
        assert!(os::num_cpus() > 0);
    }

1461 1462 1463
    #[test]
    fn test_setenv() {
        let n = make_rand_name();
1464
        setenv(n.as_slice(), "VALUE");
1465
        assert_eq!(getenv(n.as_slice()), Some("VALUE".to_string()));
1466 1467
    }

C
Corey Richardson 已提交
1468 1469 1470
    #[test]
    fn test_unsetenv() {
        let n = make_rand_name();
1471 1472
        setenv(n.as_slice(), "VALUE");
        unsetenv(n.as_slice());
1473
        assert_eq!(getenv(n.as_slice()), None);
C
Corey Richardson 已提交
1474 1475
    }

1476
    #[test]
1477
    #[ignore]
1478 1479
    fn test_setenv_overwrite() {
        let n = make_rand_name();
1480 1481
        setenv(n.as_slice(), "1");
        setenv(n.as_slice(), "2");
1482
        assert_eq!(getenv(n.as_slice()), Some("2".to_string()));
1483
        setenv(n.as_slice(), "");
1484
        assert_eq!(getenv(n.as_slice()), Some("".to_string()));
1485 1486 1487 1488 1489
    }

    // Windows GetEnvironmentVariable requires some extra work to make sure
    // the buffer the variable is copied into is the right size
    #[test]
1490
    #[ignore]
1491
    fn test_getenv_big() {
1492
        let mut s = "".to_string();
1493
        let mut i = 0i;
1494
        while i < 100 {
1495
            s.push_str("aaaaaaaaaa");
1496 1497
            i += 1;
        }
1498
        let n = make_rand_name();
1499
        setenv(n.as_slice(), s.as_slice());
1500
        debug!("{}", s.clone());
1501
        assert_eq!(getenv(n.as_slice()), Some(s));
1502 1503
    }

B
Ben Noordhuis 已提交
1504 1505 1506 1507 1508
    #[test]
    fn test_self_exe_name() {
        let path = os::self_exe_name();
        assert!(path.is_some());
        let path = path.unwrap();
L
Luqman Aden 已提交
1509
        debug!("{}", path.display());
B
Ben Noordhuis 已提交
1510 1511 1512 1513 1514

        // Hard to test this function
        assert!(path.is_absolute());
    }

1515 1516 1517
    #[test]
    fn test_self_exe_path() {
        let path = os::self_exe_path();
P
Patrick Walton 已提交
1518
        assert!(path.is_some());
1519
        let path = path.unwrap();
L
Luqman Aden 已提交
1520
        debug!("{}", path.display());
1521 1522

        // Hard to test this function
1523
        assert!(path.is_absolute());
1524 1525 1526
    }

    #[test]
1527
    #[ignore]
1528 1529
    fn test_env_getenv() {
        let e = env();
Y
Youngmin Yoo 已提交
1530
        assert!(e.len() > 0u);
D
Daniel Micay 已提交
1531
        for p in e.iter() {
1532
            let (n, v) = (*p).clone();
L
Luqman Aden 已提交
1533
            debug!("{}", n);
1534
            let v2 = getenv(n.as_slice());
1535 1536 1537
            // MingW seems to set some funky environment variables like
            // "=C:=C:\MinGW\msys\1.0\bin" and "!::=::\" that are returned
            // from env() but not visible from getenv().
1538
            assert!(v2.is_none() || v2 == Some(v));
1539 1540 1541
        }
    }

1542 1543 1544
    #[test]
    fn test_env_set_get_huge() {
        let n = make_rand_name();
1545
        let s = repeat("x").take(10000).collect::<String>();
1546 1547 1548 1549
        setenv(n.as_slice(), s.as_slice());
        assert_eq!(getenv(n.as_slice()), Some(s));
        unsetenv(n.as_slice());
        assert_eq!(getenv(n.as_slice()), None);
1550 1551
    }

1552 1553 1554 1555
    #[test]
    fn test_env_setenv() {
        let n = make_rand_name();

1556
        let mut e = env();
1557
        setenv(n.as_slice(), "VALUE");
1558
        assert!(!e.contains(&(n.clone(), "VALUE".to_string())));
1559 1560

        e = env();
1561
        assert!(e.contains(&(n, "VALUE".to_string())));
1562 1563
    }

1564 1565
    #[test]
    fn test() {
1566
        assert!((!Path::new("test-path").is_absolute()));
1567

1568
        let cwd = getcwd().unwrap();
1569
        debug!("Current working directory: {}", cwd.display());
1570

1571 1572
        debug!("{}", make_absolute(&Path::new("test-path")).unwrap().display());
        debug!("{}", make_absolute(&Path::new("/usr/bin")).unwrap().display());
1573 1574 1575
    }

    #[test]
1576
    #[cfg(unix)]
1577
    fn homedir() {
E
Erick Tryzelaar 已提交
1578
        let oldhome = getenv("HOME");
1579

E
Erick Tryzelaar 已提交
1580
        setenv("HOME", "/home/MountainView");
1581
        assert!(os::homedir() == Some(Path::new("/home/MountainView")));
1582

E
Erick Tryzelaar 已提交
1583
        setenv("HOME", "");
P
Patrick Walton 已提交
1584
        assert!(os::homedir().is_none());
1585

1586
        for s in oldhome.iter() {
A
Axel Viala 已提交
1587
            setenv("HOME", s.as_slice());
1588
        }
1589 1590 1591
    }

    #[test]
1592
    #[cfg(windows)]
1593 1594
    fn homedir() {

E
Erick Tryzelaar 已提交
1595 1596
        let oldhome = getenv("HOME");
        let olduserprofile = getenv("USERPROFILE");
1597

E
Erick Tryzelaar 已提交
1598 1599
        setenv("HOME", "");
        setenv("USERPROFILE", "");
1600

P
Patrick Walton 已提交
1601
        assert!(os::homedir().is_none());
1602

E
Erick Tryzelaar 已提交
1603
        setenv("HOME", "/home/MountainView");
1604
        assert!(os::homedir() == Some(Path::new("/home/MountainView")));
1605

E
Erick Tryzelaar 已提交
1606
        setenv("HOME", "");
1607

E
Erick Tryzelaar 已提交
1608
        setenv("USERPROFILE", "/home/MountainView");
1609
        assert!(os::homedir() == Some(Path::new("/home/MountainView")));
1610

E
Erick Tryzelaar 已提交
1611 1612
        setenv("HOME", "/home/MountainView");
        setenv("USERPROFILE", "/home/PaloAlto");
1613
        assert!(os::homedir() == Some(Path::new("/home/MountainView")));
1614

1615
        for s in oldhome.iter() {
A
Axel Viala 已提交
1616
            setenv("HOME", s.as_slice());
1617 1618
        }
        for s in olduserprofile.iter() {
A
Axel Viala 已提交
1619
            setenv("USERPROFILE", s.as_slice());
1620
        }
1621 1622
    }

1623 1624
    #[test]
    fn memory_map_rw() {
C
Corey Farwell 已提交
1625
        use result::Result::{Ok, Err};
1626

N
Nick Cameron 已提交
1627
        let chunk = match os::MemoryMap::new(16, &[
C
Corey Farwell 已提交
1628 1629
            os::MapOption::MapReadable,
            os::MapOption::MapWritable
1630 1631
        ]) {
            Ok(chunk) => chunk,
S
Steve Klabnik 已提交
1632
            Err(msg) => panic!("{}", msg)
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
        };
        assert!(chunk.len >= 16);

        unsafe {
            *chunk.data = 0xBE;
            assert!(*chunk.data == 0xBE);
        }
    }

    #[test]
    fn memory_map_file() {
1644
        use libc;
1645
        use os::*;
1646 1647 1648 1649 1650 1651
        use io::fs::{File, unlink};
        use io::SeekStyle::SeekSet;
        use io::FileMode::Open;
        use io::FileAccess::ReadWrite;

        #[cfg(not(windows))]
1652
        fn get_fd(file: &File) -> libc::c_int {
1653 1654
            use os::unix::AsRawFd;
            file.as_raw_fd()
1655
        }
1656

1657
        #[cfg(windows)]
1658
        fn get_fd(file: &File) -> libc::HANDLE {
1659 1660
            use os::windows::AsRawHandle;
            file.as_raw_handle()
1661 1662
        }

1663
        let mut path = tmpdir();
1664
        path.push("mmap_file.tmp");
V
Vadim Chugunov 已提交
1665
        let size = MemoryMap::granularity() * 2;
1666
        let mut file = File::open_mode(&path, Open, ReadWrite).unwrap();
1667 1668
        file.seek(size as i64, SeekSet).unwrap();
        file.write_u8(0).unwrap();
1669

1670
        let chunk = MemoryMap::new(size / 2, &[
C
Corey Farwell 已提交
1671 1672 1673 1674
            MapOption::MapReadable,
            MapOption::MapWritable,
            MapOption::MapFd(get_fd(&file)),
            MapOption::MapOffset(size / 2)
1675
        ]).unwrap();
1676 1677 1678 1679 1680 1681
        assert!(chunk.len > 0);

        unsafe {
            *chunk.data = 0xbe;
            assert!(*chunk.data == 0xbe);
        }
1682
        drop(chunk);
1683

1684
        unlink(&path).unwrap();
1685 1686
    }

A
Aaron Turon 已提交
1687 1688 1689 1690 1691
    #[test]
    #[cfg(windows)]
    fn split_paths_windows() {
        fn check_parse(unparsed: &str, parsed: &[&str]) -> bool {
            split_paths(unparsed) ==
J
Jorge Aparicio 已提交
1692
                parsed.iter().map(|s| Path::new(*s)).collect::<Vec<_>>()
A
Aaron Turon 已提交
1693 1694
        }

N
Nick Cameron 已提交
1695 1696 1697 1698 1699
        assert!(check_parse("", &mut [""]));
        assert!(check_parse(r#""""#, &mut [""]));
        assert!(check_parse(";;", &mut ["", "", ""]));
        assert!(check_parse(r"c:\", &mut [r"c:\"]));
        assert!(check_parse(r"c:\;", &mut [r"c:\", ""]));
A
Aaron Turon 已提交
1700
        assert!(check_parse(r"c:\;c:\Program Files\",
N
Nick Cameron 已提交
1701 1702
                            &mut [r"c:\", r"c:\Program Files\"]));
        assert!(check_parse(r#"c:\;c:\"foo"\"#, &mut [r"c:\", r"c:\foo\"]));
A
Aaron Turon 已提交
1703
        assert!(check_parse(r#"c:\;c:\"foo;bar"\;c:\baz"#,
N
Nick Cameron 已提交
1704
                            &mut [r"c:\", r"c:\foo;bar\", r"c:\baz"]));
A
Aaron Turon 已提交
1705 1706 1707 1708 1709 1710 1711
    }

    #[test]
    #[cfg(unix)]
    fn split_paths_unix() {
        fn check_parse(unparsed: &str, parsed: &[&str]) -> bool {
            split_paths(unparsed) ==
J
Jorge Aparicio 已提交
1712
                parsed.iter().map(|s| Path::new(*s)).collect::<Vec<_>>()
A
Aaron Turon 已提交
1713 1714
        }

N
Nick Cameron 已提交
1715 1716 1717 1718 1719
        assert!(check_parse("", &mut [""]));
        assert!(check_parse("::", &mut ["", "", ""]));
        assert!(check_parse("/", &mut ["/"]));
        assert!(check_parse("/:", &mut ["/", ""]));
        assert!(check_parse("/:/usr/local", &mut ["/", "/usr/local"]));
A
Aaron Turon 已提交
1720 1721
    }

1722 1723 1724 1725
    #[test]
    #[cfg(unix)]
    fn join_paths_unix() {
        fn test_eq(input: &[&str], output: &str) -> bool {
1726
            join_paths(input).unwrap() == output.as_bytes()
1727 1728
        }

N
Nick Cameron 已提交
1729 1730 1731 1732 1733 1734
        assert!(test_eq(&[], ""));
        assert!(test_eq(&["/bin", "/usr/bin", "/usr/local/bin"],
                         "/bin:/usr/bin:/usr/local/bin"));
        assert!(test_eq(&["", "/bin", "", "", "/usr/bin", ""],
                         ":/bin:::/usr/bin:"));
        assert!(join_paths(&["/te:st"]).is_err());
1735 1736 1737 1738 1739 1740
    }

    #[test]
    #[cfg(windows)]
    fn join_paths_windows() {
        fn test_eq(input: &[&str], output: &str) -> bool {
1741
            join_paths(input).unwrap() == output.as_bytes()
1742 1743
        }

N
Nick Cameron 已提交
1744 1745
        assert!(test_eq(&[], ""));
        assert!(test_eq(&[r"c:\windows", r"c:\"],
1746
                        r"c:\windows;c:\"));
N
Nick Cameron 已提交
1747
        assert!(test_eq(&["", r"c:\windows", "", "", r"c:\", ""],
1748
                        r";c:\windows;;;c:\;"));
N
Nick Cameron 已提交
1749
        assert!(test_eq(&[r"c:\te;st", r"c:\"],
1750
                        r#""c:\te;st";c:\"#));
N
Nick Cameron 已提交
1751
        assert!(join_paths(&[r#"c:\te"st"#]).is_err());
1752 1753
    }

1754
    // More recursive_mkdir tests are in extra::tempfile
1755
}