coreutils/users/users.rs

119 lines
2.6 KiB
Rust
Raw Normal View History

2014-03-31 16:40:21 +00:00
#![crate_id(name="users", vers="1.0.0", author="KokaKiwi")]
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;
2014-01-16 01:51:43 +00:00
use std::io::print;
use std::cast;
use std::libc;
use std::os;
use std::ptr;
use std::str;
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 {
2014-02-08 04:02:11 +00:00
fn getutxent() -> *c_utmp;
fn getutxid(ut: *c_utmp) -> *c_utmp;
fn getutxline(ut: *c_utmp) -> *c_utmp;
2014-01-16 01:51:43 +00:00
2014-02-08 04:02:11 +00:00
fn pututxline(ut: *c_utmp) -> *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-02-08 04:02:11 +00:00
fn utmpxname(file: *libc::c_char) -> libc::c_int;
2014-01-16 01:51:43 +00:00
}
static NAME: &'static str = "users";
2014-01-16 01:51:43 +00:00
fn main() {
let args = os::args();
let program = args[0].as_slice();
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,
Err(f) => fail!(f.to_err_msg()),
};
if matches.opt_present("help") {
println!("users 1.0.0");
println!("");
println!("Usage:");
println!(" {:s} [OPTION]... [FILE]", program);
println!("");
print(getopts::usage("Output who is currently logged in according to FILE.", opts));
2014-01-16 01:51:43 +00:00
return;
}
if matches.opt_present("version") {
println!("users 1.0.0");
return;
}
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-03-22 08:18:52 +00:00
filename = matches.free.get(0).as_slice();
2014-01-16 01:51:43 +00:00
}
exec(filename);
}
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: ~[~str] = ~[];
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 {
let user = str::raw::from_c_str(cast::transmute(&(*line).ut_user));
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
}