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:
parent
f49ee5b58b
commit
ddf47fab31
2 changed files with 25 additions and 42 deletions
|
@ -7,10 +7,10 @@ authors = []
|
||||||
name = "uu_groups"
|
name = "uu_groups"
|
||||||
path = "groups.rs"
|
path = "groups.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies.uucore]
|
||||||
getopts = "*"
|
path = "../uucore"
|
||||||
libc = "*"
|
default-features = false
|
||||||
uucore = { path="../uucore" }
|
features = ["entries"]
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "groups"
|
name = "groups"
|
||||||
|
|
|
@ -1,52 +1,35 @@
|
||||||
#![crate_name = "uu_groups"]
|
#![crate_name = "uu_groups"]
|
||||||
|
|
||||||
/*
|
// This file is part of the uutils coreutils package.
|
||||||
* This file is part of the uutils coreutils package.
|
//
|
||||||
*
|
// (c) Alan Andrade <alan.andradec@gmail.com>
|
||||||
* (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
|
// For the full copyright and license information, please view the LICENSE
|
||||||
* file that was distributed with this source code.
|
// file that was distributed with this source code.
|
||||||
*
|
//
|
||||||
*/
|
//
|
||||||
|
|
||||||
extern crate getopts;
|
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate uucore;
|
extern crate uucore;
|
||||||
|
use uucore::entries::{Passwd, Locate, get_groups, gid2grp};
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use uucore::c_types::{get_pw_from_args, group};
|
|
||||||
|
|
||||||
static NAME: &'static str = "groups";
|
static SYNTAX: &'static str = "[user]";
|
||||||
static VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
static SUMMARY: &'static str = "display current group names";
|
||||||
|
|
||||||
pub fn uumain(args: Vec<String>) -> i32 {
|
pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
let mut opts = getopts::Options::new();
|
let mut opts = new_coreopts!(SYNTAX, SUMMARY, "");
|
||||||
opts.optflag("h", "help", "display this help menu and exit");
|
let matches = opts.parse(args);
|
||||||
opts.optflag("V", "version", "display version information and exit");
|
|
||||||
|
|
||||||
let matches = match opts.parse(&args[1..]) {
|
if matches.free.is_empty() {
|
||||||
Ok(m) => { m },
|
println!("{}", get_groups().unwrap().iter().map(|&g| gid2grp(g).unwrap()).collect::<Vec<_>>().join(" "));
|
||||||
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));
|
|
||||||
} else {
|
} 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
|
0
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue