From de21ff4c2f382b6af66301fa118b8021a66282b7 Mon Sep 17 00:00:00 2001 From: Alan Andrade Date: Sun, 23 Feb 2014 14:17:48 -0800 Subject: [PATCH 1/3] Move util.rs to common --- base64/base64.rs | 2 +- basename/basename.rs | 2 +- util.rs => common/util.rs | 0 du/du.rs | 2 +- echo/echo.rs | 2 +- mkdir/mkdir.rs | 2 +- printenv/printenv.rs | 2 +- pwd/pwd.rs | 2 +- rm/rm.rs | 2 +- rmdir/rmdir.rs | 2 +- seq/seq.rs | 2 +- sleep/sleep.rs | 2 +- truncate/truncate.rs | 2 +- tty/tty.rs | 2 +- users/users.rs | 2 +- wc/wc.rs | 2 +- whoami/whoami.rs | 2 +- yes/yes.rs | 2 +- 18 files changed, 17 insertions(+), 17 deletions(-) rename util.rs => common/util.rs (100%) diff --git a/base64/base64.rs b/base64/base64.rs index ca30682c1..a05c9c520 100644 --- a/base64/base64.rs +++ b/base64/base64.rs @@ -28,7 +28,7 @@ use getopts::{ use serialize::base64; use serialize::base64::{FromBase64, ToBase64}; -#[path = "../util.rs"] +#[path = "../common/util.rs"] mod util; static NAME: &'static str = "base64"; diff --git a/basename/basename.rs b/basename/basename.rs index 7ac29c5b5..d82bd2eb1 100644 --- a/basename/basename.rs +++ b/basename/basename.rs @@ -18,7 +18,7 @@ use std::os; use std::str; use std::str::StrSlice; -#[path = "../util.rs"] +#[path = "../common/util.rs"] mod util; static NAME: &'static str = "basename"; diff --git a/util.rs b/common/util.rs similarity index 100% rename from util.rs rename to common/util.rs diff --git a/du/du.rs b/du/du.rs index 2397385aa..903a87fb4 100644 --- a/du/du.rs +++ b/du/du.rs @@ -23,7 +23,7 @@ use std::path::Path; use time::Timespec; use sync::{Arc, Future}; -#[path = "../util.rs"] +#[path = "../common/util.rs"] mod util; static NAME: &'static str = "du"; diff --git a/echo/echo.rs b/echo/echo.rs index 0780b8cac..fd6bfc8d9 100644 --- a/echo/echo.rs +++ b/echo/echo.rs @@ -17,7 +17,7 @@ use std::os; use std::io::{print, println}; use std::uint; -#[path = "../util.rs"] +#[path = "../common/util.rs"] mod util; static NAME: &'static str = "echo"; diff --git a/mkdir/mkdir.rs b/mkdir/mkdir.rs index 4cf6c78cc..80ade2501 100644 --- a/mkdir/mkdir.rs +++ b/mkdir/mkdir.rs @@ -18,7 +18,7 @@ use std::os; use std::io::fs; use std::num::strconv; -#[path = "../util.rs"] +#[path = "../common/util.rs"] mod util; static NAME: &'static str = "mkdir"; diff --git a/printenv/printenv.rs b/printenv/printenv.rs index bee1f8d0f..3b9a648e5 100644 --- a/printenv/printenv.rs +++ b/printenv/printenv.rs @@ -19,7 +19,7 @@ extern crate getopts; use std::os; use std::io::print; -#[path = "../util.rs"] +#[path = "../common/util.rs"] mod util; static NAME: &'static str = "printenv"; diff --git a/pwd/pwd.rs b/pwd/pwd.rs index be638c817..aa282abdc 100644 --- a/pwd/pwd.rs +++ b/pwd/pwd.rs @@ -17,7 +17,7 @@ extern crate getopts; use std::os; use std::io::print; -#[path = "../util.rs"] +#[path = "../common/util.rs"] mod util; static NAME: &'static str = "pwd"; diff --git a/rm/rm.rs b/rm/rm.rs index 00809d3b3..9cec82474 100644 --- a/rm/rm.rs +++ b/rm/rm.rs @@ -17,7 +17,7 @@ extern crate getopts; use std::os; use std::io::{print, stdin, stdio, fs, BufferedReader}; -#[path = "../util.rs"] +#[path = "../common/util.rs"] mod util; #[deriving(Eq)] diff --git a/rmdir/rmdir.rs b/rmdir/rmdir.rs index 6b344325e..9de1e5327 100644 --- a/rmdir/rmdir.rs +++ b/rmdir/rmdir.rs @@ -17,7 +17,7 @@ extern crate getopts; use std::os; use std::io::{print, fs}; -#[path = "../util.rs"] +#[path = "../common/util.rs"] mod util; static NAME: &'static str = "rmdir"; diff --git a/seq/seq.rs b/seq/seq.rs index baee8d492..6d8358b12 100644 --- a/seq/seq.rs +++ b/seq/seq.rs @@ -11,7 +11,7 @@ extern crate getopts; use std::os; use std::cmp::max; -#[path = "../util.rs"] +#[path = "../common/util.rs"] mod util; static NAME: &'static str = "seq"; diff --git a/sleep/sleep.rs b/sleep/sleep.rs index 5475b4b01..bd7138449 100644 --- a/sleep/sleep.rs +++ b/sleep/sleep.rs @@ -19,7 +19,7 @@ use std::cast; use std::os; use std::io::{print, timer}; -#[path = "../util.rs"] +#[path = "../common/util.rs"] mod util; static NAME: &'static str = "sleep"; diff --git a/truncate/truncate.rs b/truncate/truncate.rs index 092efc5b6..15f86c355 100644 --- a/truncate/truncate.rs +++ b/truncate/truncate.rs @@ -18,7 +18,7 @@ use std::io::{File, Open, ReadWrite, fs}; use std::os; use std::u64; -#[path = "../util.rs"] +#[path = "../common/util.rs"] mod util; macro_rules! get_file_size( diff --git a/tty/tty.rs b/tty/tty.rs index b416fecc4..773887a2b 100644 --- a/tty/tty.rs +++ b/tty/tty.rs @@ -24,7 +24,7 @@ use std::io::println; use std::io::stdio::stderr; use getopts::{optflag,getopts}; -#[path = "../util.rs"] +#[path = "../common/util.rs"] mod util; extern { diff --git a/users/users.rs b/users/users.rs index 5cf466ef9..04c228b47 100644 --- a/users/users.rs +++ b/users/users.rs @@ -27,7 +27,7 @@ use std::ptr; use std::str; use utmpx::*; -#[path = "../util.rs"] +#[path = "../common/util.rs"] mod util; #[cfg(target_os = "linux")] diff --git a/wc/wc.rs b/wc/wc.rs index 64122e5e5..ef49d6517 100644 --- a/wc/wc.rs +++ b/wc/wc.rs @@ -19,7 +19,7 @@ use std::str::from_utf8; use std::io::{print, stdin, File, BufferedReader}; use getopts::Matches; -#[path = "../util.rs"] +#[path = "../common/util.rs"] mod util; struct Result { diff --git a/whoami/whoami.rs b/whoami/whoami.rs index b6a466336..d632cf11c 100644 --- a/whoami/whoami.rs +++ b/whoami/whoami.rs @@ -23,7 +23,7 @@ use std::os; use std::str; use std::libc; -#[path = "../util.rs"] +#[path = "../common/util.rs"] mod util; struct c_passwd { diff --git a/yes/yes.rs b/yes/yes.rs index 8543f20f6..3e2d9fbcd 100644 --- a/yes/yes.rs +++ b/yes/yes.rs @@ -19,7 +19,7 @@ extern crate getopts; use std::os; use std::io::{print, println}; -#[path = "../util.rs"] +#[path = "../common/util.rs"] mod util; static NAME: &'static str = "yes"; From 2420c38b7eec3031889ea3b7768921f756dd8c2e Mon Sep 17 00:00:00 2001 From: Alan Andrade Date: Sun, 23 Feb 2014 14:18:04 -0800 Subject: [PATCH 2/3] Groom id/id.rs to work with util.rs --- id/id.rs | 89 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 49 insertions(+), 40 deletions(-) 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) { From b6018f38e8306b7fc6e16d89e83c7a8aa64219d9 Mon Sep 17 00:00:00 2001 From: Alan Andrade Date: Sun, 23 Feb 2014 21:55:01 -0800 Subject: [PATCH 3/3] whoami and id use c_types --- common/c_types.rs | 24 ++++++++++++++++++++++++ id/id.rs | 27 ++++++--------------------- whoami/whoami.rs | 10 +++------- 3 files changed, 33 insertions(+), 28 deletions(-) create mode 100644 common/c_types.rs diff --git a/common/c_types.rs b/common/c_types.rs new file mode 100644 index 000000000..6994e24e4 --- /dev/null +++ b/common/c_types.rs @@ -0,0 +1,24 @@ +#[allow(dead_code)]; + +use std::libc::{ + c_char, + c_int, + time_t +}; + +pub struct c_passwd { + pw_name: *c_char, /* user name */ + pw_passwd: *c_char, /* user name */ + pw_uid: c_int, /* user uid */ + pw_gid: c_int, /* user gid */ + pw_change: time_t, + pw_class: *c_char, + pw_gecos: *c_char, + pw_dir: *c_char, + pw_shell: *c_char, + pw_expire: time_t +} + +pub struct c_group { + gr_name: *c_char /* group name */ +} diff --git a/id/id.rs b/id/id.rs index 7970fe2aa..3a52c6d35 100644 --- a/id/id.rs +++ b/id/id.rs @@ -22,7 +22,6 @@ use std::ptr::read; use std::libc::{ c_char, c_int, - time_t, uid_t, getgid, getegid, @@ -31,27 +30,13 @@ use std::libc::{ }; use std::str::raw::from_c_str; use getopts::{getopts, optflag, usage}; +use c_types::{ + c_passwd, + c_group +}; -#[path = "../common/util.rs"] -mod util; - -// These could be extracted into their own file -struct c_passwd { - pw_name: *c_char, /* user name */ - pw_passwd: *c_char, /* user name */ - pw_uid: c_int, /* user uid */ - pw_gid: c_int, /* user gid */ - pw_change: time_t, - pw_class: *c_char, - pw_gecos: *c_char, - pw_dir: *c_char, - pw_shell: *c_char, - pw_expire: time_t -} - -struct c_group { - gr_name: *c_char /* group name */ -} +#[path = "../common/util.rs"] mod util; +#[path = "../common/c_types.rs"] mod c_types; #[cfg(not(target_os = "linux"))] mod audit { diff --git a/whoami/whoami.rs b/whoami/whoami.rs index d632cf11c..3487f31e1 100644 --- a/whoami/whoami.rs +++ b/whoami/whoami.rs @@ -22,14 +22,10 @@ use std::io::print; use std::os; use std::str; use std::libc; +use c_types::c_passwd; -#[path = "../common/util.rs"] -mod util; - -struct c_passwd { - pw_name: *libc::c_char, - // Maybe I should put here others struct members, but...Well, maybe. -} +#[path = "../common/util.rs"] mod util; +#[path = "../common/c_types.rs"] mod c_types; extern { pub fn geteuid() -> libc::c_int;