1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2026-01-16 10:11:01 +00:00
uutils-coreutils/users/users.rs
Michael Gehring 30bba07f9c always build multicall binary
squashed:
	a2c6b27 - build: automatically generate main() files
	c942f0f - remove MULTICALL=1 build from travis
	cb7b35b - make: remove unnecessary shell command
	69bbb31 - update README
	03a3168 - all: move main() into separate file that links against util crate
	8276384 - make: always build multicall binary
	aa4edeb - make: avoid 'rustc --crate-file-name'
2014-06-26 10:26:16 +02:00

118 lines
2.6 KiB
Rust

#![crate_id(name="users", vers="1.0.0", author="KokaKiwi")]
/*
* 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.
#![allow(dead_code, non_camel_case_types)]
#![feature(macro_rules, globs)]
extern crate getopts;
extern crate libc;
use std::io::print;
use std::mem;
use std::ptr;
use std::str;
use utmpx::*;
#[path = "../common/util.rs"]
mod util;
#[path = "../common/utmpx.rs"]
mod utmpx;
extern {
fn getutxent() -> *c_utmp;
fn getutxid(ut: *c_utmp) -> *c_utmp;
fn getutxline(ut: *c_utmp) -> *c_utmp;
fn pututxline(ut: *c_utmp) -> *c_utmp;
fn setutxent();
fn endutxent();
fn utmpxname(file: *libc::c_char) -> libc::c_int;
}
static NAME: &'static str = "users";
pub fn uumain(args: Vec<String>) -> int {
let program = args.get(0).as_slice();
let opts = [
getopts::optflag("h", "help", "display this help and exit"),
getopts::optflag("V", "version", "output version information and exit"),
];
let matches = match getopts::getopts(args.tail(), opts) {
Ok(m) => m,
Err(f) => fail!("{}", f),
};
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).as_slice());
return 0;
}
if matches.opt_present("version") {
println!("users 1.0.0");
return 0;
}
let mut filename = DEFAULT_FILE;
if matches.free.len() > 0 {
filename = matches.free.get(0).as_slice();
}
exec(filename);
0
}
fn exec(filename: &str) {
filename.with_c_str(|filename| {
unsafe {
utmpxname(filename);
}
});
let mut users = vec!();
unsafe {
setutxent();
loop {
let line = getutxent();
if line == ptr::null() {
break;
}
if (*line).ut_type == USER_PROCESS {
let user = str::raw::from_c_str(mem::transmute(&(*line).ut_user));
users.push(user);
}
}
endutxent();
}
if users.len() > 0 {
users.sort();
println!("{}", users.connect(" "));
}
}