From 963ab9d32159c49710689b8b5d2b282c4e096b2f Mon Sep 17 00:00:00 2001 From: Knight Date: Wed, 10 Aug 2016 15:24:55 +0800 Subject: [PATCH] users: use uucore::utmpx --- src/users/Cargo.toml | 7 +++++-- src/users/users.rs | 36 +++++++----------------------------- 2 files changed, 12 insertions(+), 31 deletions(-) diff --git a/src/users/Cargo.toml b/src/users/Cargo.toml index adfa1dac4..88fe009ea 100644 --- a/src/users/Cargo.toml +++ b/src/users/Cargo.toml @@ -9,8 +9,11 @@ path = "users.rs" [dependencies] getopts = "*" -libc = "*" -uucore = { path="../uucore" } + +[dependencies.uucore] +default-features = false +features = ["utmpx"] +path = "../uucore" [[bin]] name = "users" diff --git a/src/users/users.rs b/src/users/users.rs index d4394de47..52e6f2f8d 100644 --- a/src/users/users.rs +++ b/src/users/users.rs @@ -4,6 +4,7 @@ * This file is part of the uutils coreutils package. * * (c) KokaKiwi + * (c) Jian Zeng * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -15,16 +16,12 @@ #![allow(dead_code)] extern crate getopts; -extern crate libc; #[macro_use] extern crate uucore; +use uucore::utmpx::*; use getopts::Options; -use std::ffi::{CStr, CString}; -use std::mem; -use std::ptr; -use uucore::utmpx::*; static NAME: &'static str = "users"; static VERSION: &'static str = env!("CARGO_PKG_VERSION"); @@ -67,30 +64,11 @@ pub fn uumain(args: Vec) -> i32 { } fn exec(filename: &str) { - unsafe { - utmpxname(CString::new(filename).unwrap().as_ptr()); - } - - let mut users = vec!(); - - unsafe { - setutxent(); - - loop { - let line = getutxent(); - - if line == ptr::null() { - break; - } - - if (*line).ut_type == USER_PROCESS { - let user = String::from_utf8_lossy(CStr::from_ptr(mem::transmute(&(*line).ut_user)).to_bytes()).to_string(); - users.push(user); - } - } - - endutxent(); - } + let mut users = Utmpx::iter_all_records() + .read_from(filename) + .filter(|ut| ut.is_user_process()) + .map(|ut| ut.user().into_owned()) + .collect::>(); if !users.is_empty() { users.sort();