Skip to content

Commit

Permalink
Merge pull request rust-lang#2 from carllerche/hostname
Browse files Browse the repository at this point in the history
Add gethostname and sethostname
  • Loading branch information
wycats committed Sep 11, 2014
2 parents 1fac0ee + 98e73f1 commit d873d94
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use errno::{SysResult, SysError, from_ffi};
pub use self::linux::*;

mod ffi {
use libc::{c_char, c_int};
use libc::{c_char, c_int, size_t};
pub use libc::{close, read, write};

extern {
Expand All @@ -29,6 +29,14 @@ mod ffi {
// run the current process in the background
// doc: http://man7.org/linux/man-pages/man3/daemon.3.html
pub fn daemon(nochdir: c_int, noclose: c_int) -> c_int;

// sets the hostname to the value given
// doc: http://man7.org/linux/man-pages/man2/gethostname.2.html
pub fn gethostname(name: *mut c_char, len: size_t) -> c_int;

// gets the hostname
// doc: http://man7.org/linux/man-pages/man2/gethostname.2.html
pub fn sethostname(name: *const c_char, len: size_t) -> c_int;
}
}

Expand Down Expand Up @@ -102,6 +110,22 @@ pub fn daemon(nochdir: bool, noclose: bool) -> SysResult<()> {
from_ffi(res)
}

pub fn sethostname(name: &[u8]) -> SysResult<()> {
let ptr = name.as_ptr() as *const c_char;
let len = name.len() as u64;

let res = unsafe { ffi::sethostname(ptr, len) };
from_ffi(res)
}

pub fn gethostname(name: &mut [u8]) -> SysResult<()> {
let ptr = name.as_mut_ptr() as *mut c_char;
let len = name.len() as u64;

let res = unsafe { ffi::gethostname(ptr, len) };
from_ffi(res)
}

pub fn close(fd: Fd) -> SysResult<()> {
let res = unsafe { ffi::close(fd) };
from_ffi(res)
Expand Down

0 comments on commit d873d94

Please sign in to comment.