mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +00:00
id: use UResult
This commit is contained in:
parent
d967a7a553
commit
0f39e7c101
1 changed files with 35 additions and 22 deletions
|
@ -44,6 +44,8 @@ use clap::{crate_version, App, Arg};
|
||||||
use selinux;
|
use selinux;
|
||||||
use std::ffi::CStr;
|
use std::ffi::CStr;
|
||||||
use uucore::entries::{self, Group, Locate, Passwd};
|
use uucore::entries::{self, Group, Locate, Passwd};
|
||||||
|
use uucore::error::UResult;
|
||||||
|
use uucore::error::{set_exit_code, USimpleError};
|
||||||
pub use uucore::libc;
|
pub use uucore::libc;
|
||||||
use uucore::libc::{getlogin, uid_t};
|
use uucore::libc::{getlogin, uid_t};
|
||||||
use uucore::process::{getegid, geteuid, getgid, getuid};
|
use uucore::process::{getegid, geteuid, getgid, getuid};
|
||||||
|
@ -123,10 +125,10 @@ struct State {
|
||||||
// 1000 10 968 975
|
// 1000 10 968 975
|
||||||
// +++ exited with 0 +++
|
// +++ exited with 0 +++
|
||||||
user_specified: bool,
|
user_specified: bool,
|
||||||
exit_code: i32,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uumain(args: impl uucore::Args) -> i32 {
|
#[uucore_procs::gen_uumain]
|
||||||
|
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
let usage = get_usage();
|
let usage = get_usage();
|
||||||
let after_help = get_description();
|
let after_help = get_description();
|
||||||
|
|
||||||
|
@ -161,7 +163,6 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
},
|
},
|
||||||
user_specified: !users.is_empty(),
|
user_specified: !users.is_empty(),
|
||||||
ids: None,
|
ids: None,
|
||||||
exit_code: 0,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let default_format = {
|
let default_format = {
|
||||||
|
@ -170,14 +171,23 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
};
|
};
|
||||||
|
|
||||||
if (state.nflag || state.rflag) && default_format && !state.cflag {
|
if (state.nflag || state.rflag) && default_format && !state.cflag {
|
||||||
crash!(1, "cannot print only names or real IDs in default format");
|
return Err(USimpleError::new(
|
||||||
|
1,
|
||||||
|
"cannot print only names or real IDs in default format".to_string(),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
if state.zflag && default_format && !state.cflag {
|
if state.zflag && default_format && !state.cflag {
|
||||||
// NOTE: GNU test suite "id/zero.sh" needs this stderr output:
|
// NOTE: GNU test suite "id/zero.sh" needs this stderr output:
|
||||||
crash!(1, "option --zero not permitted in default format");
|
return Err(USimpleError::new(
|
||||||
|
1,
|
||||||
|
"option --zero not permitted in default format".to_string(),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
if state.user_specified && state.cflag {
|
if state.user_specified && state.cflag {
|
||||||
crash!(1, "cannot print security context when user specified");
|
return Err(USimpleError::new(
|
||||||
|
1,
|
||||||
|
"cannot print security context when user specified".to_string(),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let delimiter = {
|
let delimiter = {
|
||||||
|
@ -204,11 +214,14 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
print!("{}{}", String::from_utf8_lossy(bytes), line_ending);
|
print!("{}{}", String::from_utf8_lossy(bytes), line_ending);
|
||||||
} else {
|
} else {
|
||||||
// print error because `cflag` was explicitly requested
|
// print error because `cflag` was explicitly requested
|
||||||
crash!(1, "can't get process context");
|
return Err(USimpleError::new(1, "can't get process context"));
|
||||||
}
|
}
|
||||||
return state.exit_code;
|
return Ok(());
|
||||||
} else {
|
} else {
|
||||||
crash!(1, "--context (-Z) works only on an SELinux-enabled kernel");
|
return Err(USimpleError::new(
|
||||||
|
1,
|
||||||
|
"--context (-Z) works only on an SELinux-enabled kernel".to_string(),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -220,7 +233,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
Ok(p) => Some(p),
|
Ok(p) => Some(p),
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
show_error!("'{}': no such user", users[i]);
|
show_error!("'{}': no such user", users[i]);
|
||||||
state.exit_code = 1;
|
set_exit_code(1);
|
||||||
if i + 1 >= users.len() {
|
if i + 1 >= users.len() {
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
|
@ -234,17 +247,17 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
if matches.is_present(options::OPT_PASSWORD) {
|
if matches.is_present(options::OPT_PASSWORD) {
|
||||||
// BSD's `id` ignores all but the first specified user
|
// BSD's `id` ignores all but the first specified user
|
||||||
pline(possible_pw.map(|v| v.uid()));
|
pline(possible_pw.map(|v| v.uid()));
|
||||||
return state.exit_code;
|
return Ok(());
|
||||||
};
|
};
|
||||||
if matches.is_present(options::OPT_HUMAN_READABLE) {
|
if matches.is_present(options::OPT_HUMAN_READABLE) {
|
||||||
// BSD's `id` ignores all but the first specified user
|
// BSD's `id` ignores all but the first specified user
|
||||||
pretty(possible_pw);
|
pretty(possible_pw);
|
||||||
return state.exit_code;
|
return Ok(());
|
||||||
}
|
}
|
||||||
if matches.is_present(options::OPT_AUDIT) {
|
if matches.is_present(options::OPT_AUDIT) {
|
||||||
// BSD's `id` ignores specified users
|
// BSD's `id` ignores specified users
|
||||||
auditid();
|
auditid();
|
||||||
return state.exit_code;
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let (uid, gid) = possible_pw.map(|p| (p.uid(), p.gid())).unwrap_or((
|
let (uid, gid) = possible_pw.map(|p| (p.uid(), p.gid())).unwrap_or((
|
||||||
|
@ -264,7 +277,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
if state.nflag {
|
if state.nflag {
|
||||||
entries::gid2grp(gid).unwrap_or_else(|_| {
|
entries::gid2grp(gid).unwrap_or_else(|_| {
|
||||||
show_error!("cannot find name for group ID {}", gid);
|
show_error!("cannot find name for group ID {}", gid);
|
||||||
state.exit_code = 1;
|
set_exit_code(1);
|
||||||
gid.to_string()
|
gid.to_string()
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
@ -279,7 +292,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
if state.nflag {
|
if state.nflag {
|
||||||
entries::uid2usr(uid).unwrap_or_else(|_| {
|
entries::uid2usr(uid).unwrap_or_else(|_| {
|
||||||
show_error!("cannot find name for user ID {}", uid);
|
show_error!("cannot find name for user ID {}", uid);
|
||||||
state.exit_code = 1;
|
set_exit_code(1);
|
||||||
uid.to_string()
|
uid.to_string()
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
@ -304,7 +317,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
if state.nflag {
|
if state.nflag {
|
||||||
entries::gid2grp(id).unwrap_or_else(|_| {
|
entries::gid2grp(id).unwrap_or_else(|_| {
|
||||||
show_error!("cannot find name for group ID {}", id);
|
show_error!("cannot find name for group ID {}", id);
|
||||||
state.exit_code = 1;
|
set_exit_code(1);
|
||||||
id.to_string()
|
id.to_string()
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
@ -332,7 +345,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
state.exit_code
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uu_app() -> App<'static, 'static> {
|
pub fn uu_app() -> App<'static, 'static> {
|
||||||
|
@ -560,7 +573,7 @@ fn id_print(state: &mut State, groups: Vec<u32>) {
|
||||||
uid,
|
uid,
|
||||||
entries::uid2usr(uid).unwrap_or_else(|_| {
|
entries::uid2usr(uid).unwrap_or_else(|_| {
|
||||||
show_error!("cannot find name for user ID {}", uid);
|
show_error!("cannot find name for user ID {}", uid);
|
||||||
state.exit_code = 1;
|
set_exit_code(1);
|
||||||
uid.to_string()
|
uid.to_string()
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
@ -569,7 +582,7 @@ fn id_print(state: &mut State, groups: Vec<u32>) {
|
||||||
gid,
|
gid,
|
||||||
entries::gid2grp(gid).unwrap_or_else(|_| {
|
entries::gid2grp(gid).unwrap_or_else(|_| {
|
||||||
show_error!("cannot find name for group ID {}", gid);
|
show_error!("cannot find name for group ID {}", gid);
|
||||||
state.exit_code = 1;
|
set_exit_code(1);
|
||||||
gid.to_string()
|
gid.to_string()
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
@ -579,7 +592,7 @@ fn id_print(state: &mut State, groups: Vec<u32>) {
|
||||||
euid,
|
euid,
|
||||||
entries::uid2usr(euid).unwrap_or_else(|_| {
|
entries::uid2usr(euid).unwrap_or_else(|_| {
|
||||||
show_error!("cannot find name for user ID {}", euid);
|
show_error!("cannot find name for user ID {}", euid);
|
||||||
state.exit_code = 1;
|
set_exit_code(1);
|
||||||
euid.to_string()
|
euid.to_string()
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
@ -590,7 +603,7 @@ fn id_print(state: &mut State, groups: Vec<u32>) {
|
||||||
euid,
|
euid,
|
||||||
entries::gid2grp(egid).unwrap_or_else(|_| {
|
entries::gid2grp(egid).unwrap_or_else(|_| {
|
||||||
show_error!("cannot find name for group ID {}", egid);
|
show_error!("cannot find name for group ID {}", egid);
|
||||||
state.exit_code = 1;
|
set_exit_code(1);
|
||||||
egid.to_string()
|
egid.to_string()
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
@ -604,7 +617,7 @@ fn id_print(state: &mut State, groups: Vec<u32>) {
|
||||||
gr,
|
gr,
|
||||||
entries::gid2grp(gr).unwrap_or_else(|_| {
|
entries::gid2grp(gr).unwrap_or_else(|_| {
|
||||||
show_error!("cannot find name for group ID {}", gr);
|
show_error!("cannot find name for group ID {}", gr);
|
||||||
state.exit_code = 1;
|
set_exit_code(1);
|
||||||
gr.to_string()
|
gr.to_string()
|
||||||
})
|
})
|
||||||
))
|
))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue