mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 19:47:45 +00:00
Groom id/id.rs to work with util.rs
This commit is contained in:
parent
de21ff4c2f
commit
2420c38b7e
1 changed files with 49 additions and 40 deletions
89
id/id.rs
89
id/id.rs
|
@ -14,15 +14,27 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#[allow(non_camel_case_types)];
|
#[allow(non_camel_case_types)];
|
||||||
|
#[feature(macro_rules)];
|
||||||
extern crate getopts;
|
extern crate getopts;
|
||||||
|
|
||||||
use std::{libc, os, vec};
|
use std::{libc, os, vec};
|
||||||
use std::ptr::read;
|
use std::ptr::read;
|
||||||
use std::libc::{c_char, c_int, time_t, uid_t, getgid, getegid, getuid, getlogin};
|
use std::libc::{
|
||||||
|
c_char,
|
||||||
|
c_int,
|
||||||
|
time_t,
|
||||||
|
uid_t,
|
||||||
|
getgid,
|
||||||
|
getegid,
|
||||||
|
getuid,
|
||||||
|
getlogin
|
||||||
|
};
|
||||||
use std::str::raw::from_c_str;
|
use std::str::raw::from_c_str;
|
||||||
use getopts::{getopts, optflag, usage};
|
use getopts::{getopts, optflag, usage};
|
||||||
|
|
||||||
|
#[path = "../common/util.rs"]
|
||||||
|
mod util;
|
||||||
|
|
||||||
// These could be extracted into their own file
|
// These could be extracted into their own file
|
||||||
struct c_passwd {
|
struct c_passwd {
|
||||||
pw_name: *c_char, /* user name */
|
pw_name: *c_char, /* user name */
|
||||||
|
@ -87,7 +99,38 @@ extern {
|
||||||
ngroups: *mut c_int) -> c_int;
|
ngroups: *mut c_int) -> c_int;
|
||||||
}
|
}
|
||||||
|
|
||||||
static PROGRAM: &'static str = "id";
|
static NAME: &'static str = "id";
|
||||||
|
|
||||||
|
fn get_pw_from_args(matches: &getopts::Matches) -> Option<c_passwd> {
|
||||||
|
if matches.free.len() == 1 {
|
||||||
|
let username = matches.free[0].clone();
|
||||||
|
|
||||||
|
// Passed user as id
|
||||||
|
if username.chars().all(|c| c.is_digit()) {
|
||||||
|
let id = from_str::<u32>(username).unwrap();
|
||||||
|
let pw_pointer = unsafe { getpwuid(id) };
|
||||||
|
|
||||||
|
if pw_pointer.is_not_null() {
|
||||||
|
Some(unsafe { read(pw_pointer) })
|
||||||
|
} else {
|
||||||
|
crash!(1, "{:s}: no such user", username);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Passed the username as a string
|
||||||
|
} else {
|
||||||
|
let pw_pointer = unsafe {
|
||||||
|
getpwnam(username.as_slice().as_ptr() as *i8)
|
||||||
|
};
|
||||||
|
if pw_pointer.is_not_null() {
|
||||||
|
Some(unsafe { read(pw_pointer) })
|
||||||
|
} else {
|
||||||
|
crash!(1, "{:s}: no such user", username);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main () {
|
fn main () {
|
||||||
let args = os::args();
|
let args = os::args();
|
||||||
|
@ -108,13 +151,13 @@ fn main () {
|
||||||
let matches = match getopts(args_t, options) {
|
let matches = match getopts(args_t, options) {
|
||||||
Ok(m) => { m },
|
Ok(m) => { m },
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
println!("{:s}", usage(PROGRAM, options));
|
println!("{:s}", usage(NAME, options));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if matches.opt_present("h") {
|
if matches.opt_present("h") {
|
||||||
println!("{:s}", usage(PROGRAM, options));
|
println!("{:s}", usage(NAME, options));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,37 +167,7 @@ fn main () {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
let possible_pw = if matches.free.len() == 1 {
|
let possible_pw = get_pw_from_args(&matches);
|
||||||
let username = matches.free[0].clone();
|
|
||||||
|
|
||||||
// Passed user by id
|
|
||||||
if username.chars().all(|c| c.is_digit()) {
|
|
||||||
let id = from_str::<u32>(username).unwrap();
|
|
||||||
let pw_pointer = unsafe { getpwuid(id) };
|
|
||||||
|
|
||||||
if pw_pointer.is_not_null() {
|
|
||||||
Some(unsafe { read(pw_pointer) })
|
|
||||||
} else {
|
|
||||||
no_such_user(username);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Passed the username as a string
|
|
||||||
} else {
|
|
||||||
let pw_pointer = unsafe {
|
|
||||||
getpwnam(username.as_slice().as_ptr() as *i8)
|
|
||||||
};
|
|
||||||
if pw_pointer.is_not_null() {
|
|
||||||
Some(unsafe { read(pw_pointer) })
|
|
||||||
} else {
|
|
||||||
no_such_user(username);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
let nflag = matches.opt_present("n");
|
let nflag = matches.opt_present("n");
|
||||||
let uflag = matches.opt_present("u");
|
let uflag = matches.opt_present("u");
|
||||||
|
@ -312,10 +325,6 @@ fn pline(possible_pw: Option<c_passwd>) {
|
||||||
pw_shell);
|
pw_shell);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn no_such_user(username: ~str) {
|
|
||||||
println!("{:s}: {:s}: no such user", PROGRAM, username.as_slice());
|
|
||||||
}
|
|
||||||
|
|
||||||
static NGROUPS: i32 = 20;
|
static NGROUPS: i32 = 20;
|
||||||
|
|
||||||
fn group(possible_pw: Option<c_passwd>, nflag: bool) {
|
fn group(possible_pw: Option<c_passwd>, nflag: bool) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue