running-with-no-runtime.rs 1.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2014 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.

11
// ignore-emscripten spawning processes is not supported
J
Jan-Erik Rediger 已提交
12

13
#![feature(start)]
14

15
use std::ffi::CStr;
16
use std::process::{Command, Output};
17
use std::panic;
18
use std::str;
19

20
#[start]
21
fn start(argc: isize, argv: *const *const u8) -> isize {
22 23
    if argc > 1 {
        unsafe {
24 25 26
            match **argv.offset(1) as char {
                '1' => {}
                '2' => println!("foo"),
27 28
                '3' => assert!(panic::catch_unwind(|| {}).is_ok()),
                '4' => assert!(panic::catch_unwind(|| panic!()).is_err()),
29
                '5' => assert!(Command::new("test").spawn().is_err()),
S
Steve Klabnik 已提交
30
                _ => panic!()
31 32 33 34 35
            }
        }
        return 0
    }

36
    let args = unsafe {
37 38
        (0..argc as usize).map(|i| {
            let ptr = *argv.offset(i as isize) as *const _;
39
            CStr::from_ptr(ptr).to_bytes().to_vec()
A
Alex Crichton 已提交
40
        }).collect::<Vec<_>>()
41
    };
42
    let me = String::from_utf8(args[0].to_vec()).unwrap();
43

44 45 46 47 48
    pass(Command::new(&me).arg("1").output().unwrap());
    pass(Command::new(&me).arg("2").output().unwrap());
    pass(Command::new(&me).arg("3").output().unwrap());
    pass(Command::new(&me).arg("4").output().unwrap());
    pass(Command::new(&me).arg("5").output().unwrap());
49 50

    0
51 52
}

53
fn pass(output: Output) {
54
    if !output.status.success() {
55 56
        println!("{:?}", str::from_utf8(&output.stdout));
        println!("{:?}", str::from_utf8(&output.stderr));
57 58
    }
}