-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d321274
commit f0be827
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ PROGS := \ | |
wc \ | ||
whoami \ | ||
yes \ | ||
tty \ | ||
|
||
BUILD ?= $(PROGS) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#[crate_id(name="tty", version="1.0.0", author="Alan Andrade")]; | ||
|
||
|
||
/* | ||
* This file is part of the uutils coreutils package. | ||
* | ||
* (c) Jordi Boggiano <[email protected]> | ||
* | ||
* 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 mod extra; | ||
|
||
use std::{libc,str,os}; | ||
use std::io::stdio::stderr; | ||
use extra::getopts::{optflag,getopts}; | ||
|
||
extern { | ||
fn ttyname(filedesc: libc::c_int) -> *libc::c_char; | ||
fn isatty(filedesc: libc::c_int) -> libc::c_int; | ||
} | ||
|
||
fn main () { | ||
let args = os::args(); | ||
|
||
let options = [ | ||
optflag("s") | ||
]; | ||
|
||
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"); | ||
} | ||
} | ||
|
||
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 () { | ||
writeln!(&mut stderr() as &mut Writer, "usage: tty [-s]"); | ||
os::set_exit_status(2); | ||
} |