2015-12-08 02:42:08 +00:00
|
|
|
#![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
|
|
|
|
2014-02-16 21:29:31 +00:00
|
|
|
extern crate getopts;
|
2014-04-26 05:03:08 +00:00
|
|
|
extern crate libc;
|
2014-01-16 01:51:43 +00:00
|
|
|
|
2015-11-24 01:00:51 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate uucore;
|
|
|
|
|
2015-05-21 18:42:42 +00:00
|
|
|
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;
|
2015-11-24 01:00:51 +00:00
|
|
|
use uucore::utmpx::*;
|
2014-01-16 01:51:43 +00:00
|
|
|
|
|
|
|
extern {
|
2014-06-30 11:13:38 +00:00
|
|
|
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
|
|
|
|
2014-06-30 11:13:38 +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"))]
|
2014-06-30 11:13:38 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-02-07 06:39:07 +00:00
|
|
|
static NAME: &'static str = "users";
|
2015-11-25 09:52:10 +00:00
|
|
|
static VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
2014-02-07 06:39:07 +00:00
|
|
|
|
2015-02-06 13:48:07 +00:00
|
|
|
pub fn uumain(args: Vec<String>) -> i32 {
|
2015-05-21 18:42:42 +00:00
|
|
|
let mut opts = Options::new();
|
2014-01-16 01:51:43 +00:00
|
|
|
|
2015-05-21 18:42:42 +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") {
|
2015-05-21 18:42:42 +00:00
|
|
|
println!("{} {}", NAME, VERSION);
|
2014-01-16 01:51:43 +00:00
|
|
|
println!("");
|
|
|
|
println!("Usage:");
|
2015-05-21 18:42:42 +00:00
|
|
|
println!(" {} [OPTION]... [FILE]", NAME);
|
2014-01-16 01:51:43 +00:00
|
|
|
println!("");
|
2015-05-21 18:42:42 +00:00
|
|
|
println!("{}", opts.usage("Output who is currently logged in according to FILE."));
|
2014-06-08 07:56:37 +00:00
|
|
|
return 0;
|
2014-01-16 01:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if matches.opt_present("version") {
|
2015-05-21 18:42:42 +00:00
|
|
|
println!("{} {}", NAME, VERSION);
|
2014-06-08 07:56:37 +00:00
|
|
|
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);
|
2014-06-08 07:56:37 +00:00
|
|
|
|
2014-06-12 04:41:53 +00:00
|
|
|
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
|
|
|
|
2014-04-26 05:03:08 +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() {
|
2014-01-16 02:00:32 +00:00
|
|
|
users.sort();
|
2015-08-26 16:50:07 +00:00
|
|
|
println!("{}", users.join(" "));
|
2014-01-16 02:00:32 +00:00
|
|
|
}
|
2014-01-16 01:51:43 +00:00
|
|
|
}
|