From 8bbe12302311142c2230eb1887aee7f4e9ca2130 Mon Sep 17 00:00:00 2001 From: Michael Gehring Date: Tue, 17 Jun 2014 03:01:44 +0200 Subject: [PATCH] hostname: fix sethostname prototype on linux --- hostname/hostname.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/hostname/hostname.rs b/hostname/hostname.rs index ed4f30930..48426b2e9 100644 --- a/hostname/hostname.rs +++ b/hostname/hostname.rs @@ -20,9 +20,18 @@ use getopts::{optflag, getopts, usage}; extern { fn gethostname(name: *libc::c_char, namelen: libc::size_t) -> libc::c_int; +} + +#[cfg(target_os = "macos")] +extern { fn sethostname(name: *libc::c_char, namelen: libc::c_int) -> libc::c_int; } +#[cfg(target_os = "linux")] +extern { + fn sethostname(name: *libc::c_char, namelen: libc::size_t) -> libc::c_int; +} + #[allow(dead_code)] fn main () { os::set_exit_status(uumain(os::args())); } @@ -99,11 +108,25 @@ fn xgethostname() -> String { str::from_utf8(name.slice_to(last_char)).unwrap().to_string() } +#[cfg(target_os = "macos")] fn xsethostname(name: &str) { - let vec_name: Vec = name.bytes().map(|c| c as i8).collect(); + let vec_name: Vec = name.bytes().map(|c| c as libc::c_char).collect(); let err = unsafe { - sethostname (vec_name.as_ptr(), vec_name.len() as i32) + sethostname (vec_name.as_ptr(), vec_name.len() as libc::c_int) + }; + + if err != 0 { + println!("Cannot set hostname to {:s}", name); + } +} + +#[cfg(target_os = "linux")] +fn xsethostname(name: &str) { + let vec_name: Vec = name.bytes().map(|c| c as libc::c_char).collect(); + + let err = unsafe { + sethostname (vec_name.as_ptr(), vec_name.len() as libc::size_t) }; if err != 0 {