mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-31 04:57:45 +00:00
commit
eb2247fc1c
1 changed files with 31 additions and 29 deletions
|
@ -1,5 +1,5 @@
|
||||||
#![crate_name = "hostname"]
|
#![crate_name = "hostname"]
|
||||||
#![feature(collections, core, old_io, rustc_private)]
|
#![feature(collections)]
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This file is part of the uutils coreutils package.
|
* This file is part of the uutils coreutils package.
|
||||||
|
@ -18,10 +18,11 @@ extern crate getopts;
|
||||||
extern crate libc;
|
extern crate libc;
|
||||||
|
|
||||||
use std::collections::hash_set::HashSet;
|
use std::collections::hash_set::HashSet;
|
||||||
use std::old_io::net::addrinfo;
|
|
||||||
use std::iter::repeat;
|
use std::iter::repeat;
|
||||||
use std::str;
|
use std::str;
|
||||||
use getopts::{optflag, getopts, usage};
|
use std::io::Write;
|
||||||
|
use std::net::ToSocketAddrs;
|
||||||
|
use getopts::Options;
|
||||||
|
|
||||||
#[path = "../common/util.rs"]
|
#[path = "../common/util.rs"]
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
|
@ -46,22 +47,21 @@ extern {
|
||||||
pub fn uumain(args: Vec<String>) -> i32 {
|
pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
let program = &args[0];
|
let program = &args[0];
|
||||||
|
|
||||||
let options = [
|
let mut opts = Options::new();
|
||||||
optflag("d", "domain", "Display the name of the DNS domain if possible"),
|
opts.optflag("d", "domain", "Display the name of the DNS domain if possible");
|
||||||
optflag("i", "ip-address", "Display the network address(es) of the host"),
|
opts.optflag("i", "ip-address", "Display the network address(es) of the host");
|
||||||
optflag("f", "fqdn", "Display the FQDN (Fully Qualified Domain Name) (default)"), // TODO: support --long
|
opts.optflag("f", "fqdn", "Display the FQDN (Fully Qualified Domain Name) (default)"); // TODO: support --long
|
||||||
optflag("s", "short", "Display the short hostname (the portion before the first dot) if possible"),
|
opts.optflag("s", "short", "Display the short hostname (the portion before the first dot) if possible");
|
||||||
optflag("h", "help", "Show help"),
|
opts.optflag("h", "help", "Show help");
|
||||||
optflag("V", "version", "Show program's version")
|
opts.optflag("V", "version", "Show program's version");
|
||||||
];
|
|
||||||
|
|
||||||
let matches = match getopts(args.tail(), &options) {
|
let matches = match opts.parse(args.tail()) {
|
||||||
Ok(m) => { m }
|
Ok(m) => { m }
|
||||||
_ => { help_menu(program.as_slice(), &options); return 0; }
|
_ => { help_menu(program, opts); return 0; }
|
||||||
};
|
};
|
||||||
|
|
||||||
if matches.opt_present("h") {
|
if matches.opt_present("h") {
|
||||||
help_menu(program.as_slice(), &options);
|
help_menu(program, opts);
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
if matches.opt_present("V") { version(); return 0 }
|
if matches.opt_present("V") { version(); return 0 }
|
||||||
|
@ -71,21 +71,21 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
let hostname = xgethostname();
|
let hostname = xgethostname();
|
||||||
|
|
||||||
if matches.opt_present("i") {
|
if matches.opt_present("i") {
|
||||||
match addrinfo::get_host_addresses(hostname.as_slice()) {
|
match hostname.to_socket_addrs() {
|
||||||
Ok(addresses) => {
|
Ok(addresses) => {
|
||||||
let mut hashset = HashSet::new();
|
let mut hashset = HashSet::new();
|
||||||
let mut output = String::new();
|
let mut output = String::new();
|
||||||
for addr in addresses.iter() {
|
for addr in addresses {
|
||||||
// XXX: not sure why this is necessary...
|
// XXX: not sure why this is necessary...
|
||||||
if !hashset.contains(addr) {
|
if !hashset.contains(&addr) {
|
||||||
output.push_str(addr.to_string().as_slice());
|
output.push_str(&format!("{}", addr));
|
||||||
output.push_str(" ");
|
output.push_str(" ");
|
||||||
hashset.insert(addr.clone());
|
hashset.insert(addr.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let len = output.len();
|
let len = output.len();
|
||||||
if len > 0 {
|
if len > 0 {
|
||||||
println!("{}", output.as_slice().slice_to(len - 1));
|
println!("{}", &output[0 .. len - 1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(f) => {
|
Err(f) => {
|
||||||
|
@ -95,15 +95,17 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if matches.opt_present("s") {
|
if matches.opt_present("s") {
|
||||||
let pos = hostname.as_slice().find_str(".");
|
let mut it = hostname.char_indices().filter(|&ci| ci.1 == '.');
|
||||||
if pos.is_some() {
|
let ci = it.next();
|
||||||
println!("{}", hostname.as_slice().slice_to(pos.unwrap()));
|
if ci.is_some() {
|
||||||
|
println!("{}", &hostname[0 .. ci.unwrap().0]);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
} else if matches.opt_present("d") {
|
} else if matches.opt_present("d") {
|
||||||
let pos = hostname.as_slice().find_str(".");
|
let mut it = hostname.char_indices().filter(|&ci| ci.1 == '.');
|
||||||
if pos.is_some() {
|
let ci = it.next();
|
||||||
println!("{}", hostname.as_slice().slice_from(pos.unwrap() + 1));
|
if ci.is_some() {
|
||||||
|
println!("{}", &hostname[ci.unwrap().0 + 1 .. ]);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -111,8 +113,8 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
println!("{}", hostname);
|
println!("{}", hostname);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
1 => xsethostname(matches.free.last().unwrap().as_slice()),
|
1 => xsethostname(matches.free.last().unwrap()),
|
||||||
_ => help_menu(program.as_slice(), &options)
|
_ => help_menu(program, opts)
|
||||||
};
|
};
|
||||||
|
|
||||||
0
|
0
|
||||||
|
@ -122,13 +124,13 @@ fn version() {
|
||||||
println!("hostname 1.0.0");
|
println!("hostname 1.0.0");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn help_menu(program: &str, options: &[getopts::OptGroup]) {
|
fn help_menu(program: &str, options: Options) {
|
||||||
version();
|
version();
|
||||||
println!("");
|
println!("");
|
||||||
println!("Usage:");
|
println!("Usage:");
|
||||||
println!(" {} [OPTION]... [HOSTNAME]", program);
|
println!(" {} [OPTION]... [HOSTNAME]", program);
|
||||||
println!("");
|
println!("");
|
||||||
print!("{}", usage("Print or set the system's host name.", options));
|
print!("{}", options.usage("Print or set the system's host name."));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn xgethostname() -> String {
|
fn xgethostname() -> String {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue