Skip to content

Commit a113174

Browse files
committed
Add a waitpid wrapper to std::run that interprets the exit status on unix
This makes the result of running a program a little more uniform between unix and windows
1 parent 476bbca commit a113174

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

src/lib/run_program.rs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export run_program;
66
export start_program;
77
export program_output;
88
export spawn_process;
9+
export waitpid;
910

1011
native "rust" mod rustrt {
1112
fn rust_run_program(argv: *sbuf, in_fd: int, out_fd: int, err_fd: int) ->
@@ -33,7 +34,7 @@ fn spawn_process(prog: &str, args: &[str], in_fd: int, out_fd: int,
3334
}
3435

3536
fn run_program(prog: &str, args: &[str]) -> int {
36-
ret os::waitpid(spawn_process(prog, args, 0, 0, 0));
37+
ret waitpid(spawn_process(prog, args, 0, 0, 0));
3738
}
3839

3940
type program =
@@ -87,7 +88,7 @@ fn start_program(prog: &str, args: &[str]) -> @program_res {
8788
if finished { ret 0; }
8889
finished = true;
8990
self.close_input();
90-
ret os::waitpid(pid);
91+
ret waitpid(pid);
9192
}
9293
fn destroy() {
9394
self.finish();
@@ -117,6 +118,44 @@ fn program_output(prog: &str, args: &[str]) ->
117118
out: read_all(pr.output()),
118119
err: read_all(pr.err())};
119120
}
121+
122+
/* Returns an exit status */
123+
#[cfg(target_os = "win32")]
124+
fn waitpid(pid: int) -> int {
125+
os::waitpid(pid)
126+
}
127+
128+
#[cfg(target_os = "linux")]
129+
#[cfg(target_os = "macos")]
130+
fn waitpid(pid: int) -> int {
131+
#[cfg(target_os = "linux")]
132+
fn WIFEXITED(status: int) -> bool {
133+
(status & 0xff) == 0
134+
}
135+
136+
#[cfg(target_os = "macos")]
137+
fn WIFEXITED(status: int) -> bool {
138+
(status & 0x7f) == 0
139+
}
140+
141+
#[cfg(target_os = "linux")]
142+
fn WEXITSTATUS(status: int) -> int {
143+
(status >> 8) & 0xff
144+
}
145+
146+
#[cfg(target_os = "macos")]
147+
fn WEXITSTATUS(status: int) -> int {
148+
status >> 8
149+
}
150+
151+
let status = os::waitpid(pid);
152+
ret if WIFEXITED(status) {
153+
WEXITSTATUS(status)
154+
} else {
155+
1
156+
};
157+
}
158+
120159
// Local Variables:
121160
// mode: rust
122161
// fill-column: 78;

src/test/stdtest/run.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,10 @@ fn test_pipes() {
6565
ret buf;
6666
}
6767
}
68+
69+
#[test]
70+
fn waitpid() {
71+
let pid = run::spawn_process("false", [], 0, 0, 0);
72+
let status = run::waitpid(pid);
73+
assert status == 1;
74+
}

0 commit comments

Comments
 (0)