forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathswitch-stdout.rs
71 lines (57 loc) · 1.89 KB
/
switch-stdout.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#![cfg(any(target_family = "unix", target_family = "windows"))]
use std::fs::File;
use std::io::{Read, Write};
mod common;
#[cfg(unix)]
use std::os::fd::OwnedFd;
#[cfg(windows)]
use std::os::windows::io::OwnedHandle;
#[cfg(unix)]
fn switch_stdout_to(file: OwnedFd) -> OwnedFd {
use std::os::unix::prelude::*;
unsafe extern "C" {
fn dup(old: i32) -> i32;
fn dup2(old: i32, new: i32) -> i32;
}
unsafe {
let orig_fd = dup(1);
assert_ne!(orig_fd, -1);
let res = OwnedFd::from_raw_fd(orig_fd);
assert_eq!(dup2(file.as_raw_fd(), 1), 1);
res
}
}
#[cfg(windows)]
fn switch_stdout_to(file: OwnedHandle) -> OwnedHandle {
use std::os::windows::prelude::*;
unsafe extern "system" {
fn GetStdHandle(nStdHandle: u32) -> *mut u8;
fn SetStdHandle(nStdHandle: u32, handle: *mut u8) -> i32;
}
const STD_OUTPUT_HANDLE: u32 = (-11i32) as u32;
const INVALID_HANDLE_VALUE: *mut u8 = !0 as *mut u8;
unsafe {
let orig_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
assert!(!orig_hdl.is_null() && orig_hdl != INVALID_HANDLE_VALUE);
let rc = SetStdHandle(STD_OUTPUT_HANDLE, file.into_raw_handle() as *mut _);
assert!(rc != 0);
OwnedHandle::from_raw_handle(orig_hdl as _)
}
}
#[test]
#[cfg_attr(miri, ignore)] // dup/SetStdHandle not supported by Miri
fn switch_stdout() {
let temp = common::tmpdir();
let path = temp.join("switch-stdout-output");
let f = File::create(&path).unwrap();
let mut stdout = std::io::stdout();
stdout.write(b"foo\n").unwrap();
stdout.flush().unwrap();
let orig_hdl = switch_stdout_to(f.into());
stdout.write(b"bar\n").unwrap();
stdout.flush().unwrap();
switch_stdout_to(orig_hdl);
let mut contents = String::new();
File::open(&path).unwrap().read_to_string(&mut contents).unwrap();
assert_eq!(contents, "bar\n");
}