coreutils/tty/tty.rs

80 lines
1.6 KiB
Rust
Raw Normal View History

2014-03-31 16:40:21 +00:00
#![crate_id(name="tty", version="1.0.0", author="Alan Andrade")]
2014-01-10 08:23:12 +00:00
/*
* 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
*/
2014-03-31 16:40:21 +00:00
#![allow(dead_code)]
2014-03-31 16:40:21 +00:00
#![feature(macro_rules)]
extern crate getopts;
extern crate libc;
2014-01-10 08:23:12 +00:00
use std::{str,os};
use std::io::println;
2014-01-10 08:23:12 +00:00
use std::io::stdio::stderr;
use getopts::{optflag,getopts};
2014-02-23 22:17:48 +00:00
#[path = "../common/util.rs"]
mod util;
2014-01-10 08:23:12 +00:00
extern {
fn ttyname(filedesc: libc::c_int) -> *libc::c_char;
fn isatty(filedesc: libc::c_int) -> libc::c_int;
}
static NAME: &'static str = "tty";
2014-01-10 08:23:12 +00:00
fn main () {
let args = os::args();
let options = [
optflag("s", "silent", "print nothing, only return an exit status")
2014-01-10 08:23:12 +00:00
];
let silent = match getopts(args.tail(), options) {
Ok(m) => {
m.opt_present("s")
},
Err(f) => {
println(f.to_err_msg());
usage();
return
}
};
let tty = unsafe { str::raw::from_c_str(ttyname(libc::STDIN_FILENO)) };
if !silent {
if !tty.is_whitespace() {
println(tty);
} else {
println!("not a tty");
2014-01-10 08:23:12 +00:00
}
}
let exit_code = unsafe {
if isatty(libc::STDIN_FILENO) == 1 {
libc::EXIT_SUCCESS
} else {
libc::EXIT_FAILURE
}
};
os::set_exit_status(exit_code as int);
}
fn usage () {
safe_writeln!(&mut stderr() as &mut Writer, "usage: tty [-s]");
2014-01-10 08:23:12 +00:00
os::set_exit_status(2);
}