From 5f02d0fc81177c64aab924109e1b5604910ee1fa Mon Sep 17 00:00:00 2001 From: Alan Andrade Date: Mon, 9 Dec 2013 17:59:41 -0800 Subject: [PATCH 1/2] hostname implementation --- Makefile | 1 + hostname/hostname.rs | 89 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 hostname/hostname.rs diff --git a/Makefile b/Makefile index af38f5b5a..02df49e2c 100644 --- a/Makefile +++ b/Makefile @@ -29,6 +29,7 @@ PROGS := \ wc \ yes \ tty \ + hostname \ UNIX_PROGS := \ whoami \ diff --git a/hostname/hostname.rs b/hostname/hostname.rs new file mode 100644 index 000000000..ba0bf9841 --- /dev/null +++ b/hostname/hostname.rs @@ -0,0 +1,89 @@ +#[crate_id(name="hostname", vers="1.0.0", author="Alan Andrade")]; +/* + * This file is part of the uutils coreutils package. + * + * (c) Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * Synced with: + * + * https://www.opensource.apple.com/source/shell_cmds/shell_cmds-170/hostname/hostname.c?txt + */ + +extern mod extra; + +use std::{os,libc,vec,str}; +use extra::getopts::{optflag, getopts}; + +extern { + fn gethostname(name: *libc::c_char, namelen: libc::size_t) -> libc::c_int; + fn sethostname(name: *libc::c_char, namelen: libc::c_int) -> libc::c_int; +} + +fn main () { + let args = os::args(); + + let options = [ + optflag("f"), + optflag("s") + ]; + + let matches = match getopts(args.tail(), options) { + Ok(m) => { m } + _ => { usage(); return; } + }; + + match matches.free.len() { + 0 => { + let hostname: ~str = xgethostname(); + + if matches.opt_present("s") { + let pos = hostname.find_str("."); + if pos.is_some() { + println!("{:s}", hostname.slice_to(pos.unwrap())); + return; + } + } + + println!("{:s}", hostname); + } + 1 => { xsethostname( matches.free.last().unwrap() ) } + _ => { usage() } + }; +} + +fn usage() { + println!("usage: hostname [-fs] [name-of-host]"); +} + +fn xgethostname() -> ~str { + let namelen = 255u; + let mut name = vec::from_elem(namelen, 0u8); + + let err = unsafe { + gethostname (name.as_mut_ptr() as *libc::c_char, + namelen as libc::size_t) + }; + + if err != 0 { + fail!("Cannot determine hostname"); + } + + let last_char = name.iter().position(|byte| *byte == 0).unwrap_or(namelen); + + str::from_utf8(name.slice_to(last_char)).unwrap().to_owned() +} + +fn xsethostname(name: &~str) { + let vec_name: ~[libc::c_char] = name.bytes().map(|c| c as i8).collect(); + + let err = unsafe { + sethostname (vec_name.as_ptr(), vec_name.len() as i32) + }; + + if err != 0 { + println!("Cannot set hostname to {:s}", *name); + } +} From b61e3dae2ed619713871ca5de3876d1d9c7fd413 Mon Sep 17 00:00:00 2001 From: Alan Andrade Date: Fri, 7 Feb 2014 09:21:59 -0800 Subject: [PATCH 2/2] Add -h help and -V version flags --- hostname/hostname.rs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/hostname/hostname.rs b/hostname/hostname.rs index ba0bf9841..a320f822d 100644 --- a/hostname/hostname.rs +++ b/hostname/hostname.rs @@ -2,7 +2,7 @@ /* * This file is part of the uutils coreutils package. * - * (c) Jordi Boggiano + * (c) Alan Andrade * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -13,9 +13,10 @@ */ extern mod extra; +extern mod getopts; use std::{os,libc,vec,str}; -use extra::getopts::{optflag, getopts}; +use getopts::{optflag, getopts, usage}; extern { fn gethostname(name: *libc::c_char, namelen: libc::size_t) -> libc::c_int; @@ -24,17 +25,26 @@ extern { fn main () { let args = os::args(); + let program = args[0].to_owned(); let options = [ - optflag("f"), - optflag("s") + optflag("f", "full", "Default option to show full name"), + optflag("s", "slice subdomain", "Cuts the subdomain off if any"), + optflag("h", "help", "Show help"), + optflag("V", "version", "Show program's version") ]; let matches = match getopts(args.tail(), options) { Ok(m) => { m } - _ => { usage(); return; } + _ => { println!("{:s}", usage(program, options)); return; } }; + if matches.opt_present("h") { + println!("{:s}", usage(program, options)); + return + } + if matches.opt_present("V") { version(); return } + match matches.free.len() { 0 => { let hostname: ~str = xgethostname(); @@ -50,12 +60,12 @@ fn main () { println!("{:s}", hostname); } 1 => { xsethostname( matches.free.last().unwrap() ) } - _ => { usage() } + _ => { println!("{:s}", usage(program, options)); } }; } -fn usage() { - println!("usage: hostname [-fs] [name-of-host]"); +fn version() { + println!("hostname version 1.0.0"); } fn xgethostname() -> ~str {