diff options
| author | Erick Tryzelaar <erick.tryzelaar@gmail.com> | 2013-01-22 08:11:57 -0800 |
|---|---|---|
| committer | Erick Tryzelaar <erick.tryzelaar@gmail.com> | 2013-01-24 16:24:30 -0800 |
| commit | c15facb5c092f5d1e5b687111f4dbcd117d567d7 (patch) | |
| tree | 196ef509a6147d3938798405d882636b5432805f /src/libcore | |
| parent | e22d2bc4bc4ed84c72148bdd00c8da0eb4238c45 (diff) | |
| download | rust-c15facb5c092f5d1e5b687111f4dbcd117d567d7.tar.gz rust-c15facb5c092f5d1e5b687111f4dbcd117d567d7.zip | |
Remove "unsafe" from run::start_program
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/run.rs | 138 |
1 files changed, 68 insertions, 70 deletions
diff --git a/src/libcore/run.rs b/src/libcore/run.rs index ff6dc924922..8960d40b85a 100644 --- a/src/libcore/run.rs +++ b/src/libcore/run.rs @@ -204,88 +204,86 @@ pub fn run_program(prog: &str, args: &[~str]) -> int { * A class with a <program> field */ pub fn start_program(prog: &str, args: &[~str]) -> Program { - unsafe { - let pipe_input = os::pipe(); - let pipe_output = os::pipe(); - let pipe_err = os::pipe(); - let pid = - spawn_process(prog, args, &None, &None, - pipe_input.in, pipe_output.out, - pipe_err.out); + let pipe_input = os::pipe(); + let pipe_output = os::pipe(); + let pipe_err = os::pipe(); + let pid = + spawn_process(prog, args, &None, &None, + pipe_input.in, pipe_output.out, + pipe_err.out); - unsafe { - if pid == -1 as pid_t { fail; } - libc::close(pipe_input.in); - libc::close(pipe_output.out); - libc::close(pipe_err.out); - } + unsafe { + if pid == -1 as pid_t { fail; } + libc::close(pipe_input.in); + libc::close(pipe_output.out); + libc::close(pipe_err.out); + } - struct ProgRepr { - pid: pid_t, - mut in_fd: c_int, - out_file: *libc::FILE, - err_file: *libc::FILE, - mut finished: bool, - } + struct ProgRepr { + pid: pid_t, + mut in_fd: c_int, + out_file: *libc::FILE, + err_file: *libc::FILE, + mut finished: bool, + } - fn close_repr_input(r: &ProgRepr) { - let invalid_fd = -1i32; - if r.in_fd != invalid_fd { - unsafe { - libc::close(r.in_fd); - } - r.in_fd = invalid_fd; - } - } - fn finish_repr(r: &ProgRepr) -> int { - if r.finished { return 0; } - r.finished = true; - close_repr_input(r); - return waitpid(r.pid); - } - fn destroy_repr(r: &ProgRepr) { + fn close_repr_input(r: &ProgRepr) { + let invalid_fd = -1i32; + if r.in_fd != invalid_fd { unsafe { - finish_repr(r); - libc::fclose(r.out_file); - libc::fclose(r.err_file); + libc::close(r.in_fd); } + r.in_fd = invalid_fd; } - struct ProgRes { - r: ProgRepr, - drop { destroy_repr(&self.r); } + } + fn finish_repr(r: &ProgRepr) -> int { + if r.finished { return 0; } + r.finished = true; + close_repr_input(r); + return waitpid(r.pid); + } + fn destroy_repr(r: &ProgRepr) { + unsafe { + finish_repr(r); + libc::fclose(r.out_file); + libc::fclose(r.err_file); } + } + struct ProgRes { + r: ProgRepr, + drop { destroy_repr(&self.r); } + } - fn ProgRes(r: ProgRepr) -> ProgRes { - ProgRes { - r: move r - } + fn ProgRes(r: ProgRepr) -> ProgRes { + ProgRes { + r: move r } + } - impl ProgRes: Program { - fn get_id() -> pid_t { return self.r.pid; } - fn input() -> io::Writer { - io::fd_writer(self.r.in_fd, false) - } - fn output() -> io::Reader { - io::FILE_reader(self.r.out_file, false) - } - fn err() -> io::Reader { - io::FILE_reader(self.r.err_file, false) - } - fn close_input() { close_repr_input(&self.r); } - fn finish() -> int { finish_repr(&self.r) } - fn destroy() { destroy_repr(&self.r); } + impl ProgRes: Program { + fn get_id() -> pid_t { return self.r.pid; } + fn input() -> io::Writer { + io::fd_writer(self.r.in_fd, false) } - let repr = ProgRepr { - pid: pid, - in_fd: pipe_input.out, - out_file: os::fdopen(pipe_output.in), - err_file: os::fdopen(pipe_err.in), - finished: false, - }; - - ProgRes(repr) as Program + fn output() -> io::Reader { + io::FILE_reader(self.r.out_file, false) + } + fn err() -> io::Reader { + io::FILE_reader(self.r.err_file, false) + } + fn close_input() { close_repr_input(&self.r); } + fn finish() -> int { finish_repr(&self.r) } + fn destroy() { destroy_repr(&self.r); } } + let repr = ProgRepr { + pid: pid, + in_fd: pipe_input.out, + out_file: os::fdopen(pipe_output.in), + err_file: os::fdopen(pipe_err.in), + finished: false, + }; + + ProgRes(repr) as Program } fn read_all(rd: io::Reader) -> ~str { |
