mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
Merge pull request #2379 from youknowone/rotate
get_groups_gnu sort with rotate_right
This commit is contained in:
commit
3e8c009a50
1 changed files with 19 additions and 7 deletions
|
@ -96,15 +96,18 @@ pub fn get_groups() -> IOResult<Vec<gid_t>> {
|
||||||
/// the result.)
|
/// the result.)
|
||||||
#[cfg(all(unix, feature = "process"))]
|
#[cfg(all(unix, feature = "process"))]
|
||||||
pub fn get_groups_gnu(arg_id: Option<u32>) -> IOResult<Vec<gid_t>> {
|
pub fn get_groups_gnu(arg_id: Option<u32>) -> IOResult<Vec<gid_t>> {
|
||||||
let mut groups = get_groups()?;
|
let groups = get_groups()?;
|
||||||
let egid = arg_id.unwrap_or_else(crate::features::process::getegid);
|
let egid = arg_id.unwrap_or_else(crate::features::process::getegid);
|
||||||
if !groups.is_empty() && *groups.first().unwrap() == egid {
|
Ok(sort_groups(groups, egid))
|
||||||
return Ok(groups);
|
}
|
||||||
} else if let Some(index) = groups.iter().position(|&x| x == egid) {
|
|
||||||
groups.remove(index);
|
fn sort_groups(mut groups: Vec<gid_t>, egid: gid_t) -> Vec<gid_t> {
|
||||||
|
if let Some(index) = groups.iter().position(|&x| x == egid) {
|
||||||
|
groups[..=index].rotate_right(1);
|
||||||
|
} else {
|
||||||
|
groups.insert(0, egid);
|
||||||
}
|
}
|
||||||
groups.insert(0, egid);
|
groups
|
||||||
Ok(groups)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
|
@ -309,6 +312,15 @@ pub fn grp2gid(name: &str) -> IOResult<gid_t> {
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_sort_groups() {
|
||||||
|
assert_eq!(sort_groups(vec![1, 2, 3], 4), vec![4, 1, 2, 3]);
|
||||||
|
assert_eq!(sort_groups(vec![1, 2, 3], 3), vec![3, 1, 2]);
|
||||||
|
assert_eq!(sort_groups(vec![1, 2, 3], 2), vec![2, 1, 3]);
|
||||||
|
assert_eq!(sort_groups(vec![1, 2, 3], 1), vec![1, 2, 3]);
|
||||||
|
assert_eq!(sort_groups(vec![1, 2, 3], 0), vec![0, 1, 2, 3]);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_entries_get_groups_gnu() {
|
fn test_entries_get_groups_gnu() {
|
||||||
if let Ok(mut groups) = get_groups() {
|
if let Ok(mut groups) = get_groups() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue