coreutils/src/users/users.rs

126 lines
2.7 KiB
Rust
Raw Normal View History

2014-07-06 08:13:36 +00:00
#![crate_name = "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.
2014-03-31 16:40:21 +00:00
#![allow(dead_code, non_camel_case_types)]
2014-01-16 01:51:43 +00:00
2014-03-31 16:40:21 +00:00
#![feature(macro_rules, globs)]
extern crate getopts;
extern crate libc;
2014-01-16 01:51:43 +00:00
use std::io::print;
2014-05-16 09:03:19 +00:00
use std::mem;
2014-01-16 01:51:43 +00:00
use std::ptr;
2014-07-29 06:52:58 +00:00
use std::string;
2014-02-08 04:02:11 +00:00
use utmpx::*;
2014-02-23 22:17:48 +00:00
#[path = "../common/util.rs"]
mod util;
2014-01-16 01:51:43 +00:00
#[path = "../common/utmpx.rs"]
mod 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-09-23 13:40:31 +00:00
#[cfg(target_os = "linux")]
#[cfg(target_os = "macos")]
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";
pub fn uumain(args: Vec<String>) -> int {
2014-07-20 01:13:55 +00:00
let program = args[0].as_slice();
2014-05-30 08:35:54 +00:00
let opts = [
getopts::optflag("h", "help", "display this help and exit"),
getopts::optflag("V", "version", "output version information and exit"),
2014-01-16 01:51:43 +00:00
];
let matches = match getopts::getopts(args.tail(), opts) {
2014-01-16 01:51:43 +00:00
Ok(m) => m,
2014-06-15 10:50:40 +00:00
Err(f) => fail!("{}", f),
2014-01-16 01:51:43 +00:00
};
if matches.opt_present("help") {
println!("users 1.0.0");
println!("");
println!("Usage:");
println!(" {:s} [OPTION]... [FILE]", program);
println!("");
2014-05-17 10:32:14 +00:00
print(getopts::usage("Output who is currently logged in according to FILE.", opts).as_slice());
return 0;
2014-01-16 01:51:43 +00:00
}
if matches.opt_present("version") {
println!("users 1.0.0");
return 0;
2014-01-16 01:51:43 +00:00
}
2014-01-16 01:59:05 +00:00
let mut filename = DEFAULT_FILE;
2014-01-16 01:51:43 +00:00
if matches.free.len() > 0 {
2014-07-20 01:13:55 +00:00
filename = matches.free[0].as_slice();
2014-01-16 01:51:43 +00:00
}
exec(filename);
0
2014-01-16 01:51:43 +00:00
}
fn exec(filename: &str) {
filename.with_c_str(|filename| {
unsafe {
2014-02-08 04:02:11 +00:00
utmpxname(filename);
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 {
2014-07-29 06:52:58 +00:00
let user = string::raw::from_buf(mem::transmute(&(*line).ut_user));
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
}
if users.len() > 0 {
users.sort();
println!("{}", users.connect(" "));
}
2014-01-16 01:51:43 +00:00
}