From 270290efe6497b0f0be8e7b3b09dd600a6154a93 Mon Sep 17 00:00:00 2001 From: Knight Date: Sun, 14 Aug 2016 13:53:42 +0800 Subject: [PATCH] whoami: use uucore::entries --- src/whoami/Cargo.toml | 7 +++++-- src/whoami/platform/unix.rs | 23 +++++------------------ src/whoami/whoami.rs | 1 - 3 files changed, 10 insertions(+), 21 deletions(-) diff --git a/src/whoami/Cargo.toml b/src/whoami/Cargo.toml index af40ed7ed..a84dddfa9 100644 --- a/src/whoami/Cargo.toml +++ b/src/whoami/Cargo.toml @@ -9,10 +9,13 @@ path = "whoami.rs" [dependencies] getopts = "*" -libc = "*" winapi = "*" advapi32-sys = "*" -uucore = { path="../uucore" } + +[dependencies.uucore] +path = "../uucore" +default-features = false +features = ["entries"] [[bin]] name = "whoami" diff --git a/src/whoami/platform/unix.rs b/src/whoami/platform/unix.rs index f4db2e53e..ab7c42ff6 100644 --- a/src/whoami/platform/unix.rs +++ b/src/whoami/platform/unix.rs @@ -2,31 +2,18 @@ * This file is part of the uutils coreutils package. * * (c) Jordi Boggiano + * (c) Jian Zeng * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ -use std::io::{Result, Error}; -use ::libc; -use uucore::c_types::{c_passwd, getpwuid}; - -extern { - pub fn geteuid() -> libc::uid_t; -} +use std::io::Result; +use uucore::libc::geteuid; +use uucore::entries::uid2usr; pub unsafe fn getusername() -> Result { // Get effective user id let uid = geteuid(); - - // Try to find username for uid - let passwd: *const c_passwd = getpwuid(uid); - if passwd.is_null() { - return Err(Error::last_os_error()) - } - - // Extract username from passwd struct - let pw_name: *const libc::c_char = (*passwd).pw_name; - let username = String::from_utf8_lossy(::std::ffi::CStr::from_ptr(pw_name).to_bytes()).to_string(); - Ok(username) + uid2usr(uid) } diff --git a/src/whoami/whoami.rs b/src/whoami/whoami.rs index e7d01ba15..7a69bbbd0 100644 --- a/src/whoami/whoami.rs +++ b/src/whoami/whoami.rs @@ -12,7 +12,6 @@ /* last synced with: whoami (GNU coreutils) 8.21 */ extern crate getopts; -extern crate libc; #[macro_use] extern crate uucore;