mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
users: use uucore::utmpx
This commit is contained in:
parent
f4c50921d8
commit
963ab9d321
2 changed files with 12 additions and 31 deletions
|
@ -9,8 +9,11 @@ path = "users.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
getopts = "*"
|
getopts = "*"
|
||||||
libc = "*"
|
|
||||||
uucore = { path="../uucore" }
|
[dependencies.uucore]
|
||||||
|
default-features = false
|
||||||
|
features = ["utmpx"]
|
||||||
|
path = "../uucore"
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "users"
|
name = "users"
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
* This file is part of the uutils coreutils package.
|
* This file is part of the uutils coreutils package.
|
||||||
*
|
*
|
||||||
* (c) KokaKiwi <kokakiwi@kokakiwi.net>
|
* (c) KokaKiwi <kokakiwi@kokakiwi.net>
|
||||||
|
* (c) Jian Zeng <anonymousknight86@gmail.com>
|
||||||
*
|
*
|
||||||
* For the full copyright and license information, please view the LICENSE
|
* For the full copyright and license information, please view the LICENSE
|
||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
|
@ -15,16 +16,12 @@
|
||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
|
|
||||||
extern crate getopts;
|
extern crate getopts;
|
||||||
extern crate libc;
|
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate uucore;
|
extern crate uucore;
|
||||||
|
use uucore::utmpx::*;
|
||||||
|
|
||||||
use getopts::Options;
|
use getopts::Options;
|
||||||
use std::ffi::{CStr, CString};
|
|
||||||
use std::mem;
|
|
||||||
use std::ptr;
|
|
||||||
use uucore::utmpx::*;
|
|
||||||
|
|
||||||
static NAME: &'static str = "users";
|
static NAME: &'static str = "users";
|
||||||
static VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
static VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
||||||
|
@ -67,30 +64,11 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn exec(filename: &str) {
|
fn exec(filename: &str) {
|
||||||
unsafe {
|
let mut users = Utmpx::iter_all_records()
|
||||||
utmpxname(CString::new(filename).unwrap().as_ptr());
|
.read_from(filename)
|
||||||
}
|
.filter(|ut| ut.is_user_process())
|
||||||
|
.map(|ut| ut.user().into_owned())
|
||||||
let mut users = vec!();
|
.collect::<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();
|
|
||||||
}
|
|
||||||
|
|
||||||
if !users.is_empty() {
|
if !users.is_empty() {
|
||||||
users.sort();
|
users.sort();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue