1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

groups: use uucore::entries::get_groups instead

This commit is contained in:
Knight 2016-08-20 04:13:15 +08:00
parent f49ee5b58b
commit ddf47fab31
2 changed files with 25 additions and 42 deletions

View file

@ -7,10 +7,10 @@ authors = []
name = "uu_groups"
path = "groups.rs"
[dependencies]
getopts = "*"
libc = "*"
uucore = { path="../uucore" }
[dependencies.uucore]
path = "../uucore"
default-features = false
features = ["entries"]
[[bin]]
name = "groups"

View file

@ -1,52 +1,35 @@
#![crate_name = "uu_groups"]
/*
* This file is part of the uutils coreutils package.
*
* (c) Alan Andrade <alan.andradec@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
extern crate getopts;
// This file is part of the uutils coreutils package.
//
// (c) Alan Andrade <alan.andradec@gmail.com>
// (c) Jian Zeng <anonymousknight96 AT gmail.com>
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
//
//
#[macro_use]
extern crate uucore;
use uucore::entries::{Passwd, Locate, get_groups, gid2grp};
use std::io::Write;
use uucore::c_types::{get_pw_from_args, group};
static NAME: &'static str = "groups";
static VERSION: &'static str = env!("CARGO_PKG_VERSION");
static SYNTAX: &'static str = "[user]";
static SUMMARY: &'static str = "display current group names";
pub fn uumain(args: Vec<String>) -> i32 {
let mut opts = getopts::Options::new();
opts.optflag("h", "help", "display this help menu and exit");
opts.optflag("V", "version", "display version information and exit");
let mut opts = new_coreopts!(SYNTAX, SUMMARY, "");
let matches = opts.parse(args);
let matches = match opts.parse(&args[1..]) {
Ok(m) => { m },
Err(f) => {
show_error!("{}", f);
return 1;
}
};
if matches.opt_present("version") {
println!("{} {}", NAME, VERSION);
} else if matches.opt_present("help") {
let msg = format!("{0} {1}
Usage:
{0} [OPTION]... [USER]...
Prints the groups a user is in to standard output.", NAME, VERSION);
print!("{}", opts.usage(&msg));
if matches.free.is_empty() {
println!("{}", get_groups().unwrap().iter().map(|&g| gid2grp(g).unwrap()).collect::<Vec<_>>().join(" "));
} else {
group(get_pw_from_args(&matches.free), true);
if let Ok(p) = Passwd::locate(matches.free[0].as_str()) {
println!("{}", p.belongs_to().iter().map(|&g| gid2grp(g).unwrap()).collect::<Vec<_>>().join(" "));
} else {
crash!(1, "unknown user {}", matches.free[0]);
}
}
0