diff --git a/id/id.rs b/id/id.rs index c062954f0..7970fe2aa 100644 --- a/id/id.rs +++ b/id/id.rs @@ -14,15 +14,27 @@ */ #[allow(non_camel_case_types)]; - +#[feature(macro_rules)]; extern crate getopts; use std::{libc, os, vec}; 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 getopts::{getopts, optflag, usage}; +#[path = "../common/util.rs"] +mod util; + // These could be extracted into their own file struct c_passwd { pw_name: *c_char, /* user name */ @@ -87,7 +99,38 @@ extern { 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 { + 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::(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 () { let args = os::args(); @@ -108,13 +151,13 @@ fn main () { let matches = match getopts(args_t, options) { Ok(m) => { m }, Err(_) => { - println!("{:s}", usage(PROGRAM, options)); + println!("{:s}", usage(NAME, options)); return; } }; if matches.opt_present("h") { - println!("{:s}", usage(PROGRAM, options)); + println!("{:s}", usage(NAME, options)); return; } @@ -124,37 +167,7 @@ fn main () { } - let possible_pw = if matches.free.len() == 1 { - let username = matches.free[0].clone(); - - // Passed user by id - if username.chars().all(|c| c.is_digit()) { - let id = from_str::(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 possible_pw = get_pw_from_args(&matches); let nflag = matches.opt_present("n"); let uflag = matches.opt_present("u"); @@ -312,10 +325,6 @@ fn pline(possible_pw: Option) { pw_shell); } -fn no_such_user(username: ~str) { - println!("{:s}: {:s}: no such user", PROGRAM, username.as_slice()); -} - static NGROUPS: i32 = 20; fn group(possible_pw: Option, nflag: bool) {