From 0956ab8804ab138b2ca28fa1e786f0981eabc2d8 Mon Sep 17 00:00:00 2001 From: Michael Gehring Date: Sun, 15 Jun 2014 12:22:05 +0200 Subject: [PATCH 1/2] c_types: fix c_passwd (struct passwd) memory layout on linux --- common/c_types.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/common/c_types.rs b/common/c_types.rs index 55f64809a..1846a296c 100644 --- a/common/c_types.rs +++ b/common/c_types.rs @@ -6,8 +6,8 @@ use self::libc::{ c_char, c_int, uid_t, - time_t }; +#[cfg(target_os = "macos")] use self::libc::time_t; use self::libc::funcs::posix88::unistd::getgroups; use std::vec::Vec; @@ -15,6 +15,7 @@ use std::vec::Vec; use std::ptr::read; use std::str::raw::from_c_str; +#[cfg(target_os = "macos")] pub struct c_passwd { pub pw_name: *c_char, /* user name */ pub pw_passwd: *c_char, /* user name */ @@ -28,6 +29,17 @@ pub struct c_passwd { pub pw_expire: time_t } +#[cfg(target_os = "linux")] +pub struct c_passwd { + pub pw_name: *c_char, /* user name */ + pub pw_passwd: *c_char, /* user name */ + pub pw_uid: c_int, /* user uid */ + pub pw_gid: c_int, /* user gid */ + pub pw_gecos: *c_char, + pub pw_dir: *c_char, + pub pw_shell: *c_char, +} + #[cfg(target_os = "macos")] pub struct utsname { pub sysname: [c_char, ..256], From 1b8307e9d9070ff6c3ef31f19e609c73000faf55 Mon Sep 17 00:00:00 2001 From: Michael Gehring Date: Sun, 15 Jun 2014 12:23:41 +0200 Subject: [PATCH 2/2] id: linux specific pline() --- id/id.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/id/id.rs b/id/id.rs index 6952b8d2d..785f6d2ec 100644 --- a/id/id.rs +++ b/id/id.rs @@ -256,6 +256,7 @@ fn pretty(possible_pw: Option) { } } +#[cfg(target_os = "macos")] fn pline(possible_pw: Option) { let pw = if possible_pw.is_none() { unsafe { read(getpwuid(getuid() as i32)) } @@ -284,6 +285,31 @@ fn pline(possible_pw: Option) { pw_shell); } +#[cfg(target_os = "linux")] +fn pline(possible_pw: Option) { + let pw = if possible_pw.is_none() { + unsafe { read(getpwuid(getuid() as i32)) } + } else { + possible_pw.unwrap() + }; + + let pw_name = unsafe { from_c_str(pw.pw_name) }; + let pw_passwd = unsafe { from_c_str(pw.pw_passwd)}; + let pw_gecos = unsafe { from_c_str(pw.pw_gecos) }; + let pw_dir = unsafe { from_c_str(pw.pw_dir) }; + let pw_shell = unsafe { from_c_str(pw.pw_shell) }; + + println!( + "{:s}:{:s}:{:d}:{:d}:{:s}:{:s}:{:s}", + pw_name, + pw_passwd, + pw.pw_uid, + pw.pw_gid, + pw_gecos, + pw_dir, + pw_shell); +} + static NGROUPS: i32 = 20; #[cfg(target_os = "linux")]