coreutils/src/tty/tty.rs
Joseph Crail 509a35ff7a Switch to external getopts cargo (part 2).
I switched over to the getopts crate on crates.io, instead of Rust's
private implementation. This will allow coreutils to build for Rust 1.0.

I'm splitting the updates into several commits for better reviewing.
2015-05-21 15:00:34 -04:00

83 lines
2.2 KiB
Rust

#![crate_name = "tty"]
/*
* This file is part of the uutils coreutils package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Synced with http://lingrok.org/xref/coreutils/src/tty.c
*/
extern crate getopts;
extern crate libc;
use std::ffi::CStr;
use std::io::Write;
#[path = "../common/util.rs"]
#[macro_use]
mod util;
extern {
fn ttyname(filedesc: libc::c_int) -> *const libc::c_char;
fn isatty(filedesc: libc::c_int) -> libc::c_int;
}
static NAME: &'static str = "tty";
static VERSION: &'static str = "1.0.0";
pub fn uumain(args: Vec<String>) -> i32 {
let mut opts = getopts::Options::new();
opts.optflag("s", "silent", "print nothing, only return an exit status");
opts.optflag("h", "help", "display this help and exit");
opts.optflag("V", "version", "output version information and exit");
let matches = match opts.parse(&args[1..]) {
Ok(m) => m,
Err(f) => { crash!(2, "{}", f) }
};
if matches.opt_present("help") {
println!("{} {}", NAME, VERSION);
println!("");
println!("Usage:");
println!(" {} [OPTION]...", NAME);
println!("");
print!("{}", opts.usage("Print the file name of the terminal connected to standard input."));
} else if matches.opt_present("version") {
println!("{} {}", NAME, VERSION);
} else {
let silent = matches.opt_present("s");
let tty = unsafe {
let ptr = ttyname(libc::STDIN_FILENO);
if !ptr.is_null() {
String::from_utf8_lossy(CStr::from_ptr(ptr).to_bytes()).to_string()
} else {
"".to_string()
}
};
if !silent {
if !tty.chars().all(|c| c.is_whitespace()) {
println!("{}", tty);
} else {
println!("not a tty");
}
}
return unsafe {
if isatty(libc::STDIN_FILENO) == 1 {
libc::EXIT_SUCCESS
} else {
libc::EXIT_FAILURE
}
};
}
0
}