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

ls: fix target_family

This commit is contained in:
Knight 2016-06-07 20:11:40 +08:00
parent 0394d5398d
commit ca6831ef91

View file

@ -16,7 +16,9 @@ use pretty_bytes::converter::convert;
extern crate uucore; extern crate uucore;
extern crate libc; extern crate libc;
use self::libc::c_char; #[cfg(unix)]
use self::libc::{S_ISUID, S_ISGID, S_ISVTX, S_IRUSR, S_IWUSR, S_IXUSR, S_IRGRP, S_IWGRP, S_IXGRP,
S_IROTH, S_IWOTH, S_IXOTH, mode_t, c_char};
use getopts::Options; use getopts::Options;
use std::fs; use std::fs;
@ -26,6 +28,9 @@ use std::path::Path;
use std::io::Write; use std::io::Write;
use std::ptr; use std::ptr;
#[cfg(unix)]
use std::os::unix::fs::MetadataExt;
#[derive(Copy, Clone, PartialEq)] #[derive(Copy, Clone, PartialEq)]
enum Mode { enum Mode {
Help, Help,
@ -178,18 +183,16 @@ fn display_dir_entry(entry: DirEntry, options: &getopts::Matches) {
} }
fn cstr2string(cstr: *const c_char) -> String { fn cstr2string(cstr: *const c_char) -> String {
unsafe { String::from_utf8_lossy(CStr::from_ptr(cstr).to_bytes()).to_string() } unsafe { CStr::from_ptr(cstr).to_string_lossy().into_owned() }
} }
// 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(target_family = "linux")] #[cfg(unix)]
use uucore::c_types::{getpwuid, getgrgid}; use uucore::c_types::{getpwuid, getgrgid};
#[cfg(target_family = "linux")] #[cfg(unix)]
fn display_uname(metadata: &Metadata) -> String { fn display_uname(metadata: &Metadata) -> String {
use std::os::unix::fs::MetadataExt;
let pw = unsafe { getpwuid(metadata.uid()) }; let pw = unsafe { getpwuid(metadata.uid()) };
if !pw.is_null() { if !pw.is_null() {
cstr2string(unsafe { ptr::read(pw).pw_name }) cstr2string(unsafe { ptr::read(pw).pw_name })
@ -198,10 +201,8 @@ fn display_uname(metadata: &Metadata) -> String {
} }
} }
#[cfg(target_family = "linux")] #[cfg(unix)]
fn display_group(metadata: &Metadata) -> String { fn display_group(metadata: &Metadata) -> String {
use std::os::unix::fs::MetadataExt;
let ent = unsafe { getgrgid(metadata.gid()) }; let ent = unsafe { getgrgid(metadata.gid()) };
if !ent.is_null() { if !ent.is_null() {
cstr2string(unsafe { ptr::read(ent).gr_name }) cstr2string(unsafe { ptr::read(ent).gr_name })
@ -210,13 +211,13 @@ fn display_group(metadata: &Metadata) -> String {
} }
} }
#[cfg(not(target_family = "linux"))] #[cfg(not(unix))]
#[allow(unused_variables)] #[allow(unused_variables)]
fn display_uname(metadata: &Metadata) -> String { fn display_uname(metadata: &Metadata) -> String {
"somebody".to_string() "somebody".to_string()
} }
#[cfg(not(target_family = "linux"))] #[cfg(not(unix))]
#[allow(unused_variables)] #[allow(unused_variables)]
fn display_group(metadata: &Metadata) -> String { fn display_group(metadata: &Metadata) -> String {
"somegroup".to_string() "somegroup".to_string()
@ -262,7 +263,6 @@ fn display_symlink_count(metadata: &Metadata) -> String {
#[cfg(target_family = "unix")] #[cfg(target_family = "unix")]
fn display_symlink_count(metadata: &Metadata) -> String { fn display_symlink_count(metadata: &Metadata) -> String {
use std::os::unix::fs::MetadataExt;
metadata.nlink().to_string() metadata.nlink().to_string()
} }