|
| 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 | +// ignore-windows |
| 12 | + |
| 13 | +#![feature(macro_rules)] |
| 14 | + |
| 15 | +extern crate native; |
| 16 | +extern crate rustrt; |
| 17 | +extern crate libc; |
| 18 | +use libc::{c_char, c_int}; |
| 19 | +use native::io::process; |
| 20 | +use rustrt::rtio; |
| 21 | +use rustrt::c_str; |
| 22 | + |
| 23 | +macro_rules! c_string { |
| 24 | + ($s:expr) => { { |
| 25 | + let ptr = concat!($s, "\0").as_ptr() as *const i8; |
| 26 | + unsafe { &c_str::CString::new(ptr, false) } |
| 27 | + } } |
| 28 | +} |
| 29 | + |
| 30 | +static EXPECTED_ERRNO: c_int = 0x778899aa; |
| 31 | + |
| 32 | +#[no_mangle] |
| 33 | +pub unsafe extern "C" fn chdir(_: *const c_char) -> c_int { |
| 34 | + // copied from std::os::errno() |
| 35 | + #[cfg(any(target_os = "macos", |
| 36 | + target_os = "ios", |
| 37 | + target_os = "freebsd"))] |
| 38 | + fn errno_location() -> *mut c_int { |
| 39 | + extern { |
| 40 | + fn __error() -> *mut c_int; |
| 41 | + } |
| 42 | + unsafe { |
| 43 | + __error() |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + #[cfg(target_os = "dragonfly")] |
| 48 | + fn errno_location() -> *mut c_int { |
| 49 | + extern { |
| 50 | + fn __dfly_error() -> *mut c_int; |
| 51 | + } |
| 52 | + unsafe { |
| 53 | + __dfly_error() |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + #[cfg(any(target_os = "linux", target_os = "android"))] |
| 58 | + fn errno_location() -> *mut c_int { |
| 59 | + extern { |
| 60 | + fn __errno_location() -> *mut c_int; |
| 61 | + } |
| 62 | + unsafe { |
| 63 | + __errno_location() |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + *errno_location() = EXPECTED_ERRNO; |
| 68 | + return -1; |
| 69 | +} |
| 70 | + |
| 71 | +fn main() { |
| 72 | + let program = c_string!("true"); |
| 73 | + let cwd = c_string!("whatever"); |
| 74 | + let cfg = rtio::ProcessConfig { |
| 75 | + program: program, |
| 76 | + args: &[], |
| 77 | + env: None, |
| 78 | + cwd: Some(cwd), |
| 79 | + stdin: rtio::Ignored, |
| 80 | + stdout: rtio::Ignored, |
| 81 | + stderr: rtio::Ignored, |
| 82 | + extra_io: &[], |
| 83 | + uid: None, |
| 84 | + gid: None, |
| 85 | + detach: false |
| 86 | + }; |
| 87 | + |
| 88 | + match process::Process::spawn(cfg) { |
| 89 | + Ok(_) => { fail!("spawn() should have failled"); } |
| 90 | + Err(rtio::IoError { code: err, ..}) => { |
| 91 | + assert_eq!(err as c_int, EXPECTED_ERRNO); |
| 92 | + } |
| 93 | + }; |
| 94 | +} |
0 commit comments