1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 20:17:45 +00:00

Add conversion of UID to username for ls

This commit is contained in:
Steve Smith 2016-06-04 14:50:52 +01:00
parent 73fdc4824f
commit 6179b89bcc

View file

@ -19,9 +19,12 @@ extern crate uucore;
use getopts::Options;
use std::fs;
use std::fs::{ReadDir, DirEntry, FileType, Metadata};
use std::ffi::{OsString};
use std::ffi::{OsString,CStr};
use std::path::Path;
use std::io::Write;
use std::ptr;
use uucore::c_types::getpwuid;
#[derive(Copy, Clone, PartialEq)]
enum Mode {
@ -143,15 +146,36 @@ fn display_dir_entry(entry: DirEntry, options: &getopts::Matches) {
Ok(md) => md
};
println!(" {}{} {} somebody somegroup {: >9} {}",
println!(" {}{} {} {} {} {: >9} {}",
display_file_type(entry.file_type()),
display_permissions(&md),
display_symlink_count(&md),
display_uname(&md),
display_group(&md),
display_file_size(&md, options),
display_file_name(entry.file_name())
);
}
fn display_uname(metadata: &Metadata) -> String {
use std::os::unix::fs::MetadataExt;
let pw = unsafe { getpwuid(metadata.uid()) };
if !pw.is_null() {
let pw_name = unsafe {
String::from_utf8_lossy(CStr::from_ptr(ptr::read(pw).pw_name).to_bytes()).to_string()
};
pw_name
} else {
metadata.uid().to_string()
}
}
fn display_group(metadata: &Metadata) -> String {
use std::os::unix::fs::MetadataExt;
metadata.gid().to_string()
}
fn display_file_size(metadata: &Metadata, options: &getopts::Matches) -> String {
if options.opt_present("human-readable") {
convert(metadata.len() as f64)
@ -193,7 +217,6 @@ fn display_symlink_count(metadata: &Metadata) -> String {
#[cfg(target_family = "unix")]
fn display_symlink_count(metadata: &Metadata) -> String {
use std::os::unix::fs::MetadataExt;
metadata.nlink().to_string()
}