coreutils/tty/tty.rs
Michael Gehring 30bba07f9c always build multicall binary
squashed:
	a2c6b27 - build: automatically generate main() files
	c942f0f - remove MULTICALL=1 build from travis
	cb7b35b - make: remove unnecessary shell command
	69bbb31 - update README
	03a3168 - all: move main() into separate file that links against util crate
	8276384 - make: always build multicall binary
	aa4edeb - make: avoid 'rustc --crate-file-name'
2014-06-26 10:26:16 +02:00

76 lines
1.6 KiB
Rust

#![crate_id(name="tty", version="1.0.0", author="Alan Andrade")]
/*
* 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
*/
#![allow(dead_code)]
#![feature(macro_rules)]
extern crate getopts;
extern crate libc;
use std::str;
use std::io::println;
use std::io::stdio::stderr;
use getopts::{optflag,getopts};
#[path = "../common/util.rs"]
mod util;
extern {
fn ttyname(filedesc: libc::c_int) -> *libc::c_char;
fn isatty(filedesc: libc::c_int) -> libc::c_int;
}
static NAME: &'static str = "tty";
pub fn uumain(args: Vec<String>) -> int {
let options = [
optflag("s", "silent", "print nothing, only return an exit status")
];
let silent = match getopts(args.tail(), options) {
Ok(m) => {
m.opt_present("s")
},
Err(f) => {
println!("{}", f);
usage();
return 2;
}
};
let tty = unsafe { str::raw::from_c_str(ttyname(libc::STDIN_FILENO)) };
if !silent {
if !tty.as_slice().is_whitespace() {
println(tty.as_slice());
} else {
println!("not a tty");
}
}
let exit_code = unsafe {
if isatty(libc::STDIN_FILENO) == 1 {
libc::EXIT_SUCCESS
} else {
libc::EXIT_FAILURE
}
};
exit_code as int
}
fn usage () {
safe_writeln!(&mut stderr() as &mut Writer, "usage: tty [-s]");
}