Skip to content

Commit 78cd78b

Browse files
committed
Bind FD dup fns + misc tweaks
1 parent 4e7c65d commit 78cd78b

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

src/fcntl.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use std::c_str::CString;
2+
use std::path::Path;
23
use std::io::FilePermission;
34
use libc::{mode_t, c_int};
45
use errno::{SysResult, SysError, from_ffi};
56

67
pub type Fd = c_int;
78

89
bitflags!(
9-
flags OFlag: mode_t {
10+
flags OFlag: c_int {
1011
static O_ACCMODE = 0o00000003,
1112
static O_RDONLY = 0o00000000,
1213
static O_WRONLY = 0o00000001,
@@ -35,8 +36,8 @@ mod ffi {
3536
pub use libc::{open, close};
3637
}
3738

38-
pub fn open(path: &CString, oflag: OFlag, mode: FilePermission) -> SysResult<Fd> {
39-
let fd = unsafe { ffi::open(path.as_ptr(), oflag.bits as i32, mode.bits()) };
39+
pub fn open(path: &Path, oflag: OFlag, mode: FilePermission) -> SysResult<Fd> {
40+
let fd = unsafe { ffi::open(path.to_c_str().as_ptr(), oflag.bits, mode.bits()) };
4041

4142
if fd < 0 {
4243
return Err(SysError::last());

src/unistd.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@ use std::ptr;
22
use std::c_str::{CString, ToCStr};
33
use std::path::Path;
44
use libc::{c_char};
5+
use fcntl::{Fd, OFlag};
56
use syscall::{syscall, SysPivotRoot};
67
use {SysResult, SysError};
78

89
mod ffi {
910
use libc::{c_char, c_int};
1011

1112
extern {
13+
pub fn dup(oldfd: c_int) -> c_int;
14+
15+
pub fn dup2(oldfd: c_int, newfd: c_int) -> c_int;
16+
17+
pub fn dup3(oldfd: c_int, newfd: c_int, flags: c_int) -> c_int;
18+
1219
// change working directory
1320
// doc: http://man7.org/linux/man-pages/man2/chdir.2.html
1421
pub fn chdir(path: *const c_char) -> c_int;
@@ -19,6 +26,40 @@ mod ffi {
1926
}
2027
}
2128

29+
#[inline]
30+
pub fn dup(oldfd: Fd) -> SysResult<Fd> {
31+
let res = unsafe { ffi::dup(oldfd) };
32+
33+
if res < 0 {
34+
return Err(SysError::last());
35+
}
36+
37+
Ok(res)
38+
}
39+
40+
#[inline]
41+
pub fn dup2(oldfd: Fd, newfd: Fd) -> SysResult<Fd> {
42+
let res = unsafe { ffi::dup2(oldfd, newfd) };
43+
44+
if res < 0 {
45+
return Err(SysError::last());
46+
}
47+
48+
Ok(res)
49+
}
50+
51+
#[inline]
52+
pub fn dup3(oldfd: Fd, newfd: Fd, flags: OFlag) -> SysResult<Fd> {
53+
let res = unsafe { ffi::dup3(oldfd, newfd, flags.bits()) };
54+
55+
if res < 0 {
56+
return Err(SysError::last());
57+
}
58+
59+
Ok(res)
60+
}
61+
62+
#[inline]
2263
pub fn chdir<S: ToCStr>(path: S) -> SysResult<()> {
2364
let path = path.to_c_str();
2465
let res = unsafe { ffi::chdir(path.as_ptr()) };
@@ -30,6 +71,7 @@ pub fn chdir<S: ToCStr>(path: S) -> SysResult<()> {
3071
return Ok(())
3172
}
3273

74+
#[inline]
3375
pub fn execve(filename: CString, args: &[CString], env: &[CString]) -> SysResult<()> {
3476
let mut args_p: Vec<*const c_char> = args.iter().map(|s| s.as_ptr()).collect();
3577
args_p.push(ptr::null());

0 commit comments

Comments
 (0)