2014-07-06 08:13:36 +00:00
|
|
|
#![crate_name = "tty"]
|
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-02-07 06:39:07 +00:00
|
|
|
|
2014-02-16 21:29:31 +00:00
|
|
|
extern crate getopts;
|
2014-04-07 22:43:34 +00:00
|
|
|
extern crate libc;
|
2014-01-10 08:23:12 +00:00
|
|
|
|
2015-01-08 20:59:07 +00:00
|
|
|
use std::ffi::c_str_to_bytes;
|
2014-01-13 09:05:02 +00:00
|
|
|
use std::io::println;
|
2014-01-10 08:23:12 +00:00
|
|
|
use std::io::stdio::stderr;
|
2014-02-07 06:39:07 +00:00
|
|
|
use getopts::{optflag,getopts};
|
|
|
|
|
2014-02-23 22:17:48 +00:00
|
|
|
#[path = "../common/util.rs"]
|
2015-01-08 12:54:22 +00:00
|
|
|
#[macro_use]
|
2014-02-07 06:39:07 +00:00
|
|
|
mod util;
|
2014-01-10 08:23:12 +00:00
|
|
|
|
|
|
|
extern {
|
2014-06-30 11:13:38 +00:00
|
|
|
fn ttyname(filedesc: libc::c_int) -> *const libc::c_char;
|
2014-01-10 08:23:12 +00:00
|
|
|
fn isatty(filedesc: libc::c_int) -> libc::c_int;
|
|
|
|
}
|
|
|
|
|
2014-02-07 06:39:07 +00:00
|
|
|
static NAME: &'static str = "tty";
|
|
|
|
|
2014-06-08 07:56:37 +00:00
|
|
|
pub fn uumain(args: Vec<String>) -> int {
|
2014-01-10 08:23:12 +00:00
|
|
|
let options = [
|
2014-02-07 06:39:07 +00:00
|
|
|
optflag("s", "silent", "print nothing, only return an exit status")
|
2014-01-10 08:23:12 +00:00
|
|
|
];
|
|
|
|
|
2014-11-19 20:55:25 +00:00
|
|
|
let silent = match getopts(args.tail(), &options) {
|
2014-01-10 08:23:12 +00:00
|
|
|
Ok(m) => {
|
|
|
|
m.opt_present("s")
|
|
|
|
},
|
|
|
|
Err(f) => {
|
2014-06-15 10:50:40 +00:00
|
|
|
println!("{}", f);
|
2014-01-10 08:23:12 +00:00
|
|
|
usage();
|
2014-06-08 07:56:37 +00:00
|
|
|
return 2;
|
2014-01-10 08:23:12 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-08 20:59:07 +00:00
|
|
|
let tty = unsafe {
|
|
|
|
let ptr = ttyname(libc::STDIN_FILENO);
|
|
|
|
if !ptr.is_null() {
|
|
|
|
String::from_utf8_lossy(c_str_to_bytes(&ptr)).to_string()
|
|
|
|
} else {
|
|
|
|
"".to_string()
|
|
|
|
}
|
|
|
|
};
|
2014-01-10 08:23:12 +00:00
|
|
|
|
|
|
|
if !silent {
|
2015-01-01 11:14:28 +00:00
|
|
|
if !tty.as_slice().chars().all(|c| c.is_whitespace()) {
|
2014-05-23 12:28:40 +00:00
|
|
|
println(tty.as_slice());
|
2014-01-10 08:23:12 +00:00
|
|
|
} else {
|
2014-01-13 09:05:02 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-12 04:41:53 +00:00
|
|
|
exit_code as int
|
2014-01-10 08:23:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage () {
|
2014-11-22 06:27:54 +00:00
|
|
|
safe_writeln!(&mut stderr(), "usage: tty [-s]");
|
2014-01-10 08:23:12 +00:00
|
|
|
}
|