coreutils/src/users/users.rs

119 lines
2.7 KiB
Rust
Raw Normal View History

#![crate_name = "uu_users"]
2014-01-16 01:51:43 +00:00
/*
* This file is part of the uutils coreutils package.
*
* (c) KokaKiwi <kokakiwi@kokakiwi.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/* last synced with: whoami (GNU coreutils) 8.22 */
// Allow dead code here in order to keep all fields, constants here, for consistency.
2015-04-27 21:38:39 +00:00
#![allow(dead_code)]
2014-01-16 01:51:43 +00:00
extern crate getopts;
extern crate libc;
2014-01-16 01:51:43 +00:00
#[macro_use]
extern crate uucore;
use getopts::Options;
2015-02-22 12:58:57 +00:00
use std::ffi::{CStr, CString};
2014-05-16 09:03:19 +00:00
use std::mem;
2014-01-16 01:51:43 +00:00
use std::ptr;
use uucore::utmpx::*;
2014-01-16 01:51:43 +00:00
extern {
fn getutxent() -> *const c_utmp;
fn getutxid(ut: *const c_utmp) -> *const c_utmp;
fn getutxline(ut: *const c_utmp) -> *const c_utmp;
2014-01-16 01:51:43 +00:00
fn pututxline(ut: *const c_utmp) -> *const c_utmp;
2014-01-16 01:51:43 +00:00
2014-02-08 04:02:11 +00:00
fn setutxent();
fn endutxent();
2014-01-16 01:51:43 +00:00
2014-10-02 17:55:06 +00:00
#[cfg(any(target_os = "macos", target_os = "linux"))]
fn utmpxname(file: *const libc::c_char) -> libc::c_int;
2014-01-16 01:51:43 +00:00
}
2014-09-23 13:40:31 +00:00
#[cfg(target_os = "freebsd")]
unsafe extern fn utmpxname(_file: *const libc::c_char) -> libc::c_int {
0
}
static NAME: &'static str = "users";
2015-11-25 09:52:10 +00:00
static VERSION: &'static str = env!("CARGO_PKG_VERSION");
pub fn uumain(args: Vec<String>) -> i32 {
let mut opts = Options::new();
2014-01-16 01:51:43 +00:00
opts.optflag("h", "help", "display this help and exit");
opts.optflag("V", "version", "output version information and exit");
let matches = match opts.parse(&args[1..]) {
2014-01-16 01:51:43 +00:00
Ok(m) => m,
2014-10-30 09:06:47 +00:00
Err(f) => panic!("{}", f),
2014-01-16 01:51:43 +00:00
};
if matches.opt_present("help") {
println!("{} {}", NAME, VERSION);
2014-01-16 01:51:43 +00:00
println!("");
println!("Usage:");
println!(" {} [OPTION]... [FILE]", NAME);
2014-01-16 01:51:43 +00:00
println!("");
println!("{}", opts.usage("Output who is currently logged in according to FILE."));
return 0;
2014-01-16 01:51:43 +00:00
}
if matches.opt_present("version") {
println!("{} {}", NAME, VERSION);
return 0;
2014-01-16 01:51:43 +00:00
}
2016-01-05 19:42:52 +00:00
let filename = if !matches.free.is_empty() {
2015-04-27 21:38:39 +00:00
matches.free[0].as_ref()
} else {
DEFAULT_FILE
};
2014-01-16 01:51:43 +00:00
exec(filename);
0
2014-01-16 01:51:43 +00:00
}
fn exec(filename: &str) {
2015-01-10 16:55:29 +00:00
unsafe {
2015-02-22 12:22:51 +00:00
utmpxname(CString::new(filename).unwrap().as_ptr());
2015-01-10 16:55:29 +00:00
}
2014-01-16 01:51:43 +00:00
let mut users = vec!();
2014-01-16 01:51:43 +00:00
unsafe {
2014-02-08 04:02:11 +00:00
setutxent();
2014-01-16 01:51:43 +00:00
loop {
2014-02-08 04:02:11 +00:00
let line = getutxent();
2014-01-16 01:51:43 +00:00
if line == ptr::null() {
break;
}
if (*line).ut_type == USER_PROCESS {
2015-02-22 12:58:57 +00:00
let user = String::from_utf8_lossy(CStr::from_ptr(mem::transmute(&(*line).ut_user)).to_bytes()).to_string();
2014-01-16 01:51:43 +00:00
users.push(user);
}
}
2014-02-08 04:02:11 +00:00
endutxent();
2014-01-16 01:51:43 +00:00
}
2016-01-05 19:42:52 +00:00
if !users.is_empty() {
users.sort();
println!("{}", users.join(" "));
}
2014-01-16 01:51:43 +00:00
}