1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

ls: implement -i option

This commit is contained in:
Sunrin SHIMURA (keen) 2016-12-25 14:32:12 +09:00
parent 62d5a6bbc8
commit 8a51ddf6fb
2 changed files with 34 additions and 1 deletions

View file

@ -107,6 +107,9 @@ pub fn uumain(args: Vec<String>) -> i32 {
.optflag("h", .optflag("h",
"human-readable", "human-readable",
"Print human readable file sizes (e.g. 1K 234M 56G).") "Print human readable file sizes (e.g. 1K 234M 56G).")
.optflag("i",
"inode",
"print the index number of each file")
.optflag("L", .optflag("L",
"dereference", "dereference",
"When showing file information for a symbolic link, show information for the \ "When showing file information for a symbolic link, show information for the \
@ -353,7 +356,8 @@ fn display_item_long(item: &PathBuf,
Ok(md) => md, Ok(md) => md,
}; };
println!("{}{} {} {} {} {} {} {}", println!("{}{}{} {} {} {} {} {} {}",
get_inode(&md, options),
display_file_type(md.file_type()), display_file_type(md.file_type()),
display_permissions(&md), display_permissions(&md),
pad_left(display_symlink_count(&md), max_links), pad_left(display_symlink_count(&md), max_links),
@ -364,6 +368,21 @@ fn display_item_long(item: &PathBuf,
display_file_name(&item, strip, &md, options).contents); display_file_name(&item, strip, &md, options).contents);
} }
#[cfg(unix)]
fn get_inode(metadata: &Metadata, options: &getopts::Matches) -> String {
if options.opt_present("inode") {
format!("{:8} ", metadata.ino())
} else {
"".to_string()
}
}
#[cfg(not(unix))]
fn get_inode(_metadata: &Metadata, _options: &getopts::Matches) -> Option<String> {
"".to_string()
}
// Currently getpwuid is `linux` target only. If it's broken out into // Currently getpwuid is `linux` target only. If it's broken out into
// a posix-compliant attribute this can be updated... // a posix-compliant attribute this can be updated...
#[cfg(unix)] #[cfg(unix)]
@ -453,6 +472,11 @@ fn display_file_name(path: &Path,
options: &getopts::Matches) options: &getopts::Matches)
-> Cell { -> Cell {
let mut name = get_file_name(path, strip); let mut name = get_file_name(path, strip);
if ! options.opt_present("long") {
name = get_inode(metadata, options) + &name;
}
if options.opt_present("classify") { if options.opt_present("classify") {
let file_type = metadata.file_type(); let file_type = metadata.file_type();
if file_type.is_dir() { if file_type.is_dir() {
@ -512,6 +536,9 @@ fn display_file_name(path: &Path,
options: &getopts::Matches) options: &getopts::Matches)
-> Cell { -> Cell {
let mut name = get_file_name(path, strip); let mut name = get_file_name(path, strip);
if ! options.opt_present("long") {
name = get_inode(metadata, options) + &name;
}
let mut width = UnicodeWidthStr::width(&*name); let mut width = UnicodeWidthStr::width(&*name);
let color = options.opt_present("color"); let color = options.opt_present("color");

View file

@ -5,3 +5,9 @@ use common::util::*;
fn test_ls_ls() { fn test_ls_ls() {
new_ucmd!().succeeds(); new_ucmd!().succeeds();
} }
#[test]
fn test_ls_ls_i() {
new_ucmd!().arg("-i").succeeds();
new_ucmd!().arg("-il").succeeds();
}