2015-10-25 07:05:34 -05:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
// Ignore this test on Android, because it segfaults there.
|
|
|
|
|
|
|
|
// ignore-android
|
|
|
|
// ignore-windows
|
2016-02-10 13:21:56 -06:00
|
|
|
// ignore-emscripten
|
2015-10-25 07:05:34 -05:00
|
|
|
// no-prefer-dynamic
|
|
|
|
|
|
|
|
#![feature(libc)]
|
|
|
|
|
|
|
|
extern crate libc;
|
|
|
|
|
|
|
|
use libc::c_char;
|
|
|
|
use libc::execve;
|
|
|
|
use std::env;
|
2016-03-07 17:42:29 -06:00
|
|
|
use std::ffi::CString;
|
|
|
|
use std::os::unix::prelude::*;
|
2015-10-25 07:05:34 -05:00
|
|
|
use std::ptr;
|
|
|
|
|
|
|
|
fn main() {
|
2015-11-27 06:48:07 -06:00
|
|
|
if env::args_os().count() == 2 {
|
2015-10-25 07:05:34 -05:00
|
|
|
for (key, value) in env::vars_os() {
|
|
|
|
panic!("found env value {:?} {:?}", key, value);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-07 17:42:29 -06:00
|
|
|
let current_exe = CString::new(env::current_exe()
|
|
|
|
.unwrap()
|
|
|
|
.as_os_str()
|
|
|
|
.as_bytes()).unwrap();
|
|
|
|
let new_env_var = CString::new("FOOBAR").unwrap();
|
2015-10-25 07:05:34 -05:00
|
|
|
let filename: *const c_char = current_exe.as_ptr();
|
2015-11-27 06:48:07 -06:00
|
|
|
let argv: &[*const c_char] = &[filename, filename, ptr::null()];
|
2015-10-25 07:05:34 -05:00
|
|
|
let envp: &[*const c_char] = &[new_env_var.as_ptr(), ptr::null()];
|
|
|
|
unsafe {
|
|
|
|
execve(filename, &argv[0], &envp[0]);
|
|
|
|
}
|
|
|
|
panic!("execve failed");
|
|
|
|
}
|