env-funky-keys.rs 1.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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
15
// ignore-emscripten no execve
16 17 18 19 20 21 22 23 24
// no-prefer-dynamic

#![feature(libc)]

extern crate libc;

use libc::c_char;
use libc::execve;
use std::env;
A
Alex Crichton 已提交
25 26
use std::ffi::CString;
use std::os::unix::prelude::*;
27 28 29
use std::ptr;

fn main() {
30
    if env::args_os().count() == 2 {
31 32 33 34 35 36
        for (key, value) in env::vars_os() {
            panic!("found env value {:?} {:?}", key, value);
        }
        return;
    }

A
Alex Crichton 已提交
37 38 39 40 41
    let current_exe = CString::new(env::current_exe()
                                       .unwrap()
                                       .as_os_str()
                                       .as_bytes()).unwrap();
    let new_env_var = CString::new("FOOBAR").unwrap();
42
    let filename: *const c_char = current_exe.as_ptr();
43
    let argv: &[*const c_char] = &[filename, filename, ptr::null()];
44 45 46 47 48 49
    let envp: &[*const c_char] = &[new_env_var.as_ptr(), ptr::null()];
    unsafe {
        execve(filename, &argv[0], &envp[0]);
    }
    panic!("execve failed");
}