From ddf47fab313d3c37cbbe213e4c2b6fc22c20291f Mon Sep 17 00:00:00 2001 From: Knight Date: Sat, 20 Aug 2016 04:13:15 +0800 Subject: [PATCH] groups: use uucore::entries::get_groups instead --- src/groups/Cargo.toml | 8 +++--- src/groups/groups.rs | 59 +++++++++++++++---------------------------- 2 files changed, 25 insertions(+), 42 deletions(-) diff --git a/src/groups/Cargo.toml b/src/groups/Cargo.toml index c468e8a08..c5132f620 100644 --- a/src/groups/Cargo.toml +++ b/src/groups/Cargo.toml @@ -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" diff --git a/src/groups/groups.rs b/src/groups/groups.rs index 2ba06c1b4..f279eb1d3 100644 --- a/src/groups/groups.rs +++ b/src/groups/groups.rs @@ -1,52 +1,35 @@ #![crate_name = "uu_groups"] -/* - * This file is part of the uutils coreutils package. - * - * (c) Alan Andrade - * - * 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 +// (c) Jian Zeng +// +// 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) -> 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::>().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::>().join(" ")); + } else { + crash!(1, "unknown user {}", matches.free[0]); + } } 0