diff --git a/src/platform/src/linux/mod.rs b/src/platform/src/linux/mod.rs index 8ecfbc54ea9e84f3ecf7e6bfd131b10915de9a9a..0cf01a27e077961a6b4afd03f853b039319f0233 100644 --- a/src/platform/src/linux/mod.rs +++ b/src/platform/src/linux/mod.rs @@ -214,7 +214,7 @@ pub fn open(path: *const c_char, oflag: c_int, mode: mode_t) -> c_int { e(unsafe { syscall!(OPENAT, AT_FDCWD, path, oflag, mode) }) as c_int } -pub fn pipe(mut fildes: [c_int; 2]) -> c_int { +pub fn pipe(fildes: &mut [c_int]) -> c_int { e(unsafe { syscall!(PIPE2, fildes.as_mut_ptr(), 0) }) as c_int } diff --git a/src/platform/src/redox/mod.rs b/src/platform/src/redox/mod.rs index bcf4da44a9ac198c2679557b80561a2ade00c4fd..60326c4cf195b162a11c8afe3575449b6d0e89b7 100644 --- a/src/platform/src/redox/mod.rs +++ b/src/platform/src/redox/mod.rs @@ -459,7 +459,7 @@ pub fn open(path: *const c_char, oflag: c_int, mode: mode_t) -> c_int { e(syscall::open(path, (oflag as usize) | (mode as usize))) as c_int } -pub fn pipe(mut fds: [c_int; 2]) -> c_int { +pub fn pipe(fds: &mut [c_int]) -> c_int { let mut usize_fds: [usize; 2] = [0; 2]; let res = e(syscall::pipe2(&mut usize_fds, 0)); fds[0] = usize_fds[0] as c_int; diff --git a/src/unistd/src/lib.rs b/src/unistd/src/lib.rs index 408143a7bb5855e47107c65b9afdef28fedcffea..d04d44c93b8153d9c8f139af8289ac8bbe96f7a2 100644 --- a/src/unistd/src/lib.rs +++ b/src/unistd/src/lib.rs @@ -11,7 +11,7 @@ extern crate stdio; extern crate string; extern crate sys_utsname; -use core::ptr; +use core::{ptr, slice}; use platform::types::*; @@ -347,8 +347,8 @@ pub extern "C" fn pause() -> c_int { } #[no_mangle] -pub extern "C" fn pipe(fildes: [c_int; 2]) -> c_int { - platform::pipe(fildes) +pub unsafe extern "C" fn pipe(fildes: *mut c_int) -> c_int { + platform::pipe(slice::from_raw_parts_mut(fildes, 2)) } // #[no_mangle] diff --git a/tests/expected/pipe.stdout b/tests/expected/pipe.stdout index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..980a0d5f19a64b4b30a87d4206aade58726b60e3 100644 --- a/tests/expected/pipe.stdout +++ b/tests/expected/pipe.stdout @@ -0,0 +1 @@ +Hello World! diff --git a/tests/pipe.c b/tests/pipe.c index 703073cc351b64b5b8618c438348fec74492c2bc..cbd3caffb74e31bad7c0a5893090c6fda8cd1cc4 100644 --- a/tests/pipe.c +++ b/tests/pipe.c @@ -1,4 +1,5 @@ //http://www2.cs.uregina.ca/~hamilton/courses/330/notes/unix/pipes/pipes.html +#include <stdio.h> #include <string.h> #include <unistd.h> @@ -9,26 +10,63 @@ int main() char instring[20]; char * outstring = "Hello World!"; - pipe(pip); + if (pipe(pip) < 0) { + perror("pipe"); + return 1; + } pid = fork(); if (pid == 0) /* child : sends message to parent*/ { /* close read end */ close(pip[0]); + /* send 7 characters in the string, including end-of-string */ - write(pip[1], outstring, strlen(outstring)); + int bytes = write(pip[1], outstring, strlen(outstring)); + /* close write end */ close(pip[1]); + + /* check result */ + if (bytes < 0) { + perror("pipe write"); + return 1; + } else if (bytes != strlen(outstring)) { + fprintf(stderr, "pipe write: %d != %d\n", bytes, strlen(outstring)); + return 1; + } + + return 0; } else /* parent : receives message from child */ { /* close write end */ close(pip[1]); + + /* clear memory */ + memset(instring, 0, sizeof(instring)); + /* read from the pipe */ - read(pip[0], instring, 7); + int bytes = read(pip[0], instring, sizeof(instring) - 1); + /* close read end */ close(pip[0]); + + /* check result */ + if (bytes < 0) { + perror("pipe read"); + return 1; + } else if (bytes != strlen(outstring)) { + fprintf(stderr, "pipe read: %d != %d\n", bytes, strlen(outstring)); + return 1; + } else if (memcmp(instring, outstring, sizeof(outstring)) != 0) { + fprintf(stderr, "pipe read does not match pipe write\n"); + return 1; + } else { + printf("%s\n", instring); + } + + return 0; } return 0; }