diff options
author | Carl Lerche <me@carllerche.com> | 2014-08-15 23:22:04 -0700 |
---|---|---|
committer | Carl Lerche <me@carllerche.com> | 2014-08-15 23:22:04 -0700 |
commit | 78cd78bbdac732bf2114bdfadb564cd4f7232365 (patch) | |
tree | ba4838e8a1dfc149fb6ea600bff106216875047c /src | |
parent | 4e7c65d3a1cf5c8999da9dcc3f55d1ba38b8c816 (diff) | |
download | nix-78cd78bbdac732bf2114bdfadb564cd4f7232365.zip |
Bind FD dup fns + misc tweaks
Diffstat (limited to 'src')
-rw-r--r-- | src/fcntl.rs | 7 | ||||
-rw-r--r-- | src/unistd.rs | 42 |
2 files changed, 46 insertions, 3 deletions
diff --git a/src/fcntl.rs b/src/fcntl.rs index 15ff1446..57325c86 100644 --- a/src/fcntl.rs +++ b/src/fcntl.rs @@ -1,4 +1,5 @@ use std::c_str::CString; +use std::path::Path; use std::io::FilePermission; use libc::{mode_t, c_int}; use errno::{SysResult, SysError, from_ffi}; @@ -6,7 +7,7 @@ use errno::{SysResult, SysError, from_ffi}; pub type Fd = c_int; bitflags!( - flags OFlag: mode_t { + flags OFlag: c_int { static O_ACCMODE = 0o00000003, static O_RDONLY = 0o00000000, static O_WRONLY = 0o00000001, @@ -35,8 +36,8 @@ mod ffi { pub use libc::{open, close}; } -pub fn open(path: &CString, oflag: OFlag, mode: FilePermission) -> SysResult<Fd> { - let fd = unsafe { ffi::open(path.as_ptr(), oflag.bits as i32, mode.bits()) }; +pub fn open(path: &Path, oflag: OFlag, mode: FilePermission) -> SysResult<Fd> { + let fd = unsafe { ffi::open(path.to_c_str().as_ptr(), oflag.bits, mode.bits()) }; if fd < 0 { return Err(SysError::last()); diff --git a/src/unistd.rs b/src/unistd.rs index a5a60915..85770876 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -2,6 +2,7 @@ use std::ptr; use std::c_str::{CString, ToCStr}; use std::path::Path; use libc::{c_char}; +use fcntl::{Fd, OFlag}; use syscall::{syscall, SysPivotRoot}; use {SysResult, SysError}; @@ -9,6 +10,12 @@ mod ffi { use libc::{c_char, c_int}; extern { + pub fn dup(oldfd: c_int) -> c_int; + + pub fn dup2(oldfd: c_int, newfd: c_int) -> c_int; + + pub fn dup3(oldfd: c_int, newfd: c_int, flags: c_int) -> c_int; + // change working directory // doc: http://man7.org/linux/man-pages/man2/chdir.2.html pub fn chdir(path: *const c_char) -> c_int; @@ -19,6 +26,40 @@ mod ffi { } } +#[inline] +pub fn dup(oldfd: Fd) -> SysResult<Fd> { + let res = unsafe { ffi::dup(oldfd) }; + + if res < 0 { + return Err(SysError::last()); + } + + Ok(res) +} + +#[inline] +pub fn dup2(oldfd: Fd, newfd: Fd) -> SysResult<Fd> { + let res = unsafe { ffi::dup2(oldfd, newfd) }; + + if res < 0 { + return Err(SysError::last()); + } + + Ok(res) +} + +#[inline] +pub fn dup3(oldfd: Fd, newfd: Fd, flags: OFlag) -> SysResult<Fd> { + let res = unsafe { ffi::dup3(oldfd, newfd, flags.bits()) }; + + if res < 0 { + return Err(SysError::last()); + } + + Ok(res) +} + +#[inline] pub fn chdir<S: ToCStr>(path: S) -> SysResult<()> { let path = path.to_c_str(); let res = unsafe { ffi::chdir(path.as_ptr()) }; @@ -30,6 +71,7 @@ pub fn chdir<S: ToCStr>(path: S) -> SysResult<()> { return Ok(()) } +#[inline] pub fn execve(filename: CString, args: &[CString], env: &[CString]) -> SysResult<()> { let mut args_p: Vec<*const c_char> = args.iter().map(|s| s.as_ptr()).collect(); args_p.push(ptr::null()); |